8000 [HttpKernel] Add arguments for cache passes · symfony/symfony@4c56ed0 · GitHub
[go: up one dir, main page]

Skip to content

Commit 4c56ed0

Browse files
committed
[HttpKernel] Add arguments for cache passes
1 parent 4742143 commit 4c56ed0

File tree

5 files changed

+97
-8
lines changed

5 files changed

+97
-8
lines changed

src/Symfony/Bundle/FrameworkBundle/FrameworkBundle.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,8 @@ public function build(ContainerBuilder $container)
8282
$container->addCompilerPass(new FormPass());
8383
$container->addCompilerPass(new TranslatorPass());
8484
$container->addCompilerPass(new LoggingTranslatorPass());
85-
$container->addCompilerPass(new AddCacheWarmerPass());
86-
$container->addCompilerPass(new AddCacheClearerPass());
85+
$container->addCompilerPass(new AddCacheWarmerPass('cache_warmer', 'kernel.cache_warmer'));
86+
$container->addCompilerPass(new AddCacheClearerPass('cache_clearer', 'kernel.cache_clearer'));
8787
$container->addCompilerPass(new AddExpressionLanguageProvidersPass());
8888
$container->addCompilerPass(new TranslationExtractorPass());
8989
$container->addCompilerPass(new TranslationDumperPass());

src/Symfony/Component/HttpKernel/DependencyInjection/AddCacheClearerPass.php

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,20 +22,36 @@
2222
*/
2323
class AddCacheClearerPass implements CompilerPassInterface
2424
{
25+
private $tag;
26+
private $definition;
27+
28+
public function __construct($definition = null, $tag = null)
29+
{
30+
if (null === $definition || null === $tag) {
31+
@trigger_error('The '.static::class.' class\' constructor needs two parameters since version 3.2, and will become required in 4.0.', E_USER_DEPRECATED);
32+
33+
$definition = 'cache_clearer';
34+
$tag = 'kernel.cache_clearer';
35+
}
36+
37+
$this->definition = $definition;
38+
$this->tag = $tag;
39+
}
40+
2541
/**
2642
* {@inheritdoc}
2743
*/
2844
public function process(ContainerBuilder $container)
2945
{
30-
if (!$container->hasDefinition('cache_clearer')) {
46+
if (!$container->hasDefinition($this->definition)) {
3147
return;
3248
}
3349

3450
$clearers = array();
35-
foreach ($container->findTaggedServiceIds('kernel.cache_clearer') as $id => $attributes) {
51+
foreach ($container->findTaggedServiceIds($this->tag) as $id => $attributes) {
3652
$clearers[] = new Reference($id);
3753
}
3854

39-
$container->getDefinition('cache_clearer')->replaceArgument(0, $clearers);
55+
$container->getDefinition($this->definition)->replaceArgument(0, $clearers);
4056
}
4157
}

src/Symfony/Component/HttpKernel/DependencyInjection/AddCacheWarmerPass.php

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,21 +24,37 @@ class AddCacheWarmerPass implements CompilerPassInterface
2424
{
2525
use PriorityTaggedServiceTrait;
2626

27+
private $tag;
28+
private $definition;
29+
30+
public function __construct($definition = null, $tag = null)
31+
{
32+
if (null === $definition || null === $tag) {
33+
@trigger_error('The '.static::class.' class\' constructor needs two parameters since version 3.2, and will become required in 4.0.', E_USER_DEPRECATED);
34+
35+
$definition = 'cache_warmer';
36+
$tag = 'kernel.cache_warmer';
37+
}
38+
39+
$this->definition = $definition;
40+
$this->tag = $tag;
41+
}
42+
2743
/**
2844
* {@inheritdoc}
2945
*/
3046
public function process(ContainerBuilder $container)
3147
{
32-
if (!$container->hasDefinition('cache_warmer')) {
48+
if (!$container->hasDefinition($this->definition)) {
3349
return;
3450
}
3551

36-
$warmers = $this->findAndSortTaggedServices('kernel.cache_warmer', $container);
52+
$warmers = $this->findAndSortTaggedServices($this->tag, $container);
3753

3854
if (empty($warmers)) {
3955
return;
4056
}
4157

42-
$container->getDefinition('cache_warmer')->replaceArgument(0, $warmers);
58+
$container->getDefinition($this->definition)->replaceArgument(0, $warmers);
4359
}
4460
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
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\HttpKernel\Tests\DependencyInjection;
13+
14+
use Symfony\Bridge\PhpUnit\ErrorAssert;
15+
use Symfony\Component\HttpKernel\DependencyInjection\AddCacheClearerPass;
16+
17+
class AddCacheClearerPassTest extends \PHPUnit_Framework_TestCase
18+
{
19+
/**
20+
* @group legacy
21+
* @dataProvider nullProvider
22+
* @requires function Symfony\Bridge\PhpUnit\ErrorAssert::assertDeprecationsAreTriggered
23+
*/
24+
public function testThatADeprecatedErrorIsTriggeredWhenGivingNoConstructorArgs($definition, $tag)
25+
{
26+
ErrorAssert::assertDeprecationsAreTriggered('The '.AddCacheClearerPass::class.' class\' constructor needs two parameters since version 3.2, and will become required in 4.0.', function () use ($definition, $tag) { new AddCacheClearerPass($definition, $tag); });
27+
}
28+
29+
public function nullProvider()
30+
{
31+
return [
32+
'definition is null' => [null, 'tag'],
33+
'tag is null' => ['definition', null],
34+
'both args are null' => [null, null]
35+
];
36+
}
37+
}

src/Symfony/Component/HttpKernel/Tests/DependencyInjection/AddCacheWarmerPassTest.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace Symfony\Component\HttpKernel\Tests\DependencyInjection;
1313

14+
use Symfony\Bridge\PhpUnit\ErrorAssert;
1415
use Symfony\Component\DependencyInjection\Reference;
1516
use Symfony\Component\HttpKernel\DependencyInjection\AddCacheWarmerPass;
1617

@@ -96,4 +97,23 @@ public function testThatCacheWarmersMightBeNotDefined()
9697
$addCacheWarmerPass = new AddCacheWarmerPass();
9798
$addCacheWarmerPass->process($container);
9899
}
100+
101+
/**
102+
* @group legacy
103+
* @dataProvider nullProvider
104+
* @requires function Symfony\Bridge\PhpUnit\ErrorAssert::assertDeprecationsAreTriggered
105+
*/
106+
public function testThatADeprecatedErrorIsTriggeredWhenGivingNoConstructorArgs($definition, $tag)
107+
{
108+
ErrorAssert::assertDeprecationsAreTriggered('The '.AddCacheWarmerPass::class.' class\' constructor needs two parameters since version 3.2, and will become required in 4.0.', function () use ($definition, $tag) { new AddCacheWarmerPass($definition, $tag); });
109+
}
110+
111+
public function nullProvider()
112+
{
113+
return [
114+
'definition is null' => [null, 'tag'],
115+
'tag is null' => ['definition', null],
116+
'both args are null' => [null, null]
117+
];
118+
}
99119
}

0 commit comments

Comments
 (0)
0