Created
October 31, 2014 04:31
-
-
Save jm42/3c32dd50bb9d09f57c4a to your computer and use it in GitHub Desktop.
IoC in a tweet
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
List of awesome 140 bytes PHP projects.