Wallogit.com
2017 © Pedro Peláez
Jiggle is a depency injection container for php 5.3+, (*1)
By default every dependency is a singleton. If you assign an function to to the container it is considered as an singleton factory. So the function is only called once when the dependency is required the first time., (*2)
Create a singleton factory for the given class, (*3)
Create an object of the given class, (*4)
If the class constructor has arguments they are injected from the current jiggle instance., (*5)
Executes the given callable, (*6)
If the callable has arguments they are injected from the current jiggle instance., (*7)
Replace an existing dependency, (*8)
Usefull if one would like to mock some part of a bigger system or for clients to replace an unwanted implementation., (*9)
Let one provide a callable which is called if a dependency is not resolvable, (*10)
If the provided resolver can resolve the dependency it should simply return it. If the dependency could not resolved the resolver needs to throw an exception, otherwise the dependency would be resolved to null., (*11)
$jiggle = new Jiggle; $jiggle->d1 = 42; echo $jiggle->d1; // => 42
$jiggle = new Jiggle;
$jiggle->d1 = function() {
return 42;
};
echo $jiggle->d1; // => 42
$jiggle = new Jiggle;
$jiggle->d1 = 42;
$jiggle->d2 = function() use($jiggle) {
return $jiggle->d1;
};
echo $jiggle->d2; // => 42
$jiggle = new Jiggle;
$jiggle->d1 = 42;
$jiggle->d2 = function($d1) {
return $d1;
};
echo $jiggle->d2; // => 42
$jiggle = new Jiggle;
$jiggle->d1 = 40;
$jiggle->d2 = 2;
$result = $jiggle->inject(function($d1, $d2) {
return $d1 + $d2;
});
echo $result; // => 42
$jiggle = new Jiggle;
$jiggle->d1 = 20;
$jiggle->d2 = 1000;
$result = $jiggle->inject(function($d1, $d2, $d3) {
return $d1 + $d2 + $d3;
}, array('d2' => 20, 'd3' => 2));
echo $result; // => 42
$jiggle = new Jiggle;
$jiggle->d1 = 40;
$jiggle->d2 = 2;
$jiggle->d3 = function() use($jiggle) {
return new D3($jiggle->d1, $jiggle->d2);
};
echo $jiggle->d3->sum(); // => 42
$jiggle = new Jiggle;
$jiggle->d1 = 40;
$jiggle->d2 = 2;
$jiggle->d3 = function() use($jiggle) {
return $jiggle->create('D3');
};
echo $jiggle->d3->sum(); // => 42
$jiggle = new Jiggle;
$jiggle->d1 = 40;
$jiggle->d2 = 2;
$jiggle->d3 = $jiggle->singleton('D3');
echo $jiggle->d3->sum(); // => 42
$jiggle = new Jiggle;
$jiggle->d1 = function() {
return function() {
return 42;
};
};
echo $jiggle->d1(); // => 42
$jiggle = new Jiggle; $jiggle->a = true; $jiggle->a = false; // <- throws an exception
$jiggle = new Jiggle;
$jiggle->d1 = 21;
$jiggle->replace('d1', 42);
echo $jiggle->d1; // => 42
$jiggle = new Jiggle; $jiggle->d1 = 42; $this->assertTrue(isset($jiggle->d1)); $this->assertFalse(isset($jiggle->d2));
$jiggle = new Jiggle;
$jiggle->resolver(function($dependencyName) {
if ($dependencyName == 'd2') {
return 42;
}
throw new Exception("Could not resolve dependency: $dependencyName!");
});
$jiggle->d1 = function($d2) { return $d2; };
echo $jiggle->d1; // => 42
{
"require": {
"holgerk/jiggle": "*"
}
}
The MIT License (MIT), (*12)
Copyright (c) 2013 Holger Kohnen, (*13)
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:, (*14)
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software., (*15)
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE., (*16)