8000 [DI] Add "container.inline" tag to flag the hot path and inline relat… · symfony/symfony@d6350a3 · GitHub
[go: up one dir, main page]

Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit d6350a3

Browse files
[DI] Add "container.inline" tag to flag the hot path and inline related services
1 parent 8cd2193 commit d6350a3

File tree

10 files changed

+218
-14
lines changed

10 files changed

+218
-14
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ class UnusedTagsPass implements CompilerPassInterface
2424
private $whitelist = array(
2525
'cache.pool.clearer',
2626
'console.command',
27+
'container.hot_path',
2728
'container.service_locator',
2829
'container.service_subscriber',
2930
'controller.service_arguments',

src/Symfony/Bundle/FrameworkBundle/FrameworkBundle.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
use Symfony\Component\Form\DependencyInjection\FormPass;
4444
use Symfony\Component\HttpFoundation\Request;
4545
use Symfony\Component\HttpKernel\Bundle\Bundle;
46+
use Symfony\Component\HttpKernel\KernelEvents;
4647
use Symfony\Component\Config\Resource\ClassExistenceResource;
4748
use Symfony\Component\Translation\DependencyInjection\TranslationDumperPass;
4849
use Symfony\Component\Translation\DependencyInjection\TranslationExtractorPass;
@@ -83,14 +84,23 @@ public function build(ContainerBuilder $container)
8384
{
8485
parent::build($container);
8586

87+
$hotPathListeners = array(
88+
KernelEvents::REQUEST,
89+
KernelEvents::CONTROLLER,
90+
KernelEvents::CONTROLLER_ARGUMENTS,
91+
KernelEvents::RESPONSE,
92+
KernelEvents::FINISH_REQUEST,
93+
KernelEvents::TERMINATE,
94+
);
95+
8696
$container->addCompilerPass(new LoggerPass(), PassConfig::TYPE_BEFORE_OPTIMIZATION, -32);
8797
$container->addCompilerPass(new RegisterControllerArgumentLocatorsPass());
8898
$container->addCompilerPass(new RemoveEmptyControllerArgumentLocatorsPass(), PassConfig::TYPE_BEFORE_REMOVING);
8999
$container->addCompilerPass(new RoutingResolverPass());
90100
$container->addCompilerPass(new ProfilerPass());
91101
// must be registered before removing private services as some might be listeners/subscribers
92102
// but as late as possible to get resolved parameters
93-
$container->addCompilerPass(new RegisterListenersPass(), PassConfig::TYPE_BEFORE_REMOVING);
103+
$container->addCompilerPass((new RegisterListenersPass())->setHotPathListeners($hotPathListeners), PassConfig::TYPE_BEFORE_REMOVING);
94104
$container->addCompilerPass(new TemplatingPass());
95105
$this->addCompilerPassIfExists($container, AddConstraintValidatorsPass::class, PassConfig::TYPE_BEFORE_REMOVING);
96106
$container->addCompilerPass(new AddAnnotationsCachedReaderPass(), PassConfig::TYPE_BEFORE_REMOVING);

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@
7777
<call method="setConfigCacheFactory">
7878
<argument type="service" id="config_cache_factory" />
7979
</call>
80+
<tag name="container.hot_path" />
8081
</service>
8182

8283
<service id="router" alias="router.default" public="true" />

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

Lines changed: 5 additions & 1 deletion
Diff line change
Original file line numberDiff line number
@@ -9,6 +9,7 @@
99

1010
<service id="event_dispatcher" class="Symfony\Component\EventDispatcher\ContainerAwareEventDispatcher" public="true">
1111
<argument type="service" id="service_container" />
12+
<tag name="container.hot_path" />
1213
</service>
1314
<service id="Symfony\Component\EventDispatcher\EventDispatcherInterface" alias="event_dispatcher" />
1415

@@ -17,10 +18,13 @@
1718
<argument type="service" id="controller_resolver" />
1819
<argument type="service" id="request_stack" />
1920
<argument type="service" id="argument_resolver" />
21+
<tag name="container.hot_path" />
2022
</service>
2123
<service id="Symfony\Component\HttpKernel\HttpKernelInterface" alias="http_kernel" />
2224

23-
<service id="request_stack" class="Symfony\Component\HttpFoundation\RequestStack" public="true" />
25+
<service id="request_stack" class="Symfony\Component\HttpFoundation\RequestStack" public="true">
26+
<tag name="container.hot_path" />
27+
</service>
2428
<service id="Symfony\Component\HttpFoundation\RequestStack" alias="request_stack" />
2529

2630
<service id="cache_warmer" class="Symfony\Component\HttpKernel\CacheWarmer\CacheWarmerAggregate" public="true">

src/Symfony/Bundle/FrameworkBundle/composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
"symfony/class-loader": "~3.2",
2323
"symfony/dependency-injection": "~3.4|~4.0",
2424
"symfony/config": "~3.4|~4.0",
25-
"symfony/event-dispatcher": "^3.3.1|~4.0",
25+
"symfony/event-dispatcher": "^3.4-beta4|~4.0-beta4",
2626
"symfony/http-foundation": "^3.3.11|~4.0",
2727
"symfony/http-kernel": "~3.4|~4.0",
2828
"symfony/polyfill-mbstring": "~1.0",

src/Symfony/Component/DependencyInjection/Compiler/PassConfig.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ public function __construct()
8989
)),
9090
new DefinitionErrorExceptionPass(),
9191
new CheckExceptionOnInvalidReferenceBehaviorPass(),
92+
new ResolveHotPathPass(),
9293
));
9394
}
9495

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\DependencyInjection\Compiler;
13+
14+
use Symfony\Component\DependencyInjection\ArgumentInterface;
15+
use Symfony\Component\DependencyInjection\ContainerBuilder;
16+
use Symfony\Component\DependencyInjection\Definition;
17+
use Symfony\Component\DependencyInjection\Reference;
18+
19+
/**
20+
* Propagate "container.hot_path" tags to referenced services.
21+
*
22+
* @author Nicolas Grekas <p@tchwork.com>
23+
*/
24+
class ResolveHotPathPass extends AbstractRecursivePass
25+
{
26+
private $tagName;
27+
private $resolvedIds = array();
28+
29+
public function __construct($tagName = 'container.hot_path')
30+
{
31+
$this->tagName = $tagName;
32+
}
33+
34+
/**
35+
* {@inheritdoc}
36+
*/
37+
public function process(ContainerBuilder $container)
38+
{
39+
try {
40+
parent::process($container);
41+
$container->getDefinition('service_container')->clearTag($this->tagName);
42+
} finally {
43+
$this->resolvedIds = array();
44+
}
45+
}
46+
47+
/**
48+
* {@inheritdoc}
49+
*/
50+
protected function processValue($value, $isRoot = false)
51+
{
52+
if ($value instanceof ArgumentInterface) {
53+
return $value;
54+
}
55+
if ($value instanceof Definition && $isRoot && (isset($this->resolvedIds[$this->currentId]) || !$value->hasTag($this->tagName))) {
56+
return $value;
57+
}
58+
if ($value instanceof Reference && ContainerBuilder::IGNORE_ON_UNINITIALIZED_REFERENCE !== $value->getInvalidBehavior() && $this->container->has($id = (string) $value)) {
59+
$definition = $this->container->findDefinition($id);
60+
if (!$definition->hasTag($this->tagName)) {
61+
$this->resolvedIds[$id] = true;
62+
$definition->addTag($this->tagName);
63+
parent::processValue($definition, false);
64+
}
65+
66+
return $value;
67+
}
68+
69+
return parent::processValue($value, $isRoot);
70+
}
71+
}

0 commit comments

Comments
 (0)
0