Skip to content

Instantly share code, notes, and snippets.

@polonskiy
polonskiy / get-teredo-addr.sh
Created January 2, 2014 06:34
get ipv6 (teredo) address
ip -6 -o addr | grep -P -o 2001:0:[^/]+
@polonskiy
polonskiy / pre-commit
Last active March 20, 2020 07:39
PHPLint pre-commit git hook
#!/usr/bin/php
<?php
exec('git diff --name-only --diff-filter=ACM --cached', $files);
$status = 0;
foreach ($files as $file) {
if (pathinfo($file, PATHINFO_EXTENSION) != 'php') continue;
$file = escapeshellarg($file);
$message = array();
exec("git show :$file | php -l 2>&1", $message, $code);

Composer Workflow

Example of why composer.lock should be version controlled and workflow of how it can be used.

The reason we put ranges in composer.json is so that we can check for updates in our development environment and test our source code works with it BEFORE it goes into production.

The reason we have specific versions of vendors in composer.lock is so that we can version it and install the application into production environments with the versions we have tested while in development. Because of this we never run composer update on a production environment.

Creating an Application

140 byte PHP routing system.

This is a very simply routing system that makes it easy to test requests to different paths. This is very limited so do not use for your applications - it's just for fun.

require('route.php');

// A user profile
route('/(\w+)/profile', function($path, $user)
{

print "Hello " . $user;

@polonskiy
polonskiy / gist:7007016
Created October 16, 2013 12:37
fake smtp server
python -m smtpd -n -c DebuggingServer localhost:2222
<?php
class Server
{
public $master;
public $read = [];
public $write = [];
public $writeBuf = [];
public function listen($address)
@polonskiy
polonskiy / gist:6914110
Created October 10, 2013 06:48
php lint
find . -name '*.php' -exec php -l {} \; | grep -v '^No'
@polonskiy
polonskiy / gist:6412223
Created September 2, 2013 12:13 — forked from oziks/demo.php
<?php
function flatten($array, $prefix = '') {
$result = array();
foreach($array as $key=>$value) {
if(is_array($value)) {
$result = $result + flatten($value, $prefix . $key . '.');
}
else {
$result[$prefix.$key] = $value;

Microframework

This is a PHP (5.3+) microframework based on anonymous functions.

Features

  • requested URLs matched using regular expressions
  • request methods (matches using regular expressions too)
  • differenced FIFO queues for each $priority
  • command line usage
  • backward compatibility
  • integrated Dependency Injection and settings system
  • named patterns

140 byte PHP routing system.

This is a very simply routing system that makes it easy to test requests to different paths. This is very limited so do not use for your applications - it's just for fun.

require('route.php');

// A user profile
route('/(\w+)/profile', function($path, $user)
{

print "Hello " . $user;