Skip to content

Instantly share code, notes, and snippets.

@igorw
igorw / gist:2145730
Last active May 22, 2023 13:23
Composer PR Template

Composer is a new dependency manager for PHP. It allows you to specify dependencies on a per-project basis. It takes lots of inspiration from NPM and ruby's bundler.

All you need to support composer is a composer.json file. In order to allow easy installation, the repository needs to be added to packagist, which is the standard repository for composer. Packagist will fetch all the versions from your github repository tags.

Once it has been added, adding {PROJECT} to a project will be as easy as creating this composer.json file in the project's directory:

{
    "require": {
        "{VENDOR}/{PROJECT}": "{VERSION}"

}

@igorw
igorw / gist:950838
Created May 1, 2011 20:29
Closures in PHP 5.2.
<?php
function create_closure($args, $closureMap, $code) {
if ($closureMap) {
$globalRand = mt_rand();
$GLOBALS[$globalRand] = $closureMap;
$code = 'extract($GLOBALS['.$globalRand.']);'.$code;
}
@igorw
igorw / composer.json
Created February 15, 2012 12:34 — forked from michaelcullum/extension.json
Sample phpBB composer.json
{
"name": "acme/blog",
"type": "phpbb3-extension",
"description": "This will add an AI with mind reading capabilities to your board who will post your thoughts to a blog",
"homepage": "http://yourdomain.com/",
"version": "1.0.0",
"time": "2012-02-15",
"license": "GPLv2",
"authors": [
{
<?php
// AVL tree
class Node {
public $value;
public $left;
public $right;
public $height;
@igorw
igorw / make-pdf.sh
Created August 29, 2012 19:42
Generate PDF from composer documentation.
#!/bin/bash
# Generate PDF from composer documentation.
# dependencies:
# * pandoc
# * latex
for file in $(find . -type f -name '*.md' -depth 1)
do
pandoc -o $(basename $file .md).tex $file
@igorw
igorw / calc.php
Last active December 16, 2021 20:55
Shunting-yard infix to postfix conversion.
<?php
if ($argc === 1) {
echo "Usage: php calc.php EXPR\n";
exit(1);
}
$input = $argv[1];
$operators = [
@igorw
igorw / php-lazy-seq.php
Last active August 23, 2021 16:47
Lazy sequences using PHP 5.5 generators.
<?php
// thanks to metaultralurker on reddit for inspiration
function map(callable $fn, \Traversable $data) {
foreach ($data as $v) {
yield $fn($v);
}
}
@igorw
igorw / extract.php
Created July 11, 2012 19:41
Extract classes into their own files.
<?php
if ($argc < 3) {
echo "Usage: php extract.php src dest\n";
exit;
}
$src = $argv[1];
$dest = $argv[2];
@igorw
igorw / build.sh
Created December 11, 2012 19:40
Static Site Generator
#!/bin/bash
# Amazing static site generator
# Works for PHP and HTML sites
# Assumes web root to be in /web
# Dumps the site into a directory named "static"
PORT=9999
php -S localhost:$PORT -t web >/dev/null &
PID=$!
<?php
function array_map_recursive($f, $xs) {
$out = [];
foreach ($xs as $k => $x) {
$out[$k] = (is_array($x)) ? array_map_recursive($f, $x) : $f($x);
}
return $out;
}