Skip to content

Instantly share code, notes, and snippets.

@Vinai
Vinai / install-0.1.0.php
Last active November 30, 2016 18:25 — forked from kojiromike/install-0.1.0.php
Example Magento1 install script with some explanation.
<?php
// I almost never use $installer = $this; instead I prefer to use the following
$installer = Mage::getResourceModel('catalog/setup', 'catalog_setup');
// This is just an example of instantiating the setup class in the script. Of
// course I choose the appropriate setup class and resource on a case by case basis.
// That way it is very visible what setup class is being used. It can also be
// switched within a single setup script, for example to add attributes to
@Vinai
Vinai / prime-factors.sh
Created December 13, 2016 18:57
Language or testing frameworks or the lack thereof are no reason not to write tests. All that is needed is a function that calls the function to be checked.
#!/usr/bin/env bash
function assert_array_same {
if [ "${expected[*]}" != "${actual[*]}" ]; then
echo -e "\nFailed $1\nExpected: ${expected[*]}\nActual: ${actual[*]}" && exit 1
else
echo -en "."
fi
}
@Vinai
Vinai / Observer.php
Created May 1, 2013 20:52
Example custom Magento profiler via Observer. Please keep in mind this is OLD CODE ;)
<?php
// app/code/local/Example/Profiler/Model/Observer.php
class Example_Profiler_Model_Observer
{
/**
* Log the profiler statistics plus some information that helps identify the request
*
* @param Varien_Event_Observer $observer
@Vinai
Vinai / core.clj
Created September 22, 2017 08:58
Prime factors kata early in me learning Clojure
(ns prime-factors.core)
(defn generate-recursive
"Prime factor kata that does not use tail call optimization"
([n] (generate-recursive n [] 2))
([n primes candidate]
(if (= 1 n)
primes
(if (= 0 (mod n candidate))
(generate-recursive (/ n candidate) (conj primes candidate) candidate)
@Vinai
Vinai / core.clj
Created September 22, 2017 09:00
String calculator kata in Clojure done early while learning the language.
(ns string-calculator.core
(:require [clojure.string :refer [join]]))
(defn numbers-from-string
[string]
(map #(Integer. %) (re-seq #"-?\d+" string)))
(defn add
[string]
(if (empty? string) 0
@Vinai
Vinai / core.clj
Created September 22, 2017 09:02
Print diamond kata in Clojure early while I was learning the language.
(ns print-diamond.core)
(defmacro abs
"Return the absolute value of the specified number.
This is a macro only for practice.
A regular function (max value (- value)) would work just as well"
[value]
`(if (pos? ~value) ~value (- ~value)))
(defn size
@Vinai
Vinai / core.clj
Created September 22, 2017 09:03
Roman numerals kata in Clojure early while I was learning the language.
(ns roman-numerals.core)
(def roman-numerals-map (array-map "M" 1000, "CM" 900, "D" 500, "CD" 400, "C" 100, "XC" 90,
"L" 50, "XL" 40, "X" 10, "IX" 9, "V" 5, "IV" 4, "I" 1))
(defn arabic->roman
[n]
(if (= 0 n) ""
(let [[r v] (some (fn [[r v]] (if (<= v n) [r v])) roman-numerals-map)]
(apply str (concat (repeat (int (/ n v)) r) [(arabic->roman (rem n v))])))))
@Vinai
Vinai / 2_keyboard_shortcuts.md
Created October 17, 2013 08:26
Here are some things you can do with Gists in GistBox.

Create documentation for your projects. Like so:


Most popular keyboard shortcuts within GistBox

  • Up/Down - Previous/Next Gist
  • Ctrl+e - Edit a selected Gist
  • Ctrl+s - Save Gist
@Vinai
Vinai / .n98-magerun.yaml
Created November 11, 2014 10:08
How can the configured sample data package for a magento package be changed via ~/.n98-magerun.yaml?
# I want to change the configured sample data name for a given magento package.
#
# (Alternatively I would like to change the source URL for a sample data package.)
#
# The following does not work as I intend it to.
# It adds a new record to the $this->commandConfig['magento-packages']
# array instead of overwriting the extra sample-data value.
#
# Can this be done?
@Vinai
Vinai / mage-file-cache-clean.php
Last active January 8, 2019 09:25
Quick to invoke version of bin/magento cache:clean
#!/usr/bin/env php
<?php declare(strict_types=1);
use function array_reduce as reduce;
use function array_slice as slice;
use function array_filter as filter;
use function array_map as map;
$basedir = reduce(['.', '..', '../..', '../../..'], function ($acc, $basedir) {
return file_exists($basedir . '/vendor/autoload.php') ? $basedir : $acc;