8000 [DI] add tags `container.preload`/`.no_preload` to declare extra clas… · symfony/symfony@45dd84f · GitHub
[go: up one dir, main page]

Skip to content

Commit 45dd84f

Browse files
[DI] add tags container.preload/.no_preload to declare extra classes to preload/services to not preload
1 parent 4dabd00 commit 45dd84f

File tree

27 files changed

+124
-25
lines changed

27 files changed

+124
-25
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: 4 additions & 2 deletions
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)
@@ -488,7 +489,8 @@ public function load(array $configs, ContainerBuilder $container)
488489
}
489490

490491
$container->registerForAutoconfiguration(RouteLoaderInterface::class)
491-
->addTag('routing.route_loader');
492+
->addTag('routing.route_loader')
493+
->addTag('container.no_preload');
492494
}
493495

494496
/**

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
@@ -218,6 +218,7 @@
218218
<!-- Cache Warmers -->
219219
<service id="security.cache_warmer.expression" class="Symfony\Bundle\SecurityBundle\CacheWarmer\ExpressionCacheWarmer">
220220
<tag name="kernel.cache_warmer" />
221+
<tag name="container.no_preload" />
221222
<argument type="collection" /> <!-- expressions -->
222223
<argument type="service" id="security.expression_language" />
223224
</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
@@ -7,6 +7,7 @@ CHANGELOG
77
* added support to autowire public typed properties in php 7.4
88
* added support for defining method calls, a configurator, and property setters in `InlineServiceConfigurator`
99
* added possibility to define abstract service arguments
10+
* added tags `container.preload`/`.no_preload` to declare extra classes to preload/services to not preload
1011

1112
5.0.0
1213
-----

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 $preloadTag;
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'];
@@ -560,7 +563,7 @@ private function addServiceInclude(string $cId, Definition $definition): string
560563
$lineage = [];
561564
foreach ($this->inlinedDefinitions as $def) {
562565
if (!$def->isDeprecated()) {
563-
foreach ($this->getClasses($def) as $class) {
566+
foreach ($this->getClasses($def, $cId) as $class) {
564567
$this->collectLineage($class, $lineage);
565568
}
566569
}
@@ -572,7 +575,7 @@ private function addServiceInclude(string $cId, Definition $definition): string
572575
&& $this->container->has($id)
573576
&& $this->isTrivialInstance($def = $this->container->findDefinition($id))
574577
) {
575-
foreach ($this->getClasses($def) as $class) {
578+
foreach ($this->getClasses($def, $cId) as $class) {
576579
$this->collectLineage($class, $lineage);
577580
}
578581
}
@@ -824,9 +827,9 @@ protected function {$methodName}($lazyInitialization)
824827

825828
if ($definition->isDeprecated()) {
826829
$code .= sprintf(" trigger_deprecation('', '', %s);\n\n", $this->export($definition->getDeprecationMessage($id)));
827-
} else {
830+
} elseif (!$definition->hasTag($this->preloadTags[1])) {
828831
foreach ($this->inlinedDefinitions as $def) {
829-
foreach ($this->getClasses($def) as $class) {
832+
foreach ($this->getClasses($def, $id) as $class) {
830833
$this->preload[$class] = $class;
831834
}
832835
}
@@ -988,10 +991,10 @@ private function addServices(array &$services = null): string
988991
foreach ($definitions as $id => $definition) {
989992
if (!$definition->isSynthetic()) {
990993
$services[$id] = $this->addService($id, $definition);
991-
} else {
994+
} elseif (!$definition->hasTag($this->preloadTags[1])) {
992995
$services[$id] = null;
993996

994-
foreach ($this->getClasses($definition) as $class) {
997+
foreach ($this->getClasses($definition, $id) as $class) {
995998
$this->preload[$class] = $class;
996999
}
9971000
}
@@ -1376,7 +1379,7 @@ private function addInlineRequires(): string
13761379
$inlinedDefinitions = $this->getDefinitionsFromArguments([$definition]);
13771380

13781381
foreach ($inlinedDefinitions as $def) {
1379-
foreach ($this->getClasses($def) as $class) {
1382+
foreach ($this->getClasses($def, $id) as $class) {
13801383
$this->collectLineage($class, $lineage);
13811384
}
13821385
}
@@ -2099,11 +2102,19 @@ private function getAutoloadFile(): ?string
20992102
return null;
21002103
}
21012104

2102-
private function getClasses(Definition $definition): array
2105+
private function getClasses(Definition $definition, string $id): array
21032106
{
21042107
$classes = [];
21052108

21062109
while ($definition instanceof Definition) {
2110+
foreach ($definition->getTag($this->preloadTags[0]) as $tag) {
2111+
if (!isset($tag['class'])) {
2112+
throw new InvalidArgumentException(sprintf('Missing attribute "class" on tag "%s" for service "%s".', $this->preloadTags[0], $id));
2113+
}
2114+
2115+
$classes[] = trim($tag['class'], '\\');
2116+
}
2117+
21072118
$classes[] = trim($definition->getClass(), '\\');
21082119
$factory = $definition->getFactory();
21092120

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: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -308,6 +308,16 @@ $this->factories['non_shared_foo'] = function () {
308308

309309
return $this->factories['non_shared_foo']();
310310

311+
[Container%s/getPreloadSidekickService.php] => <?php
312+
313+
use Symfony\Component\DependencyInjection\Argument\RewindableGenerator;
314+
use Symfony\Component\DependencyInjection\Exception\RuntimeException;
315+
316+
// This file has been auto-generated by the Symfony Dependency Injection Component for internal use.
317+
// Returns the public 'preload_sidekick' shared service.
318+
319+
return $this->services['preload_sidekick'] = new \stdClass();
320+
311321
[Container%s/getRuntimeErrorService.php] => <?php
312322

313323
use Symfony\Component\DependencyInjection\Argument\RewindableGenerator;
@@ -412,6 +422,7 @@ class ProjectServiceContainer extends Container
412422
'method_call1' => 'getMethodCall1Service.php',
413423
'new_factory_service' => 'getNewFactoryServiceService.php',
414424
'non_shared_foo' => 'getNonSharedFooService.php',
425+
'preload_sidekick' => 'getPreloadSidekickService.php',
415426
'runtime_error' => 'getRuntimeErrorService.php',
416427
'service_from_static_method' => 'getServiceFromStaticMethodService.php',
417428
'tagged_iterator' => 'getTaggedIteratorService.php',
@@ -542,6 +553,8 @@ $classes[] = 'Foo';
542553
$classes[] = 'LazyContext';
543554
$classes[] = 'FooBarBaz';
544555
$classes[] = 'FactoryClass';
556+
$classes[] = 'Some\Sidekick1';
557+
$classes[] = 'Some\Sidekick2';
545558
$classes[] = 'Request';
546559
$classes[] = 'Symfony\Component\DependencyInjection\ContainerInterface';
547560

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ public function __construct()
4848
'lazy_context_ignore_invalid_ref' => 'getLazyContextIgnoreInvalidRefService',
4949
'method_call1' => 'getMethodCall1Service',
5050
'new_factory_service' => 'getNewFactoryServiceService',
51+
'preload_sidekick' => 'getPreloadSidekickService',
5152
'runtime_error' => 'getRuntimeErrorService',
5253
'service_from_static_method' => 'getServiceFromStaticMethodService',
5354
'tagged_iterator' => 'getTaggedIteratorService',
@@ -362,6 +363,16 @@ protected function getNewFactoryServiceService()
362363
return $instance;
363364
}
364365

366+
/**
367+
* Gets the public 'preload_sidekick' shared service.
368+
*
369+
* @return \stdClass
370+
*/
371+
protected function getPreloadSidekickService()
372+
{
373+
return $this->services['preload_sidekick'] = new \stdClass();
374+
}
375+
365376
/**
366377
* Gets the public 'runtime_error' shared service.
367378
*

0 commit comments

Comments
 (0)
0