-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[DependencyInjection] Use lazy-loading ghost object proxies out of the box #46752
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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
43 changes: 43 additions & 0 deletions
43
src/Symfony/Component/DependencyInjection/LazyProxy/Instantiator/LazyServiceInstantiator.php
This file contains hidden or 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,43 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\DependencyInjection\LazyProxy\Instantiator; | ||
|
||
use Symfony\Bridge\ProxyManager\LazyProxy\Instantiator\RuntimeInstantiator; | ||
use Symfony\Component\DependencyInjection\ContainerInterface; | ||
use Symfony\Component\DependencyInjection\Definition; | ||
use Symfony\Component\DependencyInjection\LazyProxy\PhpDumper\LazyServiceDumper; | ||
use Symfony\Component\VarExporter\LazyGhostObjectInterface; | ||
use Symfony\Component\VarExporter\LazyGhostObjectTrait; | ||
|
||
/** | ||
* @author Nicolas Grekas <[email protected]> | ||
*/ | ||
final class LazyServiceInstantiator implements InstantiatorInterface | ||
{ | ||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function instantiateProxy(ContainerInterface $container, Definition $definition, string $id, callable $realInstantiator): object | ||
{ | ||
$dumper = new LazyServiceDumper(); | ||
|
||
if ($dumper->useProxyManager($definition)) { | ||
return (new RuntimeInstantiator())->instantiateProxy($container, $definition, $id, $realInstantiator); | ||
} | ||
|
||
if (!class_exists($proxyClass = $dumper->getProxyClass($definition), false)) { | ||
eval(sprintf('class %s extends %s implements %s { use %s; }', $proxyClass, $definition->getClass(), LazyGhostObjectInterface::class, LazyGhostObjectTrait::class)); | ||
} | ||
|
||
return $proxyClass::createLazyGhostObject($realInstantiator); | ||
} | ||
} |
This file contains hidden or 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 |
---|---|---|
|
@@ -14,12 +14,16 @@ | |
use Symfony\Component\DependencyInjection\ContainerInterface; | ||
use Symfony\Component\DependencyInjection\Definition; | ||
|
||
trigger_deprecation('symfony/dependency-injection', '6.2', 'The "%s" class is deprecated, use "%s" instead.', RealServiceInstantiator::class, LazyServiceInstantiator::class); | ||
|
||
/** | ||
* {@inheritdoc} | ||
* | ||
* Noop proxy instantiator - produces the real service instead of a proxy instance. | ||
* | ||
* @author Marco Pivetta <[email protected]> | ||
* | ||
* @deprecated since Symfony 6.2, use LazyServiceInstantiator instead. | ||
*/ | ||
class RealServiceInstantiator implements InstantiatorInterface | ||
{ | ||
|
153 changes: 153 additions & 0 deletions
153
src/Symfony/Component/DependencyInjection/LazyProxy/PhpDumper/LazyServiceDumper.php
This file contains hidden or 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,153 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\DependencyInjection\LazyProxy\PhpDumper; | ||
|
||
use Symfony\Bridge\ProxyManager\LazyProxy\PhpDumper\ProxyDumper; | ||
use Symfony\Component\DependencyInjection\Definition; | ||
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException; | ||
use Symfony\Component\DependencyInjection\Exception\LogicException; | ||
use Symfony\Component\VarExporter\LazyGhostObjectInterface; | ||
use Symfony\Component\VarExporter\LazyGhostObjectTrait; | ||
|
||
/** | ||
* @author Nicolas Grekas <[email protected]> | ||
*/ | ||
final class LazyServiceDumper implements DumperInterface | ||
{ | ||
public function __construct( | ||
private string $salt = '', | ||
) { | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function isProxyCandidate(Definition $definition, bool &$asGhostObject = null): bool | ||
{ | ||
$asGhostObject = false; | ||
|
||
if ($definition->hasTag('proxy')) { | ||
if (!$definition->isLazy()) { | ||
throw new InvalidArgumentException(sprintf('Invalid definition for service of class "%s": setting the "proxy" tag on a service requires it to be "lazy".', $definition->getClass())); | ||
} | ||
|
||
return true; | ||
} | ||
|
||
if (!$definition->isLazy()) { | ||
return false; | ||
} | ||
|
||
if (!($class = $definition->getClass()) || !(class_exists($class) || interface_exists($class, false))) { | ||
return false; | ||
} | ||
|
||
$class = new \ReflectionClass($class); | ||
|
||
if ($class->isFinal()) { | ||
throw new InvalidArgumentException(sprintf('Cannot make service of class "%s" lazy because the class is final.', $definition->getClass())); | ||
} | ||
|
||
if ($asGhostObject = !$class->isAbstract() && !$class->isInterface() && (\stdClass::class === $class->name || !$class->isInternal())) { | ||
while ($class = $class->getParentClass()) { | ||
if (!$asGhostObject = \stdClass::class === $class->name || !$class->isInternal()) { | ||
break; | ||
} | ||
} | ||
} | ||
|
||
return true; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getProxyFactoryCode(Definition $definition, string $id, string $factoryCode): string | ||
{ | ||
if ($dumper = $this->useProxyManager($definition)) { | ||
return $dumper->getProxyFactoryCode($definition, $id, $factoryCode); | ||
} | ||
|
||
$instantiation = 'return'; | ||
|
||
if ($definition->isShared()) { | ||
$instantiation .= sprintf(' $this->%s[%s] =', $definition->isPublic() && !$definition->isPrivate() ? 'services' : 'privates', var_export($id, true)); | ||
} | ||
|
||
$proxyClass = $this->getProxyClass($definition); | ||
|
||
if (preg_match('/^\$this->\w++\(\$proxy\)$/', $factoryCode)) { | ||
$factoryCode = substr_replace($factoryCode, '(...)', -8); | ||
} else { | ||
$factoryCode = sprintf('function ($proxy) { return %s; }', $factoryCode); | ||
} | ||
|
||
return <<<EOF | ||
if (true === \$lazyLoad) { | ||
$instantiation \$this->createProxy('$proxyClass', function () { | ||
return \\$proxyClass::createLazyGhostObject($factoryCode); | ||
}); | ||
} | ||
|
||
|
||
EOF; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getProxyCode(Definition $definition): string | ||
{ | ||
if ($dumper = $this->useProxyManager($definition)) { | ||
return $dumper->getProxyCode($definition); | ||
} | ||
|
||
$proxyClass = $this->getProxyClass($definition); | ||
|
||
return sprintf(<<<EOF | ||
class %s extends \%s implements \%s | ||
{ | ||
use \%s; | ||
} | ||
|
||
EOF, | ||
$proxyClass, | ||
$definition->getClass(), | ||
LazyGhostObjectInterface::class, | ||
LazyGhostObjectTrait::class | ||
); | ||
} | ||
|
||
public function getProxyClass(Definition $definition): string | ||
{ | ||
$class = (new \ReflectionClass($definition->getClass()))->name; | ||
|
||
return preg_replace('/^.*\\\\/', '', $class).'_'.substr(hash('sha256', $this->salt.'+'.$class), -7); | ||
} | ||
|
||
public function useProxyManager(Definition $definition): ?ProxyDumper | ||
{ | ||
if (!$this->isProxyCandidate($definition, $asGhostObject)) { | ||
throw new InvalidArgumentException(sprintf('Cannot instantiate lazy proxy for service of class "%s".', $definition->getClass())); | ||
} | ||
|
||
if ($asGhostObject) { | ||
return null; | ||
} | ||
|
||
if (!class_exists(ProxyDumper::class)) { | ||
throw new LogicException('You cannot use virtual proxies for lazy services as the ProxyManager bridge is not installed. Try running "composer require symfony/proxy-manager-bridge".'); | ||
} | ||
|
||
return new ProxyDumper($this->salt); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.