8000 minor #24103 [FrameworkBundle] Fix Di config to allow for more privat… · symfony/symfony@4f02e91 · GitHub
[go: up one dir, main page]

Skip to content

Commit 4f02e91

Browse files
committed
minor #24103 [FrameworkBundle] Fix Di config to allow for more private services (nicolas-grekas)
This PR was merged into the 3.4 branch. Discussion ---------- [FrameworkBundle] Fix Di config to allow for more private services | Q | A | ------------- | --- | Branch? | 3.4 | Bug fix? | no | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | - | License | MIT | Doc PR | - This PR is a prelude to #23822: - allow cache pool clearers to be private by using IGNORE_ON_UNINITIALIZED_REFERENCE, allowing much more sane logic in the related passes - allow templating helpers to be private by using a service locator - replace an inline def by a reference in workflow's config (ping @lyrixx @Nyholm) - fix the Stopwatch alias, which is public in 3.4, but private in 3.3 - remove direct access to `->get('fragment.handler')` in tests so that the service could be made private Commits ------- 76c4217 [FrameworkBundle] Fix Di config to allow for more private services
2 parents aad90fa + 76c4217 commit 4f02e91

File tree

13 files changed

+65
-55
lines changed

13 files changed

+65
-55
lines changed

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

Lines changed: 7 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -27,29 +27,16 @@ final class CachePoolClearerPass implements CompilerPassInterface
2727
public function process(ContainerBuilder $container)
2828
{
2929
$container->getParameterBag()->remove('cache.prefix.seed');
30-
$poolsByClearer = array();
31-
$pools = array();
3230

33-
foreach ($container->findTaggedServiceIds('cache.pool') as $id => $attributes) {
34-
$pools[$id] = new Reference($id);
35-
foreach (array_reverse($attributes) as $attr) {
36-
if (isset($attr['clearer'])) {
37-
$poolsByClearer[$attr['clearer']][$id] = $pools[$id];
38-
}
39-
if (!empty($attr['unlazy'])) {
40-
$container->getDefinition($id)->setLazy(false);
41-
}
42-
if (array_key_exists('clearer', $attr) || array_key_exists('unlazy', $attr)) {
43-
break;
31+
foreach ($container->findTaggedServiceIds('cache.pool.clearer') as $id => $attr) {
32+
$clearer = $container->getDefinition($id);
33+
$pools = array();
34+
foreach ($clearer->getArgument(0) as $id => $ref) {
35+
if ($container->hasDefinition($id)) {
36+
$pools[$id] = new Reference($id);
4437
}
4538
}
46-
}
47-
48-
$container->getDefinition('cache.global_clearer')->addArgument($pools);
49-
50-
foreach ($poolsByClearer as $clearer => $pools) {
51-
$clearer = $container->getDefinition($clearer);
52-
$clearer->addArgument($pools);
39+
$clearer->replaceArgument(0, $pools);
5340
}
5441

5542
if (!$container->has('cache.annotations')) {

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

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ public function process(ContainerBuilder $container)
3737
}
3838
$seed .= '.'.$container->getParameter('kernel.name').'.'.$container->getParameter('kernel.environment');
3939

40+
$pools = array();
41+
$clearers = array();
4042
$attributes = array(
4143
'provider',
4244
'namespace',
@@ -47,10 +49,8 @@ public function process(ContainerBuilder $container)
4749
if ($pool->isAbstract()) {
4850
continue;
4951
}
50-
$isLazy = $pool->isLazy();
5152
while ($adapter instanceof ChildDefinition) {
5253
$adapter = $container->findDefinition($adapter->getParent());
53-
$isLazy = $isLazy || $adapter->isLazy();
5454
if ($t = $adapter->getTag('cache.pool')) {
5555
$tags[0] += $t[0];
5656
}
@@ -82,17 +82,29 @@ public function process(ContainerBuilder $container)
8282
throw new InvalidArgumentException(sprintf('Invalid "cache.pool" tag for service "%s": accepted attributes are " 10000 ;clearer", "provider", "namespace" and "default_lifetime", found "%s".', $id, implode('", "', array_keys($tags[0]))));
8383
}
8484

85-
$attr = array();
8685
if (null !== $clearer) {
87-
$attr['clearer'] = $clearer;
86+
$clearers[$clearer][$id] = new Reference($id, $container::IGNORE_ON_UNINITIALIZED_REFERENCE);
8887
}
89-
if (!$isLazy) {
90-
$pool->setLazy(true);
91-
$attr['unlazy'] = true;
92-
}
93-
if ($attr) {
94-
$pool->addTag('cache.pool', $attr);
88+
89+
$pools[$id] = new Reference($id, $container::IGNORE_ON_UNINITIALIZED_REFERENCE);
90+
}
91+
92+
$clearer = 'cache.global_clearer';
93+
while ($container->hasAlias($clearer)) {
94+
$clearer = (string) $container->getAlias($clearer);
95+
}
96+
if ($container->hasDefinition($clearer)) {
97+
$clearers['cache.global_clearer'] = $pools;
98+
}
99+
100+
foreach ($clearers as $id => $pools) {
101+
$clearer = $container->getDefinition($id);
102+
if ($clearer instanceof ChilDefinition) {
103+
$clearer->replaceArgument(0, $pools);
104+
} else {
105+
$clearer->setArgument(0, $pools);
95106
}
107+
$clearer->addTag('cache.pool.clearer');
96108
}
97109
}
98110

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use Symfony\Component\DependencyInjection\Alias;
1616
use Symfony\Component\DependencyInjection\ContainerBuilder;
1717
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
18+
use Symfony\Component\DependencyInjection\Reference;
1819
use Symfony\Component\Templating\EngineInterface as ComponentEngineInterface;
1920

2021
class TemplatingPass implements CompilerPassInterface
@@ -31,16 +32,22 @@ public function process(ContainerBuilder $container)
3132
}
3233

3334
if ($container->hasDefinition('templating.engine.php')) {
35+
$refs = array();
3436
$helpers = array();
3537
foreach ($container->findTaggedServiceIds('templating.helper', true) as $id => $attributes) {
3638
if (isset($attributes[0]['alias'])) {
3739
$helpers[$attributes[0]['alias']] = $id;
40+
$refs[$id] = new Reference($id);
3841
}
3942
}
4043

4144
if (count($helpers) > 0) {
4245
$definition = $container->getDefinition('templating.engine.php');
4346
$definition->addMethodCall('setHelpers', array($helpers));
47+
48+
if ($container->hasDefinition('templating.engine.php.helpers_locator')) {
49+
$container->getDefinition('templating.engine.php.helpers_locator')->replaceArgument(0, $refs);
50+
}
4451
}
4552
}
4653
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
class UnusedTagsPass implements CompilerPassInterface
2323
{
2424
private $whitelist = array(
25+
'cache.pool.clearer',
2526
'console.command',
2627
'container.service_locator',
2728
'container.service_subscriber',

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -597,15 +597,15 @@ private function registerWorkflowConfiguration(array $workflows, ContainerBuilde
597597
}
598598

599599
// Create Workflow
600+
$workflowId = sprintf('%s.%s', $type, $name);
600601
$workflowDefinition = new ChildDefinition(sprintf('%s.abstract', $type));
601-
$workflowDefinition->replaceArgument(0, $definitionDefinition);
602+
$workflowDefinition->replaceArgument(0, new Reference(sprintf('%s.definition', $workflowId)));
602603
if (isset($markingStoreDefinition)) {
603604
$workflowDefinition->replaceArgument(1, $markingStoreDefinition);
604605
}
605606
$workflowDefinition->replaceArgument(3, $name);
606607

607608
// Store to container
608-
$workflowId = sprintf('%s.%s', $type, $name);
609609
$container->setDefinition($workflowId, $workflowDefinition);
610610
$container->setDefinition(sprintf('%s.definition', $workflowId), $definitionDefinition);
611611

@@ -680,7 +680,7 @@ private function registerDebugConfiguration(array $config, ContainerBuilder $con
680680

681681
if (class_exists(Stopwatch::class)) {
682682
$container->register('debug.stopwatch', Stopwatch::class)->addArgument(true);
683-
$container->setAlias(Stopwatch::class, 'debug.stopwatch');
683+
$container->setAlias(Stopwatch::class, new Alias('debug.stopwatch', false));
684684
}
685685

686686
$debug = $container->getParameter('kernel.debug');

src/Symfony/Bundle/FrameworkBundle/FrameworkBundle.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ public function build(ContainerBuilder $container)
106106
$container->addCompilerPass(new AddExpressionLanguageProvidersPass());
107107
$this->addCompilerPassIfExists($container, TranslationExtractorPass::class);
108108
$this->addCompilerPassIfExists($container, TranslationDumperPass::class);
109-
$container->addCompilerPass(new FragmentRendererPass(), PassConfig::TYPE_AFTER_REMOVING);
109+
$container->addCompilerPass(new FragmentRendererPass());
110110
$this->addCompilerPassIfExists($container, SerializerPass::class);
111111
$this->addCompilerPassIfExists($container, PropertyInfoPass::class);
112112
$container->addCompilerPass(new DataCollectorTranslatorPass());

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
<service id="debug.templating.engine.php" class="Symfony\Bundle\FrameworkBundle\Templating\TimedPhpEngine">
1111
<argument type="service" id="templating.name_parser" />
12-
<argument type="service" id="service_container" />
12+
<argument type="service" id="templating.engine.php.helpers_locator" />
1313
<argument type="service" id="templating.loader" />
1414
<argument type="service" id="debug.stopwatch" />
1515
<argument type="service" id="templating.globals" />

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,17 @@
99

1010
<service id="templating.engine.php" class="Symfony\Bundle\FrameworkBundle\Templating\PhpEngine">
1111
<argument type="service" id="templating.name_parser" />
12-
<argument type="service" id="service_container" />
12+
<argument type="service" id="templating.engine.php.helpers_locator" />
1313
<argument type="service" id="templating.loader" />
1414
<argument type="service" id="templating.globals" />
1515
<call method="setCharset"><argument>%kernel.charset%</argument></call>
1616
</service>
1717

18+
<service id="templating.engine.php.helpers_locator">
19+
<tag name="container.service_locator" />
20+
<argument type="collection" />
21+
</service>
22+
1823
<service id="templating.helper.slots" class="Symfony\Component\Templating\Helper\SlotsHelper" public="true">
1924
<tag name="templating.helper" alias="slots" />
2025
</service>

src/Symfony/Bundle/FrameworkBundle/Templating/Helper/FormHelper.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ public function block(FormView $view, $blockName, array $variables = array())
226226
* Check the token in your action using the same CSRF token id.
227227
*
228228
* <code>
229-
* $csrfProvider = $this->get('security.csrf.token_generator');
229+
* // $csrfProvider being an instance of Symfony\Component\Security\Csrf\TokenGenerator\TokenGeneratorInterface
230230
* if (!$csrfProvider->isCsrfTokenValid('rm_user_'.$user->getId(), $token)) {
231231
* throw new \RuntimeException('CSRF attack detected.');
232232
* }

src/Symfony/Bundle/FrameworkBundle/Tests/Functional/Bundle/TestBundle/Controller/SubRequestController.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,8 @@ class SubRequestController implements ContainerAwareInterface
2121
{
2222
use ContainerAwareTrait;
2323

24-
public function indexAction()
24+
public function indexAction($handler)
2525
{
26-
$handler = $this->container->get('fragment.handler');
27-
2826
$errorUrl = $this->generateUrl('subrequest_fragment_error', array('_locale' => 'fr', '_format' => 'json'));
2927
$altUrl = $this->generateUrl('subrequest_fragment', array('_locale' => 'fr', '_format' => 'json'));
3028

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,7 @@
11
imports:
22
- { resource: ./../config/default.yml }
3+
4+
services:
5+
Symfony\Bundle\FrameworkBundle\Tests\Functional\Bundle\TestBundle\Controller\SubRequestController:
6+
tags:
7+
- { name: controller.service_arguments, action: indexAction, argument: handler, id: fragment.handler }

src/Symfony/Bundle/WebProfilerBundle/Tests/DependencyInjection/WebProfilerExtensionTest.php

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@
1717
use Symfony\Component\DependencyInjection\ContainerBuilder;
1818
use Symfony\Component\DependencyInjection\Definition;
1919
use Symfony\Component\DependencyInjection\Reference;
20-
use Symfony\Component\DependencyInjection\Dumper\PhpDumper;
20+
use Symfony\Component\EventDispatcher\DependencyInjection\RegisterListenersPass;
21+
use Symfony\Component\EventDispatcher\EventDispatcher;
2122

2223
class WebProfilerExtensionTest extends TestCase
2324
{
@@ -51,6 +52,7 @@ protected function setUp()
5152
$this->kernel = $this->getMockBuilder('Symfony\\Component\\HttpKernel\\KernelInterface')->getMock();
5253

5354
$this->container = new ContainerBuilder();
55+
$this->container->register('event_dispatcher', EventDispatcher::class);
5456
$this->container->register('router', $this->getMockClass('Symfony\\Component\\Routing\\RouterInterface'));
5557
$this->container->register('twig', 'Twig\Environment');
5658
$this->container->register('twig_loader', 'Twig\Loader\ArrayLoader')->addArgument(array());
@@ -66,6 +68,7 @@ protected function setUp()
6668
->addArgument(new Definition($this->getMockClass('Symfony\\Component\\HttpKernel\\Profiler\\ProfilerStorageInterface')));
6769
$this->container->setParameter('data_collector.templates', array());
6870
$this->container->set('kernel', $this->kernel);
71+
$this->container->addCompilerPass(new RegisterListenersPass());
6972
}
7073

7174
protected function tearDown()
@@ -88,7 +91,7 @@ public function testDefaultConfig($debug)
8891

8992
$this->assertFalse($this->container->has('web_profiler.debug_toolbar'));
9093

91-
$this->assertSaneContainer($this->getDumpedContainer());
94+
$this->assertSaneContainer($this->getCompiledContainer());
9295
}
9396

9497
/**
@@ -101,7 +104,7 @@ public function testToolbarConfig($toolbarEnabled, $interceptRedirects, $listene
101104

102105
$this->assertSame($listenerInjected, $this->container->has('web_profiler.debug_toolbar'));
103106

104-
$this->assertSaneContainer($this->getDumpedContainer(), '', array('web_profiler.csp.handler'));
107+
$this->assertSaneContainer($this->getCompiledContainer(), '', array('web_profiler.csp.handler'));
105108

106109
if ($listenerInjected) {
107110
$this->assertSame($listenerEnabled, $this->container->get('web_profiler.debug_toolbar')->isEnabled());
@@ -118,19 +121,11 @@ public function getDebugModes()
118121
);
119122
}
120123

121-
private function getDumpedContainer()
124+
private function getCompiledContainer()
122125
{
123-
static $i = 0;
124-
$class = 'WebProfilerExtensionTestContainer'.$i++;
125-
126126
$this->container->compile();
127+
$this->container->set('kernel', $this->kernel);
127128

128-
$dumper = new PhpDumper($this->container);
129-
eval('?>'.$dumper->dump(array('class' => $class)));
130-
131-
$container = new $class();
132-
$container->set('kernel', $this->kernel);
133-
134-
return $container;
129+
return $this->container;
135130
}
136131
}

src/Symfony/Component/Form/FormRendererInterface.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ public function searchAndRenderBlock(FormView $view, $blockNameSuffix, array $va
7676
* Check the token in your action using the same token ID.
7777
*
7878
* <code>
79-
* $csrfProvider = $this->get('security.csrf.token_generator');
79+
* // $csrfProvider being an instance of Symfony\Component\Security\Csrf\TokenGenerator\TokenGeneratorInterface
8080
* if (!$csrfProvider->isCsrfTokenValid('rm_user_'.$user->getId(), $token)) {
8181
* throw new \RuntimeException('CSRF attack detected.');
8282
* }

0 commit comments

Comments
 (0)
0