Skip to content

2.3.0

Choose a tag to compare

@koriym koriym released this 17 May 05:43
· 1293 commits to 2.x since this release

This release supports @Assisted injection.

It is possible to inject dependencies directly in the invoke method parameter(s). When doing this, add the dependency to the end of the arguments and set the default value to null, it is also required to annotate the method using @assisted.

module

class FooModule extends AbstractModule
{
    protected function configure()
    {
        $this->bind(RobotInterface::class)->to(Robot::class);
        $this->bind()->annotatedWith('one')->toInstance(1);
    }
}

dependent

class AssistedConsumer
{
    /**
     * @Assisted({"robot"})
     */
    public function foo(RobotInterface $robot)
    {
        return $robot;
    }

    /**
     * @Assisted({"var1"})
     * @Named("var1=one")
     */
    public function bar($a, $var1)
    {
        return $var1;
    }

    /**
     * @Assisted({"var2", "robot"})
     * @Named("var2=one")
     */
    public function baz($var2, FakeRobotInterface $robot)
    {
        return [$var2 , $robot];
    }
} 

$consumer->foo();  // Robot
$consumer->bar(0); // 1
$consumer->baz(); // [1, Robot]

#139