From 83727c7e3dc7c2929059025dbaa0b2dc4e05d101 Mon Sep 17 00:00:00 2001 From: Thierry Thuon Date: Wed, 3 May 2017 14:00:53 +0200 Subject: [PATCH] [FrameworkBundle][HttpKernel] Move addcachearmer, addcacheclearer compiler pass --- UPGRADE-3.4.md | 8 ++ UPGRADE-4.0.md | 6 ++ .../Bundle/FrameworkBundle/CHANGELOG.md | 2 + .../Compiler/AddCacheClearerPass.php | 26 ++----- .../Compiler/AddCacheWarmerPass.php | 29 ++----- .../FrameworkBundle/FrameworkBundle.php | 4 +- .../Compiler/AddCacheWarmerPassTest.php | 3 + .../Bundle/FrameworkBundle/composer.json | 3 +- src/Symfony/Component/HttpKernel/CHANGELOG.md | 6 ++ .../AddCacheClearerPass.php | 50 ++++++++++++ .../AddCacheWarmerPass.php | 53 +++++++++++++ .../AddCacheClearerPassTest.php | 53 +++++++++++++ .../AddCacheWarmerPassTest.php | 77 +++++++++++++++++++ 13 files changed, 274 insertions(+), 46 deletions(-) create mode 100644 src/Symfony/Component/HttpKernel/DependencyInjection/AddCacheClearerPass.php create mode 100644 src/Symfony/Component/HttpKernel/DependencyInjection/AddCacheWarmerPass.php create mode 100644 src/Symfony/Component/HttpKernel/Tests/DependencyInjection/AddCacheClearerPassTest.php create mode 100644 src/Symfony/Component/HttpKernel/Tests/DependencyInjection/AddCacheWarmerPassTest.php diff --git a/UPGRADE-3.4.md b/UPGRADE-3.4.md index a609da081c..93ca903215 100644 --- a/UPGRADE-3.4.md +++ b/UPGRADE-3.4.md @@ -38,6 +38,14 @@ FrameworkBundle will be removed in 4.0. Use the `--prefix` option with an empty string as value instead (e.g. `--prefix=""`) + * The `Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\AddCacheClearerPass` + class has been deprecated and will be removed in 4.0. Use the + `Symfony\Component\HttpKernel\DependencyInjection\AddCacheClearerPass` class instead. + + * The `Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\AddCacheWarmerPass` + class has been deprecated and will be removed in 4.0. Use the + `Symfony\Component\HttpKernel\DependencyInjection\AddCacheWarmerPass` class instead. + Process ------- diff --git a/UPGRADE-4.0.md b/UPGRADE-4.0.md index b1027dc026..6bd0dde3aa 100644 --- a/UPGRADE-4.0.md +++ b/UPGRADE-4.0.md @@ -348,6 +348,12 @@ FrameworkBundle * The `--no-prefix` option of the `translation:update` command has been removed. + * The `Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\AddCacheClearerPass` class has been removed. + Use the `Symfony\Component\HttpKernel\DependencyInjection\AddCacheClearerPass` class instead. + + * The `Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\AddCacheWarmerPass` class has been removed. + Use the `Symfony\Component\HttpKernel\DependencyInjection\AddCacheWarmerPass` class instead. + HttpFoundation -------------- diff --git a/src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md b/src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md index c089a8b40a..6c5c2fd147 100644 --- a/src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md +++ b/src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md @@ -10,6 +10,8 @@ CHANGELOG require symfony/stopwatch` in your `dev` environment. * Deprecated using the `KERNEL_DIR` environment variable with `KernelTestCase::getKernelClass()`. * Deprecated the `KernelTestCase::getPhpUnitXmlDir()` and `KernelTestCase::getPhpUnitCliConfigArgument()` methods. + * Deprecated `AddCacheClearerPass`, use `Symfony\Component\HttpKernel\DependencyInjection\AddCacheClearerPass` instead. + * Deprecated `AddCacheWarmerPass`, use `Symfony\Component\HttpKernel\DependencyInjection\AddCacheWarmerPass` instead. 3.3.0 ----- diff --git a/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Compiler/AddCacheClearerPass.php b/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Compiler/AddCacheClearerPass.php index 88cfffd04f..c58fd6843d 100644 --- a/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Compiler/AddCacheClearerPass.php +++ b/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Compiler/AddCacheClearerPass.php @@ -11,31 +11,17 @@ namespace Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler; -use Symfony\Component\DependencyInjection\ContainerBuilder; -use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; -use Symfony\Component\DependencyInjection\Reference; +@trigger_error(sprintf('The %s class is deprecated since version 3.4 and will be removed in 4.0. Use Symfony\Component\HttpKernel\DependencyInjection\AddCacheClearerPass instead.', AddCacheClearerPass::class), E_USER_DEPRECATED); + +use Symfony\Component\HttpKernel\DependencyInjection\AddCacheClearerPass as BaseAddCacheClearerPass; /** * Registers the cache clearers. * + * @deprecated since version 3.4, to be removed in 4.0. Use {@link BaseAddCacheClearerPass instead}. + * * @author Dustin Dobervich */ -class AddCacheClearerPass implements CompilerPassInterface +class AddCacheClearerPass extends BaseAddCacheClearerPass { - /** - * {@inheritdoc} - */ - public function process(ContainerBuilder $container) - { - if (!$container->hasDefinition('cache_clearer')) { - return; - } - - $clearers = array(); - foreach ($container->findTaggedServiceIds('kernel.cache_clearer', true) as $id => $attributes) { - $clearers[] = new Reference($id); - } - - $container->getDefinition('cache_clearer')->replaceArgument(0, $clearers); - } } diff --git a/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Compiler/AddCacheWarmerPass.php b/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Compiler/AddCacheWarmerPass.php index 8f5353121c..4b427684a9 100644 --- a/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Compiler/AddCacheWarmerPass.php +++ b/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Compiler/AddCacheWarmerPass.php @@ -11,34 +11,17 @@ namespace Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler; -use Symfony\Component\DependencyInjection\Compiler\PriorityTaggedServiceTrait; -use Symfony\Component\DependencyInjection\ContainerBuilder; -use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; +@trigger_error(sprintf('The %s class is deprecated since version 3.4 and will be removed in 4.0. Use Symfony\Component\HttpKernel\DependencyInjection\AddCacheWarmerPass instead.', AddCacheWarmerPass::class), E_USER_DEPRECATED); + +use Symfony\Component\HttpKernel\DependencyInjection\AddCacheWarmerPass as BaseAddCacheWarmerPass; /** * Registers the cache warmers. * + * @deprecated since version 3.4, to be removed in 4.0. Use {@link BaseAddCacheWarmerPass instead}. + * * @author Fabien Potencier */ -class AddCacheWarmerPass implements CompilerPassInterface +class AddCacheWarmerPass extends BaseAddCacheWarmerPass { - use PriorityTaggedServiceTrait; - - /** - * {@inheritdoc} - */ - public function process(ContainerBuilder $container) - { - if (!$container->hasDefinition('cache_warmer')) { - return; - } - - $warmers = $this->findAndSortTaggedServices('kernel.cache_warmer', $container); - - if (empty($warmers)) { - return; - } - - $container->getDefinition('cache_warmer')->replaceArgument(0, $warmers); - } } diff --git a/src/Symfony/Bundle/FrameworkBundle/FrameworkBundle.php b/src/Symfony/Bundle/FrameworkBundle/FrameworkBundle.php index 99ab06b337..ccf59ee4f8 100644 --- a/src/Symfony/Bundle/FrameworkBundle/FrameworkBundle.php +++ b/src/Symfony/Bundle/FrameworkBundle/FrameworkBundle.php @@ -21,8 +21,6 @@ use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\TemplatingPass; use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\ProfilerPass; use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\TranslatorPass; use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\LoggingTranslatorPass; -use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\AddCacheWarmerPass; -use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\AddCacheClearerPass; use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\AddExpressionLanguageProvidersPass; use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\ContainerBuilderDebugDumpPass; use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\TranslationExtractorPass; @@ -30,6 +28,8 @@ use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\TranslationDumpe use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\UnusedTagsPass; use Symfony\Component\Config\DependencyInjection\ConfigCachePass; use Symfony\Component\Console\DependencyInjection\AddConsoleCommandPass; +use Symfony\Component\HttpKernel\DependencyInjection\AddCacheClearerPass; +use Symfony\Component\HttpKernel\DependencyInjection\AddCacheWarmerPass; use Symfony\Component\HttpKernel\DependencyInjection\ControllerArgumentValueResolverPass; use Symfony\Component\HttpKernel\DependencyInjection\RegisterControllerArgumentLocatorsPass; use Symfony\Component\HttpKernel\DependencyInjection\RemoveEmptyControllerArgumentLocatorsPass; diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Compiler/AddCacheWarmerPassTest.php b/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Compiler/AddCacheWarmerPassTest.php index 3516aa9360..1f49022f8d 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Compiler/AddCacheWarmerPassTest.php +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Compiler/AddCacheWarmerPassTest.php @@ -16,6 +16,9 @@ use Symfony\Component\DependencyInjection\ContainerBuilder; use Symfony\Component\DependencyInjection\Reference; use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\AddCacheWarmerPass; +/** + * @group legacy + */ class AddCacheWarmerPassTest extends TestCase { public function testThatCacheWarmersAreProcessedInPriorityOrder() diff --git a/src/Symfony/Bundle/FrameworkBundle/composer.json b/src/Symfony/Bundle/FrameworkBundle/composer.json index 9699ae85d6..e181b55d98 100644 --- a/src/Symfony/Bundle/FrameworkBundle/composer.json +++ b/src/Symfony/Bundle/FrameworkBundle/composer.json @@ -24,7 +24,7 @@ "symfony/config": "~3.3|~4.0", "symfony/event-dispatcher": "^3.3.1|~4.0", "symfony/http-foundation": "~3.3|~4.0", - "symfony/http-kernel": "~3.3|~4.0", + "symfony/http-kernel": "~3.4|~4.0", "symfony/polyfill-mbstring": "~1.0", "symfony/filesystem": "~2.8|~3.0|~4.0", "symfony/finder": "~2.8|~3.0|~4.0", @@ -66,6 +66,7 @@ "symfony/asset": "<3.3", "symfony/console": "<3.3", "symfony/form": "<3.3", + "symfony/http-kernel": "<3.4", "symfony/property-info": "<3.3", "symfony/serializer": "<3.3", "symfony/translation": "<3.2", diff --git a/src/Symfony/Component/HttpKernel/CHANGELOG.md b/src/Symfony/Component/HttpKernel/CHANGELOG.md index 061f61d172..6c21cb722a 100644 --- a/src/Symfony/Component/HttpKernel/CHANGELOG.md +++ b/src/Symfony/Component/HttpKernel/CHANGELOG.md @@ -1,6 +1,12 @@ CHANGELOG ========= +3.4.0 +----- + + * added `AddCacheClearerPass` + * added `AddCacheWarmerPass` + 3.3.0 ----- diff --git a/src/Symfony/Component/HttpKernel/DependencyInjection/AddCacheClearerPass.php b/src/Symfony/Component/HttpKernel/DependencyInjection/AddCacheClearerPass.php new file mode 100644 index 0000000000..2689440bd7 --- /dev/null +++ b/src/Symfony/Component/HttpKernel/DependencyInjection/AddCacheClearerPass.php @@ -0,0 +1,50 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\HttpKernel\DependencyInjection; + +use Symfony\Component\DependencyInjection\ContainerBuilder; +use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; +use Symfony\Component\DependencyInjection\Reference; + +/** + * Registers the cache clearers. + * + * @author Dustin Dobervich + */ +class AddCacheClearerPass implements CompilerPassInterface +{ + private $cacheClearerId; + private $cacheClearerTag; + + public function __construct($cacheClearerId = 'cache_clearer', $cacheClearerTag = 'kernel.cache_clearer') + { + $this->cacheClearerId = $cacheClearerId; + $this->cacheClearerTag = $cacheClearerTag; + } + + /** + * {@inheritdoc} + */ + public function process(ContainerBuilder $container) + { + if (!$container->hasDefinition($this->cacheClearerId)) { + return; + } + + $clearers = array(); + foreach ($container->findTaggedServiceIds($this->cacheClearerTag, true) as $id => $attributes) { + $clearers[] = new Reference($id); + } + + $container->getDefinition($this->cacheClearerId)->replaceArgument(0, $clearers); + } +} diff --git a/src/Symfony/Component/HttpKernel/DependencyInjection/AddCacheWarmerPass.php b/src/Symfony/Component/HttpKernel/DependencyInjection/AddCacheWarmerPass.php new file mode 100644 index 0000000000..6d13e1cbcb --- /dev/null +++ b/src/Symfony/Component/HttpKernel/DependencyInjection/AddCacheWarmerPass.php @@ -0,0 +1,53 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\HttpKernel\DependencyInjection; + +use Symfony\Component\DependencyInjection\Compiler\PriorityTaggedServiceTrait; +use Symfony\Component\DependencyInjection\ContainerBuilder; +use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; + +/** + * Registers the cache warmers. + * + * @author Fabien Potencier + */ +class AddCacheWarmerPass implements CompilerPassInterface +{ + use PriorityTaggedServiceTrait; + + private $cacheWarmerId; + private $cacheWarmerTag; + + public function __construct($cacheWarmerId = 'cache_warmer', $cacheWarmerTag = 'kernel.cache_warmer') + { + $this->cacheWarmerId = $cacheWarmerId; + $this->cacheWarmerTag = $cacheWarmerTag; + } + + /** + * {@inheritdoc} + */ + public function process(ContainerBuilder $container) + { + if (!$container->hasDefinition($this->cacheWarmerId)) { + return; + } + + $warmers = $this->findAndSortTaggedServices($this->cacheWarmerTag, $container); + + if (empty($warmers)) { + return; + } + + $container->getDefinition($this->cacheWarmerId)->replaceArgument(0, $warmers); + } +} diff --git a/src/Symfony/Component/HttpKernel/Tests/DependencyInjection/AddCacheClearerPassTest.php b/src/Symfony/Component/HttpKernel/Tests/DependencyInjection/AddCacheClearerPassTest.php new file mode 100644 index 0000000000..3bdb84ad10 --- /dev/null +++ b/src/Symfony/Component/HttpKernel/Tests/DependencyInjection/AddCacheClearerPassTest.php @@ -0,0 +1,53 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\HttpKernel\Tests\DependencyInjection; + +use PHPUnit\Framework\TestCase; +use Symfony\Component\DependencyInjection\ContainerBuilder; +use Symfony\Component\DependencyInjection\Reference; +use Symfony\Component\HttpKernel\DependencyInjection\AddCacheClearerPass; + +class AddCacheClearerPassTest extends TestCase +{ + public function testThatCacheClearer() + { + $container = new ContainerBuilder(); + + $definition = $container->register('cache_clearer')->addArgument(null); + $container->register('my_cache_clearer_service1')->addTag('kernel.cache_clearer'); + + $addCacheWarmerPass = new AddCacheClearerPass(); + $addCacheWarmerPass->process($container); + + $expected = array( + new Reference('my_cache_clearer_service1'), + ); + $this->assertEquals($expected, $definition->getArgument(0)); + } + + public function testThatCompilerPassIsIgnoredIfThereIsNoCacheClearerDefinition() + { + $definition = $this->getMockBuilder('Symfony\Component\DependencyInjection\Definition')->getMock(); + $container = $this->getMockBuilder('Symfony\Component\DependencyInjection\ContainerBuilder')->setMethods(array('hasDefinition', 'findTaggedServiceIds', 'getDefinition'))->getMock(); + + $container->expects($this->never())->method('findTaggedServiceIds'); + $container->expects($this->never())->method('getDefinition'); + $container->expects($this->atLeastOnce()) + ->method('hasDefinition') + ->with('cache_clearer') + ->will($this->returnValue(false)); + $definition->expects($this->never())->method('replaceArgument'); + + $addCacheWarmerPass = new AddCacheClearerPass(); + $addCacheWarmerPass->process($container); + } +} diff --git a/src/Symfony/Component/HttpKernel/Tests/DependencyInjection/AddCacheWarmerPassTest.php b/src/Symfony/Component/HttpKernel/Tests/DependencyInjection/AddCacheWarmerPassTest.php new file mode 100644 index 0000000000..3f7bc6e65e --- /dev/null +++ b/src/Symfony/Component/HttpKernel/Tests/DependencyInjection/AddCacheWarmerPassTest.php @@ -0,0 +1,77 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\HttpKernel\Tests\DependencyInjection; + +use PHPUnit\Framework\TestCase; +use Symfony\Component\DependencyInjection\ContainerBuilder; +use Symfony\Component\DependencyInjection\Reference; +use Symfony\Component\HttpKernel\DependencyInjection\AddCacheWarmerPass; + +class AddCacheWarmerPassTest extends TestCase +{ + public function testThatCacheWarmersAreProcessedInPriorityOrder() + { + $container = new ContainerBuilder(); + + $definition = $container->register('cache_warmer')->addArgument(null); + $container->register('my_cache_warmer_service1')->addTag('kernel.cache_warmer', array('priority' => 100)); + $container->register('my_cache_warmer_service2')->addTag('kernel.cache_warmer', array('priority' => 200)); + $container->register('my_cache_warmer_service3')->addTag('kernel.cache_warmer'); + + $addCacheWarmerPass = new AddCacheWarmerPass(); + $addCacheWarmerPass->process($container); + + $expected = array( + new Reference('my_cache_warmer_service2'), + new Reference('my_cache_warmer_service1'), + new Reference('my_cache_warmer_service3'), + ); + $this->assertEquals($expected, $definition->getArgument(0)); + } + + public function testThatCompilerPassIsIgnoredIfThereIsNoCacheWarmerDefinition() + { + $definition = $this->getMockBuilder('Symfony\Component\DependencyInjection\Definition')->getMock(); + $container = $this->getMockBuilder('Symfony\Component\DependencyInjection\ContainerBuilder')->setMethods(array('hasDefinition', 'findTaggedServiceIds', 'getDefinition'))->getMock(); + + $container->expects($this->never())->method('findTaggedServiceIds'); + $container->expects($this->never())->method('getDefinition'); + $container->expects($this->atLeastOnce()) + ->method('hasDefinition') + ->with('cache_warmer') + ->will($this->returnValue(false)); + $definition->expects($this->never())->method('replaceArgument'); + + $addCacheWarmerPass = new AddCacheWarmerPass(); + $addCacheWarmerPass->process($container); + } + + public function testThatCacheWarmersMightBeNotDefined() + { + $definition = $this->getMockBuilder('Symfony\Component\DependencyInjection\Definition')->getMock(); + $container = $this->getMockBuilder('Symfony\Component\DependencyInjection\ContainerBuilder')->setMethods(array('hasDefinition', 'findTaggedServiceIds', 'getDefinition'))->getMock(); + + $container->expects($this->atLeastOnce()) + ->method('findTaggedServiceIds') + ->will($this->returnValue(array())); + $container->expects($this->never())->method('getDefinition'); + $container->expects($this->atLeastOnce()) + ->method('hasDefinition') + ->with('cache_warmer') + ->will($this->returnValue(true)); + + $definition->expects($this->never())->method('replaceArgument'); + + $addCacheWarmerPass = new AddCacheWarmerPass(); + $addCacheWarmerPass->process($container); + } +}