Skip to content

Instantly share code, notes, and snippets.

@jm42
Created October 31, 2014 04:31
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jm42/3c32dd50bb9d09f57c4a to your computer and use it in GitHub Desktop.
Save jm42/3c32dd50bb9d09f57c4a to your computer and use it in GitHub Desktop.
IoC in a tweet
<?php // php -dallow_url_include=1 c.php
// Compressed 122 bytes
// class C extends ArrayObject { function __get($i) { $s = $this[$i]; $s instanceof Closure && $s = $s($this); return $s; } }
class C extends ArrayObject {
function __get($i) {
$s = $this[$i];
$s instanceof Closure && $s = $s($this);
return $s;
}
}
// Include testing framework
include 'https://gist.github.com/mathiasverraes/9046427/raw/9c990407c4b5337e6df8a8ae8e044fadedbf6858/TestFrameworkInATweet.php';
// {{{ Fixtures
class A { public $v; public function __construct($val) { $this->v = $val; } }
class B { public $a; public function __construct(A $a) { $this->a = $a; } }
// }}}
$container = new C;
it('should be an array-like', $container instanceof ArrayAccess);
it('should return zero for count()', count($container) === 0);
$container['parameter'] = 'parameter-value';
it('should return one for count()', count($container) === 1);
it('should return true isset() in existing parameter', isset($container['parameter']) === true);
it('should return parameter value', $container['parameter'] === 'parameter-value');
$create_a = function($c) {
return new A($c['parameter']);
};
$container['service_a'] = $create_a;
it('should return two for count()', count($container) === 2);
it('should return the raw creator', $container['service_a'] === $create_a);
$container['service_b'] = function($c) {
return new B($c->service_a);
};
$service_b = $container->service_b;
it('should return service b', $service_b instanceof B);
it('should be injected with A', $service_b->a instanceof A);
it('should be injected with the parameter value', $service_b->a->v === 'parameter-value');
unset($container['service_b']);
it('should return two for count()', count($container) === 2);
it('should return false for isset() of service_b', isset($container['service_b']) === false);
@jm42
Copy link
Author

jm42 commented Oct 31, 2014

List of awesome 140 bytes PHP projects.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment