Skip to content

Instantly share code, notes, and snippets.

View J7mbo's full-sized avatar
🔨
Working with distributed systems

James Mallison J7mbo

🔨
Working with distributed systems
View GitHub Profile
@J7mbo
J7mbo / DigestRequestHandler.php
Last active August 29, 2015 14:04
An example DigestRequestHandler
<?php
namespace SomeEngineName\Api\DigestRequestHandler;
use SomeEngineName\Api\UserRepository\UserNotFoundException,
SomeEngineName\Api\UserRepository\UserRepository;
/**
* Class DigestRequestHandler
*
@J7mbo
J7mbo / gist:0f53e4589d441ee1e772
Created May 9, 2015 12:19
Dependency injection the ---right--- awesome way™

Dependency Injection is a posh word for a simple concept. Frameworks are starting to adopt the pattern and many developers want to use it, but simply don't know how or even why they should. For those who do, are you gaining the advantages that you should be? Should you use a service locator? How does this fit in with S.O.L.I.D? Are you doing it the awesome way?

This talk focuses on the journey I made through the discovery of the basics of DI through to how to use it to it's full potential, the misnomer that is the Dependency Injection Container, and how auto-wiring injectors can make you happy again. Learn how you can 'plug and play' third party libraries with only a few steps and reduce your development time and maintenance when working with object-oriented code.

@J7mbo
J7mbo / nogoto.php
Created August 19, 2015 15:37
Nah don't need that goto...
do {
$className = $this->jigConverter->getNamespacedClassNameFromFileName($templateFilename);
if ($this->jigConfig->compileCheck == Jig::COMPILE_CHECK_EXISTS) {
if (class_exists($className) == true) {
break
}
}
if ($this->jigConfig->compileCheck == Jig::COMPILE_CHECK_MTIME) {
if ($this->isGeneratedFileOutOfDate($templateFilename) == false) {
if (class_exists($className) == true) {
[**Please, don't use `mysql_*` functions in new code**](http://bit.ly/phpmsql). They are no longer maintained, are [officially deprecated](http://j.mp/XqV7Lp) and can be [dangerous in live code](http://bit.ly/4zUdtT). See the [**red box**](http://j.mp/Te9zIL)? Learn about [*prepared statements*](http://j.mp/T9hLWi) instead, and use [PDO](http://php.net/pdo) or [MySQLi](http://php.net/mysqli) - [this article](http://j.mp/QEx8IB) will help you decide which. If you choose PDO, [here is a good tutorial](http://j.mp/PoWehJ).

Please, don't use mysql_* functions in new code. They are no longer maintained, are officially deprecated and can be dangerous in live code. See the red box? Learn about prepared statements instead, and use PDO or MySQLi - this article will help you decide which. If you choose PDO, [here is a go

@J7mbo
J7mbo / PHP Reflection Haxx
Created April 9, 2013 14:49
Haxx your way into a Class's private / protected variables using Reflection and variable variables! Pretty neat!
<?php
class Parameters
{
protected $var1;
protected $var2;
protected $var3;
public function __construct($one, $two, $three)
{
$this->var1 = $one;
<?php
class B {
protected $c;
public function __construct(C $c) {
$this->c = $c;
}
}
class C {}
@J7mbo
J7mbo / MySQL PDO Select Abstraction
Last active December 17, 2015 01:59
Abstraction for MySQL PDO SELECT statement. Running `findByValue(array('id', 'username'), 'users', array('website' => 'j7mbo.co.uk'), true);` is the same as: `SELECT id, username FROM `users` WHERE website = 'j7mbo.co.uk' LIMIT 1;`
public function findByValue($values, $location, $requirements, $single = false)
{
$return = array();
$conn = $this->conn;
$query = 'SELECT ' . implode(", ", $values) . ' FROM ' . $location . ' WHERE';
$i = 0;
foreach ($requirements as $key => $value)
{
@J7mbo
J7mbo / gist:6388265
Created August 30, 2013 09:57
ALMOST awesomeness
<?php
// EchoEndpoint.php
use Aerys\Handlers\Websocket\Message,
Aerys\Handlers\Websocket\Endpoint,
Aerys\Handlers\Websocket\WebsocketHandler,
Alert\Reactor,
Amp\JobDispatcher,
Amp\CallResult;
@J7mbo
J7mbo / gist:6388474
Created August 30, 2013 10:24
aerys conf
<?php
use Aerys\Config\Bootstrapper;
use Aerys\Handlers\ReverseProxy\ReverseProxyLauncher;
use Aerys\Handlers\DocRoot\DocRootLauncher;
require dirname(__DIR__).'/vendor/autoload.php';
require '/var/www/Aerys/autoload.php';
require __DIR__ . '/EchoEndpoint.php';
<VirtualHost *:1337>
ServerName silex.local
ServerAlias silex.local
DocumentRoot "/home/james/Dev/github/Silex/web"
DirectoryIndex index.php
<Directory "/home/james/Dev/github/Silex/web">
AllowOverride All