Skip to content

[DependencyInjection] Remove static from closures #49474

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions src/Symfony/Component/DependencyInjection/Dumper/PhpDumper.php
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ public function dump(array $options = []): string|array
if ($this->addGetService) {
$code = preg_replace(
"/(\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()); };",
"\n protected \Closure \$getService;$1 \$containerRef = $2\n \$this->getService = function () use (\$containerRef) { return \$containerRef->get()->getService(...\\func_get_args()); };",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this one does not look like a closure being in a static method, as it has $this->getService =

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@nicolas-grekas Do I need to create a PR to fix this?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If it's needed, here it is: #49476

$code,
1
);
Expand Down Expand Up @@ -941,7 +941,7 @@ protected static function {$methodName}(\$container$lazyInitialization)
$c = implode("\n", array_map(fn ($line) => $line ? ' '.$line : $line, explode("\n", $c)));
$lazyloadInitialization = $definition->isLazy() ? ', $lazyLoad = true' : '';

$c = sprintf(" %s = static function (\$container%s) {\n%s };\n\n return %1\$s(\$container);\n", $factory, $lazyloadInitialization, $c);
$c = sprintf(" %s = function (\$container%s) {\n%s };\n\n return %1\$s(\$container);\n", $factory, $lazyloadInitialization, $c);
}

$code .= $c;
Expand Down Expand Up @@ -1545,7 +1545,7 @@ private function addInlineRequires(bool $hasProxyClasses): string
$code .= "\n include_once __DIR__.'/proxy-classes.php';";
}

return $code ? sprintf("\n \$this->privates['service_container'] = static function (\$container) {%s\n };\n", $code) : '';
return $code ? sprintf("\n \$this->privates['service_container'] = function (\$container) {%s\n };\n", $code) : '';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same here

}

private function addDefaultParametersMethod(): string
Expand Down Expand Up @@ -1831,23 +1831,23 @@ private function dumpValue(mixed $value, bool $interpolate = true): string
}
$this->addContainerRef = true;

return sprintf("%sstatic function () use (\$containerRef)%s {\n \$container = \$containerRef->get();\n\n %s\n }", $attribute, $returnedType, $code);
return sprintf("%sfunction () use (\$containerRef)%s {\n \$container = \$containerRef->get();\n\n %s\n }", $attribute, $returnedType, $code);
}

if ($value instanceof IteratorArgument) {
$operands = [0];
$code = [];

if (!$values = $value->getValues()) {
$code[] = 'new RewindableGenerator(static function () {';
$code[] = 'new RewindableGenerator(function () {';
$code[] = ' return new \EmptyIterator();';
} else {
$this->addContainerRef = true;
$code[] = 'new RewindableGenerator(static function () use ($containerRef) {';
$code[] = 'new RewindableGenerator(function () use ($containerRef) {';
$code[] = ' $container = $containerRef->get();';
$code[] = '';
$countCode = [];
$countCode[] = 'static function () use ($containerRef) {';
$countCode[] = 'function () use ($containerRef) {';

foreach ($values as $k => $v) {
($c = $this->getServiceConditionals($v)) ? $operands[] = "(int) ($c)" : ++$operands[0];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ protected static function getClosureOfServiceClosureService($container)
{
$containerRef = $container->ref;

return $container->services['closure_of_service_closure'] = #[\Closure(name: 'bar2', class: 'stdClass')] static function () use ($containerRef) {
return $container->services['closure_of_service_closure'] = #[\Closure(name: 'bar2', class: 'stdClass')] function () use ($containerRef) {
$container = $containerRef->get();

return ($container->privates['bar2'] ??= new \stdClass());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class getClosureService extends ProjectServiceContainer

$container->services['closure'] = $instance = new \stdClass();

$instance->closures = [#[\Closure(name: 'foo', class: 'FooClass')] static function () use ($containerRef): ?\stdClass {
$instance->closures = [#[\Closure(name: 'foo', class: 'FooClass')] function () use ($containerRef): ?\stdClass {
$container = $containerRef->get();

return ($container->services['foo'] ?? null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,7 @@ class getFooBarService extends ProjectServiceContainer
*/
public static function do($container, $lazyLoad = true)
{
$container->factories['foo_bar'] = static function ($container) {
$container->factories['foo_bar'] = function ($container) {
return new \Bar\FooClass(($container->services['deprecated_service'] ?? $container->load('getDeprecatedServiceService')));
};

Expand Down Expand Up @@ -363,12 +363,12 @@ class getLazyContextService extends ProjectServiceContainer
{
$containerRef = $container->ref;

return $container->services['lazy_context'] = new \LazyContext(new RewindableGenerator(static function () use ($containerRef) {
return $container->services['lazy_context'] = new \LazyContext(new RewindableGenerator(function () use ($containerRef) {
$container = $containerRef->get();

yield 'k1' => ($container->services['foo.baz'] ?? $container->load('getFoo_BazService'));
yield 'k2' => $container;
}, 2), new RewindableGenerator(static function () {
}, 2), new RewindableGenerator(function () {
return new \EmptyIterator();
}, 0));
}
Expand All @@ -387,11 +387,11 @@ class getLazyContextIgnoreInvalidRefService extends ProjectServiceContainer
{
$containerRef = $container->ref;

return $container->services['lazy_context_ignore_invalid_ref'] = new \LazyContext(new RewindableGenerator(static function () use ($containerRef) {
return $container->services['lazy_context_ignore_invalid_ref'] = new \LazyContext(new RewindableGenerator(function () use ($containerRef) {
$container = $containerRef->get();

yield 0 => ($container->services['foo.baz'] ?? $container->load('getFoo_BazService'));
}, 1), new RewindableGenerator(static function () {
}, 1), new RewindableGenerator(function () {
return new \EmptyIterator();
}, 0));
}
Expand Down Expand Up @@ -455,7 +455,7 @@ class getNonSharedFooService extends ProjectServiceContainer
{
include_once $container->targetDir.''.'/Fixtures/includes/foo.php';

$container->factories['non_shared_foo'] = static function ($container) {
$container->factories['non_shared_foo'] = function ($container) {
return new \Bar\FooClass();
};

Expand Down Expand Up @@ -521,7 +521,7 @@ class getTaggedIteratorService extends ProjectServiceContainer
{
$containerRef = $container->ref;

return $container->services['tagged_iterator'] = new \Bar(new RewindableGenerator(static function () use ($containerRef) {
return $container->services['tagged_iterator'] = new \Bar(new RewindableGenerator(function () use ($containerRef) {
$container = $containerRef->get();

yield 0 => ($container->services['foo'] ?? $container->load('getFooService'));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@ protected static function getFoo_BazService($container)
*/
protected static function getFooBarService($container)
{
$container->factories['foo_bar'] = static function ($container) {
$container->factories['foo_bar'] = function ($container) {
return new \Bar\FooClass(($container->services['deprecated_service'] ?? self::getDeprecatedServiceService($container)));
};

Expand Down Expand Up @@ -332,12 +332,12 @@ protected static function getLazyContextService($container)
{
$containerRef = $container->ref;

return $container->services['lazy_context'] = new \LazyContext(new RewindableGenerator(static function () use ($containerRef) {
return $container->services['lazy_context'] = new \LazyContext(new RewindableGenerator(function () use ($containerRef) {
$container = $containerRef->get();

yield 'k1' => ($container->services['foo.baz'] ?? self::getFoo_BazService($container));
yield 'k2' => $container;
}, 2), new RewindableGenerator(static function () {
}, 2), new RewindableGenerator(function () {
return new \EmptyIterator();
}, 0));
}
Expand All @@ -351,11 +351,11 @@ protected static function getLazyContextIgnoreInvalidRefService($container)
{
$containerRef = $container->ref;

return $container->services['lazy_context_ignore_invalid_ref'] = new \LazyContext(new RewindableGenerator(static function () use ($containerRef) {
return $container->services['lazy_context_ignore_invalid_ref'] = new \LazyContext(new RewindableGenerator(function () use ($containerRef) {
$container = $containerRef->get();

yield 0 => ($container->services['foo.baz'] ?? self::getFoo_BazService($container));
}, 1), new RewindableGenerator(static function () {
}, 1), new RewindableGenerator(function () {
return new \EmptyIterator();
}, 0));
}
Expand Down Expand Up @@ -434,7 +434,7 @@ protected static function getTaggedIteratorService($container)
{
$containerRef = $container->ref;

return $container->services['tagged_iterator'] = new \Bar(new RewindableGenerator(static function () use ($containerRef) {
return $container->services['tagged_iterator'] = new \Bar(new RewindableGenerator(function () use ($containerRef) {
$container = $containerRef->get();

yield 0 => ($container->services['foo'] ?? self::getFooService($container));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ class ProjectServiceContainer extends Container
'decorated' => 'decorator_service_with_name',
];

$this->privates['service_container'] = static function ($container) {
$this->privates['service_container'] = function ($container) {
include_once $container->targetDir.''.'/Fixtures/includes/foo.php';
};
}
Expand Down Expand Up @@ -319,7 +319,7 @@ class ProjectServiceContainer extends Container
*/
protected static function getFooBarService($container)
{
$container->factories['foo_bar'] = static function ($container) {
$container->factories['foo_bar'] = function ($container) {
return new \Bar\FooClass(($container->services['deprecated_service'] ?? self::getDeprecatedServiceService($container)));
};

Expand Down Expand Up @@ -355,12 +355,12 @@ class ProjectServiceContainer extends Container

include_once $container->targetDir.''.'/Fixtures/includes/classes.php';

return $container->services['lazy_context'] = new \LazyContext(new RewindableGenerator(static function () use ($containerRef) {
return $container->services['lazy_context'] = new \LazyContext(new RewindableGenerator(function () use ($containerRef) {
$container = $containerRef->get();

yield 'k1' => ($container->services['foo.baz'] ?? self::getFoo_BazService($container));
yield 'k2' => $container;
}, 2), new RewindableGenerator(static function () {
}, 2), new RewindableGenerator(function () {
return new \EmptyIterator();
}, 0));
}
Expand All @@ -376,11 +376,11 @@ class ProjectServiceContainer extends Container

include_once $container->targetDir.''.'/Fixtures/includes/classes.php';

return $container->services['lazy_context_ignore_invalid_ref'] = new \LazyContext(new RewindableGenerator(static function () use ($containerRef) {
return $container->services['lazy_context_ignore_invalid_ref'] = new \LazyContext(new RewindableGenerator(function () use ($containerRef) {
$container = $containerRef->get();

yield 0 => ($container->services['foo.baz'] ?? self::getFoo_BazService($container));
}, 1), new RewindableGenerator(static function () {
}, 1), new RewindableGenerator(function () {
return new \EmptyIterator();
}, 0));
}
Expand Down Expand Up @@ -429,7 +429,7 @@ class ProjectServiceContainer extends Container
{
include_once $container->targetDir.''.'/Fixtures/includes/foo.php';

$container->factories['non_shared_foo'] = static function ($container) {
$container->factories['non_shared_foo'] = function ($container) {
return new \Bar\FooClass();
};

Expand Down Expand Up @@ -475,7 +475,7 @@ class ProjectServiceContainer extends Container
{
$containerRef = $container->ref;

return $container->services['tagged_iterator'] = new \Bar(new RewindableGenerator(static function () use ($containerRef) {
return $container->services['tagged_iterator'] = new \Bar(new RewindableGenerator(function () use ($containerRef) {
$container = $containerRef->get();

yield 0 => ($container->services['foo'] ?? self::getFooService($container));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ class ProjectServiceContainer extends Container

$this->aliases = [];

$this->privates['service_container'] = static function ($container) {
$this->privates['service_container'] = function ($container) {
include_once __DIR__.'/proxy-classes.php';
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ protected static function getDoctrine_EntityManagerService($container)
$containerRef = $container->ref;

$a = new \stdClass();
$a->resolver = new \stdClass(new RewindableGenerator(static function () use ($containerRef) {
$a->resolver = new \stdClass(new RewindableGenerator(function () use ($containerRef) {
$container = $containerRef->get();

yield 0 => ($container->privates['doctrine.listener'] ?? self::getDoctrine_ListenerService($container));
Expand Down Expand Up @@ -528,7 +528,7 @@ protected static function getMailer_TransportService($container)
{
$containerRef = $container->ref;

return $container->privates['mailer.transport'] = (new \FactoryCircular(new RewindableGenerator(static function () use ($containerRef) {
return $container->privates['mailer.transport'] = (new \FactoryCircular(new RewindableGenerator(function () use ($containerRef) {
$container = $containerRef->get();

yield 0 => ($container->privates['mailer.transport_factory.amazon'] ?? self::getMailer_TransportFactory_AmazonService($container));
Expand Down Expand Up @@ -559,7 +559,7 @@ protected static function getMailer_TransportFactory_AmazonService($container)
*/
protected static function getMailerInline_MailerService($container)
{
return $container->privates['mailer_inline.mailer'] = new \stdClass((new \FactoryCircular(new RewindableGenerator(static function () {
return $container->privates['mailer_inline.mailer'] = new \stdClass((new \FactoryCircular(new RewindableGenerator(function () {
return new \EmptyIterator();
}, 0)))->create());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ protected static function getDoctrine_EntityListenerResolverService($container)
{
$containerRef = $container->ref;

return $container->services['doctrine.entity_listener_resolver'] = new \stdClass(new RewindableGenerator(static function () use ($containerRef) {
return $container->services['doctrine.entity_listener_resolver'] = new \stdClass(new RewindableGenerator(function () use ($containerRef) {
$container = $containerRef->get();

yield 0 => ($container->services['doctrine.listener'] ?? self::getDoctrine_ListenerService($container));
Expand Down Expand Up @@ -356,7 +356,7 @@ protected static function getFoo2Service($container)
*/
protected static function getFoo4Service($container)
{
$container->factories['foo4'] = static function ($container) {
$container->factories['foo4'] = function ($container) {
$instance = new \stdClass();

$instance->foobar = ($container->services['foobar4'] ?? self::getFoobar4Service($container));
Expand Down Expand Up @@ -528,7 +528,7 @@ protected static function getMailer_TransportFactoryService($container)
{
$containerRef = $container->ref;

return $container->services['mailer.transport_factory'] = new \FactoryCircular(new RewindableGenerator(static function () use ($containerRef) {
return $container->services['mailer.transport_factory'] = new \FactoryCircular(new RewindableGenerator(function () use ($containerRef) {
$container = $containerRef->get();

yield 0 => ($container->services['mailer.transport_factory.amazon'] ?? self::getMailer_TransportFactory_AmazonService($container));
Expand Down Expand Up @@ -559,7 +559,7 @@ protected static function getMailer_TransportFactory_AmazonService($container)
*/
protected static function getMailerInline_TransportFactoryService($container)
{
return $container->services['mailer_inline.transport_factory'] = new \FactoryCircular(new RewindableGenerator(static function () {
return $container->services['mailer_inline.transport_factory'] = new \FactoryCircular(new RewindableGenerator(function () {
return new \EmptyIterator();
}, 0));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ protected static function getServiceClosureService($container)
{
$containerRef = $container->ref;

return $container->services['service_closure'] = new \Bar(#[\Closure(name: 'foo', class: 'Foo')] static function () use ($containerRef) {
return $container->services['service_closure'] = new \Bar(#[\Closure(name: 'foo', class: 'Foo')] function () use ($containerRef) {
$container = $containerRef->get();

return ($container->services['foo'] ??= new \Foo());
Expand All @@ -75,7 +75,7 @@ protected static function getServiceClosureInvalidService($container)
{
$containerRef = $container->ref;

return $container->services['service_closure_invalid'] = new \Bar(static function () use ($containerRef) {
return $container->services['service_closure_invalid'] = new \Bar(function () use ($containerRef) {
$container = $containerRef->get();

return NULL;
Expand Down
Loading
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy