I’ve discovered Value Lifecycle in the Phemto code. it is not covered by documentation though.
I’ve expected to use the following code
class A
{
protected $data;
function __construct(array $data)
{
$this->data=$data;
}
}
$class B
{
public function __construc(A $a){}
}
$a=new A(array('param'=>'taram'));
$injector=new Phemto();
$injector->willUse($a); //or I can use $injector->willUse(new Value($a));
$b=$injector->create('B');
But I have an error here. In Phemto.php, line 154, injector tries to createDependencies, and fails to create dependency for an ‘array’.
I think it should just return value. So for my own use I’ve hacked code here and just return if $lifecycle is instance of Value. Is it correct?
Here is my snippet, from line 153 (Phemto 0.1, aplha 9)
class class Context {
//...
function create($type, $nesting = array()) {
$lifecycle = $this->pick($type, $this->repository()->candidatesFor($type));
$context = $this->determineContext($lifecycle->class);
if ($wrapper = $context->hasWrapper($type, $nesting)) {
return $this->create($wrapper, $this->cons($wrapper, $nesting));
}
//Change here:
if ($lifecycle instanceof Value) return $lifecycle->instantiate(null);
//end of change
$instance = $lifecycle->instantiate($context->createDependencies(
$this->repository()->getConstructorParameters($lifecycle->class),
$this->cons($lifecycle->class, $nesting)));
foreach ($context->settersFor($lifecycle->class) as $setter) {
$context->invoke($instance, $setter, $context->createDependencies(
$this->repository()->getParameters($lifecycle->class, $setter),
$this->cons($lifecycle->class, $nesting)));
}
return $instance;
}
//...
}