8000 feature #36195 [DI] add tags `container.preload`/`.no_preload` to dec… · symfony/symfony@3b38f38 · GitHub
[go: up one dir, main page]

Skip to content

Commit 3b38f38

Browse files
committed
feature #36195 [DI] add tags container.preload/.no_preload to declare extra classes to preload/services to not preload (nicolas-grekas)
This PR was merged into the 5.1-dev branch. Discussion ---------- [DI] add tags `container.preload`/`.no_preload` to declare extra classes to preload/services to not preload | Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | yes | Deprecations? | no | Tickets | - | License | MIT | Doc PR | - To allow fine-grained declaration of sidekick classes in DI extensions. Follows #36103 Commits ------- fb04711 [DI] add tags `container.preload`/`.no_preload` to declare extra classes to preload/services to not preload
2 parents 8a2a69f + fb04711 commit 3b38f38

File tree

27 files changed

+136
-24
lines changed

27 files changed

+136
-24
lines changed

src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Compiler/UnusedTagsPass.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ class UnusedTagsPass implements CompilerPassInterface
3232
'container.env_var_loader',
3333
'container.env_var_processor',
3434
'container.hot_path',
35+
'container.no_preload',
36+
'container.preload',
3537
'container.reversible',
3638
'container.service_locator',
3739
'container.service_locator_context',

src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -434,7 +434,8 @@ public function load(array $configs, ContainerBuilder $container)
434434
$container->registerForAutoconfiguration(CacheClearerInterface::class)
435435
->addTag('kernel.cache_clearer');
436436
$container->registerForAutoconfiguration(CacheWarmerInterface::class)
437-
->addTag('kernel.cache_warmer');
437+
->addTag('kernel.cache_warmer')
438+
->addTag('container.no_preload');
438439
$container->registerForAutoconfiguration(EventSubscriberInterface::class)
439440
->addTag('kernel.event_subscriber');
440441
$container->registerForAutoconfiguration(LocaleAwareInterface::class)

src/Symfony/Bundle/FrameworkBundle/Resources/config/annotations.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
</service>
3535

3636
<service id="annotations.cache_warmer" class="Symfony\Bundle\FrameworkBundle\CacheWarmer\AnnotationsCacheWarmer">
37+
<tag name="container.no_preload" />
3738
<argument type="service" id="annotations.reader" />
3839
<argument>%kernel.cache_dir%/annotations.php</argument>
3940
<argument>#^Symfony\\(?:Component\\HttpKernel\\|Bundle\\FrameworkBundle\\Controller\\(?!.*Controller$))#</argument>

src/Symfony/Bundle/FrameworkBundle/Resources/config/cache_debug.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
<argument>cache.serializer</argument>
2121
</argument>
2222
<tag name="kernel.cache_warmer" priority="64" />
23+
<tag name="container.no_preload" />
2324
</service>
2425
</services>
2526
</container>

src/Symfony/Bundle/FrameworkBundle/Resources/config/console.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,12 @@
1111
<argument type="service" id="logger" on-invalid="null" />
1212
<tag name="kernel.event_subscriber" />
1313
<tag name="monolog.logger" channel="console" />
14+
<tag name="container.no_preload" />
1415
</service>
1516

1617
<service id="console.suggest_missing_package_subscriber" class="Symfony\Bundle\FrameworkBundle\EventListener\SuggestMissingPackageSubscriber">
1718
<tag name="kernel.event_subscriber" />
19+
<tag name="container.no_preload" />
1820
</service>
1921

2022
<service id="console.command.about" class="Symfony\Bundle\FrameworkBundle\Command\AboutCommand">

src/Symfony/Bundle/FrameworkBundle/Resources/config/routing.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@
101101
<service id="router.cache_warmer" class="Symfony\Bundle\FrameworkBundle\CacheWarmer\RouterCacheWarmer">
102102
<tag name="container.service_subscriber" id="router" />
103103
<tag name="kernel.cache_warmer" />
104+
<tag name="container.no_preload" />
104105
<argument type="service" id="Psr\Container\ContainerInterface" />
105106
</service>
106107

src/Symfony/Bundle/FrameworkBundle/Resources/config/serializer.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@
107107
<argument type="collection" /><!-- Loaders injected by the extension -->
108108
<argument>%serializer.mapping.cache.file%</argument>
109109
<tag name="kernel.cache_warmer" />
110+
<tag name="container.no_preload" />
110111
</service>
111112

112113
<service id="serializer.mapping.cache.symfony" class="Psr\Cache\CacheItemPoolInterface">

src/Symfony/Bundle/FrameworkBundle/Resources/config/services.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@
6666
<service id="Symfony\Component\HttpFoundation\UrlHelper" alias="url_helper" />
6767

6868
<service id="cache_warmer" class="Symfony\Component\HttpKernel\CacheWarmer\CacheWarmerAggregate" public="true">
69+
<tag name="container.no_preload" />
6970
<argument type="tagged_iterator" tag="kernel.cache_warmer" />
7071
<argument>%kernel.debug%</argument>
7172
<argument>%kernel.cache_dir%/%kernel.container_class%Deprecations.log</argument>

src/Symfony/Bundle/FrameworkBundle/Resources/config/translation.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,7 @@
139139
<service id="translation.warmer" class="Symfony\Bundle\FrameworkBundle\CacheWarmer\TranslationsCacheWarmer">
140140
<tag name="container.service_subscriber" id="translator" />
141141
<tag name="kernel.cache_warmer" />
142+
<tag name="container.no_preload" />
142143
<argument type="service" id="Psr\Container\ContainerInterface" />
143144
</service>
144145
</services>

src/Symfony/Bundle/FrameworkBundle/Resources/config/validator.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
<argument type="service" id="validator.builder" />
3737
<argument>%validator.mapping.cache.file%</argument>
3838
<tag name="kernel.cache_warmer" />
39+
<tag name="container.no_preload" />
3940
</service>
4041

4142
<service id="validator.mapping.cache.adapter" class="Symfony\Component\Cache\Adapter\PhpArrayAdapter">

src/Symfony/Bundle/SecurityBundle/Resources/config/security.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,7 @@
222222
<!-- Cache Warmers -->
223223
<service id="security.cache_warmer.expression" class="Symfony\Bundle\SecurityBundle\CacheWarmer\ExpressionCacheWarmer">
224224
<tag name="kernel.cache_warmer" />
225+
<tag name="container.no_preload" />
225226
<argument type="collection" /> <!-- expressions -->
226227
<argument type="service" id="security.expression_language" />
227228
</service>

src/Symfony/Bundle/TwigBundle/Resources/config/twig.xml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,14 @@
1818
<argument type="service" id="twig.runtime_loader" />
1919
</call>
2020
<configurator service="twig.configurator.environment" method="configure" />
21+
<tag name="container.preload" class="Twig\Cache\FilesystemCache" />
22+
<tag name="container.preload" class="Twig\Extension\CoreExtension" />
23+
<tag name="container.preload" class="Twig\Extension\EscaperExtension" />
24+
<tag name="container.preload" class="Twig\Extension\OptimizerExtension" />
25+
<tag name="container.preload" class="Twig\Extension\StagingExtension" />
26+
<tag name="container.preload" class="Twig\ExtensionSet" />
27+
<tag name="container.preload" class="Twig\Template" />
28+
<tag name="container.preload" class="Twig\TemplateWrapper" />
2129
</service>
2230
<service id="Twig_Environment" alias="twig" />
2331
<service id="Twig\Environment" alias="twig" />
@@ -37,6 +45,7 @@
3745

3846
<service id="twig.template_cache_warmer" class="Symfony\Bundle\TwigBundle\CacheWarmer\TemplateCacheWarmer">
3947
<tag name="kernel.cache_warmer" />
48+
<tag name="container.no_preload" />
4049
<tag name="container.service_subscriber" id="twig" />
4150
<argument type="service" id="Psr\Container\ContainerInterface" />
4251
<argument type="service" id="twig.template_iterator" />

src/Symfony/Bundle/TwigBundle/TwigBundle.php

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -19,20 +19,6 @@
1919
use Symfony\Component\DependencyInjection\Compiler\PassConfig;
2020
use Symfony\Component\DependencyInjection\ContainerBuilder;
2121
use Symfony\Component\HttpKernel\Bundle\Bundle;
22-
use Twig\Cache\FilesystemCache;
23-
use Twig\Extension\CoreExtension;
24-
use Twig\Extension\EscaperExtension;
25-
use Twig\Extension\OptimizerExtension;
26-
use Twig\Extension\StagingExtension;
27-
use Twig\ExtensionSet;
28-
29-
// Help opcache.preload discover always-needed symbols
30-
class_exists(FilesystemCache::class);
31-
class_exists(CoreExtension::class);
32-
class_exists(EscaperExtension::class);
33-
class_exists(OptimizerExtension::class);
34-
class_exists(StagingExtension::class);
35-
class_exists(ExtensionSet::class);
3622

3723
/**
3824
* Bundle.

src/Symfony/Component/Console/DependencyInjection/AddConsoleCommandPass.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,13 @@ class AddConsoleCommandPass implements CompilerPassInterface
2828
{
2929
private $commandLoaderServiceId;
3030
private $commandTag;
31+
private $noPreloadTag;
3132

32-
public function __construct(string $commandLoaderServiceId = 'console.command_loader', string $commandTag = 'console.command')
33+
public function __construct(string $commandLoaderServiceId = 'console.command_loader', string $commandTag = 'console.command', string $noPreloadTag = 'container.no_preload')
3334
{
3435
$this->commandLoaderServiceId = $commandLoaderServiceId;
3536
$this->commandTag = $commandTag;
37+
$this->noPreloadTag = $noPreloadTag;
3638
}
3739

3840
public function process(ContainerBuilder $container)
@@ -44,6 +46,7 @@ public function process(ContainerBuilder $container)
4446

4547
foreach ($commandServices as $id => $tags) {
4648
$definition = $container->getDefinition($id);
49+
$definition->addTag($this->noPreloadTag);
4750
$class = $container->getParameterBag()->resolveValue($definition->getClass());
4851

4952
if (isset($tags[0]['command'])) {
@@ -91,6 +94,7 @@ public function process(ContainerBuilder $container)
9194
$container
9295
->register($this->commandLoaderServiceId, ContainerCommandLoader::class)
9396
->setPublic(true)
97+
->addTag($this->noPreloadTag)
9498
->setArguments([ServiceLocatorTagPass::register($container, $lazyCommandRefs), $lazyCommandMap]);
9599

96100
$container->setParameter('console.command.ids', $serviceIds);

src/Symfony/Component/DependencyInjection/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ CHANGELOG
1313
* deprecated the `Psr\Container\ContainerInterface` and `Symfony\Component\DependencyInjection\ContainerInterface` aliases of the `service_container` service,
1414
configure them explicitly instead
1515
* added class `Symfony\Component\DependencyInjection\Dumper\Preloader` to help with preloading on PHP 7.4+
16+
* added tags `container.preload`/`.no_preload` to declare extra classes to preload/services to not preload
1617

1718
5.0.0
1819
-----

src/Symfony/Component/DependencyInjection/Dumper/PhpDumper.php

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ class PhpDumper extends Dumper
7878
private $namespace;
7979
private $asFiles;
8080
private $hotPathTag;
81+
private $preloadTags;
8182
private $inlineFactories;
8283
private $inlineRequires;
8384
private $inlinedRequires = [];
@@ -143,6 +144,7 @@ public function dump(array $options = [])
143144
'as_files' => false,
144145
'debug' => true,
145146
'hot_path_tag' => 'container.hot_path',
147+
'preload_tags' => ['container.preload', 'container.no_preload'],
146148
'inline_factories_parameter' => 'container.dumper.inline_factories',
147149
'inline_class_loader_parameter' => 'container.dumper.inline_class_loader',
148150
'preload_classes' => [],
@@ -154,6 +156,7 @@ public function dump(array $options = [])
154156
$this->namespace = $options['namespace'];
155157
$this->asFiles = $options['as_files'];
156158
$this->hotPathTag = $options['hot_path_tag'];
159+
$this->preloadTags = $options['preload_tags'];
157160
$this->inlineFactories = $this->asFiles && $options['inline_factories_parameter'] && (!$this->container->hasParameter($options['inline_factories_parameter']) || $this->container->getParameter($options['inline_factories_parameter']));
158161
$this->inlineRequires = $options['inline_class_loader_parameter'] && ($this->container->hasParameter($options['inline_class_loader_parameter']) ? $this->container->getParameter($options['inline_class_loader_parameter']) : (\PHP_VERSION_ID < 70400 || $options['debug']));
159162
$this->serviceLocatorTag = $options['service_locator_tag'];
@@ -571,7 +574,7 @@ private function addServiceInclude(string $cId, Definition $definition): string
571574
$lineage = [];
572575
foreach ($this->inlinedDefinitions as $def) {
573576
if (!$def->isDeprecated()) {
574-
foreach ($this->getClasses($def) as $class) {
577+
foreach ($this->getClasses($def, $cId) as $class) {
575578
$this->collectLineage($class, $lineage);
576579
}
577580
}
@@ -583,7 +586,7 @@ private function addServiceInclude(string $cId, Definition $definition): string
583586
&& $this->container->has($id)
584587
&& $this->isTrivialInstance($def = $this->container->findDefinition($id))
585588
) {
586-
foreach ($this->getClasses($def) as $class) {
589+
foreach ($this->getClasses($def, $cId) as $class) {
587590
$this->collectLineage($class, $lineage);
588591
}
589592
}
@@ -838,9 +841,9 @@ protected function {$methodName}($lazyInitialization)
838841
if ($definition->isDeprecated()) {
839842
$deprecation = $definition->getDeprecation($id);
840843
$code .= sprintf(" trigger_deprecation(%s, %s, %s);\n\n", $this->export($deprecation['package']), $this->export($deprecation['version']), $this->export($deprecation['message']));
841-
} else {
844+
} elseif (!$definition->hasTag($this->preloadTags[1])) {
842845
foreach ($this->inlinedDefinitions as $def) {
843-
foreach ($this->getClasses($def) as $class) {
846+
foreach ($this->getClasses($def, $id) as $class) {
844847
$this->preload[$class] = $class;
845848
}
846849
}
@@ -1003,10 +1006,10 @@ private function addServices(array &$services = null): string
10031006
foreach ($definitions as $id => $definition) {
10041007
if (!$definition->isSynthetic()) {
10051008
$services[$id] = $this->addService($id, $definition);
1006-
} else {
1009+
} elseif (!$definition->hasTag($this->preloadTags[1])) {
10071010
$services[$id] = null;
10081011

1009-
foreach ($this->getClasses($definition) as $class) {
1012+
foreach ($this->getClasses($definition, $id) as $class) {
10101013
$this->preload[$class] = $class;
10111014
}
10121015
}
@@ -1385,7 +1388,7 @@ private function addInlineRequires(): string
13851388
$inlinedDefinitions = $this->getDefinitionsFromArguments([$definition]);
13861389

13871390
foreach ($inlinedDefinitions as $def) {
1388-
foreach ($this->getClasses($def) as $class) {
1391+
foreach ($this->getClasses($def, $id) as $class) {
13891392
$this->collectLineage($class, $lineage);
13901393
}
13911394
}
@@ -2112,11 +2115,19 @@ private function getAutoloadFile(): ?string
21122115
return null;
21132116
}
21142117

2115-
private function getClasses(Definition $definition): array
2118+
private function getClasses(Definition $definition, string $id): array
21162119
{
21172120
$classes = [];
21182121

21192122
while ($definition instanceof Definition) {
2123+
foreach ($definition->getTag($this->preloadTags[0]) as $tag) {
2124+
if (!isset($tag['class'])) {
2125+
throw new InvalidArgumentException(sprintf('Missing attribute "class" on tag "%s" for service "%s".', $this->preloadTags[0], $id));
2126+
}
2127+
2128+
$classes[] = trim($tag['class'], '\\');
2129+
}
2130+
21202131
$classes[] = trim($definition->getClass(), '\\');
21212132
$factory = $definition->getFactory();
21222133

src/Symfony/Component/DependencyInjection/Tests/Fixtures/config/services9.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,10 @@
134134
->args([new Reference('errored_definition', ContainerInterface::RUNTIME_EXCEPTION_ON_INVALID_REFERENCE)])
135135
->public();
136136
$s->set('errored_definition', 'stdClass')->private();
137+
$s->set('preload_sidekick', 'stdClass')
138+
->tag('container.preload', ['class' => 'Some\Sidekick1'])
139+
->tag('container.preload', ['class' => 'Some\Sidekick2'])
140+
->public();
137141

138142
$s->alias('alias_for_foo', 'foo')->private()->public();
139143
$s->alias('alias_for_alias', ref('alias_for_foo'));

src/Symfony/Component/DependencyInjection/Tests/Fixtures/containers/container9.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,4 +187,9 @@
187187
$container->register('errored_definition', 'stdClass')
188188
->addError('Service "errored_definition" is broken.');
189189

190+
$container->register('preload_sidekick', 'stdClass')
191+
->setPublic(true)
192+
->addTag('container.preload', ['class' => 'Some\Sidekick1'])
193+
->addTag('container.preload', ['class' => 'Some\Sidekick2']);
194+
190195
return $container;

src/Symfony/Component/DependencyInjection/Tests/Fixtures/graphviz/services9.dot

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ digraph sc {
3636
node_tagged_iterator [label="tagged_iterator\nBar\n", shape=record, fillcolor="#eeeeee", style="filled"];
3737
node_runtime_error [label="runtime_error\nstdClass\n", shape=record, fillcolor="#eeeeee", style="filled"];
3838
node_errored_definition [label="errored_definition\nstdClass\n", shape=record, fillcolor="#eeeeee", style="filled"];
39+
node_preload_sidekick [label="preload_sidekick\nstdClass\n", shape=record, fillcolor="#eeeeee", style="filled"];
3940
node_foo2 [label="foo2\n\n", shape=record, fillcolor="#ff9999", style="filled"];
4041
node_foo3 [label="foo3\n\n", shape=record, fillcolor="#ff9999", style="filled"];
4142
node_foobaz [label="foobaz\n\n", shape=record, fillcolor="#ff9999", style="filled"];

src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services9_as_files.txt

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -578,6 +578,29 @@ class getNonSharedFooService extends ProjectServiceContainer
578578
}
579579
}
580580

581+
[Container%s/getPreloadSidekickService.php] => <?php
582+
583+
namespace Container%s;
584+
585+
use Symfony\Component\DependencyInjection\Argument\RewindableGenerator;
586+
use Symfony\Component\DependencyInjection\Exception\RuntimeException;
587+
588+
/**
589+
* @internal This class has been auto-generated by the Symfony Dependency Injection Component.
590+
*/
591+
class getPreloadSidekickService extends ProjectServiceContainer
592+
{
593+
/**
594+
* Gets the public 'preload_sidekick' shared service.
595+
*
596+
* @return \stdClass
597+
*/
598+
public static function do($container, $lazyLoad = true)
599+
{
600+
return $container->services['preload_sidekick'] = new \stdClass();
601+
}
602+
}
603+
581604
[Container%s/getRuntimeErrorService.php] => <?php
582605

583606
namespace Container%s;
@@ -731,6 +754,7 @@ class ProjectServiceContainer extends Container
731754
'method_call1' => 'getMethodCall1Service',
732755
'new_factory_service' => 'getNewFactoryServiceService',
733756
'non_shared_foo' => 'getNonSharedFooService',
757+
'preload_sidekick' => 'getPreloadSidekickService',
734758
'runtime_error' => 'getRuntimeErrorService',
735759
'service_from_static_method' => 'getServiceFromStaticMethodService',
736760
'tagged_iterator' => 'getTaggedIteratorService',
@@ -873,6 +897,7 @@ require __DIR__.'/Container%s/getThrowingOneService.php';
873897
require __DIR__.'/Container%s/getTaggedIteratorService.php';
874898
require __DIR__.'/Container%s/getServiceFromStaticMethodService.php';
875899
require __DIR__.'/Container%s/getRuntimeErrorService.php';
900+
require __DIR__.'/Container%s/getPreloadSidekickService.php';
876901
require __DIR__.'/Container%s/getNonSharedFooService.php';
877902
require __DIR__.'/Container%s/getNewFactoryServiceService.php';
878903
require __DIR__.'/Container%s/getMethodCall1Service.php';
@@ -906,6 +931,8 @@ $classes[] = ' 10000 Foo';
906931
$classes[] = 'LazyContext';
907932
$classes[] = 'FooBarBaz';
908933
$classes[] = 'FactoryClass';
934+
$classes[] = 'Some\Sidekick1';
935+
$classes[] = 'Some\Sidekick2';
909936
$classes[] = 'Request';
910937
$classes[] = 'Symfony\Component\DependencyInjection\ContainerInterface';
911938

src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services9_compiled.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ public function __construct()
4545
'lazy_context_ignore_invalid_ref' => 'getLazyContextIgnoreInvalidRefService',
4646
'method_call1' => 'getMethodCall1Service',
4747
'new_factory_service' => 'getNewFactoryServiceService',
48+
'preload_sidekick' => 'getPreloadSidekickService',
4849
'runtime_error' => 'getRuntimeErrorService',
4950
'service_from_static_method' => 'getServiceFromStaticMethodService',
5051
'tagged_iterator' => 'getTaggedIteratorService',
@@ -359,6 +360,16 @@ protected function getNewFactoryServiceService()
359360
return $instance;
360361
}
361362

363+
/**
364+
* Gets the public 'preload_sidekick' shared service.
365+
*
366+
* @return \stdClass
367+
*/
368+
protected function getPreloadSidekickService()
369+
{
370+
return $this->services['preload_sidekick'] = new \stdClass();
371+
}
372+
362373
/**
363374
* Gets the public 'runtime_error' shared service.
364375
*

0 commit comments

Comments
 (0)
0