diff --git a/source/Spiral/Core/Container.php b/source/Spiral/Core/Container.php index ed3ed26c..cb58f34d 100644 --- a/source/Spiral/Core/Container.php +++ b/source/Spiral/Core/Container.php @@ -10,6 +10,7 @@ use Interop\Container\ContainerInterface; use ReflectionFunctionAbstract as ContextFunction; +use Spiral\Core\Container\Autowire; use Spiral\Core\Container\InjectableInterface; use Spiral\Core\Container\InjectorInterface; use Spiral\Core\Container\SingletonInterface; @@ -206,6 +207,11 @@ final public function resolveArguments( throw new ContainerException($e->getMessage(), $e->getCode(), $e); } + if (array_key_exists($name, $parameters) && $parameters[$name] instanceof Autowire) { + $arguments[] = $parameters[$name]->resolve($this); + continue; + } + //No declared type or scalar type or array if (empty($class)) { //Provided from outside diff --git a/source/Spiral/Core/Container/Autowire.php b/source/Spiral/Core/Container/Autowire.php new file mode 100644 index 00000000..2a54641e --- /dev/null +++ b/source/Spiral/Core/Container/Autowire.php @@ -0,0 +1,57 @@ +alias = $alias; + } + + /** + * @param \Interop\Container\ContainerInterface $container + * + * @return mixed + * + * @throws \Interop\Container\Exception\NotFoundException No entry was found for this + * identifier. + * @throws \Interop\Container\Exception\ContainerException Error while retrieving the entry. + */ + public function resolve(ContainerInterface $container) + { + return $container->get($this->alias); + } + + /** + * @param $an_array + * + * @return static + */ + public static function __set_state($an_array) + { + return new static($an_array['alias']); + } +} \ No newline at end of file diff --git a/tests/Core/AutowiringTest.php b/tests/Core/AutowiringTest.php index 3ce837c7..5862a064 100644 --- a/tests/Core/AutowiringTest.php +++ b/tests/Core/AutowiringTest.php @@ -303,4 +303,18 @@ public function testAutowireOptionalString() $this->assertInstanceOf(TypedClass::class, $object); } + + public function testAutowireDelegate() + { + $container = new Container(); + + $container->bind('sample-binding', $s = new SampleClass()); + + $object = $container->make(SoftDependedClass::class, [ + 'name' => 'some-name', + 'sample' => new Container\Autowire('sample-binding') + ]); + + $this->assertSame($s, $object->getSample()); + } }