-
-
Notifications
You must be signed in to change notification settings - Fork 9.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature #49836 [DependencyInjection] Add support for `#[Autowire(lazy…
…: class-string)]` (nicolas-grekas) This PR was merged into the 6.3 branch. Discussion ---------- [DependencyInjection] Add support for `#[Autowire(lazy: class-string)]` | Q | A | ------------- | --- | Branch? | 6.3 | Bug fix? | no | New feature? | yes | Deprecations? | no | Tickets | - | License | MIT | Doc PR | - This PR finishes what started in #49685. It adds support for defining the interfaces to proxy when using laziness with the Autowire attribute. ```php public function __construct( #[Autowire(service: 'foo', lazy: FooInterface::class)] FooInterface|BarInterface $foo, ) { } ``` It also adds support for lazy-autowiring of intersection types: ```php public function __construct( #[Autowire(service: 'foobar', lazy: true)] FooInterface&BarInterface $foobar, ) { } ``` Commits ------- d127ebf [DependencyInjection] Add support for `#[Autowire(lazy: class-string)]`
- Loading branch information
Showing
14 changed files
with
199 additions
and
34 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
105 changes: 105 additions & 0 deletions
105
...nent/DependencyInjection/Tests/Fixtures/php/lazy_autowire_attribute_with_intersection.php
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
<?php | ||
|
||
use Symfony\Component\DependencyInjection\Argument\RewindableGenerator; | ||
use Symfony\Component\DependencyInjection\ContainerInterface; | ||
use Symfony\Component\DependencyInjection\Container; | ||
use Symfony\Component\DependencyInjection\Exception\LogicException; | ||
use Symfony\Component\DependencyInjection\Exception\ParameterNotFoundException; | ||
use Symfony\Component\DependencyInjection\Exception\RuntimeException; | ||
use Symfony\Component\DependencyInjection\ParameterBag\FrozenParameterBag; | ||
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface; | ||
|
||
/** | ||
* @internal This class has been auto-generated by the Symfony Dependency Injection Component. | ||
*/ | ||
class ProjectServiceContainer extends Container | ||
{ | ||
protected $parameters = []; | ||
protected readonly \WeakReference $ref; | ||
|
||
public function __construct() | ||
{ | ||
$this->ref = \WeakReference::create($this); | ||
$this->services = $this->privates = []; | ||
$this->methodMap = [ | ||
'foo' => 'getFooService', | ||
]; | ||
|
||
$this->aliases = []; | ||
} | ||
|
||
public function compile(): void | ||
{ | ||
throw new LogicException('You cannot compile a dumped container that was already compiled.'); | ||
} | ||
|
||
public function isCompiled(): bool | ||
{ | ||
return true; | ||
} | ||
|
||
public function getRemovedIds(): array | ||
{ | ||
return [ | ||
'.lazy.foo.gDmfket' => true, | ||
]; | ||
} | ||
|
||
protected function createProxy($class, \Closure $factory) | ||
{ | ||
return $factory(); | ||
} | ||
|
||
/** | ||
* Gets the public 'foo' shared autowired service. | ||
* | ||
* @return \Symfony\Component\DependencyInjection\Tests\Compiler\AAndIInterfaceConsumer | ||
*/ | ||
protected static function getFooService($container) | ||
{ | ||
$a = ($container->privates['.lazy.foo.gDmfket'] ?? self::get_Lazy_Foo_GDmfketService($container)); | ||
|
||
if (isset($container->services['foo'])) { | ||
return $container->services['foo']; | ||
} | ||
|
||
return $container->services['foo'] = new \Symfony\Component\DependencyInjection\Tests\Compiler\AAndIInterfaceConsumer($a); | ||
} | ||
|
||
/** | ||
* Gets the private '.lazy.foo.gDmfket' shared service. | ||
* | ||
* @return \object | ||
*/ | ||
protected static function get_Lazy_Foo_GDmfketService($container, $lazyLoad = true) | ||
{ | ||
$containerRef = $container->ref; | ||
|
||
if (true === $lazyLoad) { | ||
return $container->privates['.lazy.foo.gDmfket'] = $container->createProxy('objectProxy8ac8e9a', static fn () => \objectProxy8ac8e9a::createLazyProxy(static fn () => self::get_Lazy_Foo_GDmfketService($containerRef->get(), false))); | ||
} | ||
|
||
return ($container->services['foo'] ?? self::getFooService($container)); | ||
} | ||
} | ||
|
||
class objectProxy8ac8e9a implements \Symfony\Component\DependencyInjection\Tests\Compiler\AInterface, \Symfony\Component\DependencyInjection\Tests\Compiler\IInterface, \Symfony\Component\VarExporter\LazyObjectInterface | ||
{ | ||
use \Symfony\Component\VarExporter\LazyProxyTrait; | ||
|
||
private const LAZY_OBJECT_PROPERTY_SCOPES = []; | ||
|
||
public function initializeLazyObject(): \Symfony\Component\DependencyInjection\Tests\Compiler\AInterface&\Symfony\Component\DependencyInjection\Tests\Compiler\IInterface | ||
{ | ||
if ($state = $this->lazyObjectState ?? null) { | ||
return $state->realInstance ??= ($state->initializer)(); | ||
} | ||
|
||
return $this; | ||
} | ||
} | ||
|
||
// Help opcache.preload discover always-needed symbols | ||
class_exists(\Symfony\Component\VarExporter\Internal\Hydrator::class); | ||
class_exists(\Symfony\Component\VarExporter\Internal\LazyObjectRegistry::class); | ||
class_exists(\Symfony\Component\VarExporter\Internal\LazyObjectState::class); |
This file contains 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
This file contains 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
Oops, something went wrong.