diff --git a/src/Symfony/Bridge/ProxyManager/LazyProxy/PhpDumper/ProxyDumper.php b/src/Symfony/Bridge/ProxyManager/LazyProxy/PhpDumper/ProxyDumper.php index b5f3560a4f401..fd610860f534f 100644 --- a/src/Symfony/Bridge/ProxyManager/LazyProxy/PhpDumper/ProxyDumper.php +++ b/src/Symfony/Bridge/ProxyManager/LazyProxy/PhpDumper/ProxyDumper.php @@ -53,7 +53,7 @@ public function getProxyFactoryCode(Definition $definition, string $id, string $ $instantiation = 'return'; if ($definition->isShared()) { - $instantiation .= sprintf(' $this->%s[%s] =', $definition->isPublic() && !$definition->isPrivate() ? 'services' : 'privates', var_export($id, true)); + $instantiation .= sprintf(' $container->%s[%s] =', $definition->isPublic() && !$definition->isPrivate() ? 'services' : 'privates', var_export($id, true)); } $proxifiedClass = new \ReflectionClass($this->proxyGenerator->getProxifiedClass($definition)); @@ -61,8 +61,9 @@ public function getProxyFactoryCode(Definition $definition, string $id, string $ return <<createProxy('$proxyClass', function () { - return \\$proxyClass::staticProxyConstructor(function (&\$wrappedInstance, \ProxyManager\Proxy\LazyLoadingInterface \$proxy) { + $instantiation \$container->createProxy('$proxyClass', static function () use (\$containerRef) { + return \\$proxyClass::staticProxyConstructor(static function (&\$wrappedInstance, \ProxyManager\Proxy\LazyLoadingInterface \$proxy) use (\$containerRef) { + \$container = \$containerRef->get(); \$wrappedInstance = $factoryCode; \$proxy->setProxyInitializer(null); diff --git a/src/Symfony/Bridge/ProxyManager/Tests/LazyProxy/Fixtures/php/lazy_service_structure.txt b/src/Symfony/Bridge/ProxyManager/Tests/LazyProxy/Fixtures/php/lazy_service_structure.txt index 825c1051ca38f..84995384157b6 100644 --- a/src/Symfony/Bridge/ProxyManager/Tests/LazyProxy/Fixtures/php/lazy_service_structure.txt +++ b/src/Symfony/Bridge/ProxyManager/Tests/LazyProxy/Fixtures/php/lazy_service_structure.txt @@ -3,12 +3,15 @@ use %a class LazyServiceProjectServiceContainer extends Container {%a - protected function getFooService($lazyLoad = true) + protected static function getFooService($container, $lazyLoad = true) { + $containerRef = $container->ref; + if (true === $lazyLoad) { - return $this->services['foo'] = $this->createProxy('stdClass_%s', function () { - return %S\stdClass_%s(function (&$wrappedInstance, \ProxyManager\Proxy\LazyLoadingInterface $proxy) { - $wrappedInstance = $this->getFooService(false); + return $container->services['foo'] = $container->createProxy('stdClass_%s', static function () use ($containerRef) { + return %S\stdClass_%s(static function (&$wrappedInstance, \ProxyManager\Proxy\LazyLoadingInterface $proxy) use ($containerRef) { + $container = $containerRef->get(); + $wrappedInstance = self::getFooService($containerRef->get(), false); $proxy->setProxyInitializer(null); diff --git a/src/Symfony/Bridge/ProxyManager/Tests/LazyProxy/PhpDumper/Fixtures/proxy-factory.php b/src/Symfony/Bridge/ProxyManager/Tests/LazyProxy/PhpDumper/Fixtures/proxy-factory.php index c06cb534b6e79..12a7de3483761 100644 --- a/src/Symfony/Bridge/ProxyManager/Tests/LazyProxy/PhpDumper/Fixtures/proxy-factory.php +++ b/src/Symfony/Bridge/ProxyManager/Tests/LazyProxy/PhpDumper/Fixtures/proxy-factory.php @@ -7,10 +7,14 @@ public function getFooService($lazyLoad = true) { + $container = $this; + $containerRef = \WeakReference::create($this); + if (true === $lazyLoad) { - return $this->privates['foo'] = $this->createProxy('SunnyInterface_1eff735', function () { - return \SunnyInterface_1eff735::staticProxyConstructor(function (&$wrappedInstance, \ProxyManager\Proxy\LazyLoadingInterface $proxy) { - $wrappedInstance = $this->getFooService(false); + return $container->privates['foo'] = $container->createProxy('SunnyInterface_1eff735', static function () use ($containerRef) { + return \SunnyInterface_1eff735::staticProxyConstructor(static function (&$wrappedInstance, \ProxyManager\Proxy\LazyLoadingInterface $proxy) use ($containerRef) { + $container = $containerRef->get(); + $wrappedInstance = $container->getFooService(false); $proxy->setProxyInitializer(null); diff --git a/src/Symfony/Bridge/ProxyManager/Tests/LazyProxy/PhpDumper/ProxyDumperTest.php b/src/Symfony/Bridge/ProxyManager/Tests/LazyProxy/PhpDumper/ProxyDumperTest.php index 61630b3e46938..7fd212c3f9d3e 100644 --- a/src/Symfony/Bridge/ProxyManager/Tests/LazyProxy/PhpDumper/ProxyDumperTest.php +++ b/src/Symfony/Bridge/ProxyManager/Tests/LazyProxy/PhpDumper/ProxyDumperTest.php @@ -72,10 +72,10 @@ public function testGetProxyFactoryCode() $definition->setLazy(true); - $code = $this->dumper->getProxyFactoryCode($definition, 'foo', '$this->getFoo2Service(false)'); + $code = $this->dumper->getProxyFactoryCode($definition, 'foo', '$container->getFoo2Service(false)'); $this->assertStringMatchesFormat( - '%A$wrappedInstance = $this->getFoo2Service(false);%w$proxy->setProxyInitializer(null);%A', + '%A$wrappedInstance = $container->getFoo2Service(false);%w$proxy->setProxyInitializer(null);%A', $code ); } @@ -87,9 +87,9 @@ public function testCorrectAssigning(Definition $definition, $access) { $definition->setLazy(true); - $code = $this->dumper->getProxyFactoryCode($definition, 'foo', '$this->getFoo2Service(false)'); + $code = $this->dumper->getProxyFactoryCode($definition, 'foo', '$container->getFoo2Service(false)'); - $this->assertStringMatchesFormat('%A$this->'.$access.'[\'foo\'] = %A', $code); + $this->assertStringMatchesFormat('%A$container->'.$access.'[\'foo\'] = %A', $code); } public function getPrivatePublicDefinitions() @@ -118,7 +118,7 @@ public function testGetProxyFactoryCodeForInterface() $definition->addTag('proxy', ['interface' => SunnyInterface::class]); $implem = "dumper->getProxyCode($definition); - $factory = $this->dumper->getProxyFactoryCode($definition, 'foo', '$this->getFooService(false)'); + $factory = $this->dumper->getProxyFactoryCode($definition, 'foo', '$container->getFooService(false)'); $factory = <<=8.1", "friendsofphp/proxy-manager-lts": "^1.0.2", - "symfony/dependency-injection": "^6.2", + "symfony/dependency-injection": "^6.3", "symfony/deprecation-contracts": "^2.1|^3" }, "require-dev": { diff --git a/src/Symfony/Component/DependencyInjection/Container.php b/src/Symfony/Component/DependencyInjection/Container.php index 9c830e77c8fe2..1cb5ca7403a76 100644 --- a/src/Symfony/Component/DependencyInjection/Container.php +++ b/src/Symfony/Component/DependencyInjection/Container.php @@ -63,6 +63,8 @@ class Container implements ContainerInterface, ResetInterface private bool $compiled = false; private \Closure $getEnv; + private static $make; + public function __construct(ParameterBagInterface $parameterBag = null) { $this->parameterBag = $parameterBag ?? new EnvPlaceholderParameterBag(); @@ -135,7 +137,7 @@ public function set(string $id, ?object $service) if (isset($this->privates['service_container']) && $this->privates['service_container'] instanceof \Closure) { $initialize = $this->privates['service_container']; unset($this->privates['service_container']); - $initialize(); + $initialize($this); } if ('service_container' === $id) { @@ -195,7 +197,7 @@ public function get(string $id, int $invalidBehavior = self::EXCEPTION_ON_INVALI { return $this->services[$id] ?? $this->services[$id = $this->aliases[$id] ?? $id] - ?? ('service_container' === $id ? $this : ($this->factories[$id] ?? $this->make(...))($id, $invalidBehavior)); + ?? ('service_container' === $id ? $this : ($this->factories[$id] ?? self::$make ??= self::make(...))($this, $id, $invalidBehavior)); } /** @@ -203,41 +205,41 @@ public function get(string $id, int $invalidBehavior = self::EXCEPTION_ON_INVALI * * As a separate method to allow "get()" to use the really fast `??` operator. */ - private function make(string $id, int $invalidBehavior) + private static function make($container, string $id, int $invalidBehavior) { - if (isset($this->loading[$id])) { - throw new ServiceCircularReferenceException($id, array_merge(array_keys($this->loading), [$id])); + if (isset($container->loading[$id])) { + throw new ServiceCircularReferenceException($id, array_merge(array_keys($container->loading), [$id])); } - $this->loading[$id] = true; + $container->loading[$id] = true; try { - if (isset($this->fileMap[$id])) { - return /* self::IGNORE_ON_UNINITIALIZED_REFERENCE */ 4 === $invalidBehavior ? null : $this->load($this->fileMap[$id]); - } elseif (isset($this->methodMap[$id])) { - return /* self::IGNORE_ON_UNINITIALIZED_REFERENCE */ 4 === $invalidBehavior ? null : $this->{$this->methodMap[$id]}(); + if (isset($container->fileMap[$id])) { + return /* self::IGNORE_ON_UNINITIALIZED_REFERENCE */ 4 === $invalidBehavior ? null : $container->load($container->fileMap[$id]); + } elseif (isset($container->methodMap[$id])) { + return /* self::IGNORE_ON_UNINITIALIZED_REFERENCE */ 4 === $invalidBehavior ? null : $container->{$container->methodMap[$id]}($container); } } catch (\Exception $e) { - unset($this->services[$id]); + unset($container->services[$id]); throw $e; } finally { - unset($this->loading[$id]); + unset($container->loading[$id]); } if (self::EXCEPTION_ON_INVALID_REFERENCE === $invalidBehavior) { if (!$id) { throw new ServiceNotFoundException($id); } - if (isset($this->syntheticIds[$id])) { + if (isset($container->syntheticIds[$id])) { throw new ServiceNotFoundException($id, null, null, [], sprintf('The "%s" service is synthetic, it needs to be set at boot time before it can be used.', $id)); } - if (isset($this->getRemovedIds()[$id])) { + if (isset($container->getRemovedIds()[$id])) { throw new ServiceNotFoundException($id, null, null, [], sprintf('The "%s" service or alias has been removed or inlined when the container was compiled. You should either make it public, or stop using the container directly and use dependency injection instead.', $id)); } $alternatives = []; - foreach ($this->getServiceIds() as $knownId) { + foreach ($container->getServiceIds() as $knownId) { if ('' === $knownId || '.' === $knownId[0]) { continue; } @@ -378,13 +380,13 @@ final protected function getService(string|false $registry, string $id, ?string return false !== $registry ? $this->{$registry}[$id] ?? null : null; } if (false !== $registry) { - return $this->{$registry}[$id] ??= $load ? $this->load($method) : $this->{$method}(); + return $this->{$registry}[$id] ??= $load ? $this->load($method) : $this->{$method}($this); } if (!$load) { - return $this->{$method}(); + return $this->{$method}($this); } - return ($factory = $this->factories[$id] ?? $this->factories['service_container'][$id] ?? null) ? $factory() : $this->load($method); + return ($factory = $this->factories[$id] ?? $this->factories['service_container'][$id] ?? null) ? $factory($this) : $this->load($method); } private function __clone() diff --git a/src/Symfony/Component/DependencyInjection/Dumper/PhpDumper.php b/src/Symfony/Component/DependencyInjection/Dumper/PhpDumper.php index ad7b82c95e3db..0cd43d1504cc2 100644 --- a/src/Symfony/Component/DependencyInjection/Dumper/PhpDumper.php +++ b/src/Symfony/Component/DependencyInjection/Dumper/PhpDumper.php @@ -67,7 +67,7 @@ class PhpDumper extends Dumper private int $variableCount; private ?\SplObjectStorage $inlinedDefinitions = null; private ?array $serviceCalls = null; - private array $reservedVariables = ['instance', 'class', 'this', 'container']; + private array $reservedVariables = ['instance', 'class', 'this', 'container', 'containerRef']; private ExpressionLanguage $expressionLanguage; private ?string $targetDirRegex = null; private int $targetDirMaxMatches; @@ -85,6 +85,7 @@ class PhpDumper extends Dumper private array $singleUsePrivateIds = []; private array $preload = []; private bool $addGetService = false; + private bool $addContainerRef = false; private array $locatedIds = []; private string $serviceLocatorTag; private array $exportedVariables = []; @@ -237,8 +238,8 @@ public function dump(array $options = []): string|array if ($this->addGetService) { $code = preg_replace( - "/(\r?\n\r?\n public function __construct.+?\\{\r?\n)/s", - "\n protected \Closure \$getService;$1 \$this->getService = \$this->getService(...);\n", + "/(\r?\n\r?\n public function __construct.+?\\{\r?\n) ++([^\r\n]++)/s", + "\n protected \Closure \$getService;$1 \$containerRef = $2\n \$this->getService = static function () use (\$containerRef) { return \$containerRef->get()->getService(...\\func_get_args()); };", $code, 1 ); @@ -679,7 +680,7 @@ private function addServiceInstance(string $id, Definition $definition, bool $is } if (!$isProxyCandidate && $definition->isShared() && !isset($this->singleUsePrivateIds[$id]) && null === $lastWitherIndex) { - $instantiation = sprintf('$this->%s[%s] = %s', $this->container->getDefinition($id)->isPublic() ? 'services' : 'privates', $this->doExport($id), $isSimpleInstance ? '' : '$instance'); + $instantiation = sprintf('$container->%s[%s] = %s', $this->container->getDefinition($id)->isPublic() ? 'services' : 'privates', $this->doExport($id), $isSimpleInstance ? '' : '$instance'); } elseif (!$isSimpleInstance) { $instantiation = '$instance'; } @@ -755,7 +756,7 @@ private function addServiceMethodCalls(Definition $definition, string $variableN if ($call[2] ?? false) { if (null !== $sharedNonLazyId && $lastWitherIndex === $k) { - $witherAssignation = sprintf('$this->%s[\'%s\'] = ', $definition->isPublic() ? 'services' : 'privates', $sharedNonLazyId); + $witherAssignation = sprintf('$container->%s[\'%s\'] = ', $definition->isPublic() ? 'services' : 'privates', $sharedNonLazyId); } $witherAssignation .= sprintf('$%s = ', $variableName); } @@ -847,10 +848,11 @@ private function addService(string $id, Definition $definition): array $methodName = $this->generateMethodName($id); if ($asFile || $definition->isLazy()) { - $lazyInitialization = '$lazyLoad = true'; + $lazyInitialization = ', $lazyLoad = true'; } else { $lazyInitialization = ''; } + $this->addContainerRef = false; $code = <<isShared()) { - $factory = sprintf('$this->factories%s[%s]', $definition->isPublic() ? '' : "['service_container']", $this->doExport($id)); + $factory = sprintf('$container->factories%s[%s]', $definition->isPublic() ? '' : "['service_container']", $this->doExport($id)); } $asGhostObject = false; @@ -903,16 +905,16 @@ protected function {$methodName}($lazyInitialization) $code .= sprintf(' %s ??= ', $factory); if ($asFile) { - $code .= "fn () => self::do(\$container);\n\n"; + $code .= "self::do(...);\n\n"; } else { - $code .= sprintf("\$this->%s(...);\n\n", $methodName); + $code .= sprintf("self::%s(...);\n\n", $methodName); } } $lazyLoad = $asGhostObject ? '$proxy' : 'false'; + $this->addContainerRef = true; - $factoryCode = $asFile ? sprintf('self::do($container, %s)', $lazyLoad) : sprintf('$this->%s(%s)', $methodName, $lazyLoad); - $factoryCode = $this->getProxyDumper()->getProxyFactoryCode($definition, $id, $factoryCode); - $code .= $asFile ? preg_replace('/function \(([^)]*+)\)( {|:)/', 'function (\1) use ($container)\2', $factoryCode) : $factoryCode; + $factoryCode = $asFile ? sprintf('self::do($containerRef->get(), %s)', $lazyLoad) : sprintf('self::%s($containerRef->get(), %s)', $methodName, $lazyLoad); + $code .= $this->getProxyDumper()->getProxyFactoryCode($definition, $id, $factoryCode); } $c = $this->addServiceInclude($id, $definition, null !== $isProxyCandidate); @@ -932,24 +934,21 @@ protected function {$methodName}($lazyInitialization) if (!$isProxyCandidate && !$definition->isShared()) { $c = implode("\n", array_map(function ($line) { return $line ? ' '.$line : $line; }, explode("\n", $c))); - $lazyloadInitialization = $definition->isLazy() ? '$lazyLoad = true' : ''; + $lazyloadInitialization = $definition->isLazy() ? ', $lazyLoad = true' : ''; - $c = sprintf(" %s = function (%s) {\n%s };\n\n return %1\$s();\n", $factory, $lazyloadInitialization, $c); + $c = sprintf(" %s = static function (\$container%s) {\n%s };\n\n return %1\$s(\$container);\n", $factory, $lazyloadInitialization, $c); } $code .= $c; } - if ($asFile) { - $code = str_replace('$this', '$container', $code); - $code = preg_replace('/function \(([^)]*+)\)( {|:)/', 'function (\1) use ($container)\2', $code); - } - $code .= " }\n"; $this->definitionVariables = $this->inlinedDefinitions = null; $this->referenceVariables = $this->serviceCalls = null; + $code = preg_replace('/%container_ref%/', $this->addContainerRef ? "\n \$containerRef = \$container->ref;\n" : '', $code, 1); + return [$file, $code]; } @@ -1014,8 +1013,8 @@ private function addInlineReference(string $id, Definition $definition, string $ $code .= sprintf(<<<'EOTXT' - if (isset($this->%s[%s])) { - return $this->%1$s[%2$s]; + if (isset($container->%s[%s])) { + return $container->%1$s[%2$s]; } EOTXT @@ -1190,7 +1189,7 @@ private function addNewInstance(Definition $definition, string $return = '', str if (\is_string($callable) && str_starts_with($callable, '@=')) { return $return.sprintf('(($args = %s) ? (%s) : null)', $this->dumpValue(new ServiceLocatorArgument($definition->getArguments())), - $this->getExpressionLanguage()->compile(substr($callable, 2), ['this' => 'container', 'args' => 'args']) + $this->getExpressionLanguage()->compile(substr($callable, 2), ['container' => 'container', 'args' => 'args']) ).$tail; } @@ -1234,9 +1233,11 @@ private function startClass(string $class, string $baseClass, bool $hasProxyClas class $class extends $baseClass { protected \$parameters = []; + protected readonly \WeakReference \$ref; public function __construct() { + \$this->ref = \WeakReference::create(\$this); EOF; if ($this->asFiles) { @@ -1470,11 +1471,11 @@ private function addDeprecatedAliases(): string * * @return object The "$id" service. */ - protected function {$methodNameAlias}() + protected static function {$methodNameAlias}(\$container) { trigger_deprecation($packageExported, $versionExported, $messageExported); - return \$this->get($idExported); + return \$container->get($idExported); } EOF; @@ -1517,7 +1518,7 @@ private function addInlineRequires(bool $hasProxyClasses): string $code .= "\n include_once __DIR__.'/proxy-classes.php';"; } - return $code ? sprintf("\n \$this->privates['service_container'] = function () {%s\n };\n", $code) : ''; + return $code ? sprintf("\n \$this->privates['service_container'] = static function (\$container) {%s\n };\n", $code) : ''; } private function addDefaultParametersMethod(): string @@ -1537,7 +1538,7 @@ private function addDefaultParametersMethod(): string $export = $this->exportParameters([$value], '', 12, $hasEnum); $export = explode('0 => ', substr(rtrim($export, " ]\n"), 2, -1), 2); - if ($hasEnum || preg_match("/\\\$this->(?:getEnv\('(?:[-.\w\\\\]*+:)*+\w++'\)|targetDir\.'')/", $export[1])) { + if ($hasEnum || preg_match("/\\\$container->(?:getEnv\('(?:[-.\w\\\\]*+:)*+\w++'\)|targetDir\.'')/", $export[1])) { $dynamicPhp[$key] = sprintf('%s%s => %s,', $export[0], $this->export($key), $export[1]); } else { $php[] = sprintf('%s%s => %s,', $export[0], $this->export($key), $export[1]); @@ -1601,6 +1602,7 @@ public function getParameterBag(): ParameterBagInterface if ($dynamicPhp) { $loadedDynamicParameters = $this->exportParameters(array_combine(array_keys($dynamicPhp), array_fill(0, \count($dynamicPhp), false)), '', 8); $getDynamicParameter = <<<'EOF' + $container = $this; $value = match ($name) { %s default => throw new ParameterNotFoundException($name), @@ -1694,14 +1696,14 @@ private function getServiceConditionals(mixed $value): string if (!$this->container->hasDefinition($service)) { return 'false'; } - $conditions[] = sprintf('isset($this->%s[%s])', $this->container->getDefinition($service)->isPublic() ? 'services' : 'privates', $this->doExport($service)); + $conditions[] = sprintf('isset($container->%s[%s])', $this->container->getDefinition($service)->isPublic() ? 'services' : 'privates', $this->doExport($service)); } foreach (ContainerBuilder::getServiceConditionals($value) as $service) { if ($this->container->hasDefinition($service) && !$this->container->getDefinition($service)->isPublic()) { continue; } - $conditions[] = sprintf('$this->has(%s)', $this->doExport($service)); + $conditions[] = sprintf('$container->has(%s)', $this->doExport($service)); } if (!$conditions) { @@ -1789,20 +1791,25 @@ private function dumpValue(mixed $value, bool $interpolate = true): string $attribute = sprintf('#[\Closure(%s)] ', $attribute); } + $this->addContainerRef = true; - return sprintf("%sfunction ()%s {\n %s\n }", $attribute, $returnedType, $code); + return sprintf("%sstatic function () use (\$containerRef)%s {\n \$container = \$containerRef->get();\n\n %s\n }", $attribute, $returnedType, $code); } if ($value instanceof IteratorArgument) { $operands = [0]; $code = []; - $code[] = 'new RewindableGenerator(function () {'; if (!$values = $value->getValues()) { + $code[] = 'new RewindableGenerator(static function () {'; $code[] = ' return new \EmptyIterator();'; } else { + $this->addContainerRef = true; + $code[] = 'new RewindableGenerator(static function () use ($containerRef) {'; + $code[] = ' $container = $containerRef->get();'; + $code[] = ''; $countCode = []; - $countCode[] = 'function () {'; + $countCode[] = 'static function () use ($containerRef) {'; foreach ($values as $k => $v) { ($c = $this->getServiceConditionals($v)) ? $operands[] = "(int) ($c)" : ++$operands[0]; @@ -1814,6 +1821,8 @@ private function dumpValue(mixed $value, bool $interpolate = true): string } } + $countCode[] = ' $container = $containerRef->get();'; + $countCode[] = ''; $countCode[] = sprintf(' return %s;', implode(' + ', $operands)); $countCode[] = ' }'; } @@ -1850,7 +1859,7 @@ private function dumpValue(mixed $value, bool $interpolate = true): string } $this->addGetService = true; - return sprintf('new \%s($this->getService, [%s%s], [%s%s])', ServiceLocator::class, $serviceMap, $serviceMap ? "\n " : '', $serviceTypes, $serviceTypes ? "\n " : ''); + return sprintf('new \%s($container->getService, [%s%s], [%s%s])', ServiceLocator::class, $serviceMap, $serviceMap ? "\n " : '', $serviceTypes, $serviceTypes ? "\n " : ''); } } finally { [$this->definitionVariables, $this->referenceVariables] = $scope; @@ -1888,7 +1897,7 @@ private function dumpValue(mixed $value, bool $interpolate = true): string return $this->getServiceCall($id, $value); } elseif ($value instanceof Expression) { - return $this->getExpressionLanguage()->compile((string) $value, ['this' => 'container']); + return $this->getExpressionLanguage()->compile((string) $value, ['container' => 'container']); } elseif ($value instanceof Parameter) { return $this->dumpParameter($value); } elseif (true === $interpolate && \is_string($value)) { @@ -1945,12 +1954,12 @@ private function dumpParameter(string $name): string return $dumpedValue; } - if (!preg_match("/\\\$this->(?:getEnv\('(?:[-.\w\\\\]*+:)*+\w++'\)|targetDir\.'')/", $dumpedValue)) { - return sprintf('$this->parameters[%s]', $this->doExport($name)); + if (!preg_match("/\\\$container->(?:getEnv\('(?:[-.\w\\\\]*+:)*+\w++'\)|targetDir\.'')/", $dumpedValue)) { + return sprintf('$container->parameters[%s]', $this->doExport($name)); } } - return sprintf('$this->getParameter(%s)', $this->doExport($name)); + return sprintf('$container->getParameter(%s)', $this->doExport($name)); } private function getServiceCall(string $id, Reference $reference = null): string @@ -1960,12 +1969,12 @@ private function getServiceCall(string $id, Reference $reference = null): string } if ('service_container' === $id) { - return '$this'; + return '$container'; } if ($this->container->hasDefinition($id) && $definition = $this->container->getDefinition($id)) { if ($definition->isSynthetic()) { - $code = sprintf('$this->get(%s%s)', $this->doExport($id), null !== $reference ? ', '.$reference->getInvalidBehavior() : ''); + $code = sprintf('$container->get(%s%s)', $this->doExport($id), null !== $reference ? ', '.$reference->getInvalidBehavior() : ''); } elseif (null !== $reference && ContainerInterface::IGNORE_ON_UNINITIALIZED_REFERENCE === $reference->getInvalidBehavior()) { $code = 'null'; if (!$definition->isShared()) { @@ -1977,20 +1986,20 @@ private function getServiceCall(string $id, Reference $reference = null): string } $code = $this->addNewInstance($definition, '', $id); if ($definition->isShared() && !isset($this->singleUsePrivateIds[$id])) { - return sprintf('($this->%s[%s] ??= %s)', $definition->isPublic() ? 'services' : 'privates', $this->doExport($id), $code); + return sprintf('($container->%s[%s] ??= %s)', $definition->isPublic() ? 'services' : 'privates', $this->doExport($id), $code); } $code = "($code)"; } else { - $code = $this->asFiles && !$this->inlineFactories && !$this->isHotPath($definition) ? "\$this->load('%s')" : '$this->%s()'; + $code = $this->asFiles && !$this->inlineFactories && !$this->isHotPath($definition) ? "\$container->load('%s')" : 'self::%s($container)'; $code = sprintf($code, $this->generateMethodName($id)); if (!$definition->isShared()) { - $factory = sprintf('$this->factories%s[%s]', $definition->isPublic() ? '' : "['service_container']", $this->doExport($id)); - $code = sprintf('(isset(%s) ? %1$s() : %s)', $factory, $code); + $factory = sprintf('$container->factories%s[%s]', $definition->isPublic() ? '' : "['service_container']", $this->doExport($id)); + $code = sprintf('(isset(%s) ? %1$s($container) : %s)', $factory, $code); } } if ($definition->isShared() && !isset($this->singleUsePrivateIds[$id])) { - $code = sprintf('($this->%s[%s] ?? %s)', $definition->isPublic() ? 'services' : 'privates', $this->doExport($id), $code); + $code = sprintf('($container->%s[%s] ?? %s)', $definition->isPublic() ? 'services' : 'privates', $this->doExport($id), $code); } return $code; @@ -1999,12 +2008,12 @@ private function getServiceCall(string $id, Reference $reference = null): string return 'null'; } if (null !== $reference && ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE < $reference->getInvalidBehavior()) { - $code = sprintf('$this->get(%s, ContainerInterface::NULL_ON_INVALID_REFERENCE)', $this->doExport($id)); + $code = sprintf('$container->get(%s, ContainerInterface::NULL_ON_INVALID_REFERENCE)', $this->doExport($id)); } else { - $code = sprintf('$this->get(%s)', $this->doExport($id)); + $code = sprintf('$container->get(%s)', $this->doExport($id)); } - return sprintf('($this->services[%s] ?? %s)', $this->doExport($id), $code); + return sprintf('($container->services[%s] ?? %s)', $this->doExport($id), $code); } /** @@ -2095,7 +2104,7 @@ private function getExpressionLanguage(): ExpressionLanguage return $this->getServiceCall($id); } - return sprintf('$this->get(%s)', $arg); + return sprintf('$container->get(%s)', $arg); }); if ($this->container->isTrackingResources()) { @@ -2147,13 +2156,13 @@ private function export(mixed $value): mixed $suffix = isset($value[$suffix]) ? '.'.$this->doExport(substr($value, $suffix), true) : ''; } - $dirname = $this->asFiles ? '$this->containerDir' : '__DIR__'; + $dirname = $this->asFiles ? '$container->containerDir' : '__DIR__'; $offset = 2 + $this->targetDirMaxMatches - \count($matches); if (0 < $offset) { $dirname = sprintf('\dirname(__DIR__, %d)', $offset + (int) $this->asFiles); } elseif ($this->asFiles) { - $dirname = "\$this->targetDir.''"; // empty string concatenation on purpose + $dirname = "\$container->targetDir.''"; // empty string concatenation on purpose } if ($prefix || $suffix) { @@ -2179,21 +2188,13 @@ private function doExport(mixed $value, bool $resolveEnv = false): mixed } else { $export = var_export($value, true); } - if ($this->asFiles) { - if (str_contains($export, '$this')) { - $export = str_replace('$this', "$'.'this", $export); - } - if (str_contains($export, 'function () {')) { - $export = str_replace('function () {', "function ('.') {", $export); - } - } - if ($resolveEnv && "'" === $export[0] && $export !== $resolvedExport = $this->container->resolveEnvPlaceholders($export, "'.\$this->getEnv('string:%s').'")) { + if ($resolveEnv && "'" === $export[0] && $export !== $resolvedExport = $this->container->resolveEnvPlaceholders($export, "'.\$container->getEnv('string:%s').'")) { $export = $resolvedExport; if (str_ends_with($export, ".''")) { $export = substr($export, 0, -3); if ("'" === $export[1]) { - $export = substr_replace($export, '', 18, 7); + $export = substr_replace($export, '', 23, 7); } } if ("'" === $export[1]) { diff --git a/src/Symfony/Component/DependencyInjection/ExpressionLanguageProvider.php b/src/Symfony/Component/DependencyInjection/ExpressionLanguageProvider.php index f476c5fe6e0d4..05028781a340d 100644 --- a/src/Symfony/Component/DependencyInjection/ExpressionLanguageProvider.php +++ b/src/Symfony/Component/DependencyInjection/ExpressionLanguageProvider.php @@ -40,19 +40,19 @@ public function getFunctions(): array { return [ new ExpressionFunction('service', $this->serviceCompiler ?? function ($arg) { - return sprintf('$this->get(%s)', $arg); + return sprintf('$container->get(%s)', $arg); }, function (array $variables, $value) { return $variables['container']->get($value); }), new ExpressionFunction('parameter', function ($arg) { - return sprintf('$this->getParameter(%s)', $arg); + return sprintf('$container->getParameter(%s)', $arg); }, function (array $variables, $value) { return $variables['container']->getParameter($value); }), new ExpressionFunction('env', function ($arg) { - return sprintf('$this->getEnv(%s)', $arg); + return sprintf('$container->getEnv(%s)', $arg); }, function (array $variables, $value) { if (!$this->getEnv) { throw new LogicException('You need to pass a getEnv closure to the expression langage provider to use the "env" function.'); diff --git a/src/Symfony/Component/DependencyInjection/LazyProxy/PhpDumper/LazyServiceDumper.php b/src/Symfony/Component/DependencyInjection/LazyProxy/PhpDumper/LazyServiceDumper.php index eeef902b0f889..09426c7e4627e 100644 --- a/src/Symfony/Component/DependencyInjection/LazyProxy/PhpDumper/LazyServiceDumper.php +++ b/src/Symfony/Component/DependencyInjection/LazyProxy/PhpDumper/LazyServiceDumper.php @@ -69,7 +69,7 @@ public function getProxyFactoryCode(Definition $definition, string $id, string $ $instantiation = 'return'; if ($definition->isShared()) { - $instantiation .= sprintf(' $this->%s[%s] =', $definition->isPublic() && !$definition->isPrivate() ? 'services' : 'privates', var_export($id, true)); + $instantiation .= sprintf(' $container->%s[%s] =', $definition->isPublic() && !$definition->isPrivate() ? 'services' : 'privates', var_export($id, true)); } $asGhostObject = str_contains($factoryCode, '$proxy'); @@ -78,22 +78,18 @@ public function getProxyFactoryCode(Definition $definition, string $id, string $ if (!$asGhostObject) { return <<createProxy('$proxyClass', fn () => \\$proxyClass::createLazyProxy(fn () => $factoryCode)); + $instantiation \$container->createProxy('$proxyClass', static fn () => \\$proxyClass::createLazyProxy(static fn () => $factoryCode)); } EOF; } - if (preg_match('/^\$this->\w++\(\$proxy\)$/', $factoryCode)) { - $factoryCode = substr_replace($factoryCode, '(...)', -8); - } else { - $factoryCode = sprintf('fn ($proxy) => %s', $factoryCode); - } + $factoryCode = sprintf('static fn ($proxy) => %s', $factoryCode); return <<createProxy('$proxyClass', fn () => \\$proxyClass::createLazyGhost($factoryCode)); + $instantiation \$container->createProxy('$proxyClass', static fn () => \\$proxyClass::createLazyGhost($factoryCode)); } diff --git a/src/Symfony/Component/DependencyInjection/Tests/Dumper/PhpDumperTest.php b/src/Symfony/Component/DependencyInjection/Tests/Dumper/PhpDumperTest.php index 6be39f17e0a6d..b6cf6678fd5e5 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/Dumper/PhpDumperTest.php +++ b/src/Symfony/Component/DependencyInjection/Tests/Dumper/PhpDumperTest.php @@ -1243,6 +1243,7 @@ public function testDumpHandlesEnumeration() %A private function getDynamicParameter(string $name) { + $container = $this; $value = match ($name) { 'unit_enum' => \Symfony\Component\DependencyInjection\Tests\Fixtures\FooUnitEnum::BAR, 'enum_array' => [ diff --git a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/closure.php b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/closure.php index cb9a3805400be..41cae53e5dcc4 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/closure.php +++ b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/closure.php @@ -15,9 +15,11 @@ class ProjectServiceContainer extends Container { protected $parameters = []; + protected readonly \WeakReference $ref; public function __construct() { + $this->ref = \WeakReference::create($this); $this->services = $this->privates = []; $this->methodMap = [ 'closure' => 'getClosureService', @@ -48,8 +50,8 @@ public function getRemovedIds(): array * * @return \Closure */ - protected function getClosureService() + protected static function getClosureService($container) { - return $this->services['closure'] = (new \stdClass())->__invoke(...); + return $container->services['closure'] = (new \stdClass())->__invoke(...); } } diff --git a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/custom_container_class_constructor_without_arguments.php b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/custom_container_class_constructor_without_arguments.php index 8bdda6b602b9b..3745b291db52f 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/custom_container_class_constructor_without_arguments.php +++ b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/custom_container_class_constructor_without_arguments.php @@ -17,9 +17,11 @@ class ProjectServiceContainer extends \Symfony\Component\DependencyInjection\Tests\Fixtures\Container\ConstructorWithoutArgumentsContainer { protected $parameters = []; + protected readonly \WeakReference $ref; public function __construct() { + $this->ref = \WeakReference::create($this); parent::__construct(); $this->parameterBag = null; diff --git a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/custom_container_class_with_mandatory_constructor_arguments.php b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/custom_container_class_with_mandatory_constructor_arguments.php index 85b8bc6dae27d..482316c0ff1a0 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/custom_container_class_with_mandatory_constructor_arguments.php +++ b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/custom_container_class_with_mandatory_constructor_arguments.php @@ -17,9 +17,11 @@ class ProjectServiceContainer extends \Symfony\Component\DependencyInjection\Tests\Fixtures\Container\ConstructorWithMandatoryArgumentsContainer { protected $parameters = []; + protected readonly \WeakReference $ref; public function __construct() { + $this->ref = \WeakReference::create($this); $this->services = $this->privates = []; $this->aliases = []; diff --git a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/custom_container_class_with_optional_constructor_arguments.php b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/custom_container_class_with_optional_constructor_arguments.php index 6aeea149719b9..adb80f267dd0b 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/custom_container_class_with_optional_constructor_arguments.php +++ b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/custom_container_class_with_optional_constructor_arguments.php @@ -17,9 +17,11 @@ class ProjectServiceContainer extends \Symfony\Component\DependencyInjection\Tests\Fixtures\Container\ConstructorWithOptionalArgumentsContainer { protected $parameters = []; + protected readonly \WeakReference $ref; public function __construct() { + $this->ref = \WeakReference::create($this); parent::__construct(); $this->parameterBag = null; diff --git a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/custom_container_class_without_constructor.php b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/custom_container_class_without_constructor.php index ee47766101427..ff3750b0847cb 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/custom_container_class_without_constructor.php +++ b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/custom_container_class_without_constructor.php @@ -17,9 +17,11 @@ class ProjectServiceContainer extends \Symfony\Component\DependencyInjection\Tests\Fixtures\Container\NoConstructorContainer { protected $parameters = []; + protected readonly \WeakReference $ref; public function __construct() { + $this->ref = \WeakReference::create($this); $this->services = $this->privates = []; $this->aliases = []; diff --git a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services1-1.php b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services1-1.php index da000bf8e41e3..98ea8c666ba36 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services1-1.php +++ b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services1-1.php @@ -17,9 +17,11 @@ class Container extends \Symfony\Component\DependencyInjection\Dump\AbstractContainer { protected $parameters = []; + protected readonly \WeakReference $ref; public function __construct() { + $this->ref = \WeakReference::create($this); $this->services = $this->privates = []; $this->aliases = []; diff --git a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services1.php b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services1.php index 4cec1eb29b06e..a1398a57c976d 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services1.php +++ b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services1.php @@ -15,9 +15,11 @@ class ProjectServiceContainer extends Container { protected $parameters = []; + protected readonly \WeakReference $ref; public function __construct() { + $this->ref = \WeakReference::create($this); $this->services = $this->privates = []; $this->aliases = []; diff --git a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services10.php b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services10.php index f391e0d81e6de..a98229eb58979 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services10.php +++ b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services10.php @@ -15,9 +15,11 @@ class ProjectServiceContainer extends Container { protected $parameters = []; + protected readonly \WeakReference $ref; public function __construct() { + $this->ref = \WeakReference::create($this); $this->parameters = $this->getDefaultParameters(); $this->services = $this->privates = []; @@ -43,9 +45,9 @@ public function isCompiled(): bool * * @return \stdClass */ - protected function getTestService() + protected static function getTestService($container) { - return $this->services['test'] = new \stdClass(['only dot' => '.', 'concatenation as value' => '.\'\'.', 'concatenation from the start value' => '\'\'.', '.' => 'dot as a key', '.\'\'.' => 'concatenation as a key', '\'\'.' => 'concatenation from the start key', 'optimize concatenation' => 'string1-string2', 'optimize concatenation with empty string' => 'string1string2', 'optimize concatenation from the start' => 'start', 'optimize concatenation at the end' => 'end', 'new line' => 'string with '."\n".'new line']); + return $container->services['test'] = new \stdClass(['only dot' => '.', 'concatenation as value' => '.\'\'.', 'concatenation from the start value' => '\'\'.', '.' => 'dot as a key', '.\'\'.' => 'concatenation as a key', '\'\'.' => 'concatenation from the start key', 'optimize concatenation' => 'string1-string2', 'optimize concatenation with empty string' => 'string1string2', 'optimize concatenation from the start' => 'start', 'optimize concatenation at the end' => 'end', 'new line' => 'string with '."\n".'new line']); } public function getParameter(string $name): array|bool|string|int|float|\UnitEnum|null diff --git a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services10_as_files.txt b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services10_as_files.txt index 60742f551a2e1..b7c31e8137aa4 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services10_as_files.txt +++ b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services10_as_files.txt @@ -27,9 +27,13 @@ class getClosureService extends ProjectServiceContainer */ public static function do($container, $lazyLoad = true) { + $containerRef = $container->ref; + $container->services['closure'] = $instance = new \stdClass(); - $instance->closures = [0 => #[\Closure(name: 'foo', class: 'FooClass')] function () use ($container): ?\stdClass { + $instance->closures = [0 => #[\Closure(name: 'foo', class: 'FooClass')] static function () use ($containerRef): ?\stdClass { + $container = $containerRef->get(); + return ($container->services['foo'] ?? null); }]; @@ -59,9 +63,11 @@ class ProjectServiceContainer extends Container protected $targetDir; protected $parameters = []; private $buildParameters; + protected readonly \WeakReference $ref; public function __construct(array $buildParameters = [], $containerDir = __DIR__) { + $this->ref = \WeakReference::create($this); $this->buildParameters = $buildParameters; $this->containerDir = $containerDir; $this->targetDir = \dirname($containerDir); @@ -113,9 +119,9 @@ class ProjectServiceContainer extends Container * * @return \FooClass */ - protected function getFooService() + protected static function getFooService($container) { - return $this->services['foo'] = new \FooClass(new \stdClass()); + return $container->services['foo'] = new \FooClass(new \stdClass()); } } diff --git a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services12.php b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services12.php index 18f89e9caf754..d2a31115d1187 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services12.php +++ b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services12.php @@ -15,9 +15,11 @@ class ProjectServiceContainer extends Container { protected $parameters = []; + protected readonly \WeakReference $ref; public function __construct() { + $this->ref = \WeakReference::create($this); $this->parameters = $this->getDefaultParameters(); $this->services = $this->privates = []; @@ -43,9 +45,9 @@ public function isCompiled(): bool * * @return \stdClass */ - protected function getTestService() + protected static function getTestService($container) { - return $this->services['test'] = new \stdClass(('file://'.\dirname(__DIR__, 1)), [('file://'.\dirname(__DIR__, 1)) => (\dirname(__DIR__, 2).'/')]); + return $container->services['test'] = new \stdClass(('file://'.\dirname(__DIR__, 1)), [('file://'.\dirname(__DIR__, 1)) => (\dirname(__DIR__, 2).'/')]); } public function getParameter(string $name): array|bool|string|int|float|\UnitEnum|null diff --git a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services13.php b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services13.php index 9af8e4cd09f71..de0550de9b79c 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services13.php +++ b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services13.php @@ -15,9 +15,11 @@ class ProjectServiceContainer extends Container { protected $parameters = []; + protected readonly \WeakReference $ref; public function __construct() { + $this->ref = \WeakReference::create($this); $this->services = $this->privates = []; $this->methodMap = [ 'bar' => 'getBarService', @@ -48,11 +50,11 @@ public function getRemovedIds(): array * * @return \stdClass */ - protected function getBarService() + protected static function getBarService($container) { $a = new \stdClass(); - $a->add($this); + $a->add($container); - return $this->services['bar'] = new \stdClass($a); + return $container->services['bar'] = new \stdClass($a); } } diff --git a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services19.php b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services19.php index cc56de79dfea3..328a9180ac5ab 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services19.php +++ b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services19.php @@ -15,9 +15,11 @@ class ProjectServiceContainer extends Container { protected $parameters = []; + protected readonly \WeakReference $ref; public function __construct() { + $this->ref = \WeakReference::create($this); $this->parameters = $this->getDefaultParameters(); $this->services = $this->privates = []; @@ -44,9 +46,9 @@ public function isCompiled(): bool * * @return object A %env(FOO)% instance */ - protected function getServiceFromAnonymousFactoryService() + protected static function getServiceFromAnonymousFactoryService($container) { - return $this->services['service_from_anonymous_factory'] = (new ${($_ = $this->getEnv('FOO')) && false ?: "_"}())->getInstance(); + return $container->services['service_from_anonymous_factory'] = (new ${($_ = $container->getEnv('FOO')) && false ?: "_"}())->getInstance(); } /** @@ -54,9 +56,9 @@ protected function getServiceFromAnonymousFactoryService() * * @return \Bar\FooClass */ - protected function getServiceWithMethodCallAndFactoryService() + protected static function getServiceWithMethodCallAndFactoryService($container) { - $this->services['service_with_method_call_and_factory'] = $instance = new \Bar\FooClass(); + $container->services['service_with_method_call_and_factory'] = $instance = new \Bar\FooClass(); $instance->setBar(\Bar\FooClass::getInstance()); @@ -105,8 +107,9 @@ public function getParameterBag(): ParameterBagInterface private function getDynamicParameter(string $name) { + $container = $this; $value = match ($name) { - 'foo' => $this->getEnv('FOO'), + 'foo' => $container->getEnv('FOO'), default => throw new ParameterNotFoundException($name), }; $this->loadedDynamicParameters[$name] = true; diff --git a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services24.php b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services24.php index 320bdc3897c5f..b2bfd9e0865af 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services24.php +++ b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services24.php @@ -15,9 +15,11 @@ 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', @@ -41,8 +43,8 @@ public function isCompiled(): bool * * @return \Foo */ - protected function getFooService() + protected static function getFooService($container) { - return $this->services['foo'] = new \Foo(); + return $container->services['foo'] = new \Foo(); } } diff --git a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services26.php b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services26.php index 29820767b8bf6..ad424a8e467a6 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services26.php +++ b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services26.php @@ -15,9 +15,11 @@ class Symfony_DI_PhpDumper_Test_EnvParameters extends Container { protected $parameters = []; + protected readonly \WeakReference $ref; public function __construct() { + $this->ref = \WeakReference::create($this); $this->parameters = $this->getDefaultParameters(); $this->services = $this->privates = []; @@ -44,9 +46,9 @@ public function isCompiled(): bool * * @return \Symfony\Component\DependencyInjection\Tests\Fixtures\Bar */ - protected function getBarService() + protected static function getBarService($container) { - return $this->services['bar'] = new \Symfony\Component\DependencyInjection\Tests\Fixtures\Bar($this->getEnv('QUZ')); + return $container->services['bar'] = new \Symfony\Component\DependencyInjection\Tests\Fixtures\Bar($container->getEnv('QUZ')); } /** @@ -54,9 +56,9 @@ protected function getBarService() * * @return object A %env(FOO)% instance */ - protected function getTestService() + protected static function getTestService($container) { - return $this->services['test'] = new ${($_ = $this->getEnv('FOO')) && false ?: "_"}($this->getEnv('Bar'), 'foo'.$this->getEnv('string:FOO').'baz', $this->getEnv('int:Baz')); + return $container->services['test'] = new ${($_ = $container->getEnv('FOO')) && false ?: "_"}($container->getEnv('Bar'), 'foo'.$container->getEnv('string:FOO').'baz', $container->getEnv('int:Baz')); } public function getParameter(string $name): array|bool|string|int|float|\UnitEnum|null @@ -104,11 +106,12 @@ public function getParameterBag(): ParameterBagInterface private function getDynamicParameter(string $name) { + $container = $this; $value = match ($name) { - 'bar' => $this->getEnv('FOO'), - 'baz' => $this->getEnv('int:Baz'), - 'json' => $this->getEnv('json:file:json_file'), - 'db_dsn' => $this->getEnv('resolve:DB'), + 'bar' => $container->getEnv('FOO'), + 'baz' => $container->getEnv('int:Baz'), + 'json' => $container->getEnv('json:file:json_file'), + 'db_dsn' => $container->getEnv('resolve:DB'), default => throw new ParameterNotFoundException($name), }; $this->loadedDynamicParameters[$name] = true; diff --git a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services33.php b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services33.php index 2e8c166d16ac4..eed5f3f6270dd 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services33.php +++ b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services33.php @@ -15,9 +15,11 @@ class ProjectServiceContainer extends Container { protected $parameters = []; + protected readonly \WeakReference $ref; public function __construct() { + $this->ref = \WeakReference::create($this); $this->services = $this->privates = []; $this->methodMap = [ 'Bar\\Foo' => 'getFooService', @@ -42,9 +44,9 @@ public function isCompiled(): bool * * @return \Bar\Foo */ - protected function getFooService() + protected static function getFooService($container) { - return $this->services['Bar\\Foo'] = new \Bar\Foo(); + return $container->services['Bar\\Foo'] = new \Bar\Foo(); } /** @@ -52,8 +54,8 @@ protected function getFooService() * * @return \Foo\Foo */ - protected function getFoo2Service() + protected static function getFoo2Service($container) { - return $this->services['Foo\\Foo'] = new \Foo\Foo(); + return $container->services['Foo\\Foo'] = new \Foo\Foo(); } } diff --git a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services8.php b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services8.php index 88dcb00075cb5..b5d86ce86b753 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services8.php +++ b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services8.php @@ -15,9 +15,11 @@ class ProjectServiceContainer extends Container { protected $parameters = []; + protected readonly \WeakReference $ref; public function __construct() { + $this->ref = \WeakReference::create($this); $this->parameters = $this->getDefaultParameters(); $this->services = $this->privates = []; diff --git a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services9_as_files.txt b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services9_as_files.txt index d4ef329a1ad29..1321a66ec1bce 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services9_as_files.txt +++ b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services9_as_files.txt @@ -39,7 +39,7 @@ class getBAR2Service extends ProjectServiceContainer { $container->services['BAR'] = $instance = new \stdClass(); - $instance->bar = ($container->services['bar'] ?? $container->getBarService()); + $instance->bar = ($container->services['bar'] ?? self::getBarService($container)); return $instance; } @@ -281,7 +281,7 @@ class getFooService extends ProjectServiceContainer $instance->foo = 'bar'; $instance->moo = $a; $instance->qux = ['bar' => 'foo is bar', 'foobar' => 'bar']; - $instance->setBar(($container->services['bar'] ?? $container->getBarService())); + $instance->setBar(($container->services['bar'] ?? self::getBarService($container))); $instance->initialize(); sc_configure($instance); @@ -319,11 +319,11 @@ class getFooBarService extends ProjectServiceContainer */ public static function do($container, $lazyLoad = true) { - $container->factories['foo_bar'] = function () use ($container) { + $container->factories['foo_bar'] = static function ($container) { return new \Bar\FooClass(($container->services['deprecated_service'] ?? $container->load('getDeprecatedServiceService'))); }; - return $container->factories['foo_bar'](); + return $container->factories['foo_bar']($container); } } @@ -361,10 +361,14 @@ class getLazyContextService extends ProjectServiceContainer */ public static function do($container, $lazyLoad = true) { - return $container->services['lazy_context'] = new \LazyContext(new RewindableGenerator(function () use ($container) { + $containerRef = $container->ref; + + return $container->services['lazy_context'] = new \LazyContext(new RewindableGenerator(static function () use ($containerRef) { + $container = $containerRef->get(); + yield 'k1' => ($container->services['foo.baz'] ?? $container->load('getFoo_BazService')); yield 'k2' => $container; - }, 2), new RewindableGenerator(function () use ($container) { + }, 2), new RewindableGenerator(static function () { return new \EmptyIterator(); }, 0)); } @@ -381,9 +385,13 @@ class getLazyContextIgnoreInvalidRefService extends ProjectServiceContainer */ public static function do($container, $lazyLoad = true) { - return $container->services['lazy_context_ignore_invalid_ref'] = new \LazyContext(new RewindableGenerator(function () use ($container) { + $containerRef = $container->ref; + + return $container->services['lazy_context_ignore_invalid_ref'] = new \LazyContext(new RewindableGenerator(static function () use ($containerRef) { + $container = $containerRef->get(); + yield 0 => ($container->services['foo.baz'] ?? $container->load('getFoo_BazService')); - }, 1), new RewindableGenerator(function () use ($container) { + }, 1), new RewindableGenerator(static function () { return new \EmptyIterator(); }, 0)); } @@ -447,11 +455,11 @@ class getNonSharedFooService extends ProjectServiceContainer { include_once $container->targetDir.''.'/Fixtures/includes/foo.php'; - $container->factories['non_shared_foo'] = function () use ($container) { + $container->factories['non_shared_foo'] = static function ($container) { return new \Bar\FooClass(); }; - return $container->factories['non_shared_foo'](); + return $container->factories['non_shared_foo']($container); } } @@ -511,7 +519,11 @@ class getTaggedIteratorService extends ProjectServiceContainer */ public static function do($container, $lazyLoad = true) { - return $container->services['tagged_iterator'] = new \Bar(new RewindableGenerator(function () use ($container) { + $containerRef = $container->ref; + + return $container->services['tagged_iterator'] = new \Bar(new RewindableGenerator(static function () use ($containerRef) { + $container = $containerRef->get(); + yield 0 => ($container->services['foo'] ?? $container->load('getFooService')); yield 1 => ($container->privates['tagged_iterator_foo'] ??= new \Bar()); }, 2)); @@ -555,9 +567,11 @@ class ProjectServiceContainer extends Container protected $targetDir; protected $parameters = []; private $buildParameters; + protected readonly \WeakReference $ref; public function __construct(array $buildParameters = [], $containerDir = __DIR__) { + $this->ref = \WeakReference::create($this); $this->buildParameters = $buildParameters; $this->containerDir = $containerDir; $this->targetDir = \dirname($containerDir); @@ -643,11 +657,11 @@ class ProjectServiceContainer extends Container * * @return \Bar\FooClass */ - protected function getBarService() + protected static function getBarService($container) { - $a = ($this->services['foo.baz'] ?? $this->load('getFoo_BazService')); + $a = ($container->services['foo.baz'] ?? $container->load('getFoo_BazService')); - $this->services['bar'] = $instance = new \Bar\FooClass('foo', $a, $this->getParameter('foo_bar')); + $container->services['bar'] = $instance = new \Bar\FooClass('foo', $a, $container->getParameter('foo_bar')); $a->configure($instance); diff --git a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services9_compiled.php b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services9_compiled.php index 4c4d4b2fbb06b..7b4c50ffb94d7 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services9_compiled.php +++ b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services9_compiled.php @@ -15,9 +15,11 @@ class ProjectServiceContainer extends Container { protected $parameters = []; + protected readonly \WeakReference $ref; public function __construct() { + $this->ref = \WeakReference::create($this); $this->parameters = $this->getDefaultParameters(); $this->services = $this->privates = []; @@ -90,11 +92,11 @@ public function getRemovedIds(): array * * @return \stdClass */ - protected function getBARService() + protected static function getBARService($container) { - $this->services['BAR'] = $instance = new \stdClass(); + $container->services['BAR'] = $instance = new \stdClass(); - $instance->bar = ($this->services['bar'] ?? $this->getBar3Service()); + $instance->bar = ($container->services['bar'] ?? self::getBar3Service($container)); return $instance; } @@ -104,9 +106,9 @@ protected function getBARService() * * @return \stdClass */ - protected function getBAR2Service() + protected static function getBAR2Service($container) { - return $this->services['BAR2'] = new \stdClass(); + return $container->services['BAR2'] = new \stdClass(); } /** @@ -114,9 +116,9 @@ protected function getBAR2Service() * * @return \Bar */ - protected function getAServiceService() + protected static function getAServiceService($container) { - return $this->services['a_service'] = ($this->privates['a_factory'] ??= new \Bar())->getBar(); + return $container->services['a_service'] = ($container->privates['a_factory'] ??= new \Bar())->getBar(); } /** @@ -124,9 +126,9 @@ protected function getAServiceService() * * @return \Bar */ - protected function getBServiceService() + protected static function getBServiceService($container) { - return $this->services['b_service'] = ($this->privates['a_factory'] ??= new \Bar())->getBar(); + return $container->services['b_service'] = ($container->privates['a_factory'] ??= new \Bar())->getBar(); } /** @@ -134,11 +136,11 @@ protected function getBServiceService() * * @return \Bar\FooClass */ - protected function getBar3Service() + protected static function getBar3Service($container) { - $a = ($this->services['foo.baz'] ?? $this->getFoo_BazService()); + $a = ($container->services['foo.baz'] ?? self::getFoo_BazService($container)); - $this->services['bar'] = $instance = new \Bar\FooClass('foo', $a, $this->getParameter('foo_bar')); + $container->services['bar'] = $instance = new \Bar\FooClass('foo', $a, $container->getParameter('foo_bar')); $a->configure($instance); @@ -150,9 +152,9 @@ protected function getBar3Service() * * @return \stdClass */ - protected function getBar22Service() + protected static function getBar22Service($container) { - return $this->services['bar2'] = new \stdClass(); + return $container->services['bar2'] = new \stdClass(); } /** @@ -160,11 +162,11 @@ protected function getBar22Service() * * @return \Baz */ - protected function getBazService() + protected static function getBazService($container) { - $this->services['baz'] = $instance = new \Baz(); + $container->services['baz'] = $instance = new \Baz(); - $instance->setFoo(($this->services['foo_with_inline'] ?? $this->getFooWithInlineService())); + $instance->setFoo(($container->services['foo_with_inline'] ?? self::getFooWithInlineService($container))); return $instance; } @@ -174,12 +176,12 @@ protected function getBazService() * * @return \stdClass */ - protected function getConfiguredServiceService() + protected static function getConfiguredServiceService($container) { - $this->services['configured_service'] = $instance = new \stdClass(); + $container->services['configured_service'] = $instance = new \stdClass(); $a = new \ConfClass(); - $a->setFoo(($this->services['baz'] ?? $this->getBazService())); + $a->setFoo(($container->services['baz'] ?? self::getBazService($container))); $a->configureStdClass($instance); @@ -191,9 +193,9 @@ protected function getConfiguredServiceService() * * @return \stdClass */ - protected function getConfiguredServiceSimpleService() + protected static function getConfiguredServiceSimpleService($container) { - $this->services['configured_service_simple'] = $instance = new \stdClass(); + $container->services['configured_service_simple'] = $instance = new \stdClass(); (new \ConfClass('bar'))->configureStdClass($instance); @@ -205,9 +207,9 @@ protected function getConfiguredServiceSimpleService() * * @return \stdClass */ - protected function getDecoratorServiceService() + protected static function getDecoratorServiceService($container) { - return $this->services['decorator_service'] = new \stdClass(); + return $container->services['decorator_service'] = new \stdClass(); } /** @@ -215,9 +217,9 @@ protected function getDecoratorServiceService() * * @return \stdClass */ - protected function getDecoratorServiceWithNameService() + protected static function getDecoratorServiceWithNameService($container) { - return $this->services['decorator_service_with_name'] = new \stdClass(); + return $container->services['decorator_service_with_name'] = new \stdClass(); } /** @@ -227,11 +229,11 @@ protected function getDecoratorServiceWithNameService() * * @deprecated Since vendor/package 1.1: The "deprecated_service" service is deprecated. You should stop using it, as it will be removed in the future. */ - protected function getDeprecatedServiceService() + protected static function getDeprecatedServiceService($container) { trigger_deprecation('vendor/package', '1.1', 'The "deprecated_service" service is deprecated. You should stop using it, as it will be removed in the future.'); - return $this->services['deprecated_service'] = new \stdClass(); + return $container->services['deprecated_service'] = new \stdClass(); } /** @@ -239,9 +241,9 @@ protected function getDeprecatedServiceService() * * @return \Bar */ - protected function getFactoryServiceService() + protected static function getFactoryServiceService($container) { - return $this->services['factory_service'] = ($this->services['foo.baz'] ?? $this->getFoo_BazService())->getInstance(); + return $container->services['factory_service'] = ($container->services['foo.baz'] ?? self::getFoo_BazService($container))->getInstance(); } /** @@ -249,9 +251,9 @@ protected function getFactoryServiceService() * * @return \Bar */ - protected function getFactoryServiceSimpleService() + protected static function getFactoryServiceSimpleService($container) { - return $this->services['factory_service_simple'] = $this->getFactorySimpleService()->getInstance(); + return $container->services['factory_service_simple'] = self::getFactorySimpleService($container)->getInstance(); } /** @@ -259,16 +261,16 @@ protected function getFactoryServiceSimpleService() * * @return \Bar\FooClass */ - protected function getFooService() + protected static function getFooService($container) { - $a = ($this->services['foo.baz'] ?? $this->getFoo_BazService()); + $a = ($container->services['foo.baz'] ?? self::getFoo_BazService($container)); - $this->services['foo'] = $instance = \Bar\FooClass::getInstance('foo', $a, ['bar' => 'foo is bar', 'foobar' => 'bar'], true, $this); + $container->services['foo'] = $instance = \Bar\FooClass::getInstance('foo', $a, ['bar' => 'foo is bar', 'foobar' => 'bar'], true, $container); $instance->foo = 'bar'; $instance->moo = $a; $instance->qux = ['bar' => 'foo is bar', 'foobar' => 'bar']; - $instance->setBar(($this->services['bar'] ?? $this->getBar3Service())); + $instance->setBar(($container->services['bar'] ?? self::getBar3Service($container))); $instance->initialize(); sc_configure($instance); @@ -280,9 +282,9 @@ protected function getFooService() * * @return \BazClass */ - protected function getFoo_BazService() + protected static function getFoo_BazService($container) { - $this->services['foo.baz'] = $instance = \BazClass::getInstance(); + $container->services['foo.baz'] = $instance = \BazClass::getInstance(); \BazClass::configureStatic1($instance); @@ -294,13 +296,13 @@ protected function getFoo_BazService() * * @return \Bar\FooClass */ - protected function getFooBarService() + protected static function getFooBarService($container) { - $this->factories['foo_bar'] = function () { - return new \Bar\FooClass(($this->services['deprecated_service'] ?? $this->getDeprecatedServiceService())); + $container->factories['foo_bar'] = static function ($container) { + return new \Bar\FooClass(($container->services['deprecated_service'] ?? self::getDeprecatedServiceService($container))); }; - return $this->factories['foo_bar'](); + return $container->factories['foo_bar']($container); } /** @@ -308,13 +310,13 @@ protected function getFooBarService() * * @return \Foo */ - protected function getFooWithInlineService() + protected static function getFooWithInlineService($container) { - $this->services['foo_with_inline'] = $instance = new \Foo(); + $container->services['foo_with_inline'] = $instance = new \Foo(); $a = new \Bar(); $a->pub = 'pub'; - $a->setBaz(($this->services['baz'] ?? $this->getBazService())); + $a->setBaz(($container->services['baz'] ?? self::getBazService($container))); $instance->setBar($a); @@ -326,12 +328,16 @@ protected function getFooWithInlineService() * * @return \LazyContext */ - protected function getLazyContextService() + protected static function getLazyContextService($container) { - return $this->services['lazy_context'] = new \LazyContext(new RewindableGenerator(function () { - yield 'k1' => ($this->services['foo.baz'] ?? $this->getFoo_BazService()); - yield 'k2' => $this; - }, 2), new RewindableGenerator(function () { + $containerRef = $container->ref; + + return $container->services['lazy_context'] = new \LazyContext(new RewindableGenerator(static function () use ($containerRef) { + $container = $containerRef->get(); + + yield 'k1' => ($container->services['foo.baz'] ?? self::getFoo_BazService($container)); + yield 'k2' => $container; + }, 2), new RewindableGenerator(static function () { return new \EmptyIterator(); }, 0)); } @@ -341,11 +347,15 @@ protected function getLazyContextService() * * @return \LazyContext */ - protected function getLazyContextIgnoreInvalidRefService() + protected static function getLazyContextIgnoreInvalidRefService($container) { - return $this->services['lazy_context_ignore_invalid_ref'] = new \LazyContext(new RewindableGenerator(function () { - yield 0 => ($this->services['foo.baz'] ?? $this->getFoo_BazService()); - }, 1), new RewindableGenerator(function () { + $containerRef = $container->ref; + + return $container->services['lazy_context_ignore_invalid_ref'] = new \LazyContext(new RewindableGenerator(static function () use ($containerRef) { + $container = $containerRef->get(); + + yield 0 => ($container->services['foo.baz'] ?? self::getFoo_BazService($container)); + }, 1), new RewindableGenerator(static function () { return new \EmptyIterator(); }, 0)); } @@ -355,15 +365,15 @@ protected function getLazyContextIgnoreInvalidRefService() * * @return \Bar\FooClass */ - protected function getMethodCall1Service() + protected static function getMethodCall1Service($container) { include_once '%path%foo.php'; - $this->services['method_call1'] = $instance = new \Bar\FooClass(); + $container->services['method_call1'] = $instance = new \Bar\FooClass(); - $instance->setBar(($this->services['foo'] ?? $this->getFooService())); + $instance->setBar(($container->services['foo'] ?? self::getFooService($container))); $instance->setBar(NULL); - $instance->setBar((($this->services['foo'] ?? $this->getFooService())->foo() . (($this->hasParameter("foo")) ? ($this->getParameter("foo")) : ("default")))); + $instance->setBar((($container->services['foo'] ?? self::getFooService($container))->foo() . (($container->hasParameter("foo")) ? ($container->getParameter("foo")) : ("default")))); return $instance; } @@ -373,12 +383,12 @@ protected function getMethodCall1Service() * * @return \FooBarBaz */ - protected function getNewFactoryServiceService() + protected static function getNewFactoryServiceService($container) { $a = new \FactoryClass(); $a->foo = 'bar'; - $this->services['new_factory_service'] = $instance = $a->getInstance(); + $container->services['new_factory_service'] = $instance = $a->getInstance(); $instance->foo = 'bar'; @@ -390,9 +400,9 @@ protected function getNewFactoryServiceService() * * @return \stdClass */ - protected function getPreloadSidekickService() + protected static function getPreloadSidekickService($container) { - return $this->services['preload_sidekick'] = new \stdClass(); + return $container->services['preload_sidekick'] = new \stdClass(); } /** @@ -400,9 +410,9 @@ protected function getPreloadSidekickService() * * @return \stdClass */ - protected function getRuntimeErrorService() + protected static function getRuntimeErrorService($container) { - return $this->services['runtime_error'] = new \stdClass(throw new RuntimeException('Service "errored_definition" is broken.')); + return $container->services['runtime_error'] = new \stdClass(throw new RuntimeException('Service "errored_definition" is broken.')); } /** @@ -410,9 +420,9 @@ protected function getRuntimeErrorService() * * @return \Bar\FooClass */ - protected function getServiceFromStaticMethodService() + protected static function getServiceFromStaticMethodService($container) { - return $this->services['service_from_static_method'] = \Bar\FooClass::getInstance(); + return $container->services['service_from_static_method'] = \Bar\FooClass::getInstance(); } /** @@ -420,11 +430,15 @@ protected function getServiceFromStaticMethodService() * * @return \Bar */ - protected function getTaggedIteratorService() + protected static function getTaggedIteratorService($container) { - return $this->services['tagged_iterator'] = new \Bar(new RewindableGenerator(function () { - yield 0 => ($this->services['foo'] ?? $this->getFooService()); - yield 1 => ($this->privates['tagged_iterator_foo'] ??= new \Bar()); + $containerRef = $container->ref; + + return $container->services['tagged_iterator'] = new \Bar(new RewindableGenerator(static function () use ($containerRef) { + $container = $containerRef->get(); + + yield 0 => ($container->services['foo'] ?? self::getFooService($container)); + yield 1 => ($container->privates['tagged_iterator_foo'] ??= new \Bar()); }, 2)); } @@ -435,7 +449,7 @@ protected function getTaggedIteratorService() * * @deprecated Since vendor/package 1.1: The "factory_simple" service is deprecated. You should stop using it, as it will be removed in the future. */ - protected function getFactorySimpleService() + protected static function getFactorySimpleService($container) { trigger_deprecation('vendor/package', '1.1', 'The "factory_simple" service is deprecated. You should stop using it, as it will be removed in the future.'); diff --git a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services9_inlined_factories.txt b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services9_inlined_factories.txt index 137506abdf4b0..920e4036507a4 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services9_inlined_factories.txt +++ b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services9_inlined_factories.txt @@ -40,9 +40,11 @@ class ProjectServiceContainer extends Container protected $targetDir; protected $parameters = []; private $buildParameters; + protected readonly \WeakReference $ref; public function __construct(array $buildParameters = [], $containerDir = __DIR__) { + $this->ref = \WeakReference::create($this); $this->buildParameters = $buildParameters; $this->containerDir = $containerDir; $this->targetDir = \dirname($containerDir); @@ -88,8 +90,8 @@ class ProjectServiceContainer extends Container 'decorated' => 'decorator_service_with_name', ]; - $this->privates['service_container'] = function () { - include_once $this->targetDir.''.'/Fixtures/includes/foo.php'; + $this->privates['service_container'] = static function ($container) { + include_once $container->targetDir.''.'/Fixtures/includes/foo.php'; }; } @@ -113,11 +115,11 @@ class ProjectServiceContainer extends Container * * @return \stdClass */ - protected function getBARService() + protected static function getBARService($container) { - $this->services['BAR'] = $instance = new \stdClass(); + $container->services['BAR'] = $instance = new \stdClass(); - $instance->bar = ($this->services['bar'] ?? $this->getBar3Service()); + $instance->bar = ($container->services['bar'] ?? self::getBar3Service($container)); return $instance; } @@ -127,9 +129,9 @@ class ProjectServiceContainer extends Container * * @return \stdClass */ - protected function getBAR2Service() + protected static function getBAR2Service($container) { - return $this->services['BAR2'] = new \stdClass(); + return $container->services['BAR2'] = new \stdClass(); } /** @@ -137,9 +139,9 @@ class ProjectServiceContainer extends Container * * @return \Bar */ - protected function getAServiceService() + protected static function getAServiceService($container) { - return $this->services['a_service'] = ($this->privates['a_factory'] ??= new \Bar())->getBar(); + return $container->services['a_service'] = ($container->privates['a_factory'] ??= new \Bar())->getBar(); } /** @@ -147,9 +149,9 @@ class ProjectServiceContainer extends Container * * @return \Bar */ - protected function getBServiceService() + protected static function getBServiceService($container) { - return $this->services['b_service'] = ($this->privates['a_factory'] ??= new \Bar())->getBar(); + return $container->services['b_service'] = ($container->privates['a_factory'] ??= new \Bar())->getBar(); } /** @@ -157,11 +159,11 @@ class ProjectServiceContainer extends Container * * @return \Bar\FooClass */ - protected function getBar3Service() + protected static function getBar3Service($container) { - $a = ($this->services['foo.baz'] ?? $this->getFoo_BazService()); + $a = ($container->services['foo.baz'] ?? self::getFoo_BazService($container)); - $this->services['bar'] = $instance = new \Bar\FooClass('foo', $a, $this->getParameter('foo_bar')); + $container->services['bar'] = $instance = new \Bar\FooClass('foo', $a, $container->getParameter('foo_bar')); $a->configure($instance); @@ -173,9 +175,9 @@ class ProjectServiceContainer extends Container * * @return \stdClass */ - protected function getBar22Service() + protected static function getBar22Service($container) { - return $this->services['bar2'] = new \stdClass(); + return $container->services['bar2'] = new \stdClass(); } /** @@ -183,11 +185,11 @@ class ProjectServiceContainer extends Container * * @return \Baz */ - protected function getBazService() + protected static function getBazService($container) { - $this->services['baz'] = $instance = new \Baz(); + $container->services['baz'] = $instance = new \Baz(); - $instance->setFoo(($this->services['foo_with_inline'] ?? $this->getFooWithInlineService())); + $instance->setFoo(($container->services['foo_with_inline'] ?? self::getFooWithInlineService($container))); return $instance; } @@ -197,12 +199,12 @@ class ProjectServiceContainer extends Container * * @return \stdClass */ - protected function getConfiguredServiceService() + protected static function getConfiguredServiceService($container) { - $this->services['configured_service'] = $instance = new \stdClass(); + $container->services['configured_service'] = $instance = new \stdClass(); $a = new \ConfClass(); - $a->setFoo(($this->services['baz'] ?? $this->getBazService())); + $a->setFoo(($container->services['baz'] ?? self::getBazService($container))); $a->configureStdClass($instance); @@ -214,9 +216,9 @@ class ProjectServiceContainer extends Container * * @return \stdClass */ - protected function getConfiguredServiceSimpleService() + protected static function getConfiguredServiceSimpleService($container) { - $this->services['configured_service_simple'] = $instance = new \stdClass(); + $container->services['configured_service_simple'] = $instance = new \stdClass(); (new \ConfClass('bar'))->configureStdClass($instance); @@ -228,9 +230,9 @@ class ProjectServiceContainer extends Container * * @return \stdClass */ - protected function getDecoratorServiceService() + protected static function getDecoratorServiceService($container) { - return $this->services['decorator_service'] = new \stdClass(); + return $container->services['decorator_service'] = new \stdClass(); } /** @@ -238,9 +240,9 @@ class ProjectServiceContainer extends Container * * @return \stdClass */ - protected function getDecoratorServiceWithNameService() + protected static function getDecoratorServiceWithNameService($container) { - return $this->services['decorator_service_with_name'] = new \stdClass(); + return $container->services['decorator_service_with_name'] = new \stdClass(); } /** @@ -250,11 +252,11 @@ class ProjectServiceContainer extends Container * * @deprecated Since vendor/package 1.1: The "deprecated_service" service is deprecated. You should stop using it, as it will be removed in the future. */ - protected function getDeprecatedServiceService() + protected static function getDeprecatedServiceService($container) { trigger_deprecation('vendor/package', '1.1', 'The "deprecated_service" service is deprecated. You should stop using it, as it will be removed in the future.'); - return $this->services['deprecated_service'] = new \stdClass(); + return $container->services['deprecated_service'] = new \stdClass(); } /** @@ -262,9 +264,9 @@ class ProjectServiceContainer extends Container * * @return \Bar */ - protected function getFactoryServiceService() + protected static function getFactoryServiceService($container) { - return $this->services['factory_service'] = ($this->services['foo.baz'] ?? $this->getFoo_BazService())->getInstance(); + return $container->services['factory_service'] = ($container->services['foo.baz'] ?? self::getFoo_BazService($container))->getInstance(); } /** @@ -272,9 +274,9 @@ class ProjectServiceContainer extends Container * * @return \Bar */ - protected function getFactoryServiceSimpleService() + protected static function getFactoryServiceSimpleService($container) { - return $this->services['factory_service_simple'] = $this->getFactorySimpleService()->getInstance(); + return $container->services['factory_service_simple'] = self::getFactorySimpleService($container)->getInstance(); } /** @@ -282,16 +284,16 @@ class ProjectServiceContainer extends Container * * @return \Bar\FooClass */ - protected function getFooService() + protected static function getFooService($container) { - $a = ($this->services['foo.baz'] ?? $this->getFoo_BazService()); + $a = ($container->services['foo.baz'] ?? self::getFoo_BazService($container)); - $this->services['foo'] = $instance = \Bar\FooClass::getInstance('foo', $a, ['bar' => 'foo is bar', 'foobar' => 'bar'], true, $this); + $container->services['foo'] = $instance = \Bar\FooClass::getInstance('foo', $a, ['bar' => 'foo is bar', 'foobar' => 'bar'], true, $container); $instance->foo = 'bar'; $instance->moo = $a; $instance->qux = ['bar' => 'foo is bar', 'foobar' => 'bar']; - $instance->setBar(($this->services['bar'] ?? $this->getBar3Service())); + $instance->setBar(($container->services['bar'] ?? self::getBar3Service($container))); $instance->initialize(); sc_configure($instance); @@ -303,11 +305,11 @@ class ProjectServiceContainer extends Container * * @return \BazClass */ - protected function getFoo_BazService() + protected static function getFoo_BazService($container) { - include_once $this->targetDir.''.'/Fixtures/includes/classes.php'; + include_once $container->targetDir.''.'/Fixtures/includes/classes.php'; - $this->services['foo.baz'] = $instance = \BazClass::getInstance(); + $container->services['foo.baz'] = $instance = \BazClass::getInstance(); \BazClass::configureStatic1($instance); @@ -319,13 +321,13 @@ class ProjectServiceContainer extends Container * * @return \Bar\FooClass */ - protected function getFooBarService() + protected static function getFooBarService($container) { - $this->factories['foo_bar'] = function () { - return new \Bar\FooClass(($this->services['deprecated_service'] ?? $this->getDeprecatedServiceService())); + $container->factories['foo_bar'] = static function ($container) { + return new \Bar\FooClass(($container->services['deprecated_service'] ?? self::getDeprecatedServiceService($container))); }; - return $this->factories['foo_bar'](); + return $container->factories['foo_bar']($container); } /** @@ -333,13 +335,13 @@ class ProjectServiceContainer extends Container * * @return \Foo */ - protected function getFooWithInlineService() + protected static function getFooWithInlineService($container) { - $this->services['foo_with_inline'] = $instance = new \Foo(); + $container->services['foo_with_inline'] = $instance = new \Foo(); $a = new \Bar(); $a->pub = 'pub'; - $a->setBaz(($this->services['baz'] ?? $this->getBazService())); + $a->setBaz(($container->services['baz'] ?? self::getBazService($container))); $instance->setBar($a); @@ -351,14 +353,18 @@ class ProjectServiceContainer extends Container * * @return \LazyContext */ - protected function getLazyContextService() + protected static function getLazyContextService($container) { - include_once $this->targetDir.''.'/Fixtures/includes/classes.php'; + $containerRef = $container->ref; - return $this->services['lazy_context'] = new \LazyContext(new RewindableGenerator(function () { - yield 'k1' => ($this->services['foo.baz'] ?? $this->getFoo_BazService()); - yield 'k2' => $this; - }, 2), new RewindableGenerator(function () { + include_once $container->targetDir.''.'/Fixtures/includes/classes.php'; + + return $container->services['lazy_context'] = new \LazyContext(new RewindableGenerator(static function () use ($containerRef) { + $container = $containerRef->get(); + + yield 'k1' => ($container->services['foo.baz'] ?? self::getFoo_BazService($container)); + yield 'k2' => $container; + }, 2), new RewindableGenerator(static function () { return new \EmptyIterator(); }, 0)); } @@ -368,13 +374,17 @@ class ProjectServiceContainer extends Container * * @return \LazyContext */ - protected function getLazyContextIgnoreInvalidRefService() + protected static function getLazyContextIgnoreInvalidRefService($container) { - include_once $this->targetDir.''.'/Fixtures/includes/classes.php'; + $containerRef = $container->ref; + + include_once $container->targetDir.''.'/Fixtures/includes/classes.php'; - return $this->services['lazy_context_ignore_invalid_ref'] = new \LazyContext(new RewindableGenerator(function () { - yield 0 => ($this->services['foo.baz'] ?? $this->getFoo_BazService()); - }, 1), new RewindableGenerator(function () { + return $container->services['lazy_context_ignore_invalid_ref'] = new \LazyContext(new RewindableGenerator(static function () use ($containerRef) { + $container = $containerRef->get(); + + yield 0 => ($container->services['foo.baz'] ?? self::getFoo_BazService($container)); + }, 1), new RewindableGenerator(static function () { return new \EmptyIterator(); }, 0)); } @@ -384,15 +394,15 @@ class ProjectServiceContainer extends Container * * @return \Bar\FooClass */ - protected function getMethodCall1Service() + protected static function getMethodCall1Service($container) { - include_once $this->targetDir.''.'/Fixtures/includes/foo.php'; + include_once $container->targetDir.''.'/Fixtures/includes/foo.php'; - $this->services['method_call1'] = $instance = new \Bar\FooClass(); + $container->services['method_call1'] = $instance = new \Bar\FooClass(); - $instance->setBar(($this->services['foo'] ?? $this->getFooService())); + $instance->setBar(($container->services['foo'] ?? self::getFooService($container))); $instance->setBar(NULL); - $instance->setBar((($this->services['foo'] ?? $this->getFooService())->foo() . (($this->hasParameter("foo")) ? ($this->getParameter("foo")) : ("default")))); + $instance->setBar((($container->services['foo'] ?? self::getFooService($container))->foo() . (($container->hasParameter("foo")) ? ($container->getParameter("foo")) : ("default")))); return $instance; } @@ -402,12 +412,12 @@ class ProjectServiceContainer extends Container * * @return \FooBarBaz */ - protected function getNewFactoryServiceService() + protected static function getNewFactoryServiceService($container) { $a = new \FactoryClass(); $a->foo = 'bar'; - $this->services['new_factory_service'] = $instance = $a->getInstance(); + $container->services['new_factory_service'] = $instance = $a->getInstance(); $instance->foo = 'bar'; @@ -419,15 +429,15 @@ class ProjectServiceContainer extends Container * * @return \Bar\FooClass */ - protected function getNonSharedFooService() + protected static function getNonSharedFooService($container) { - include_once $this->targetDir.''.'/Fixtures/includes/foo.php'; + include_once $container->targetDir.''.'/Fixtures/includes/foo.php'; - $this->factories['non_shared_foo'] = function () { + $container->factories['non_shared_foo'] = static function ($container) { return new \Bar\FooClass(); }; - return $this->factories['non_shared_foo'](); + return $container->factories['non_shared_foo']($container); } /** @@ -435,9 +445,9 @@ class ProjectServiceContainer extends Container * * @return \stdClass */ - protected function getPreloadSidekickService() + protected static function getPreloadSidekickService($container) { - return $this->services['preload_sidekick'] = new \stdClass(); + return $container->services['preload_sidekick'] = new \stdClass(); } /** @@ -445,9 +455,9 @@ class ProjectServiceContainer extends Container * * @return \stdClass */ - protected function getRuntimeErrorService() + protected static function getRuntimeErrorService($container) { - return $this->services['runtime_error'] = new \stdClass(throw new RuntimeException('Service "errored_definition" is broken.')); + return $container->services['runtime_error'] = new \stdClass(throw new RuntimeException('Service "errored_definition" is broken.')); } /** @@ -455,9 +465,9 @@ class ProjectServiceContainer extends Container * * @return \Bar\FooClass */ - protected function getServiceFromStaticMethodService() + protected static function getServiceFromStaticMethodService($container) { - return $this->services['service_from_static_method'] = \Bar\FooClass::getInstance(); + return $container->services['service_from_static_method'] = \Bar\FooClass::getInstance(); } /** @@ -465,11 +475,15 @@ class ProjectServiceContainer extends Container * * @return \Bar */ - protected function getTaggedIteratorService() + protected static function getTaggedIteratorService($container) { - return $this->services['tagged_iterator'] = new \Bar(new RewindableGenerator(function () { - yield 0 => ($this->services['foo'] ?? $this->getFooService()); - yield 1 => ($this->privates['tagged_iterator_foo'] ??= new \Bar()); + $containerRef = $container->ref; + + return $container->services['tagged_iterator'] = new \Bar(new RewindableGenerator(static function () use ($containerRef) { + $container = $containerRef->get(); + + yield 0 => ($container->services['foo'] ?? self::getFooService($container)); + yield 1 => ($container->privates['tagged_iterator_foo'] ??= new \Bar()); }, 2)); } @@ -478,9 +492,9 @@ class ProjectServiceContainer extends Container * * @return \Bar\FooClass */ - protected function getThrowingOneService() + protected static function getThrowingOneService($container) { - return $this->services['throwing_one'] = new \Bar\FooClass(throw new RuntimeException('No-no-no-no')); + return $container->services['throwing_one'] = new \Bar\FooClass(throw new RuntimeException('No-no-no-no')); } /** @@ -490,7 +504,7 @@ class ProjectServiceContainer extends Container * * @deprecated Since vendor/package 1.1: The "factory_simple" service is deprecated. You should stop using it, as it will be removed in the future. */ - protected function getFactorySimpleService() + protected static function getFactorySimpleService($container) { trigger_deprecation('vendor/package', '1.1', 'The "factory_simple" service is deprecated. You should stop using it, as it will be removed in the future.'); diff --git a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services9_lazy_inlined_factories.txt b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services9_lazy_inlined_factories.txt index b1bf8826b43dd..31cf95ed38123 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services9_lazy_inlined_factories.txt +++ b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services9_lazy_inlined_factories.txt @@ -4,7 +4,7 @@ Array namespace Container%s; -include_once $this->targetDir.''.'/Fixtures/includes/foo.php'; +include_once $container->targetDir.''.'/Fixtures/includes/foo.php'; class FooClassGhost2b16075 extends \Bar\FooClass implements \Symfony\Component\VarExporter\LazyObjectInterface %A @@ -35,9 +35,11 @@ class ProjectServiceContainer extends Container protected $targetDir; protected $parameters = []; private $buildParameters; + protected readonly \WeakReference $ref; public function __construct(array $buildParameters = [], $containerDir = __DIR__) { + $this->ref = \WeakReference::create($this); $this->buildParameters = $buildParameters; $this->containerDir = $containerDir; $this->targetDir = \dirname($containerDir); @@ -50,7 +52,7 @@ class ProjectServiceContainer extends Container $this->aliases = []; - $this->privates['service_container'] = function () { + $this->privates['service_container'] = static function ($container) { include_once __DIR__.'/proxy-classes.php'; }; } @@ -75,13 +77,15 @@ class ProjectServiceContainer extends Container * * @return object A %lazy_foo_class% instance */ - protected function getLazyFooService($lazyLoad = true) + protected static function getLazyFooService($container, $lazyLoad = true) { + $containerRef = $container->ref; + if (true === $lazyLoad) { - return $this->services['lazy_foo'] = $this->createProxy('FooClassGhost2b16075', fn () => \FooClassGhost2b16075::createLazyGhost($this->getLazyFooService(...))); + return $container->services['lazy_foo'] = $container->createProxy('FooClassGhost2b16075', static fn () => \FooClassGhost2b16075::createLazyGhost(static fn ($proxy) => self::getLazyFooService($containerRef->get(), $proxy))); } - include_once $this->targetDir.''.'/Fixtures/includes/foo_lazy.php'; + include_once $container->targetDir.''.'/Fixtures/includes/foo_lazy.php'; return ($lazyLoad->__construct(new \Bar\FooLazyClass()) && false ?: $lazyLoad); } diff --git a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services_adawson.php b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services_adawson.php index b07280c763cb3..ff6b744a8fa9c 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services_adawson.php +++ b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services_adawson.php @@ -15,9 +15,11 @@ class ProjectServiceContainer extends Container { protected $parameters = []; + protected readonly \WeakReference $ref; public function __construct() { + $this->ref = \WeakReference::create($this); $this->services = $this->privates = []; $this->methodMap = [ 'App\\Bus' => 'getBusService', @@ -53,13 +55,13 @@ public function getRemovedIds(): array * * @return \App\Bus */ - protected function getBusService() + protected static function getBusService($container) { - $a = ($this->services['App\\Db'] ?? $this->getDbService()); + $a = ($container->services['App\\Db'] ?? self::getDbService($container)); - $this->services['App\\Bus'] = $instance = new \App\Bus($a); + $container->services['App\\Bus'] = $instance = new \App\Bus($a); - $b = ($this->privates['App\\Schema'] ?? $this->getSchemaService()); + $b = ($container->privates['App\\Schema'] ?? self::getSchemaService($container)); $c = new \App\Registry(); $c->processor = [0 => $a, 1 => $instance]; @@ -76,11 +78,11 @@ protected function getBusService() * * @return \App\Db */ - protected function getDbService() + protected static function getDbService($container) { - $this->services['App\\Db'] = $instance = new \App\Db(); + $container->services['App\\Db'] = $instance = new \App\Db(); - $instance->schema = ($this->privates['App\\Schema'] ?? $this->getSchemaService()); + $instance->schema = ($container->privates['App\\Schema'] ?? self::getSchemaService($container)); return $instance; } @@ -90,14 +92,14 @@ protected function getDbService() * * @return \App\Schema */ - protected function getSchemaService() + protected static function getSchemaService($container) { - $a = ($this->services['App\\Db'] ?? $this->getDbService()); + $a = ($container->services['App\\Db'] ?? self::getDbService($container)); - if (isset($this->privates['App\\Schema'])) { - return $this->privates['App\\Schema']; + if (isset($container->privates['App\\Schema'])) { + return $container->privates['App\\Schema']; } - return $this->privates['App\\Schema'] = new \App\Schema($a); + return $container->privates['App\\Schema'] = new \App\Schema($a); } } diff --git a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services_almost_circular_private.php b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services_almost_circular_private.php index 9762e7411df37..1f79a3e7dda55 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services_almost_circular_private.php +++ b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services_almost_circular_private.php @@ -15,9 +15,11 @@ class Symfony_DI_PhpDumper_Test_Almost_Circular_Private extends Container { protected $parameters = []; + protected readonly \WeakReference $ref; public function __construct() { + $this->ref = \WeakReference::create($this); $this->services = $this->privates = []; $this->methodMap = [ 'bar2' => 'getBar2Service', @@ -104,11 +106,11 @@ public function getRemovedIds(): array * * @return \BarCircular */ - protected function getBar2Service() + protected static function getBar2Service($container) { - $this->services['bar2'] = $instance = new \BarCircular(); + $container->services['bar2'] = $instance = new \BarCircular(); - $instance->addFoobar(new \FoobarCircular(($this->services['foo2'] ?? $this->getFoo2Service()))); + $instance->addFoobar(new \FoobarCircular(($container->services['foo2'] ?? self::getFoo2Service($container)))); return $instance; } @@ -118,9 +120,9 @@ protected function getBar2Service() * * @return \BarCircular */ - protected function getBar3Service() + protected static function getBar3Service($container) { - $this->services['bar3'] = $instance = new \BarCircular(); + $container->services['bar3'] = $instance = new \BarCircular(); $a = new \FoobarCircular(); @@ -134,11 +136,11 @@ protected function getBar3Service() * * @return \stdClass */ - protected function getBaz6Service() + protected static function getBaz6Service($container) { - $this->services['baz6'] = $instance = new \stdClass(); + $container->services['baz6'] = $instance = new \stdClass(); - $instance->bar6 = ($this->privates['bar6'] ?? $this->getBar6Service()); + $instance->bar6 = ($container->privates['bar6'] ?? self::getBar6Service($container)); return $instance; } @@ -148,17 +150,17 @@ protected function getBaz6Service() * * @return \stdClass */ - protected function getConnectionService() + protected static function getConnectionService($container) { $a = new \stdClass(); $b = new \stdClass(); - $this->services['connection'] = $instance = new \stdClass($a, $b); + $container->services['connection'] = $instance = new \stdClass($a, $b); - $b->logger = ($this->services['logger'] ?? $this->getLoggerService()); + $b->logger = ($container->services['logger'] ?? self::getLoggerService($container)); - $a->subscriber = ($this->services['subscriber'] ?? $this->getSubscriberService()); + $a->subscriber = ($container->services['subscriber'] ?? self::getSubscriberService($container)); return $instance; } @@ -168,17 +170,17 @@ protected function getConnectionService() * * @return \stdClass */ - protected function getConnection2Service() + protected static function getConnection2Service($container) { $a = new \stdClass(); $b = new \stdClass(); - $this->services['connection2'] = $instance = new \stdClass($a, $b); + $container->services['connection2'] = $instance = new \stdClass($a, $b); $c = new \stdClass($instance); - $d = ($this->services['manager2'] ?? $this->getManager2Service()); + $d = ($container->services['manager2'] ?? self::getManager2Service($container)); $c->handler2 = new \stdClass($d); @@ -194,15 +196,19 @@ protected function getConnection2Service() * * @return \stdClass */ - protected function getDoctrine_EntityManagerService() + protected static function getDoctrine_EntityManagerService($container) { + $containerRef = $container->ref; + $a = new \stdClass(); - $a->resolver = new \stdClass(new RewindableGenerator(function () { - yield 0 => ($this->privates['doctrine.listener'] ?? $this->getDoctrine_ListenerService()); + $a->resolver = new \stdClass(new RewindableGenerator(static function () use ($containerRef) { + $container = $containerRef->get(); + + yield 0 => ($container->privates['doctrine.listener'] ?? self::getDoctrine_ListenerService($container)); }, 1)); $a->flag = 'ok'; - return $this->services['doctrine.entity_manager'] = \FactoryChecker::create($a); + return $container->services['doctrine.entity_manager'] = \FactoryChecker::create($a); } /** @@ -210,11 +216,11 @@ protected function getDoctrine_EntityManagerService() * * @return \FooCircular */ - protected function getFooService() + protected static function getFooService($container) { $a = new \BarCircular(); - $this->services['foo'] = $instance = new \FooCircular($a); + $container->services['foo'] = $instance = new \FooCircular($a); $a->addFoobar(new \FoobarCircular($instance)); @@ -226,15 +232,15 @@ protected function getFooService() * * @return \FooCircular */ - protected function getFoo2Service() + protected static function getFoo2Service($container) { - $a = ($this->services['bar2'] ?? $this->getBar2Service()); + $a = ($container->services['bar2'] ?? self::getBar2Service($container)); - if (isset($this->services['foo2'])) { - return $this->services['foo2']; + if (isset($container->services['foo2'])) { + return $container->services['foo2']; } - return $this->services['foo2'] = new \FooCircular($a); + return $container->services['foo2'] = new \FooCircular($a); } /** @@ -242,9 +248,9 @@ protected function getFoo2Service() * * @return \stdClass */ - protected function getFoo5Service() + protected static function getFoo5Service($container) { - $this->services['foo5'] = $instance = new \stdClass(); + $container->services['foo5'] = $instance = new \stdClass(); $a = new \stdClass($instance); $a->foo = $instance; @@ -259,11 +265,11 @@ protected function getFoo5Service() * * @return \stdClass */ - protected function getFoo6Service() + protected static function getFoo6Service($container) { - $this->services['foo6'] = $instance = new \stdClass(); + $container->services['foo6'] = $instance = new \stdClass(); - $instance->bar6 = ($this->privates['bar6'] ?? $this->getBar6Service()); + $instance->bar6 = ($container->privates['bar6'] ?? self::getBar6Service($container)); return $instance; } @@ -273,11 +279,11 @@ protected function getFoo6Service() * * @return \stdClass */ - protected function getFoobar4Service() + protected static function getFoobar4Service($container) { $a = new \stdClass(); - $this->services['foobar4'] = $instance = new \stdClass($a); + $container->services['foobar4'] = $instance = new \stdClass($a); $a->foobar = $instance; @@ -289,11 +295,11 @@ protected function getFoobar4Service() * * @return \stdClass */ - protected function getListener3Service() + protected static function getListener3Service($container) { - $this->services['listener3'] = $instance = new \stdClass(); + $container->services['listener3'] = $instance = new \stdClass(); - $instance->manager = ($this->services['manager3'] ?? $this->getManager3Service()); + $instance->manager = ($container->services['manager3'] ?? self::getManager3Service($container)); return $instance; } @@ -303,15 +309,15 @@ protected function getListener3Service() * * @return \stdClass */ - protected function getListener4Service() + protected static function getListener4Service($container) { - $a = ($this->privates['manager4'] ?? $this->getManager4Service()); + $a = ($container->privates['manager4'] ?? self::getManager4Service($container)); - if (isset($this->services['listener4'])) { - return $this->services['listener4']; + if (isset($container->services['listener4'])) { + return $container->services['listener4']; } - return $this->services['listener4'] = new \stdClass($a); + return $container->services['listener4'] = new \stdClass($a); } /** @@ -319,17 +325,17 @@ protected function getListener4Service() * * @return \stdClass */ - protected function getLoggerService() + protected static function getLoggerService($container) { - $a = ($this->services['connection'] ?? $this->getConnectionService()); + $a = ($container->services['connection'] ?? self::getConnectionService($container)); - if (isset($this->services['logger'])) { - return $this->services['logger']; + if (isset($container->services['logger'])) { + return $container->services['logger']; } - $this->services['logger'] = $instance = new \stdClass($a); + $container->services['logger'] = $instance = new \stdClass($a); - $instance->handler = new \stdClass(($this->services['manager'] ?? $this->getManagerService())); + $instance->handler = new \stdClass(($container->services['manager'] ?? self::getManagerService($container))); return $instance; } @@ -339,15 +345,15 @@ protected function getLoggerService() * * @return \stdClass */ - protected function getManagerService() + protected static function getManagerService($container) { - $a = ($this->services['connection'] ?? $this->getConnectionService()); + $a = ($container->services['connection'] ?? self::getConnectionService($container)); - if (isset($this->services['manager'])) { - return $this->services['manager']; + if (isset($container->services['manager'])) { + return $container->services['manager']; } - return $this->services['manager'] = new \stdClass($a); + return $container->services['manager'] = new \stdClass($a); } /** @@ -355,15 +361,15 @@ protected function getManagerService() * * @return \stdClass */ - protected function getManager2Service() + protected static function getManager2Service($container) { - $a = ($this->services['connection2'] ?? $this->getConnection2Service()); + $a = ($container->services['connection2'] ?? self::getConnection2Service($container)); - if (isset($this->services['manager2'])) { - return $this->services['manager2']; + if (isset($container->services['manager2'])) { + return $container->services['manager2']; } - return $this->services['manager2'] = new \stdClass($a); + return $container->services['manager2'] = new \stdClass($a); } /** @@ -371,17 +377,17 @@ protected function getManager2Service() * * @return \stdClass */ - protected function getManager3Service($lazyLoad = true) + protected static function getManager3Service($container, $lazyLoad = true) { - $a = ($this->services['listener3'] ?? $this->getListener3Service()); + $a = ($container->services['listener3'] ?? self::getListener3Service($container)); - if (isset($this->services['manager3'])) { - return $this->services['manager3']; + if (isset($container->services['manager3'])) { + return $container->services['manager3']; } $b = new \stdClass(); $b->listener = [0 => $a]; - return $this->services['manager3'] = new \stdClass($b); + return $container->services['manager3'] = new \stdClass($b); } /** @@ -389,11 +395,11 @@ protected function getManager3Service($lazyLoad = true) * * @return \stdClass */ - protected function getMonolog_LoggerService() + protected static function getMonolog_LoggerService($container) { - $this->services['monolog.logger'] = $instance = new \stdClass(); + $container->services['monolog.logger'] = $instance = new \stdClass(); - $instance->handler = ($this->privates['mailer.transport'] ?? $this->getMailer_TransportService()); + $instance->handler = ($container->privates['mailer.transport'] ?? self::getMailer_TransportService($container)); return $instance; } @@ -403,11 +409,11 @@ protected function getMonolog_LoggerService() * * @return \stdClass */ - protected function getMonologInline_LoggerService() + protected static function getMonologInline_LoggerService($container) { - $this->services['monolog_inline.logger'] = $instance = new \stdClass(); + $container->services['monolog_inline.logger'] = $instance = new \stdClass(); - $instance->handler = ($this->privates['mailer_inline.mailer'] ?? $this->getMailerInline_MailerService()); + $instance->handler = ($container->privates['mailer_inline.mailer'] ?? self::getMailerInline_MailerService($container)); return $instance; } @@ -417,19 +423,19 @@ protected function getMonologInline_LoggerService() * * @return \stdClass */ - protected function getPAService() + protected static function getPAService($container) { $a = new \stdClass(); - $b = ($this->privates['pC'] ?? $this->getPCService()); + $b = ($container->privates['pC'] ?? self::getPCService($container)); - if (isset($this->services['pA'])) { - return $this->services['pA']; + if (isset($container->services['pA'])) { + return $container->services['pA']; } - $this->services['pA'] = $instance = new \stdClass($a, $b); + $container->services['pA'] = $instance = new \stdClass($a, $b); - $a->d = ($this->privates['pD'] ?? $this->getPDService()); + $a->d = ($container->privates['pD'] ?? self::getPDService($container)); return $instance; } @@ -439,15 +445,15 @@ protected function getPAService() * * @return \stdClass */ - protected function getRootService() + protected static function getRootService($container) { $a = new \Symfony\Component\DependencyInjection\Tests\Fixtures\FooForCircularWithAddCalls(); $b = new \stdClass(); - $a->call(new \stdClass(new \stdClass($b, ($this->privates['level5'] ?? $this->getLevel5Service())))); + $a->call(new \stdClass(new \stdClass($b, ($container->privates['level5'] ?? self::getLevel5Service($container))))); - return $this->services['root'] = new \stdClass($a, $b); + return $container->services['root'] = new \stdClass($a, $b); } /** @@ -455,15 +461,15 @@ protected function getRootService() * * @return \stdClass */ - protected function getSubscriberService() + protected static function getSubscriberService($container) { - $a = ($this->services['manager'] ?? $this->getManagerService()); + $a = ($container->services['manager'] ?? self::getManagerService($container)); - if (isset($this->services['subscriber'])) { - return $this->services['subscriber']; + if (isset($container->services['subscriber'])) { + return $container->services['subscriber']; } - return $this->services['subscriber'] = new \stdClass($a); + return $container->services['subscriber'] = new \stdClass($a); } /** @@ -471,15 +477,15 @@ protected function getSubscriberService() * * @return \stdClass */ - protected function getBar6Service() + protected static function getBar6Service($container) { - $a = ($this->services['foo6'] ?? $this->getFoo6Service()); + $a = ($container->services['foo6'] ?? self::getFoo6Service($container)); - if (isset($this->privates['bar6'])) { - return $this->privates['bar6']; + if (isset($container->privates['bar6'])) { + return $container->privates['bar6']; } - return $this->privates['bar6'] = new \stdClass($a); + return $container->privates['bar6'] = new \stdClass($a); } /** @@ -487,9 +493,9 @@ protected function getBar6Service() * * @return \stdClass */ - protected function getDoctrine_ListenerService() + protected static function getDoctrine_ListenerService($container) { - return $this->privates['doctrine.listener'] = new \stdClass(($this->services['doctrine.entity_manager'] ?? $this->getDoctrine_EntityManagerService())); + return $container->privates['doctrine.listener'] = new \stdClass(($container->services['doctrine.entity_manager'] ?? self::getDoctrine_EntityManagerService($container))); } /** @@ -497,11 +503,11 @@ protected function getDoctrine_ListenerService() * * @return \stdClass */ - protected function getLevel5Service() + protected static function getLevel5Service($container) { $a = new \Symfony\Component\DependencyInjection\Tests\Fixtures\FooForCircularWithAddCalls(); - $this->privates['level5'] = $instance = new \stdClass($a); + $container->privates['level5'] = $instance = new \stdClass($a); $a->call($instance); @@ -513,11 +519,15 @@ protected function getLevel5Service() * * @return \stdClass */ - protected function getMailer_TransportService() + protected static function getMailer_TransportService($container) { - return $this->privates['mailer.transport'] = (new \FactoryCircular(new RewindableGenerator(function () { - yield 0 => ($this->privates['mailer.transport_factory.amazon'] ?? $this->getMailer_TransportFactory_AmazonService()); - yield 1 => $this->getMailerInline_TransportFactory_AmazonService(); + $containerRef = $container->ref; + + return $container->privates['mailer.transport'] = (new \FactoryCircular(new RewindableGenerator(static function () use ($containerRef) { + $container = $containerRef->get(); + + yield 0 => ($container->privates['mailer.transport_factory.amazon'] ?? self::getMailer_TransportFactory_AmazonService($container)); + yield 1 => self::getMailerInline_TransportFactory_AmazonService($container); }, 2)))->create(); } @@ -526,13 +536,13 @@ protected function getMailer_TransportService() * * @return \stdClass */ - protected function getMailer_TransportFactory_AmazonService() + protected static function getMailer_TransportFactory_AmazonService($container) { $a = new \stdClass(); - $this->privates['mailer.transport_factory.amazon'] = $instance = new \stdClass($a); + $container->privates['mailer.transport_factory.amazon'] = $instance = new \stdClass($a); - $a->handler = ($this->privates['mailer.transport'] ?? $this->getMailer_TransportService()); + $a->handler = ($container->privates['mailer.transport'] ?? self::getMailer_TransportService($container)); return $instance; } @@ -542,9 +552,9 @@ protected function getMailer_TransportFactory_AmazonService() * * @return \stdClass */ - protected function getMailerInline_MailerService() + protected static function getMailerInline_MailerService($container) { - return $this->privates['mailer_inline.mailer'] = new \stdClass((new \FactoryCircular(new RewindableGenerator(function () { + return $container->privates['mailer_inline.mailer'] = new \stdClass((new \FactoryCircular(new RewindableGenerator(static function () { return new \EmptyIterator(); }, 0)))->create()); } @@ -554,10 +564,10 @@ protected function getMailerInline_MailerService() * * @return \stdClass */ - protected function getMailerInline_TransportFactory_AmazonService() + protected static function getMailerInline_TransportFactory_AmazonService($container) { $a = new \stdClass(); - $a->handler = ($this->privates['mailer_inline.mailer'] ?? $this->getMailerInline_MailerService()); + $a->handler = ($container->privates['mailer_inline.mailer'] ?? self::getMailerInline_MailerService($container)); return new \stdClass($a); } @@ -567,13 +577,13 @@ protected function getMailerInline_TransportFactory_AmazonService() * * @return \stdClass */ - protected function getManager4Service($lazyLoad = true) + protected static function getManager4Service($container, $lazyLoad = true) { $a = new \stdClass(); - $this->privates['manager4'] = $instance = new \stdClass($a); + $container->privates['manager4'] = $instance = new \stdClass($a); - $a->listener = [0 => ($this->services['listener4'] ?? $this->getListener4Service())]; + $a->listener = [0 => ($container->services['listener4'] ?? self::getListener4Service($container))]; return $instance; } @@ -583,11 +593,11 @@ protected function getManager4Service($lazyLoad = true) * * @return \stdClass */ - protected function getPCService($lazyLoad = true) + protected static function getPCService($container, $lazyLoad = true) { - $this->privates['pC'] = $instance = new \stdClass(); + $container->privates['pC'] = $instance = new \stdClass(); - $instance->d = ($this->privates['pD'] ?? $this->getPDService()); + $instance->d = ($container->privates['pD'] ?? self::getPDService($container)); return $instance; } @@ -597,14 +607,14 @@ protected function getPCService($lazyLoad = true) * * @return \stdClass */ - protected function getPDService() + protected static function getPDService($container) { - $a = ($this->services['pA'] ?? $this->getPAService()); + $a = ($container->services['pA'] ?? self::getPAService($container)); - if (isset($this->privates['pD'])) { - return $this->privates['pD']; + if (isset($container->privates['pD'])) { + return $container->privates['pD']; } - return $this->privates['pD'] = new \stdClass($a); + return $container->privates['pD'] = new \stdClass($a); } } diff --git a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services_almost_circular_public.php b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services_almost_circular_public.php index bff47395118b4..f7dc4133f3d4a 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services_almost_circular_public.php +++ b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services_almost_circular_public.php @@ -15,9 +15,11 @@ class Symfony_DI_PhpDumper_Test_Almost_Circular_Public extends Container { protected $parameters = []; + protected readonly \WeakReference $ref; public function __construct() { + $this->ref = \WeakReference::create($this); $this->services = $this->privates = []; $this->methodMap = [ 'bar' => 'getBarService', @@ -104,11 +106,11 @@ public function getRemovedIds(): array * * @return \BarCircular */ - protected function getBarService() + protected static function getBarService($container) { - $this->services['bar'] = $instance = new \BarCircular(); + $container->services['bar'] = $instance = new \BarCircular(); - $instance->addFoobar(($this->services['foobar'] ?? $this->getFoobarService())); + $instance->addFoobar(($container->services['foobar'] ?? self::getFoobarService($container))); return $instance; } @@ -118,11 +120,11 @@ protected function getBarService() * * @return \BarCircular */ - protected function getBar3Service() + protected static function getBar3Service($container) { - $this->services['bar3'] = $instance = new \BarCircular(); + $container->services['bar3'] = $instance = new \BarCircular(); - $a = ($this->services['foobar3'] ??= new \FoobarCircular()); + $a = ($container->services['foobar3'] ??= new \FoobarCircular()); $instance->addFoobar($a, $a); @@ -134,15 +136,15 @@ protected function getBar3Service() * * @return \stdClass */ - protected function getBar5Service() + protected static function getBar5Service($container) { - $a = ($this->services['foo5'] ?? $this->getFoo5Service()); + $a = ($container->services['foo5'] ?? self::getFoo5Service($container)); - if (isset($this->services['bar5'])) { - return $this->services['bar5']; + if (isset($container->services['bar5'])) { + return $container->services['bar5']; } - $this->services['bar5'] = $instance = new \stdClass($a); + $container->services['bar5'] = $instance = new \stdClass($a); $instance->foo = $a; @@ -154,11 +156,11 @@ protected function getBar5Service() * * @return \stdClass */ - protected function getBaz6Service() + protected static function getBaz6Service($container) { - $this->services['baz6'] = $instance = new \stdClass(); + $container->services['baz6'] = $instance = new \stdClass(); - $instance->bar6 = ($this->privates['bar6'] ?? $this->getBar6Service()); + $instance->bar6 = ($container->privates['bar6'] ?? self::getBar6Service($container)); return $instance; } @@ -168,18 +170,18 @@ protected function getBaz6Service() * * @return \stdClass */ - protected function getConnectionService() + protected static function getConnectionService($container) { - $a = ($this->services['dispatcher'] ?? $this->getDispatcherService()); + $a = ($container->services['dispatcher'] ?? self::getDispatcherService($container)); - if (isset($this->services['connection'])) { - return $this->services['connection']; + if (isset($container->services['connection'])) { + return $container->services['connection']; } $b = new \stdClass(); - $this->services['connection'] = $instance = new \stdClass($a, $b); + $container->services['connection'] = $instance = new \stdClass($a, $b); - $b->logger = ($this->services['logger'] ?? $this->getLoggerService()); + $b->logger = ($container->services['logger'] ?? self::getLoggerService($container)); return $instance; } @@ -189,19 +191,19 @@ protected function getConnectionService() * * @return \stdClass */ - protected function getConnection2Service() + protected static function getConnection2Service($container) { - $a = ($this->services['dispatcher2'] ?? $this->getDispatcher2Service()); + $a = ($container->services['dispatcher2'] ?? self::getDispatcher2Service($container)); - if (isset($this->services['connection2'])) { - return $this->services['connection2']; + if (isset($container->services['connection2'])) { + return $container->services['connection2']; } $b = new \stdClass(); - $this->services['connection2'] = $instance = new \stdClass($a, $b); + $container->services['connection2'] = $instance = new \stdClass($a, $b); $c = new \stdClass($instance); - $c->handler2 = new \stdClass(($this->services['manager2'] ?? $this->getManager2Service())); + $c->handler2 = new \stdClass(($container->services['manager2'] ?? self::getManager2Service($container))); $b->logger2 = $c; @@ -213,11 +215,11 @@ protected function getConnection2Service() * * @return \stdClass */ - protected function getConnection3Service() + protected static function getConnection3Service($container) { - $this->services['connection3'] = $instance = new \stdClass(); + $container->services['connection3'] = $instance = new \stdClass(); - $instance->listener = [0 => ($this->services['listener3'] ?? $this->getListener3Service())]; + $instance->listener = [0 => ($container->services['listener3'] ?? self::getListener3Service($container))]; return $instance; } @@ -227,11 +229,11 @@ protected function getConnection3Service() * * @return \stdClass */ - protected function getConnection4Service() + protected static function getConnection4Service($container) { - $this->services['connection4'] = $instance = new \stdClass(); + $container->services['connection4'] = $instance = new \stdClass(); - $instance->listener = [0 => ($this->services['listener4'] ?? $this->getListener4Service())]; + $instance->listener = [0 => ($container->services['listener4'] ?? self::getListener4Service($container))]; return $instance; } @@ -241,11 +243,11 @@ protected function getConnection4Service() * * @return \stdClass */ - protected function getDispatcherService($lazyLoad = true) + protected static function getDispatcherService($container, $lazyLoad = true) { - $this->services['dispatcher'] = $instance = new \stdClass(); + $container->services['dispatcher'] = $instance = new \stdClass(); - $instance->subscriber = ($this->services['subscriber'] ?? $this->getSubscriberService()); + $instance->subscriber = ($container->services['subscriber'] ?? self::getSubscriberService($container)); return $instance; } @@ -255,11 +257,11 @@ protected function getDispatcherService($lazyLoad = true) * * @return \stdClass */ - protected function getDispatcher2Service($lazyLoad = true) + protected static function getDispatcher2Service($container, $lazyLoad = true) { - $this->services['dispatcher2'] = $instance = new \stdClass(); + $container->services['dispatcher2'] = $instance = new \stdClass(); - $instance->subscriber2 = new \stdClass(($this->services['manager2'] ?? $this->getManager2Service())); + $instance->subscriber2 = new \stdClass(($container->services['manager2'] ?? self::getManager2Service($container))); return $instance; } @@ -269,10 +271,14 @@ protected function getDispatcher2Service($lazyLoad = true) * * @return \stdClass */ - protected function getDoctrine_EntityListenerResolverService() + protected static function getDoctrine_EntityListenerResolverService($container) { - return $this->services['doctrine.entity_listener_resolver'] = new \stdClass(new RewindableGenerator(function () { - yield 0 => ($this->services['doctrine.listener'] ?? $this->getDoctrine_ListenerService()); + $containerRef = $container->ref; + + return $container->services['doctrine.entity_listener_resolver'] = new \stdClass(new RewindableGenerator(static function () use ($containerRef) { + $container = $containerRef->get(); + + yield 0 => ($container->services['doctrine.listener'] ?? self::getDoctrine_ListenerService($container)); }, 1)); } @@ -281,13 +287,13 @@ protected function getDoctrine_EntityListenerResolverService() * * @return \stdClass */ - protected function getDoctrine_EntityManagerService() + protected static function getDoctrine_EntityManagerService($container) { $a = new \stdClass(); - $a->resolver = ($this->services['doctrine.entity_listener_resolver'] ?? $this->getDoctrine_EntityListenerResolverService()); + $a->resolver = ($container->services['doctrine.entity_listener_resolver'] ?? self::getDoctrine_EntityListenerResolverService($container)); $a->flag = 'ok'; - return $this->services['doctrine.entity_manager'] = \FactoryChecker::create($a); + return $container->services['doctrine.entity_manager'] = \FactoryChecker::create($a); } /** @@ -295,9 +301,9 @@ protected function getDoctrine_EntityManagerService() * * @return \stdClass */ - protected function getDoctrine_ListenerService() + protected static function getDoctrine_ListenerService($container) { - return $this->services['doctrine.listener'] = new \stdClass(($this->services['doctrine.entity_manager'] ?? $this->getDoctrine_EntityManagerService())); + return $container->services['doctrine.listener'] = new \stdClass(($container->services['doctrine.entity_manager'] ?? self::getDoctrine_EntityManagerService($container))); } /** @@ -305,15 +311,15 @@ protected function getDoctrine_ListenerService() * * @return \FooCircular */ - protected function getFooService() + protected static function getFooService($container) { - $a = ($this->services['bar'] ?? $this->getBarService()); + $a = ($container->services['bar'] ?? self::getBarService($container)); - if (isset($this->services['foo'])) { - return $this->services['foo']; + if (isset($container->services['foo'])) { + return $container->services['foo']; } - return $this->services['foo'] = new \FooCircular($a); + return $container->services['foo'] = new \FooCircular($a); } /** @@ -321,13 +327,13 @@ protected function getFooService() * * @return \FooCircular */ - protected function getFoo2Service() + protected static function getFoo2Service($container) { $a = new \BarCircular(); - $this->services['foo2'] = $instance = new \FooCircular($a); + $container->services['foo2'] = $instance = new \FooCircular($a); - $a->addFoobar(($this->services['foobar2'] ?? $this->getFoobar2Service())); + $a->addFoobar(($container->services['foobar2'] ?? self::getFoobar2Service($container))); return $instance; } @@ -337,17 +343,17 @@ protected function getFoo2Service() * * @return \stdClass */ - protected function getFoo4Service() + protected static function getFoo4Service($container) { - $this->factories['foo4'] = function () { + $container->factories['foo4'] = static function ($container) { $instance = new \stdClass(); - $instance->foobar = ($this->services['foobar4'] ?? $this->getFoobar4Service()); + $instance->foobar = ($container->services['foobar4'] ?? self::getFoobar4Service($container)); return $instance; }; - return $this->factories['foo4'](); + return $container->factories['foo4']($container); } /** @@ -355,11 +361,11 @@ protected function getFoo4Service() * * @return \stdClass */ - protected function getFoo5Service() + protected static function getFoo5Service($container) { - $this->services['foo5'] = $instance = new \stdClass(); + $container->services['foo5'] = $instance = new \stdClass(); - $instance->bar = ($this->services['bar5'] ?? $this->getBar5Service()); + $instance->bar = ($container->services['bar5'] ?? self::getBar5Service($container)); return $instance; } @@ -369,11 +375,11 @@ protected function getFoo5Service() * * @return \stdClass */ - protected function getFoo6Service() + protected static function getFoo6Service($container) { - $this->services['foo6'] = $instance = new \stdClass(); + $container->services['foo6'] = $instance = new \stdClass(); - $instance->bar6 = ($this->privates['bar6'] ?? $this->getBar6Service()); + $instance->bar6 = ($container->privates['bar6'] ?? self::getBar6Service($container)); return $instance; } @@ -383,15 +389,15 @@ protected function getFoo6Service() * * @return \FoobarCircular */ - protected function getFoobarService() + protected static function getFoobarService($container) { - $a = ($this->services['foo'] ?? $this->getFooService()); + $a = ($container->services['foo'] ?? self::getFooService($container)); - if (isset($this->services['foobar'])) { - return $this->services['foobar']; + if (isset($container->services['foobar'])) { + return $container->services['foobar']; } - return $this->services['foobar'] = new \FoobarCircular($a); + return $container->services['foobar'] = new \FoobarCircular($a); } /** @@ -399,15 +405,15 @@ protected function getFoobarService() * * @return \FoobarCircular */ - protected function getFoobar2Service() + protected static function getFoobar2Service($container) { - $a = ($this->services['foo2'] ?? $this->getFoo2Service()); + $a = ($container->services['foo2'] ?? self::getFoo2Service($container)); - if (isset($this->services['foobar2'])) { - return $this->services['foobar2']; + if (isset($container->services['foobar2'])) { + return $container->services['foobar2']; } - return $this->services['foobar2'] = new \FoobarCircular($a); + return $container->services['foobar2'] = new \FoobarCircular($a); } /** @@ -415,9 +421,9 @@ protected function getFoobar2Service() * * @return \FoobarCircular */ - protected function getFoobar3Service() + protected static function getFoobar3Service($container) { - return $this->services['foobar3'] = new \FoobarCircular(); + return $container->services['foobar3'] = new \FoobarCircular(); } /** @@ -425,11 +431,11 @@ protected function getFoobar3Service() * * @return \stdClass */ - protected function getFoobar4Service() + protected static function getFoobar4Service($container) { $a = new \stdClass(); - $this->services['foobar4'] = $instance = new \stdClass($a); + $container->services['foobar4'] = $instance = new \stdClass($a); $a->foobar = $instance; @@ -441,11 +447,11 @@ protected function getFoobar4Service() * * @return \stdClass */ - protected function getListener3Service() + protected static function getListener3Service($container) { - $this->services['listener3'] = $instance = new \stdClass(); + $container->services['listener3'] = $instance = new \stdClass(); - $instance->manager = ($this->services['manager3'] ?? $this->getManager3Service()); + $instance->manager = ($container->services['manager3'] ?? self::getManager3Service($container)); return $instance; } @@ -455,15 +461,15 @@ protected function getListener3Service() * * @return \stdClass */ - protected function getListener4Service() + protected static function getListener4Service($container) { - $a = ($this->privates['manager4'] ?? $this->getManager4Service()); + $a = ($container->privates['manager4'] ?? self::getManager4Service($container)); - if (isset($this->services['listener4'])) { - return $this->services['listener4']; + if (isset($container->services['listener4'])) { + return $container->services['listener4']; } - return $this->services['listener4'] = new \stdClass($a); + return $container->services['listener4'] = new \stdClass($a); } /** @@ -471,17 +477,17 @@ protected function getListener4Service() * * @return \stdClass */ - protected function getLoggerService() + protected static function getLoggerService($container) { - $a = ($this->services['connection'] ?? $this->getConnectionService()); + $a = ($container->services['connection'] ?? self::getConnectionService($container)); - if (isset($this->services['logger'])) { - return $this->services['logger']; + if (isset($container->services['logger'])) { + return $container->services['logger']; } - $this->services['logger'] = $instance = new \stdClass($a); + $container->services['logger'] = $instance = new \stdClass($a); - $instance->handler = new \stdClass(($this->services['manager'] ?? $this->getManagerService())); + $instance->handler = new \stdClass(($container->services['manager'] ?? self::getManagerService($container))); return $instance; } @@ -491,9 +497,9 @@ protected function getLoggerService() * * @return \stdClass */ - protected function getMailer_TransportService() + protected static function getMailer_TransportService($container) { - return $this->services['mailer.transport'] = ($this->services['mailer.transport_factory'] ?? $this->getMailer_TransportFactoryService())->create(); + return $container->services['mailer.transport'] = ($container->services['mailer.transport_factory'] ?? self::getMailer_TransportFactoryService($container))->create(); } /** @@ -501,11 +507,15 @@ protected function getMailer_TransportService() * * @return \FactoryCircular */ - protected function getMailer_TransportFactoryService() + protected static function getMailer_TransportFactoryService($container) { - return $this->services['mailer.transport_factory'] = new \FactoryCircular(new RewindableGenerator(function () { - yield 0 => ($this->services['mailer.transport_factory.amazon'] ?? $this->getMailer_TransportFactory_AmazonService()); - yield 1 => ($this->services['mailer_inline.transport_factory.amazon'] ?? $this->getMailerInline_TransportFactory_AmazonService()); + $containerRef = $container->ref; + + return $container->services['mailer.transport_factory'] = new \FactoryCircular(new RewindableGenerator(static function () use ($containerRef) { + $container = $containerRef->get(); + + yield 0 => ($container->services['mailer.transport_factory.amazon'] ?? self::getMailer_TransportFactory_AmazonService($container)); + yield 1 => ($container->services['mailer_inline.transport_factory.amazon'] ?? self::getMailerInline_TransportFactory_AmazonService($container)); }, 2)); } @@ -514,9 +524,9 @@ protected function getMailer_TransportFactoryService() * * @return \stdClass */ - protected function getMailer_TransportFactory_AmazonService() + protected static function getMailer_TransportFactory_AmazonService($container) { - return $this->services['mailer.transport_factory.amazon'] = new \stdClass(($this->services['monolog.logger_2'] ?? $this->getMonolog_Logger2Service())); + return $container->services['mailer.transport_factory.amazon'] = new \stdClass(($container->services['monolog.logger_2'] ?? self::getMonolog_Logger2Service($container))); } /** @@ -524,9 +534,9 @@ protected function getMailer_TransportFactory_AmazonService() * * @return \FactoryCircular */ - protected function getMailerInline_TransportFactoryService() + protected static function getMailerInline_TransportFactoryService($container) { - return $this->services['mailer_inline.transport_factory'] = new \FactoryCircular(new RewindableGenerator(function () { + return $container->services['mailer_inline.transport_factory'] = new \FactoryCircular(new RewindableGenerator(static function () { return new \EmptyIterator(); }, 0)); } @@ -536,9 +546,9 @@ protected function getMailerInline_TransportFactoryService() * * @return \stdClass */ - protected function getMailerInline_TransportFactory_AmazonService() + protected static function getMailerInline_TransportFactory_AmazonService($container) { - return $this->services['mailer_inline.transport_factory.amazon'] = new \stdClass(($this->services['monolog_inline.logger_2'] ?? $this->getMonologInline_Logger2Service())); + return $container->services['mailer_inline.transport_factory.amazon'] = new \stdClass(($container->services['monolog_inline.logger_2'] ?? self::getMonologInline_Logger2Service($container))); } /** @@ -546,15 +556,15 @@ protected function getMailerInline_TransportFactory_AmazonService() * * @return \stdClass */ - protected function getManagerService() + protected static function getManagerService($container) { - $a = ($this->services['connection'] ?? $this->getConnectionService()); + $a = ($container->services['connection'] ?? self::getConnectionService($container)); - if (isset($this->services['manager'])) { - return $this->services['manager']; + if (isset($container->services['manager'])) { + return $container->services['manager']; } - return $this->services['manager'] = new \stdClass($a); + return $container->services['manager'] = new \stdClass($a); } /** @@ -562,15 +572,15 @@ protected function getManagerService() * * @return \stdClass */ - protected function getManager2Service() + protected static function getManager2Service($container) { - $a = ($this->services['connection2'] ?? $this->getConnection2Service()); + $a = ($container->services['connection2'] ?? self::getConnection2Service($container)); - if (isset($this->services['manager2'])) { - return $this->services['manager2']; + if (isset($container->services['manager2'])) { + return $container->services['manager2']; } - return $this->services['manager2'] = new \stdClass($a); + return $container->services['manager2'] = new \stdClass($a); } /** @@ -578,15 +588,15 @@ protected function getManager2Service() * * @return \stdClass */ - protected function getManager3Service($lazyLoad = true) + protected static function getManager3Service($container, $lazyLoad = true) { - $a = ($this->services['connection3'] ?? $this->getConnection3Service()); + $a = ($container->services['connection3'] ?? self::getConnection3Service($container)); - if (isset($this->services['manager3'])) { - return $this->services['manager3']; + if (isset($container->services['manager3'])) { + return $container->services['manager3']; } - return $this->services['manager3'] = new \stdClass($a); + return $container->services['manager3'] = new \stdClass($a); } /** @@ -594,11 +604,11 @@ protected function getManager3Service($lazyLoad = true) * * @return \stdClass */ - protected function getMonolog_LoggerService() + protected static function getMonolog_LoggerService($container) { - $this->services['monolog.logger'] = $instance = new \stdClass(); + $container->services['monolog.logger'] = $instance = new \stdClass(); - $instance->handler = ($this->services['mailer.transport'] ?? $this->getMailer_TransportService()); + $instance->handler = ($container->services['mailer.transport'] ?? self::getMailer_TransportService($container)); return $instance; } @@ -608,11 +618,11 @@ protected function getMonolog_LoggerService() * * @return \stdClass */ - protected function getMonolog_Logger2Service() + protected static function getMonolog_Logger2Service($container) { - $this->services['monolog.logger_2'] = $instance = new \stdClass(); + $container->services['monolog.logger_2'] = $instance = new \stdClass(); - $instance->handler = ($this->services['mailer.transport'] ?? $this->getMailer_TransportService()); + $instance->handler = ($container->services['mailer.transport'] ?? self::getMailer_TransportService($container)); return $instance; } @@ -622,11 +632,11 @@ protected function getMonolog_Logger2Service() * * @return \stdClass */ - protected function getMonologInline_LoggerService() + protected static function getMonologInline_LoggerService($container) { - $this->services['monolog_inline.logger'] = $instance = new \stdClass(); + $container->services['monolog_inline.logger'] = $instance = new \stdClass(); - $instance->handler = ($this->privates['mailer_inline.mailer'] ?? $this->getMailerInline_MailerService()); + $instance->handler = ($container->privates['mailer_inline.mailer'] ?? self::getMailerInline_MailerService($container)); return $instance; } @@ -636,11 +646,11 @@ protected function getMonologInline_LoggerService() * * @return \stdClass */ - protected function getMonologInline_Logger2Service() + protected static function getMonologInline_Logger2Service($container) { - $this->services['monolog_inline.logger_2'] = $instance = new \stdClass(); + $container->services['monolog_inline.logger_2'] = $instance = new \stdClass(); - $instance->handler = ($this->privates['mailer_inline.mailer'] ?? $this->getMailerInline_MailerService()); + $instance->handler = ($container->privates['mailer_inline.mailer'] ?? self::getMailerInline_MailerService($container)); return $instance; } @@ -650,20 +660,20 @@ protected function getMonologInline_Logger2Service() * * @return \stdClass */ - protected function getPAService() + protected static function getPAService($container) { - $a = ($this->services['pB'] ?? $this->getPBService()); + $a = ($container->services['pB'] ?? self::getPBService($container)); - if (isset($this->services['pA'])) { - return $this->services['pA']; + if (isset($container->services['pA'])) { + return $container->services['pA']; } - $b = ($this->services['pC'] ?? $this->getPCService()); + $b = ($container->services['pC'] ?? self::getPCService($container)); - if (isset($this->services['pA'])) { - return $this->services['pA']; + if (isset($container->services['pA'])) { + return $container->services['pA']; } - return $this->services['pA'] = new \stdClass($a, $b); + return $container->services['pA'] = new \stdClass($a, $b); } /** @@ -671,11 +681,11 @@ protected function getPAService() * * @return \stdClass */ - protected function getPBService() + protected static function getPBService($container) { - $this->services['pB'] = $instance = new \stdClass(); + $container->services['pB'] = $instance = new \stdClass(); - $instance->d = ($this->services['pD'] ?? $this->getPDService()); + $instance->d = ($container->services['pD'] ?? self::getPDService($container)); return $instance; } @@ -685,11 +695,11 @@ protected function getPBService() * * @return \stdClass */ - protected function getPCService($lazyLoad = true) + protected static function getPCService($container, $lazyLoad = true) { - $this->services['pC'] = $instance = new \stdClass(); + $container->services['pC'] = $instance = new \stdClass(); - $instance->d = ($this->services['pD'] ?? $this->getPDService()); + $instance->d = ($container->services['pD'] ?? self::getPDService($container)); return $instance; } @@ -699,15 +709,15 @@ protected function getPCService($lazyLoad = true) * * @return \stdClass */ - protected function getPDService() + protected static function getPDService($container) { - $a = ($this->services['pA'] ?? $this->getPAService()); + $a = ($container->services['pA'] ?? self::getPAService($container)); - if (isset($this->services['pD'])) { - return $this->services['pD']; + if (isset($container->services['pD'])) { + return $container->services['pD']; } - return $this->services['pD'] = new \stdClass($a); + return $container->services['pD'] = new \stdClass($a); } /** @@ -715,15 +725,15 @@ protected function getPDService() * * @return \stdClass */ - protected function getRootService() + protected static function getRootService($container) { $a = new \Symfony\Component\DependencyInjection\Tests\Fixtures\FooForCircularWithAddCalls(); $b = new \stdClass(); - $a->call(new \stdClass(new \stdClass($b, ($this->privates['level5'] ?? $this->getLevel5Service())))); + $a->call(new \stdClass(new \stdClass($b, ($container->privates['level5'] ?? self::getLevel5Service($container))))); - return $this->services['root'] = new \stdClass($a, $b); + return $container->services['root'] = new \stdClass($a, $b); } /** @@ -731,15 +741,15 @@ protected function getRootService() * * @return \stdClass */ - protected function getSubscriberService() + protected static function getSubscriberService($container) { - $a = ($this->services['manager'] ?? $this->getManagerService()); + $a = ($container->services['manager'] ?? self::getManagerService($container)); - if (isset($this->services['subscriber'])) { - return $this->services['subscriber']; + if (isset($container->services['subscriber'])) { + return $container->services['subscriber']; } - return $this->services['subscriber'] = new \stdClass($a); + return $container->services['subscriber'] = new \stdClass($a); } /** @@ -747,15 +757,15 @@ protected function getSubscriberService() * * @return \stdClass */ - protected function getBar6Service() + protected static function getBar6Service($container) { - $a = ($this->services['foo6'] ?? $this->getFoo6Service()); + $a = ($container->services['foo6'] ?? self::getFoo6Service($container)); - if (isset($this->privates['bar6'])) { - return $this->privates['bar6']; + if (isset($container->privates['bar6'])) { + return $container->privates['bar6']; } - return $this->privates['bar6'] = new \stdClass($a); + return $container->privates['bar6'] = new \stdClass($a); } /** @@ -763,11 +773,11 @@ protected function getBar6Service() * * @return \stdClass */ - protected function getLevel5Service() + protected static function getLevel5Service($container) { $a = new \Symfony\Component\DependencyInjection\Tests\Fixtures\FooForCircularWithAddCalls(); - $this->privates['level5'] = $instance = new \stdClass($a); + $container->privates['level5'] = $instance = new \stdClass($a); $a->call($instance); @@ -779,9 +789,9 @@ protected function getLevel5Service() * * @return \stdClass */ - protected function getMailerInline_MailerService() + protected static function getMailerInline_MailerService($container) { - return $this->privates['mailer_inline.mailer'] = new \stdClass(($this->services['mailer_inline.transport_factory'] ?? $this->getMailerInline_TransportFactoryService())->create()); + return $container->privates['mailer_inline.mailer'] = new \stdClass(($container->services['mailer_inline.transport_factory'] ?? self::getMailerInline_TransportFactoryService($container))->create()); } /** @@ -789,14 +799,14 @@ protected function getMailerInline_MailerService() * * @return \stdClass */ - protected function getManager4Service($lazyLoad = true) + protected static function getManager4Service($container, $lazyLoad = true) { - $a = ($this->services['connection4'] ?? $this->getConnection4Service()); + $a = ($container->services['connection4'] ?? self::getConnection4Service($container)); - if (isset($this->privates['manager4'])) { - return $this->privates['manager4']; + if (isset($container->privates['manager4'])) { + return $container->privates['manager4']; } - return $this->privates['manager4'] = new \stdClass($a); + return $container->privates['manager4'] = new \stdClass($a); } } diff --git a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services_array_params.php b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services_array_params.php index 740c006687a65..41e1be5a93b20 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services_array_params.php +++ b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services_array_params.php @@ -15,9 +15,11 @@ class ProjectServiceContainer extends Container { protected $parameters = []; + protected readonly \WeakReference $ref; public function __construct() { + $this->ref = \WeakReference::create($this); $this->parameters = $this->getDefaultParameters(); $this->services = $this->privates = []; @@ -43,11 +45,11 @@ public function isCompiled(): bool * * @return \BarClass */ - protected function getBarService() + protected static function getBarService($container) { - $this->services['bar'] = $instance = new \BarClass(); + $container->services['bar'] = $instance = new \BarClass(); - $instance->setBaz($this->parameters['array_1'], $this->parameters['array_2'], '%array_1%', $this->parameters['array_1']); + $instance->setBaz($container->parameters['array_1'], $container->parameters['array_2'], '%array_1%', $container->parameters['array_1']); return $instance; } diff --git a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services_base64_env.php b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services_base64_env.php index c7428f14f420b..8f64e9292ca0e 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services_base64_env.php +++ b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services_base64_env.php @@ -15,9 +15,11 @@ class Symfony_DI_PhpDumper_Test_Base64Parameters extends Container { protected $parameters = []; + protected readonly \WeakReference $ref; public function __construct() { + $this->ref = \WeakReference::create($this); $this->parameters = $this->getDefaultParameters(); $this->services = $this->privates = []; @@ -77,8 +79,9 @@ public function getParameterBag(): ParameterBagInterface private function getDynamicParameter(string $name) { + $container = $this; $value = match ($name) { - 'hello' => $this->getEnv('base64:foo'), + 'hello' => $container->getEnv('base64:foo'), default => throw new ParameterNotFoundException($name), }; $this->loadedDynamicParameters[$name] = true; diff --git a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services_closure_argument_compiled.php b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services_closure_argument_compiled.php index ae06f37899d04..ca6262cb29d17 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services_closure_argument_compiled.php +++ b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services_closure_argument_compiled.php @@ -15,9 +15,11 @@ 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', @@ -43,9 +45,9 @@ public function isCompiled(): bool * * @return \Foo */ - protected function getFooService() + protected static function getFooService($container) { - return $this->services['foo'] = new \Foo(); + return $container->services['foo'] = new \Foo(); } /** @@ -53,10 +55,14 @@ protected function getFooService() * * @return \Bar */ - protected function getServiceClosureService() + protected static function getServiceClosureService($container) { - return $this->services['service_closure'] = new \Bar(#[\Closure(name: 'foo', class: 'Foo')] function () { - return ($this->services['foo'] ??= new \Foo()); + $containerRef = $container->ref; + + return $container->services['service_closure'] = new \Bar(#[\Closure(name: 'foo', class: 'Foo')] static function () use ($containerRef) { + $container = $containerRef->get(); + + return ($container->services['foo'] ??= new \Foo()); }); } @@ -65,9 +71,13 @@ protected function getServiceClosureService() * * @return \Bar */ - protected function getServiceClosureInvalidService() + protected static function getServiceClosureInvalidService($container) { - return $this->services['service_closure_invalid'] = new \Bar(function () { + $containerRef = $container->ref; + + return $container->services['service_closure_invalid'] = new \Bar(static function () use ($containerRef) { + $container = $containerRef->get(); + return NULL; }); } diff --git a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services_csv_env.php b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services_csv_env.php index 103f1dc7cc22a..dc12622f95c41 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services_csv_env.php +++ b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services_csv_env.php @@ -15,9 +15,11 @@ class Symfony_DI_PhpDumper_Test_CsvParameters extends Container { protected $parameters = []; + protected readonly \WeakReference $ref; public function __construct() { + $this->ref = \WeakReference::create($this); $this->parameters = $this->getDefaultParameters(); $this->services = $this->privates = []; @@ -77,8 +79,9 @@ public function getParameterBag(): ParameterBagInterface private function getDynamicParameter(string $name) { + $container = $this; $value = match ($name) { - 'hello' => $this->getEnv('csv:foo'), + 'hello' => $container->getEnv('csv:foo'), default => throw new ParameterNotFoundException($name), }; $this->loadedDynamicParameters[$name] = true; diff --git a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services_dedup_lazy.php b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services_dedup_lazy.php index 867cda7e7a289..caf9b2f6c2310 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services_dedup_lazy.php +++ b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services_dedup_lazy.php @@ -15,9 +15,11 @@ class ProjectServiceContainer extends Container { protected $parameters = []; + protected readonly \WeakReference $ref; public function __construct() { + $this->ref = \WeakReference::create($this); $this->services = $this->privates = []; $this->methodMap = [ 'bar' => 'getBarService', @@ -49,10 +51,12 @@ protected function createProxy($class, \Closure $factory) * * @return \stdClass */ - protected function getBarService($lazyLoad = true) + protected static function getBarService($container, $lazyLoad = true) { + $containerRef = $container->ref; + if (true === $lazyLoad) { - return $this->services['bar'] = $this->createProxy('stdClassGhost5a8a5eb', fn () => \stdClassGhost5a8a5eb::createLazyGhost($this->getBarService(...))); + return $container->services['bar'] = $container->createProxy('stdClassGhost5a8a5eb', static fn () => \stdClassGhost5a8a5eb::createLazyGhost(static fn ($proxy) => self::getBarService($containerRef->get(), $proxy))); } return $lazyLoad; @@ -63,10 +67,12 @@ protected function getBarService($lazyLoad = true) * * @return \stdClass */ - protected function getBazService($lazyLoad = true) + protected static function getBazService($container, $lazyLoad = true) { + $containerRef = $container->ref; + if (true === $lazyLoad) { - return $this->services['baz'] = $this->createProxy('stdClassProxy5a8a5eb', fn () => \stdClassProxy5a8a5eb::createLazyProxy(fn () => $this->getBazService(false))); + return $container->services['baz'] = $container->createProxy('stdClassProxy5a8a5eb', static fn () => \stdClassProxy5a8a5eb::createLazyProxy(static fn () => self::getBazService($containerRef->get(), false))); } return \foo_bar(); @@ -77,10 +83,12 @@ protected function getBazService($lazyLoad = true) * * @return \stdClass */ - protected function getBuzService($lazyLoad = true) + protected static function getBuzService($container, $lazyLoad = true) { + $containerRef = $container->ref; + if (true === $lazyLoad) { - return $this->services['buz'] = $this->createProxy('stdClassProxy5a8a5eb', fn () => \stdClassProxy5a8a5eb::createLazyProxy(fn () => $this->getBuzService(false))); + return $container->services['buz'] = $container->createProxy('stdClassProxy5a8a5eb', static fn () => \stdClassProxy5a8a5eb::createLazyProxy(static fn () => self::getBuzService($containerRef->get(), false))); } return \foo_bar(); @@ -91,10 +99,12 @@ protected function getBuzService($lazyLoad = true) * * @return \stdClass */ - protected function getFooService($lazyLoad = true) + protected static function getFooService($container, $lazyLoad = true) { + $containerRef = $container->ref; + if (true === $lazyLoad) { - return $this->services['foo'] = $this->createProxy('stdClassGhost5a8a5eb', fn () => \stdClassGhost5a8a5eb::createLazyGhost($this->getFooService(...))); + return $container->services['foo'] = $container->createProxy('stdClassGhost5a8a5eb', static fn () => \stdClassGhost5a8a5eb::createLazyGhost(static fn ($proxy) => self::getFooService($containerRef->get(), $proxy))); } return $lazyLoad; diff --git a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services_deep_graph.php b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services_deep_graph.php index 8c8cc1e30ce2d..4b6c1e3936440 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services_deep_graph.php +++ b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services_deep_graph.php @@ -15,9 +15,11 @@ class Symfony_DI_PhpDumper_Test_Deep_Graph extends Container { protected $parameters = []; + protected readonly \WeakReference $ref; public function __construct() { + $this->ref = \WeakReference::create($this); $this->services = $this->privates = []; $this->methodMap = [ 'bar' => 'getBarService', @@ -48,11 +50,11 @@ public function getRemovedIds(): array * * @return \stdClass */ - protected function getBarService() + protected static function getBarService($container) { - $this->services['bar'] = $instance = new \stdClass(); + $container->services['bar'] = $instance = new \stdClass(); - $instance->p5 = new \stdClass(($this->services['foo'] ?? $this->getFooService())); + $instance->p5 = new \stdClass(($container->services['foo'] ?? self::getFooService($container))); return $instance; } @@ -62,12 +64,12 @@ protected function getBarService() * * @return \Symfony\Component\DependencyInjection\Tests\Dumper\FooForDeepGraph */ - protected function getFooService() + protected static function getFooService($container) { - $a = ($this->services['bar'] ?? $this->getBarService()); + $a = ($container->services['bar'] ?? self::getBarService($container)); - if (isset($this->services['foo'])) { - return $this->services['foo']; + if (isset($container->services['foo'])) { + return $container->services['foo']; } $b = new \stdClass(); @@ -76,6 +78,6 @@ protected function getFooService() $b->p2 = $c; - return $this->services['foo'] = new \Symfony\Component\DependencyInjection\Tests\Dumper\FooForDeepGraph($a, $b); + return $container->services['foo'] = new \Symfony\Component\DependencyInjection\Tests\Dumper\FooForDeepGraph($a, $b); } } diff --git a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services_default_env.php b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services_default_env.php index 1f7f4967b4588..39e0c7f7570b1 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services_default_env.php +++ b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services_default_env.php @@ -15,9 +15,11 @@ class Symfony_DI_PhpDumper_Test_DefaultParameters extends Container { protected $parameters = []; + protected readonly \WeakReference $ref; public function __construct() { + $this->ref = \WeakReference::create($this); $this->parameters = $this->getDefaultParameters(); $this->services = $this->privates = []; @@ -79,10 +81,11 @@ public function getParameterBag(): ParameterBagInterface private function getDynamicParameter(string $name) { + $container = $this; $value = match ($name) { - 'fallback_env' => $this->getEnv('foobar'), - 'hello' => $this->getEnv('default:fallback_param:bar'), - 'hello-bar' => $this->getEnv('default:fallback_env:key:baz:json:foo'), + 'fallback_env' => $container->getEnv('foobar'), + 'hello' => $container->getEnv('default:fallback_param:bar'), + 'hello-bar' => $container->getEnv('default:fallback_env:key:baz:json:foo'), default => throw new ParameterNotFoundException($name), }; $this->loadedDynamicParameters[$name] = true; diff --git a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services_env_in_id.php b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services_env_in_id.php index 1fa0137e41cd3..3673a1cdfe1fe 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services_env_in_id.php +++ b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services_env_in_id.php @@ -15,9 +15,11 @@ class ProjectServiceContainer extends Container { protected $parameters = []; + protected readonly \WeakReference $ref; public function __construct() { + $this->ref = \WeakReference::create($this); $this->parameters = $this->getDefaultParameters(); $this->services = $this->privates = []; @@ -52,9 +54,9 @@ public function getRemovedIds(): array * * @return \stdClass */ - protected function getBarService() + protected static function getBarService($container) { - return $this->services['bar'] = new \stdClass(($this->privates['bar_%env(BAR)%'] ??= new \stdClass())); + return $container->services['bar'] = new \stdClass(($container->privates['bar_%env(BAR)%'] ??= new \stdClass())); } /** @@ -62,9 +64,9 @@ protected function getBarService() * * @return \stdClass */ - protected function getFooService() + protected static function getFooService($container) { - return $this->services['foo'] = new \stdClass(($this->privates['bar_%env(BAR)%'] ??= new \stdClass()), ['baz_'.$this->getEnv('string:BAR') => new \stdClass()]); + return $container->services['foo'] = new \stdClass(($container->privates['bar_%env(BAR)%'] ??= new \stdClass()), ['baz_'.$container->getEnv('string:BAR') => new \stdClass()]); } public function getParameter(string $name): array|bool|string|int|float|\UnitEnum|null diff --git a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services_errored_definition.php b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services_errored_definition.php index 861aecbbf92d9..82409d438413e 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services_errored_definition.php +++ b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services_errored_definition.php @@ -15,9 +15,11 @@ class Symfony_DI_PhpDumper_Errored_Definition extends Container { protected $parameters = []; + protected readonly \WeakReference $ref; public function __construct() { + $this->ref = \WeakReference::create($this); $this->parameters = $this->getDefaultParameters(); $this->services = $this->privates = []; @@ -90,11 +92,11 @@ public function getRemovedIds(): array * * @return \stdClass */ - protected function getBARService() + protected static function getBARService($container) { - $this->services['BAR'] = $instance = new \stdClass(); + $container->services['BAR'] = $instance = new \stdClass(); - $instance->bar = ($this->services['bar'] ?? $this->getBar3Service()); + $instance->bar = ($container->services['bar'] ?? self::getBar3Service($container)); return $instance; } @@ -104,9 +106,9 @@ protected function getBARService() * * @return \stdClass */ - protected function getBAR2Service() + protected static function getBAR2Service($container) { - return $this->services['BAR2'] = new \stdClass(); + return $container->services['BAR2'] = new \stdClass(); } /** @@ -114,9 +116,9 @@ protected function getBAR2Service() * * @return \Bar */ - protected function getAServiceService() + protected static function getAServiceService($container) { - return $this->services['a_service'] = ($this->privates['a_factory'] ??= new \Bar())->getBar(); + return $container->services['a_service'] = ($container->privates['a_factory'] ??= new \Bar())->getBar(); } /** @@ -124,9 +126,9 @@ protected function getAServiceService() * * @return \Bar */ - protected function getBServiceService() + protected static function getBServiceService($container) { - return $this->services['b_service'] = ($this->privates['a_factory'] ??= new \Bar())->getBar(); + return $container->services['b_service'] = ($container->privates['a_factory'] ??= new \Bar())->getBar(); } /** @@ -134,11 +136,11 @@ protected function getBServiceService() * * @return \Bar\FooClass */ - protected function getBar3Service() + protected static function getBar3Service($container) { - $a = ($this->services['foo.baz'] ?? $this->getFoo_BazService()); + $a = ($container->services['foo.baz'] ?? self::getFoo_BazService($container)); - $this->services['bar'] = $instance = new \Bar\FooClass('foo', $a, 'foo_bar'); + $container->services['bar'] = $instance = new \Bar\FooClass('foo', $a, 'foo_bar'); $a->configure($instance); @@ -150,9 +152,9 @@ protected function getBar3Service() * * @return \stdClass */ - protected function getBar22Service() + protected static function getBar22Service($container) { - return $this->services['bar2'] = new \stdClass(); + return $container->services['bar2'] = new \stdClass(); } /** @@ -160,11 +162,11 @@ protected function getBar22Service() * * @return \Baz */ - protected function getBazService() + protected static function getBazService($container) { - $this->services['baz'] = $instance = new \Baz(); + $container->services['baz'] = $instance = new \Baz(); - $instance->setFoo(($this->services['foo_with_inline'] ?? $this->getFooWithInlineService())); + $instance->setFoo(($container->services['foo_with_inline'] ?? self::getFooWithInlineService($container))); return $instance; } @@ -174,12 +176,12 @@ protected function getBazService() * * @return \stdClass */ - protected function getConfiguredServiceService() + protected static function getConfiguredServiceService($container) { - $this->services['configured_service'] = $instance = new \stdClass(); + $container->services['configured_service'] = $instance = new \stdClass(); $a = new \ConfClass(); - $a->setFoo(($this->services['baz'] ?? $this->getBazService())); + $a->setFoo(($container->services['baz'] ?? self::getBazService($container))); $a->configureStdClass($instance); @@ -191,9 +193,9 @@ protected function getConfiguredServiceService() * * @return \stdClass */ - protected function getConfiguredServiceSimpleService() + protected static function getConfiguredServiceSimpleService($container) { - $this->services['configured_service_simple'] = $instance = new \stdClass(); + $container->services['configured_service_simple'] = $instance = new \stdClass(); (new \ConfClass('bar'))->configureStdClass($instance); @@ -205,9 +207,9 @@ protected function getConfiguredServiceSimpleService() * * @return \stdClass */ - protected function getDecoratorServiceService() + protected static function getDecoratorServiceService($container) { - return $this->services['decorator_service'] = new \stdClass(); + return $container->services['decorator_service'] = new \stdClass(); } /** @@ -215,9 +217,9 @@ protected function getDecoratorServiceService() * * @return \stdClass */ - protected function getDecoratorServiceWithNameService() + protected static function getDecoratorServiceWithNameService($container) { - return $this->services['decorator_service_with_name'] = new \stdClass(); + return $container->services['decorator_service_with_name'] = new \stdClass(); } /** @@ -227,11 +229,11 @@ protected function getDecoratorServiceWithNameService() * * @deprecated Since vendor/package 1.1: The "deprecated_service" service is deprecated. You should stop using it, as it will be removed in the future. */ - protected function getDeprecatedServiceService() + protected static function getDeprecatedServiceService($container) { trigger_deprecation('vendor/package', '1.1', 'The "deprecated_service" service is deprecated. You should stop using it, as it will be removed in the future.'); - return $this->services['deprecated_service'] = new \stdClass(); + return $container->services['deprecated_service'] = new \stdClass(); } /** @@ -239,9 +241,9 @@ protected function getDeprecatedServiceService() * * @return \Bar */ - protected function getFactoryServiceService() + protected static function getFactoryServiceService($container) { - return $this->services['factory_service'] = ($this->services['foo.baz'] ?? $this->getFoo_BazService())->getInstance(); + return $container->services['factory_service'] = ($container->services['foo.baz'] ?? self::getFoo_BazService($container))->getInstance(); } /** @@ -249,9 +251,9 @@ protected function getFactoryServiceService() * * @return \Bar */ - protected function getFactoryServiceSimpleService() + protected static function getFactoryServiceSimpleService($container) { - return $this->services['factory_service_simple'] = $this->getFactorySimpleService()->getInstance(); + return $container->services['factory_service_simple'] = self::getFactorySimpleService($container)->getInstance(); } /** @@ -259,16 +261,16 @@ protected function getFactoryServiceSimpleService() * * @return \Bar\FooClass */ - protected function getFooService() + protected static function getFooService($container) { - $a = ($this->services['foo.baz'] ?? $this->getFoo_BazService()); + $a = ($container->services['foo.baz'] ?? self::getFoo_BazService($container)); - $this->services['foo'] = $instance = \Bar\FooClass::getInstance('foo', $a, ['bar' => 'foo is bar', 'foobar' => 'bar'], true, $this); + $container->services['foo'] = $instance = \Bar\FooClass::getInstance('foo', $a, ['bar' => 'foo is bar', 'foobar' => 'bar'], true, $container); $instance->foo = 'bar'; $instance->moo = $a; $instance->qux = ['bar' => 'foo is bar', 'foobar' => 'bar']; - $instance->setBar(($this->services['bar'] ?? $this->getBar3Service())); + $instance->setBar(($container->services['bar'] ?? self::getBar3Service($container))); $instance->initialize(); sc_configure($instance); @@ -280,9 +282,9 @@ protected function getFooService() * * @return \BazClass */ - protected function getFoo_BazService() + protected static function getFoo_BazService($container) { - $this->services['foo.baz'] = $instance = \BazClass::getInstance(); + $container->services['foo.baz'] = $instance = \BazClass::getInstance(); \BazClass::configureStatic1($instance); @@ -294,13 +296,13 @@ protected function getFoo_BazService() * * @return \Bar\FooClass */ - protected function getFooBarService() + protected static function getFooBarService($container) { - $this->factories['foo_bar'] = function () { - return new \Bar\FooClass(($this->services['deprecated_service'] ?? $this->getDeprecatedServiceService())); + $container->factories['foo_bar'] = static function ($container) { + return new \Bar\FooClass(($container->services['deprecated_service'] ?? self::getDeprecatedServiceService($container))); }; - return $this->factories['foo_bar'](); + return $container->factories['foo_bar']($container); } /** @@ -308,13 +310,13 @@ protected function getFooBarService() * * @return \Foo */ - protected function getFooWithInlineService() + protected static function getFooWithInlineService($container) { - $this->services['foo_with_inline'] = $instance = new \Foo(); + $container->services['foo_with_inline'] = $instance = new \Foo(); $a = new \Bar(); $a->pub = 'pub'; - $a->setBaz(($this->services['baz'] ?? $this->getBazService())); + $a->setBaz(($container->services['baz'] ?? self::getBazService($container))); $instance->setBar($a); @@ -326,12 +328,16 @@ protected function getFooWithInlineService() * * @return \LazyContext */ - protected function getLazyContextService() + protected static function getLazyContextService($container) { - return $this->services['lazy_context'] = new \LazyContext(new RewindableGenerator(function () { - yield 'k1' => ($this->services['foo.baz'] ?? $this->getFoo_BazService()); - yield 'k2' => $this; - }, 2), new RewindableGenerator(function () { + $containerRef = $container->ref; + + return $container->services['lazy_context'] = new \LazyContext(new RewindableGenerator(static function () use ($containerRef) { + $container = $containerRef->get(); + + yield 'k1' => ($container->services['foo.baz'] ?? self::getFoo_BazService($container)); + yield 'k2' => $container; + }, 2), new RewindableGenerator(static function () { return new \EmptyIterator(); }, 0)); } @@ -341,11 +347,15 @@ protected function getLazyContextService() * * @return \LazyContext */ - protected function getLazyContextIgnoreInvalidRefService() + protected static function getLazyContextIgnoreInvalidRefService($container) { - return $this->services['lazy_context_ignore_invalid_ref'] = new \LazyContext(new RewindableGenerator(function () { - yield 0 => ($this->services['foo.baz'] ?? $this->getFoo_BazService()); - }, 1), new RewindableGenerator(function () { + $containerRef = $container->ref; + + return $container->services['lazy_context_ignore_invalid_ref'] = new \LazyContext(new RewindableGenerator(static function () use ($containerRef) { + $container = $containerRef->get(); + + yield 0 => ($container->services['foo.baz'] ?? self::getFoo_BazService($container)); + }, 1), new RewindableGenerator(static function () { return new \EmptyIterator(); }, 0)); } @@ -355,15 +365,15 @@ protected function getLazyContextIgnoreInvalidRefService() * * @return \Bar\FooClass */ - protected function getMethodCall1Service() + protected static function getMethodCall1Service($container) { include_once '%path%foo.php'; - $this->services['method_call1'] = $instance = new \Bar\FooClass(); + $container->services['method_call1'] = $instance = new \Bar\FooClass(); - $instance->setBar(($this->services['foo'] ?? $this->getFooService())); + $instance->setBar(($container->services['foo'] ?? self::getFooService($container))); $instance->setBar(NULL); - $instance->setBar((($this->services['foo'] ?? $this->getFooService())->foo() . (($this->hasParameter("foo")) ? ($this->getParameter("foo")) : ("default")))); + $instance->setBar((($container->services['foo'] ?? self::getFooService($container))->foo() . (($container->hasParameter("foo")) ? ($container->getParameter("foo")) : ("default")))); return $instance; } @@ -373,12 +383,12 @@ protected function getMethodCall1Service() * * @return \FooBarBaz */ - protected function getNewFactoryServiceService() + protected static function getNewFactoryServiceService($container) { $a = new \FactoryClass(); $a->foo = 'bar'; - $this->services['new_factory_service'] = $instance = $a->getInstance(); + $container->services['new_factory_service'] = $instance = $a->getInstance(); $instance->foo = 'bar'; @@ -390,9 +400,9 @@ protected function getNewFactoryServiceService() * * @return \stdClass */ - protected function getPreloadSidekickService() + protected static function getPreloadSidekickService($container) { - return $this->services['preload_sidekick'] = new \stdClass(); + return $container->services['preload_sidekick'] = new \stdClass(); } /** @@ -400,9 +410,9 @@ protected function getPreloadSidekickService() * * @return \stdClass */ - protected function getRuntimeErrorService() + protected static function getRuntimeErrorService($container) { - return $this->services['runtime_error'] = new \stdClass(throw new RuntimeException('Service "errored_definition" is broken.')); + return $container->services['runtime_error'] = new \stdClass(throw new RuntimeException('Service "errored_definition" is broken.')); } /** @@ -410,9 +420,9 @@ protected function getRuntimeErrorService() * * @return \Bar\FooClass */ - protected function getServiceFromStaticMethodService() + protected static function getServiceFromStaticMethodService($container) { - return $this->services['service_from_static_method'] = \Bar\FooClass::getInstance(); + return $container->services['service_from_static_method'] = \Bar\FooClass::getInstance(); } /** @@ -420,11 +430,15 @@ protected function getServiceFromStaticMethodService() * * @return \Bar */ - protected function getTaggedIteratorService() + protected static function getTaggedIteratorService($container) { - return $this->services['tagged_iterator'] = new \Bar(new RewindableGenerator(function () { - yield 0 => ($this->services['foo'] ?? $this->getFooService()); - yield 1 => ($this->privates['tagged_iterator_foo'] ??= new \Bar()); + $containerRef = $container->ref; + + return $container->services['tagged_iterator'] = new \Bar(new RewindableGenerator(static function () use ($containerRef) { + $container = $containerRef->get(); + + yield 0 => ($container->services['foo'] ?? self::getFooService($container)); + yield 1 => ($container->privates['tagged_iterator_foo'] ??= new \Bar()); }, 2)); } @@ -435,7 +449,7 @@ protected function getTaggedIteratorService() * * @deprecated Since vendor/package 1.1: The "factory_simple" service is deprecated. You should stop using it, as it will be removed in the future. */ - protected function getFactorySimpleService() + protected static function getFactorySimpleService($container) { trigger_deprecation('vendor/package', '1.1', 'The "factory_simple" service is deprecated. You should stop using it, as it will be removed in the future.'); diff --git a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services_inline_requires.php b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services_inline_requires.php index d0509ad594369..f184f64dca43a 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services_inline_requires.php +++ b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services_inline_requires.php @@ -15,9 +15,11 @@ class ProjectServiceContainer extends Container { protected $parameters = []; + protected readonly \WeakReference $ref; public function __construct() { + $this->ref = \WeakReference::create($this); $this->services = $this->privates = []; $this->methodMap = [ 'Symfony\\Component\\DependencyInjection\\Tests\\Fixtures\\ParentNotExists' => 'getParentNotExistsService', @@ -27,7 +29,7 @@ public function __construct() $this->aliases = []; - $this->privates['service_container'] = function () { + $this->privates['service_container'] = static function ($container) { include_once \dirname(__DIR__, 1).'/includes/HotPath/I1.php'; include_once \dirname(__DIR__, 1).'/includes/HotPath/P1.php'; include_once \dirname(__DIR__, 1).'/includes/HotPath/T1.php'; @@ -57,9 +59,9 @@ public function getRemovedIds(): array * * @return \Symfony\Component\DependencyInjection\Tests\Fixtures\ParentNotExists */ - protected function getParentNotExistsService() + protected static function getParentNotExistsService($container) { - return $this->services['Symfony\\Component\\DependencyInjection\\Tests\\Fixtures\\ParentNotExists'] = new \Symfony\Component\DependencyInjection\Tests\Fixtures\ParentNotExists(); + return $container->services['Symfony\\Component\\DependencyInjection\\Tests\\Fixtures\\ParentNotExists'] = new \Symfony\Component\DependencyInjection\Tests\Fixtures\ParentNotExists(); } /** @@ -67,9 +69,9 @@ protected function getParentNotExistsService() * * @return \Symfony\Component\DependencyInjection\Tests\Fixtures\includes\HotPath\C1 */ - protected function getC1Service() + protected static function getC1Service($container) { - return $this->services['Symfony\\Component\\DependencyInjection\\Tests\\Fixtures\\includes\\HotPath\\C1'] = new \Symfony\Component\DependencyInjection\Tests\Fixtures\includes\HotPath\C1(); + return $container->services['Symfony\\Component\\DependencyInjection\\Tests\\Fixtures\\includes\\HotPath\\C1'] = new \Symfony\Component\DependencyInjection\Tests\Fixtures\includes\HotPath\C1(); } /** @@ -77,11 +79,11 @@ protected function getC1Service() * * @return \Symfony\Component\DependencyInjection\Tests\Fixtures\includes\HotPath\C2 */ - protected function getC2Service() + protected static function getC2Service($container) { include_once \dirname(__DIR__, 1).'/includes/HotPath/C2.php'; include_once \dirname(__DIR__, 1).'/includes/HotPath/C3.php'; - return $this->services['Symfony\\Component\\DependencyInjection\\Tests\\Fixtures\\includes\\HotPath\\C2'] = new \Symfony\Component\DependencyInjection\Tests\Fixtures\includes\HotPath\C2(new \Symfony\Component\DependencyInjection\Tests\Fixtures\includes\HotPath\C3()); + return $container->services['Symfony\\Component\\DependencyInjection\\Tests\\Fixtures\\includes\\HotPath\\C2'] = new \Symfony\Component\DependencyInjection\Tests\Fixtures\includes\HotPath\C2(new \Symfony\Component\DependencyInjection\Tests\Fixtures\includes\HotPath\C3()); } } diff --git a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services_inline_self_ref.php b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services_inline_self_ref.php index ed8c7c17e9fcc..4ccee1b0c2bc4 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services_inline_self_ref.php +++ b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services_inline_self_ref.php @@ -15,9 +15,11 @@ class Symfony_DI_PhpDumper_Test_Inline_Self_Ref extends Container { protected $parameters = []; + protected readonly \WeakReference $ref; public function __construct() { + $this->ref = \WeakReference::create($this); $this->services = $this->privates = []; $this->methodMap = [ 'App\\Foo' => 'getFooService', @@ -41,14 +43,14 @@ public function isCompiled(): bool * * @return \App\Foo */ - protected function getFooService() + protected static function getFooService($container) { $a = new \App\Bar(); $b = new \App\Baz($a); $b->bar = $a; - $this->services['App\\Foo'] = $instance = new \App\Foo($b); + $container->services['App\\Foo'] = $instance = new \App\Foo($b); $a->foo = $instance; diff --git a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services_json_env.php b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services_json_env.php index 0b4337e2dfc06..35aa89cd07e72 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services_json_env.php +++ b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services_json_env.php @@ -15,9 +15,11 @@ class Symfony_DI_PhpDumper_Test_JsonParameters extends Container { protected $parameters = []; + protected readonly \WeakReference $ref; public function __construct() { + $this->ref = \WeakReference::create($this); $this->parameters = $this->getDefaultParameters(); $this->services = $this->privates = []; @@ -78,9 +80,10 @@ public function getParameterBag(): ParameterBagInterface private function getDynamicParameter(string $name) { + $container = $this; $value = match ($name) { - 'hello' => $this->getEnv('json:foo'), - 'hello-bar' => $this->getEnv('json:bar'), + 'hello' => $container->getEnv('json:foo'), + 'hello-bar' => $container->getEnv('json:bar'), default => throw new ParameterNotFoundException($name), }; $this->loadedDynamicParameters[$name] = true; diff --git a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services_locator.php b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services_locator.php index cf3abe06e3f00..4239d82f1e721 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services_locator.php +++ b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services_locator.php @@ -15,9 +15,11 @@ class ProjectServiceContainer extends Container { protected $parameters = []; + protected readonly \WeakReference $ref; public function __construct() { + $this->ref = \WeakReference::create($this); $this->services = $this->privates = []; $this->methodMap = [ 'bar_service' => 'getBarServiceService', @@ -58,9 +60,9 @@ public function getRemovedIds(): array * * @return \stdClass */ - protected function getBarServiceService() + protected static function getBarServiceService($container) { - return $this->services['bar_service'] = new \stdClass(($this->privates['baz_service'] ??= new \stdClass())); + return $container->services['bar_service'] = new \stdClass(($container->privates['baz_service'] ??= new \stdClass())); } /** @@ -68,13 +70,21 @@ protected function getBarServiceService() * * @return \Symfony\Component\DependencyInjection\ServiceLocator */ - protected function getFooServiceService() + protected static function getFooServiceService($container) { - return $this->services['foo_service'] = new \Symfony\Component\DependencyInjection\ServiceLocator(['bar' => #[\Closure(name: 'bar_service', class: 'stdClass')] function () { - return ($this->services['bar_service'] ?? $this->getBarServiceService()); - }, 'baz' => #[\Closure(name: 'baz_service', class: 'stdClass')] function (): \stdClass { - return ($this->privates['baz_service'] ??= new \stdClass()); - }, 'nil' => function () { + $containerRef = $container->ref; + + return $container->services['foo_service'] = new \Symfony\Component\DependencyInjection\ServiceLocator(['bar' => #[\Closure(name: 'bar_service', class: 'stdClass')] static function () use ($containerRef) { + $container = $containerRef->get(); + + return ($container->services['bar_service'] ?? self::getBarServiceService($container)); + }, 'baz' => #[\Closure(name: 'baz_service', class: 'stdClass')] static function () use ($containerRef): \stdClass { + $container = $containerRef->get(); + + return ($container->privates['baz_service'] ??= new \stdClass()); + }, 'nil' => static function () use ($containerRef) { + $container = $containerRef->get(); + return NULL; }]); } @@ -84,9 +94,9 @@ protected function getFooServiceService() * * @return \stdClass */ - protected function getTranslator_Loader1Service() + protected static function getTranslator_Loader1Service($container) { - return $this->services['translator.loader_1'] = new \stdClass(); + return $container->services['translator.loader_1'] = new \stdClass(); } /** @@ -94,9 +104,9 @@ protected function getTranslator_Loader1Service() * * @return \stdClass */ - protected function getTranslator_Loader2Service() + protected static function getTranslator_Loader2Service($container) { - return $this->services['translator.loader_2'] = new \stdClass(); + return $container->services['translator.loader_2'] = new \stdClass(); } /** @@ -104,9 +114,9 @@ protected function getTranslator_Loader2Service() * * @return \stdClass */ - protected function getTranslator_Loader3Service() + protected static function getTranslator_Loader3Service($container) { - return $this->services['translator.loader_3'] = new \stdClass(); + return $container->services['translator.loader_3'] = new \stdClass(); } /** @@ -114,10 +124,14 @@ protected function getTranslator_Loader3Service() * * @return \Symfony\Component\DependencyInjection\Tests\Fixtures\StubbedTranslator */ - protected function getTranslator1Service() + protected static function getTranslator1Service($container) { - return $this->services['translator_1'] = new \Symfony\Component\DependencyInjection\Tests\Fixtures\StubbedTranslator(new \Symfony\Component\DependencyInjection\ServiceLocator(['translator.loader_1' => #[\Closure(name: 'translator.loader_1', class: 'stdClass')] function () { - return ($this->services['translator.loader_1'] ??= new \stdClass()); + $containerRef = $container->ref; + + return $container->services['translator_1'] = new \Symfony\Component\DependencyInjection\Tests\Fixtures\StubbedTranslator(new \Symfony\Component\DependencyInjection\ServiceLocator(['translator.loader_1' => #[\Closure(name: 'translator.loader_1', class: 'stdClass')] static function () use ($containerRef) { + $container = $containerRef->get(); + + return ($container->services['translator.loader_1'] ??= new \stdClass()); }])); } @@ -126,13 +140,17 @@ protected function getTranslator1Service() * * @return \Symfony\Component\DependencyInjection\Tests\Fixtures\StubbedTranslator */ - protected function getTranslator2Service() + protected static function getTranslator2Service($container) { - $this->services['translator_2'] = $instance = new \Symfony\Component\DependencyInjection\Tests\Fixtures\StubbedTranslator(new \Symfony\Component\DependencyInjection\ServiceLocator(['translator.loader_2' => #[\Closure(name: 'translator.loader_2', class: 'stdClass')] function () { - return ($this->services['translator.loader_2'] ??= new \stdClass()); + $containerRef = $container->ref; + + $container->services['translator_2'] = $instance = new \Symfony\Component\DependencyInjection\Tests\Fixtures\StubbedTranslator(new \Symfony\Component\DependencyInjection\ServiceLocator(['translator.loader_2' => #[\Closure(name: 'translator.loader_2', class: 'stdClass')] static function () use ($containerRef) { + $container = $containerRef->get(); + + return ($container->services['translator.loader_2'] ??= new \stdClass()); }])); - $instance->addResource('db', ($this->services['translator.loader_2'] ??= new \stdClass()), 'nl'); + $instance->addResource('db', ($container->services['translator.loader_2'] ??= new \stdClass()), 'nl'); return $instance; } @@ -142,13 +160,17 @@ protected function getTranslator2Service() * * @return \Symfony\Component\DependencyInjection\Tests\Fixtures\StubbedTranslator */ - protected function getTranslator3Service() + protected static function getTranslator3Service($container) { - $this->services['translator_3'] = $instance = new \Symfony\Component\DependencyInjection\Tests\Fixtures\StubbedTranslator(new \Symfony\Component\DependencyInjection\ServiceLocator(['translator.loader_3' => #[\Closure(name: 'translator.loader_3', class: 'stdClass')] function () { - return ($this->services['translator.loader_3'] ??= new \stdClass()); + $containerRef = $container->ref; + + $container->services['translator_3'] = $instance = new \Symfony\Component\DependencyInjection\Tests\Fixtures\StubbedTranslator(new \Symfony\Component\DependencyInjection\ServiceLocator(['translator.loader_3' => #[\Closure(name: 'translator.loader_3', class: 'stdClass')] static function () use ($containerRef) { + $container = $containerRef->get(); + + return ($container->services['translator.loader_3'] ??= new \stdClass()); }])); - $a = ($this->services['translator.loader_3'] ??= new \stdClass()); + $a = ($container->services['translator.loader_3'] ??= new \stdClass()); $instance->addResource('db', $a, 'nl'); $instance->addResource('db', $a, 'en'); diff --git a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services_new_in_initializer.php b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services_new_in_initializer.php index 0928b5c6f2c1c..238fabbae8c6f 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services_new_in_initializer.php +++ b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services_new_in_initializer.php @@ -15,9 +15,11 @@ 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', @@ -41,8 +43,8 @@ public function isCompiled(): bool * * @return \Symfony\Component\DependencyInjection\Tests\Fixtures\NewInInitializer */ - protected function getFooService() + protected static function getFooService($container) { - return $this->services['foo'] = new \Symfony\Component\DependencyInjection\Tests\Fixtures\NewInInitializer(bar: 234); + return $container->services['foo'] = new \Symfony\Component\DependencyInjection\Tests\Fixtures\NewInInitializer(bar: 234); } } diff --git a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services_non_shared_duplicates.php b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services_non_shared_duplicates.php index 3802fc28ffc1b..4ce242bb265d8 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services_non_shared_duplicates.php +++ b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services_non_shared_duplicates.php @@ -15,11 +15,13 @@ class ProjectServiceContainer extends Container { protected $parameters = []; + protected readonly \WeakReference $ref; protected \Closure $getService; public function __construct() { - $this->getService = $this->getService(...); + $containerRef = $this->ref = \WeakReference::create($this); + $this->getService = static function () use ($containerRef) { return $containerRef->get()->getService(...\func_get_args()); }; $this->services = $this->privates = []; $this->methodMap = [ 'bar' => 'getBarService', @@ -52,9 +54,9 @@ public function getRemovedIds(): array * * @return \stdClass */ - protected function getBarService() + protected static function getBarService($container) { - return $this->services['bar'] = new \stdClass((new \stdClass()), (new \stdClass())); + return $container->services['bar'] = new \stdClass((new \stdClass()), (new \stdClass())); } /** @@ -62,9 +64,9 @@ protected function getBarService() * * @return \stdClass */ - protected function getBazService() + protected static function getBazService($container) { - return $this->services['baz'] = new \stdClass(new \Symfony\Component\DependencyInjection\Argument\ServiceLocator($this->getService, [ + return $container->services['baz'] = new \stdClass(new \Symfony\Component\DependencyInjection\Argument\ServiceLocator($container->getService, [ 'foo' => [false, 'foo', 'getFooService', false], ], [ 'foo' => '?', @@ -76,12 +78,12 @@ protected function getBazService() * * @return \stdClass */ - protected function getFooService() + protected static function getFooService($container) { - $this->factories['service_container']['foo'] = function () { + $container->factories['service_container']['foo'] = static function ($container) { return new \stdClass(); }; - return $this->factories['service_container']['foo'](); + return $container->factories['service_container']['foo']($container); } } diff --git a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services_non_shared_lazy.php b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services_non_shared_lazy.php index 001d7746da3bb..d00f210156e34 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services_non_shared_lazy.php +++ b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services_non_shared_lazy.php @@ -15,9 +15,11 @@ class ProjectServiceContainer extends Container { protected $parameters = []; + protected readonly \WeakReference $ref; public function __construct() { + $this->ref = \WeakReference::create($this); $this->services = $this->privates = []; $this->methodMap = [ 'bar' => 'getBarService', @@ -53,9 +55,9 @@ protected function createProxy($class, \Closure $factory) * * @return \stdClass */ - protected function getBarService() + protected static function getBarService($container) { - return $this->services['bar'] = new \stdClass((isset($this->factories['service_container']['foo']) ? $this->factories['service_container']['foo']() : $this->getFooService())); + return $container->services['bar'] = new \stdClass((isset($container->factories['service_container']['foo']) ? $container->factories['service_container']['foo']($container) : self::getFooService($container))); } /** @@ -63,9 +65,11 @@ protected function getBarService() * * @return \stdClass */ - protected function getFooService($lazyLoad = true) + protected static function getFooService($container, $lazyLoad = true) { - $this->factories['service_container']['foo'] ??= $this->getFooService(...); + $containerRef = $container->ref; + + $container->factories['service_container']['foo'] ??= self::getFooService(...); // lazy factory for stdClass diff --git a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services_non_shared_lazy_as_files.txt b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services_non_shared_lazy_as_files.txt index a4e8807c71039..0594c76789555 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services_non_shared_lazy_as_files.txt +++ b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services_non_shared_lazy_as_files.txt @@ -19,10 +19,12 @@ class getNonSharedFooService extends ProjectServiceContainer */ public static function do($container, $lazyLoad = true) { - $container->factories['non_shared_foo'] ??= fn () => self::do($container); + $containerRef = $container->ref; + + $container->factories['non_shared_foo'] ??= self::do(...); if (true === $lazyLoad) { - return $container->createProxy('FooLazyClassGhostF814e3a', fn () => \FooLazyClassGhostF814e3a::createLazyGhost(fn ($proxy) => self::do($container, $proxy))); + return $container->createProxy('FooLazyClassGhostF814e3a', static fn () => \FooLazyClassGhostF814e3a::createLazyGhost(static fn ($proxy) => self::do($containerRef->get(), $proxy))); } static $include = true; @@ -79,9 +81,11 @@ class ProjectServiceContainer extends Container protected $targetDir; protected $parameters = []; private $buildParameters; + protected readonly \WeakReference $ref; public function __construct(array $buildParameters = [], $containerDir = __DIR__) { + $this->ref = \WeakReference::create($this); $this->buildParameters = $buildParameters; $this->containerDir = $containerDir; $this->targetDir = \dirname($containerDir); diff --git a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services_non_shared_lazy_ghost.php b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services_non_shared_lazy_ghost.php index 7d584eef41063..b318bb5cfec61 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services_non_shared_lazy_ghost.php +++ b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services_non_shared_lazy_ghost.php @@ -15,9 +15,11 @@ class ProjectServiceContainer extends Container { protected $parameters = []; + protected readonly \WeakReference $ref; public function __construct() { + $this->ref = \WeakReference::create($this); $this->services = $this->privates = []; $this->methodMap = [ 'bar' => 'getBarService', @@ -53,9 +55,9 @@ protected function createProxy($class, \Closure $factory) * * @return \stdClass */ - protected function getBarService() + protected static function getBarService($container) { - return $this->services['bar'] = new \stdClass((isset($this->factories['service_container']['foo']) ? $this->factories['service_container']['foo']() : $this->getFooService())); + return $container->services['bar'] = new \stdClass((isset($container->factories['service_container']['foo']) ? $container->factories['service_container']['foo']($container) : self::getFooService($container))); } /** @@ -63,12 +65,14 @@ protected function getBarService() * * @return \stdClass */ - protected function getFooService($lazyLoad = true) + protected static function getFooService($container, $lazyLoad = true) { - $this->factories['service_container']['foo'] ??= $this->getFooService(...); + $containerRef = $container->ref; + + $container->factories['service_container']['foo'] ??= self::getFooService(...); if (true === $lazyLoad) { - return $this->createProxy('stdClassGhost5a8a5eb', fn () => \stdClassGhost5a8a5eb::createLazyGhost($this->getFooService(...))); + return $container->createProxy('stdClassGhost5a8a5eb', static fn () => \stdClassGhost5a8a5eb::createLazyGhost(static fn ($proxy) => self::getFooService($containerRef->get(), $proxy))); } return $lazyLoad; diff --git a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services_private_frozen.php b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services_private_frozen.php index 3f3470a7b85db..5d627cb89f0db 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services_private_frozen.php +++ b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services_private_frozen.php @@ -15,9 +15,11 @@ class ProjectServiceContainer extends Container { protected $parameters = []; + protected readonly \WeakReference $ref; public function __construct() { + $this->ref = \WeakReference::create($this); $this->services = $this->privates = []; $this->methodMap = [ 'bar_service' => 'getBarServiceService', @@ -49,9 +51,9 @@ public function getRemovedIds(): array * * @return \stdClass */ - protected function getBarServiceService() + protected static function getBarServiceService($container) { - return $this->services['bar_service'] = new \stdClass(($this->privates['baz_service'] ??= new \stdClass())); + return $container->services['bar_service'] = new \stdClass(($container->privates['baz_service'] ??= new \stdClass())); } /** @@ -59,8 +61,8 @@ protected function getBarServiceService() * * @return \stdClass */ - protected function getFooServiceService() + protected static function getFooServiceService($container) { - return $this->services['foo_service'] = new \stdClass(($this->privates['baz_service'] ??= new \stdClass())); + return $container->services['foo_service'] = new \stdClass(($container->privates['baz_service'] ??= new \stdClass())); } } diff --git a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services_private_in_expression.php b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services_private_in_expression.php index 0bd20277b273d..6badcf4d0a48a 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services_private_in_expression.php +++ b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services_private_in_expression.php @@ -15,9 +15,11 @@ class ProjectServiceContainer extends Container { protected $parameters = []; + protected readonly \WeakReference $ref; public function __construct() { + $this->ref = \WeakReference::create($this); $this->services = $this->privates = []; $this->methodMap = [ 'public_foo' => 'getPublicFooService', @@ -49,8 +51,8 @@ public function getRemovedIds(): array * * @return \stdClass */ - protected function getPublicFooService() + protected static function getPublicFooService($container) { - return $this->services['public_foo'] = new \stdClass((new \stdClass())->bar); + return $container->services['public_foo'] = new \stdClass((new \stdClass())->bar); } } diff --git a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services_query_string_env.php b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services_query_string_env.php index 738722b7549b3..d3dc84d68c7c4 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services_query_string_env.php +++ b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services_query_string_env.php @@ -15,9 +15,11 @@ class Symfony_DI_PhpDumper_Test_QueryStringParameters extends Container { protected $parameters = []; + protected readonly \WeakReference $ref; public function __construct() { + $this->ref = \WeakReference::create($this); $this->parameters = $this->getDefaultParameters(); $this->services = $this->privates = []; @@ -77,8 +79,9 @@ public function getParameterBag(): ParameterBagInterface private function getDynamicParameter(string $name) { + $container = $this; $value = match ($name) { - 'hello' => $this->getEnv('query_string:foo'), + 'hello' => $container->getEnv('query_string:foo'), default => throw new ParameterNotFoundException($name), }; $this->loadedDynamicParameters[$name] = true; diff --git a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services_rot13_env.php b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services_rot13_env.php index 31b003e2eb08b..60700526ead94 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services_rot13_env.php +++ b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services_rot13_env.php @@ -15,11 +15,13 @@ class Symfony_DI_PhpDumper_Test_Rot13Parameters extends Container { protected $parameters = []; + protected readonly \WeakReference $ref; protected \Closure $getService; public function __construct() { - $this->getService = $this->getService(...); + $containerRef = $this->ref = \WeakReference::create($this); + $this->getService = static function () use ($containerRef) { return $containerRef->get()->getService(...\func_get_args()); }; $this->parameters = $this->getDefaultParameters(); $this->services = $this->privates = []; @@ -53,9 +55,9 @@ public function getRemovedIds(): array * * @return \Symfony\Component\DependencyInjection\Tests\Dumper\Rot13EnvVarProcessor */ - protected function getRot13EnvVarProcessorService() + protected static function getRot13EnvVarProcessorService($container) { - return $this->services['Symfony\\Component\\DependencyInjection\\Tests\\Dumper\\Rot13EnvVarProcessor'] = new \Symfony\Component\DependencyInjection\Tests\Dumper\Rot13EnvVarProcessor(); + return $container->services['Symfony\\Component\\DependencyInjection\\Tests\\Dumper\\Rot13EnvVarProcessor'] = new \Symfony\Component\DependencyInjection\Tests\Dumper\Rot13EnvVarProcessor(); } /** @@ -63,9 +65,9 @@ protected function getRot13EnvVarProcessorService() * * @return \Symfony\Component\DependencyInjection\ServiceLocator */ - protected function getContainer_EnvVarProcessorsLocatorService() + protected static function getContainer_EnvVarProcessorsLocatorService($container) { - return $this->services['container.env_var_processors_locator'] = new \Symfony\Component\DependencyInjection\Argument\ServiceLocator($this->getService, [ + return $container->services['container.env_var_processors_locator'] = new \Symfony\Component\DependencyInjection\Argument\ServiceLocator($container->getService, [ 'rot13' => ['services', 'Symfony\\Component\\DependencyInjection\\Tests\\Dumper\\Rot13EnvVarProcessor', 'getRot13EnvVarProcessorService', false], ], [ 'rot13' => '?', @@ -114,8 +116,9 @@ public function getParameterBag(): ParameterBagInterface private function getDynamicParameter(string $name) { + $container = $this; $value = match ($name) { - 'hello' => $this->getEnv('rot13:foo'), + 'hello' => $container->getEnv('rot13:foo'), default => throw new ParameterNotFoundException($name), }; $this->loadedDynamicParameters[$name] = true; diff --git a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services_service_locator_argument.php b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services_service_locator_argument.php index c8125acfb0cba..d28800abf0540 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services_service_locator_argument.php +++ b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services_service_locator_argument.php @@ -15,11 +15,13 @@ class Symfony_DI_PhpDumper_Service_Locator_Argument extends Container { protected $parameters = []; + protected readonly \WeakReference $ref; protected \Closure $getService; public function __construct() { - $this->getService = $this->getService(...); + $containerRef = $this->ref = \WeakReference::create($this); + $this->getService = static function () use ($containerRef) { return $containerRef->get()->getService(...\func_get_args()); }; $this->services = $this->privates = []; $this->syntheticIds = [ 'foo5' => true, @@ -57,11 +59,11 @@ public function getRemovedIds(): array * * @return \stdClass */ - protected function getBarService() + protected static function getBarService($container) { - $this->services['bar'] = $instance = new \stdClass(); + $container->services['bar'] = $instance = new \stdClass(); - $instance->locator = new \Symfony\Component\DependencyInjection\Argument\ServiceLocator($this->getService, [ + $instance->locator = new \Symfony\Component\DependencyInjection\Argument\ServiceLocator($container->getService, [ 'foo1' => ['services', 'foo1', 'getFoo1Service', false], 'foo2' => ['privates', 'foo2', 'getFoo2Service', false], 'foo3' => [false, 'foo3', 'getFoo3Service', false], @@ -83,9 +85,9 @@ protected function getBarService() * * @return \stdClass */ - protected function getFoo1Service() + protected static function getFoo1Service($container) { - return $this->services['foo1'] = new \stdClass(); + return $container->services['foo1'] = new \stdClass(); } /** @@ -93,9 +95,9 @@ protected function getFoo1Service() * * @return \stdClass */ - protected function getFoo2Service() + protected static function getFoo2Service($container) { - return $this->privates['foo2'] = new \stdClass(); + return $container->privates['foo2'] = new \stdClass(); } /** @@ -103,13 +105,13 @@ protected function getFoo2Service() * * @return \stdClass */ - protected function getFoo3Service() + protected static function getFoo3Service($container) { - $this->factories['service_container']['foo3'] = function () { + $container->factories['service_container']['foo3'] = static function ($container) { return new \stdClass(); }; - return $this->factories['service_container']['foo3'](); + return $container->factories['service_container']['foo3']($container); } /** @@ -117,7 +119,7 @@ protected function getFoo3Service() * * @return \stdClass */ - protected function getFoo4Service() + protected static function getFoo4Service($container) { throw new RuntimeException('BOOM'); } diff --git a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services_subscriber.php b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services_subscriber.php index 1928f94a41110..77ed9e02882a4 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services_subscriber.php +++ b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services_subscriber.php @@ -15,11 +15,13 @@ class ProjectServiceContainer extends Container { protected $parameters = []; + protected readonly \WeakReference $ref; protected \Closure $getService; public function __construct() { - $this->getService = $this->getService(...); + $containerRef = $this->ref = \WeakReference::create($this); + $this->getService = static function () use ($containerRef) { return $containerRef->get()->getService(...\func_get_args()); }; $this->services = $this->privates = []; $this->methodMap = [ 'Symfony\\Component\\DependencyInjection\\Tests\\Fixtures\\TestServiceSubscriber' => 'getTestServiceSubscriberService', @@ -57,9 +59,9 @@ public function getRemovedIds(): array * * @return \Symfony\Component\DependencyInjection\Tests\Fixtures\TestServiceSubscriber */ - protected function getTestServiceSubscriberService() + protected static function getTestServiceSubscriberService($container) { - return $this->services['Symfony\\Component\\DependencyInjection\\Tests\\Fixtures\\TestServiceSubscriber'] = new \Symfony\Component\DependencyInjection\Tests\Fixtures\TestServiceSubscriber(); + return $container->services['Symfony\\Component\\DependencyInjection\\Tests\\Fixtures\\TestServiceSubscriber'] = new \Symfony\Component\DependencyInjection\Tests\Fixtures\TestServiceSubscriber(); } /** @@ -67,9 +69,9 @@ protected function getTestServiceSubscriberService() * * @return \Symfony\Component\DependencyInjection\Tests\Fixtures\TestServiceSubscriber */ - protected function getFooServiceService() + protected static function getFooServiceService($container) { - return $this->services['foo_service'] = new \Symfony\Component\DependencyInjection\Tests\Fixtures\TestServiceSubscriber((new \Symfony\Component\DependencyInjection\Argument\ServiceLocator($this->getService, [ + return $container->services['foo_service'] = new \Symfony\Component\DependencyInjection\Tests\Fixtures\TestServiceSubscriber((new \Symfony\Component\DependencyInjection\Argument\ServiceLocator($container->getService, [ 'Symfony\\Component\\DependencyInjection\\Tests\\Fixtures\\CustomDefinition' => ['privates', 'Symfony\\Component\\DependencyInjection\\Tests\\Fixtures\\CustomDefinition', 'getCustomDefinitionService', false], 'Symfony\\Component\\DependencyInjection\\Tests\\Fixtures\\TestServiceSubscriber' => ['services', 'Symfony\\Component\\DependencyInjection\\Tests\\Fixtures\\TestServiceSubscriber', 'getTestServiceSubscriberService', false], 'bar' => ['services', 'Symfony\\Component\\DependencyInjection\\Tests\\Fixtures\\TestServiceSubscriber', 'getTestServiceSubscriberService', false], @@ -81,7 +83,7 @@ protected function getFooServiceService() 'bar' => 'Symfony\\Component\\DependencyInjection\\Tests\\Fixtures\\CustomDefinition', 'baz' => 'Symfony\\Component\\DependencyInjection\\Tests\\Fixtures\\CustomDefinition', 'late_alias' => 'Symfony\\Component\\DependencyInjection\\Tests\\Fixtures\\TestDefinition1', - ]))->withContext('foo_service', $this)); + ]))->withContext('foo_service', $container)); } /** @@ -89,9 +91,9 @@ protected function getFooServiceService() * * @return \Symfony\Component\DependencyInjection\Tests\Fixtures\TestDefinition1 */ - protected function getLateAliasService() + protected static function getLateAliasService($container) { - return $this->services['late_alias'] = new \Symfony\Component\DependencyInjection\Tests\Fixtures\TestDefinition1(); + return $container->services['late_alias'] = new \Symfony\Component\DependencyInjection\Tests\Fixtures\TestDefinition1(); } /** @@ -99,8 +101,8 @@ protected function getLateAliasService() * * @return \Symfony\Component\DependencyInjection\Tests\Fixtures\CustomDefinition */ - protected function getCustomDefinitionService() + protected static function getCustomDefinitionService($container) { - return $this->privates['Symfony\\Component\\DependencyInjection\\Tests\\Fixtures\\CustomDefinition'] = new \Symfony\Component\DependencyInjection\Tests\Fixtures\CustomDefinition(); + return $container->privates['Symfony\\Component\\DependencyInjection\\Tests\\Fixtures\\CustomDefinition'] = new \Symfony\Component\DependencyInjection\Tests\Fixtures\CustomDefinition(); } } diff --git a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services_tsantos.php b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services_tsantos.php index 2cf21ad94dbd2..00dab8c875eee 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services_tsantos.php +++ b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services_tsantos.php @@ -15,9 +15,11 @@ class ProjectServiceContainer extends Container { protected $parameters = []; + protected readonly \WeakReference $ref; public function __construct() { + $this->ref = \WeakReference::create($this); $this->services = $this->privates = []; $this->methodMap = [ 'tsantos_serializer' => 'getTsantosSerializerService', @@ -48,7 +50,7 @@ public function getRemovedIds(): array * * @return \TSantos\Serializer\EventEmitterSerializer */ - protected function getTsantosSerializerService() + protected static function getTsantosSerializerService($container) { $a = new \TSantos\Serializer\NormalizerRegistry(); @@ -57,7 +59,7 @@ protected function getTsantosSerializerService() $c = new \TSantos\Serializer\EventDispatcher\EventDispatcher(); $c->addSubscriber(new \TSantos\SerializerBundle\EventListener\StopwatchListener(new \Symfony\Component\Stopwatch\Stopwatch(true))); - $this->services['tsantos_serializer'] = $instance = new \TSantos\Serializer\EventEmitterSerializer(new \TSantos\Serializer\Encoder\JsonEncoder(), $a, $c); + $container->services['tsantos_serializer'] = $instance = new \TSantos\Serializer\EventEmitterSerializer(new \TSantos\Serializer\Encoder\JsonEncoder(), $a, $c); $b->setSerializer($instance); $d = new \TSantos\Serializer\Normalizer\JsonNormalizer(); diff --git a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services_uninitialized_ref.php b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services_uninitialized_ref.php index d528f0d9f092d..35b5a3260f008 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services_uninitialized_ref.php +++ b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services_uninitialized_ref.php @@ -15,9 +15,11 @@ class Symfony_DI_PhpDumper_Test_Uninitialized_Reference extends Container { protected $parameters = []; + protected readonly \WeakReference $ref; public function __construct() { + $this->ref = \WeakReference::create($this); $this->services = $this->privates = []; $this->methodMap = [ 'bar' => 'getBarService', @@ -51,32 +53,44 @@ public function getRemovedIds(): array * * @return \stdClass */ - protected function getBarService() + protected static function getBarService($container) { - $this->services['bar'] = $instance = new \stdClass(); + $containerRef = $container->ref; - $instance->foo1 = ($this->services['foo1'] ?? null); + $container->services['bar'] = $instance = new \stdClass(); + + $instance->foo1 = ($container->services['foo1'] ?? null); $instance->foo2 = null; - $instance->foo3 = ($this->privates['foo3'] ?? null); - $instance->closures = [0 => #[\Closure(name: 'foo1', class: 'stdClass')] function () { - return ($this->services['foo1'] ?? null); - }, 1 => #[\Closure(name: 'foo2')] function () { + $instance->foo3 = ($container->privates['foo3'] ?? null); + $instance->closures = [0 => #[\Closure(name: 'foo1', class: 'stdClass')] static function () use ($containerRef) { + $container = $containerRef->get(); + + return ($container->services['foo1'] ?? null); + }, 1 => #[\Closure(name: 'foo2')] static function () use ($containerRef) { + $container = $containerRef->get(); + return null; - }, 2 => #[\Closure(name: 'foo3', class: 'stdClass')] function () { - return ($this->privates['foo3'] ?? null); + }, 2 => #[\Closure(name: 'foo3', class: 'stdClass')] static function () use ($containerRef) { + $container = $containerRef->get(); + + return ($container->privates['foo3'] ?? null); }]; - $instance->iter = new RewindableGenerator(function () { - if (isset($this->services['foo1'])) { - yield 'foo1' => ($this->services['foo1'] ?? null); + $instance->iter = new RewindableGenerator(static function () use ($containerRef) { + $container = $containerRef->get(); + + if (isset($container->services['foo1'])) { + yield 'foo1' => ($container->services['foo1'] ?? null); } if (false) { yield 'foo2' => null; } - if (isset($this->privates['foo3'])) { - yield 'foo3' => ($this->privates['foo3'] ?? null); + if (isset($container->privates['foo3'])) { + yield 'foo3' => ($container->privates['foo3'] ?? null); } - }, function () { - return 0 + (int) (isset($this->services['foo1'])) + (int) (false) + (int) (isset($this->privates['foo3'])); + }, static function () use ($containerRef) { + $container = $containerRef->get(); + + return 0 + (int) (isset($container->services['foo1'])) + (int) (false) + (int) (isset($container->privates['foo3'])); }); return $instance; @@ -87,11 +101,11 @@ protected function getBarService() * * @return \stdClass */ - protected function getBazService() + protected static function getBazService($container) { - $this->services['baz'] = $instance = new \stdClass(); + $container->services['baz'] = $instance = new \stdClass(); - $instance->foo3 = ($this->privates['foo3'] ??= new \stdClass()); + $instance->foo3 = ($container->privates['foo3'] ??= new \stdClass()); return $instance; } @@ -101,8 +115,8 @@ protected function getBazService() * * @return \stdClass */ - protected function getFoo1Service() + protected static function getFoo1Service($container) { - return $this->services['foo1'] = new \stdClass(); + return $container->services['foo1'] = new \stdClass(); } } diff --git a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services_unsupported_characters.php b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services_unsupported_characters.php index 3a0f8d7539e94..3473edb993327 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services_unsupported_characters.php +++ b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services_unsupported_characters.php @@ -15,9 +15,11 @@ class Symfony_DI_PhpDumper_Test_Unsupported_Characters extends Container { protected $parameters = []; + protected readonly \WeakReference $ref; public function __construct() { + $this->ref = \WeakReference::create($this); $this->parameters = $this->getDefaultParameters(); $this->services = $this->privates = []; @@ -45,9 +47,9 @@ public function isCompiled(): bool * * @return \FooClass */ - protected function getBarService() + protected static function getBarService($container) { - return $this->services['bar$'] = new \FooClass(); + return $container->services['bar$'] = new \FooClass(); } /** @@ -55,9 +57,9 @@ protected function getBarService() * * @return \FooClass */ - protected function getBar2Service() + protected static function getBar2Service($container) { - return $this->services['bar$!'] = new \FooClass(); + return $container->services['bar$!'] = new \FooClass(); } /** @@ -65,9 +67,9 @@ protected function getBar2Service() * * @return \FooClass */ - protected function getFooohnoService() + protected static function getFooohnoService($container) { - return $this->services['foo*/oh-no'] = new \FooClass(); + return $container->services['foo*/oh-no'] = new \FooClass(); } public function getParameter(string $name): array|bool|string|int|float|\UnitEnum|null diff --git a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services_url_env.php b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services_url_env.php index 6a899ee42b9e9..154080cdbf257 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services_url_env.php +++ b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services_url_env.php @@ -15,9 +15,11 @@ class Symfony_DI_PhpDumper_Test_UrlParameters extends Container { protected $parameters = []; + protected readonly \WeakReference $ref; public function __construct() { + $this->ref = \WeakReference::create($this); $this->parameters = $this->getDefaultParameters(); $this->services = $this->privates = []; @@ -77,8 +79,9 @@ public function getParameterBag(): ParameterBagInterface private function getDynamicParameter(string $name) { + $container = $this; $value = match ($name) { - 'hello' => $this->getEnv('url:foo'), + 'hello' => $container->getEnv('url:foo'), default => throw new ParameterNotFoundException($name), }; $this->loadedDynamicParameters[$name] = true; diff --git a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services_wither.php b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services_wither.php index 3be9833ea75ea..5493b8bc719a4 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services_wither.php +++ b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services_wither.php @@ -15,9 +15,11 @@ class Symfony_DI_PhpDumper_Service_Wither extends Container { protected $parameters = []; + protected readonly \WeakReference $ref; public function __construct() { + $this->ref = \WeakReference::create($this); $this->services = $this->privates = []; $this->methodMap = [ 'wither' => 'getWitherService', @@ -48,14 +50,14 @@ public function getRemovedIds(): array * * @return \Symfony\Component\DependencyInjection\Tests\Compiler\Wither */ - protected function getWitherService() + protected static function getWitherService($container) { $instance = new \Symfony\Component\DependencyInjection\Tests\Compiler\Wither(); $a = new \Symfony\Component\DependencyInjection\Tests\Compiler\Foo(); $instance = $instance->withFoo1($a); - $this->services['wither'] = $instance = $instance->withFoo2($a); + $container->services['wither'] = $instance = $instance->withFoo2($a); $instance->setFoo($a); return $instance; diff --git a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services_wither_lazy.php b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services_wither_lazy.php index b0bbc7b640be8..e1f9a5caf605e 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services_wither_lazy.php +++ b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services_wither_lazy.php @@ -15,9 +15,11 @@ class Symfony_DI_PhpDumper_Service_Wither_Lazy extends Container { protected $parameters = []; + protected readonly \WeakReference $ref; public function __construct() { + $this->ref = \WeakReference::create($this); $this->services = $this->privates = []; $this->methodMap = [ 'wither' => 'getWitherService', @@ -53,10 +55,12 @@ protected function createProxy($class, \Closure $factory) * * @return \Symfony\Component\DependencyInjection\Tests\Compiler\Wither */ - protected function getWitherService($lazyLoad = true) + protected static function getWitherService($container, $lazyLoad = true) { + $containerRef = $container->ref; + if (true === $lazyLoad) { - return $this->services['wither'] = $this->createProxy('WitherProxy94fa281', fn () => \WitherProxy94fa281::createLazyProxy(fn () => $this->getWitherService(false))); + return $container->services['wither'] = $container->createProxy('WitherProxy94fa281', static fn () => \WitherProxy94fa281::createLazyProxy(static fn () => self::getWitherService($containerRef->get(), false))); } $instance = new \Symfony\Component\DependencyInjection\Tests\Compiler\Wither(); diff --git a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services_wither_staticreturntype.php b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services_wither_staticreturntype.php index ea71c2796a54c..61de4e02dcadf 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services_wither_staticreturntype.php +++ b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services_wither_staticreturntype.php @@ -15,9 +15,11 @@ class Symfony_DI_PhpDumper_Service_WitherStaticReturnType extends Container { protected $parameters = []; + protected readonly \WeakReference $ref; public function __construct() { + $this->ref = \WeakReference::create($this); $this->services = $this->privates = []; $this->methodMap = [ 'wither' => 'getWitherService', @@ -48,13 +50,13 @@ public function getRemovedIds(): array * * @return \Symfony\Component\DependencyInjection\Tests\Fixtures\WitherStaticReturnType */ - protected function getWitherService() + protected static function getWitherService($container) { $instance = new \Symfony\Component\DependencyInjection\Tests\Fixtures\WitherStaticReturnType(); $a = new \Symfony\Component\DependencyInjection\Tests\Compiler\Foo(); - $this->services['wither'] = $instance = $instance->withFoo($a); + $container->services['wither'] = $instance = $instance->withFoo($a); $instance->setFoo($a); return $instance; diff --git a/src/Symfony/Component/DependencyInjection/composer.json b/src/Symfony/Component/DependencyInjection/composer.json index baaef5da89b33..307a78fa1c33e 100644 --- a/src/Symfony/Component/DependencyInjection/composer.json +++ b/src/Symfony/Component/DependencyInjection/composer.json @@ -37,7 +37,7 @@ "ext-psr": "<1.1|>=2", "symfony/config": "<6.1", "symfony/finder": "<5.4", - "symfony/proxy-manager-bridge": "<6.2", + "symfony/proxy-manager-bridge": "<6.3", "symfony/yaml": "<5.4" }, "provide": {