diff --git a/UPGRADE-3.3.md b/UPGRADE-3.3.md index afd31e75ec..7af68ab384 100644 --- a/UPGRADE-3.3.md +++ b/UPGRADE-3.3.md @@ -35,6 +35,10 @@ FrameworkBundle * The `Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\AddConsoleCommandPass` has been deprecated. Use `Symfony\Component\Console\DependencyInjection\AddConsoleCommandPass` instead. + * The `Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\SerializerPass` class has been + deprecated and will be removed in 4.0. + Use the `Symfony\Component\Serializer\DependencyInjection\SerializerPass` class instead. + HttpKernel ----------- diff --git a/UPGRADE-4.0.md b/UPGRADE-4.0.md index d88fc607d7..8fb7bef8cd 100644 --- a/UPGRADE-4.0.md +++ b/UPGRADE-4.0.md @@ -156,6 +156,9 @@ FrameworkBundle * The `Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\AddConsoleCommandPass` has been removed. Use `Symfony\Component\Console\DependencyInjection\AddConsoleCommandPass` instead. + * The `Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\SerializerPass` class has been removed. + Use the `Symfony\Component\Serializer\DependencyInjection\SerializerPass` class instead. + SecurityBundle -------------- diff --git a/src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md b/src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md index 735f116abe..6dad6e9566 100644 --- a/src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md +++ b/src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md @@ -13,6 +13,7 @@ CHANGELOG * Added `GlobalVariables::getToken()` * Deprecated `Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\AddConsoleCommandPass`. Use `Symfony\Component\Console\DependencyInjection\AddConsoleCommandPass` instead. * Added configurable paths for validation files + * Deprecated `SerializerPass`, use `Symfony\Component\Serializer\DependencyInjection\SerializerPass` instead. 3.2.0 ----- diff --git a/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Compiler/SerializerPass.php b/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Compiler/SerializerPass.php index d3d3019998..d30e8eba43 100644 --- a/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Compiler/SerializerPass.php +++ b/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Compiler/SerializerPass.php @@ -11,40 +11,18 @@ namespace Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler; -use Symfony\Component\DependencyInjection\Compiler\PriorityTaggedServiceTrait; -use Symfony\Component\DependencyInjection\ContainerBuilder; -use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; -use Symfony\Component\DependencyInjection\Exception\RuntimeException; +@trigger_error(sprintf('The %s class is deprecated since version 3.3 and will be removed in 4.0. Use Symfony\Component\Serializer\DependencyInjection\SerializerPass instead.', SerializerPass::class), E_USER_DEPRECATED); + +use Symfony\Component\Serializer\DependencyInjection\SerializerPass as BaseSerializerPass; /** * Adds all services with the tags "serializer.encoder" and "serializer.normalizer" as * encoders and normalizers to the Serializer service. * + * @deprecated since version 3.3, to be removed in 4.0. Use {@link BaseSerializerPass} instead. + * * @author Javier Lopez */ -class SerializerPass implements CompilerPassInterface +class SerializerPass extends BaseSerializerPass { - use PriorityTaggedServiceTrait; - - public function process(ContainerBuilder $container) - { - if (!$container->hasDefinition('serializer')) { - return; - } - - // Looks for all the services tagged "serializer.normalizer" and adds them to the Serializer service - $normalizers = $this->findAndSortTaggedServices('serializer.normalizer', $container); - - if (empty($normalizers)) { - throw new RuntimeException('You must tag at least one service as "serializer.normalizer" to use the Serializer service'); - } - $container->getDefinition('serializer')->replaceArgument(0, $normalizers); - - // Looks for all the services tagged "serializer.encoders" and adds them to the Serializer service - $encoders = $this->findAndSortTaggedServices('serializer.encoder', $container); - if (empty($encoders)) { - throw new RuntimeException('You must tag at least one service as "serializer.encoder" to use the Serializer service'); - } - $container->getDefinition('serializer')->replaceArgument(1, $encoders); - } } diff --git a/src/Symfony/Bundle/FrameworkBundle/FrameworkBundle.php b/src/Symfony/Bundle/FrameworkBundle/FrameworkBundle.php index 535fa0a5b7..2a8a3ad484 100644 --- a/src/Symfony/Bundle/FrameworkBundle/FrameworkBundle.php +++ b/src/Symfony/Bundle/FrameworkBundle/FrameworkBundle.php @@ -32,11 +32,11 @@ use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\ContainerBuilder use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\CompilerDebugDumpPass; use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\TranslationExtractorPass; use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\TranslationDumperPass; -use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\SerializerPass; use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\UnusedTagsPass; use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\ConfigCachePass; use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\ValidateWorkflowsPass; use Symfony\Component\Console\DependencyInjection\AddConsoleCommandPass; +use Symfony\Component\Serializer\DependencyInjection\SerializerPass; use Symfony\Component\Debug\ErrorHandler; use Symfony\Component\DependencyInjection\ContainerBuilder; use Symfony\Component\DependencyInjection\Compiler\PassConfig; @@ -93,7 +93,9 @@ class FrameworkBundle extends Bundle $container->addCompilerPass(new TranslationExtractorPass()); $container->addCompilerPass(new TranslationDumperPass()); $container->addCompilerPass(new FragmentRendererPass(), PassConfig::TYPE_AFTER_REMOVING); - $container->addCompilerPass(new SerializerPass()); + if (class_exists(SerializerPass::class)) { + $container->addCompilerPass(new SerializerPass()); + } $container->addCompilerPass(new PropertyInfoPass()); $container->addCompilerPass(new ControllerArgumentValueResolverPass()); $container->addCompilerPass(new CachePoolPass(), PassConfig::TYPE_BEFORE_OPTIMIZATION, 32); diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Compiler/SerializerPassTest.php b/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Compiler/SerializerPassTest.php index 16a724b460..26e6753fba 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Compiler/SerializerPassTest.php +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Compiler/SerializerPassTest.php @@ -17,6 +17,8 @@ use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\SerializerPass; /** * Tests for the SerializerPass class. * + * @group legacy + * * @author Javier Lopez */ class SerializerPassTest extends \PHPUnit_Framework_TestCase diff --git a/src/Symfony/Bundle/FrameworkBundle/composer.json b/src/Symfony/Bundle/FrameworkBundle/composer.json index add66921d3..f31806e997 100644 --- a/src/Symfony/Bundle/FrameworkBundle/composer.json +++ b/src/Symfony/Bundle/FrameworkBundle/composer.json @@ -44,7 +44,7 @@ "symfony/process": "~2.8|~3.0", "symfony/security-core": "~3.2", "symfony/security-csrf": "~2.8|~3.0", - "symfony/serializer": "~2.8|~3.0", + "symfony/serializer": "~3.3", "symfony/translation": "~2.8|~3.0", "symfony/templating": "~2.8|~3.0", "symfony/validator": "~3.2", @@ -57,7 +57,8 @@ "conflict": { "phpdocumentor/reflection-docblock": "<3.0", "phpdocumentor/type-resolver": "<0.2.0", - "symfony/console": "<3.3" + "symfony/console": "<3.3", + "symfony/serializer": "<3.3" }, "suggest": { "ext-apcu": "For best performance of the system caches", diff --git a/src/Symfony/Component/Serializer/CHANGELOG.md b/src/Symfony/Component/Serializer/CHANGELOG.md index de24d9ed74..01c16d00cf 100644 --- a/src/Symfony/Component/Serializer/CHANGELOG.md +++ b/src/Symfony/Component/Serializer/CHANGELOG.md @@ -1,6 +1,11 @@ CHANGELOG ========= +3.3.0 +----- + + * added `SerializerPass` + 3.1.0 ----- diff --git a/src/Symfony/Component/Serializer/DependencyInjection/SerializerPass.php b/src/Symfony/Component/Serializer/DependencyInjection/SerializerPass.php new file mode 100644 index 0000000000..e6202b8bf9 --- /dev/null +++ b/src/Symfony/Component/Serializer/DependencyInjection/SerializerPass.php @@ -0,0 +1,60 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\Serializer\DependencyInjection; + +use Symfony\Component\DependencyInjection\Compiler\PriorityTaggedServiceTrait; +use Symfony\Component\DependencyInjection\ContainerBuilder; +use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; +use Symfony\Component\DependencyInjection\Exception\RuntimeException; + +/** + * Adds all services with the tags "serializer.encoder" and "serializer.normalizer" as + * encoders and normalizers to the "serializer" service. + * + * @author Javier Lopez + * @author Robin Chalas + */ +class SerializerPass implements CompilerPassInterface +{ + use PriorityTaggedServiceTrait; + + private $serializerService; + private $normalizerTag; + private $encoderTag; + + public function __construct($serializerService = 'serializer', $normalizerTag = 'serializer.normalizer', $encoderTag = 'serializer.encoder') + { + $this->serializerService = $serializerService; + $this->normalizerTag = $normalizerTag; + $this->encoderTag = $encoderTag; + } + + public function process(ContainerBuilder $container) + { + if (!$container->hasDefinition($this->serializerService)) { + return; + } + + if (!$normalizers = $this->findAndSortTaggedServices($this->normalizerTag, $container)) { + throw new RuntimeException(sprintf('You must tag at least one service as "%s" to use the "%s" service.', $this->normalizerTag, $this->serializerService)); + } + + $serializerDefinition = $container->getDefinition($this->serializerService); + $serializerDefinition->replaceArgument(0, $normalizers); + + if (!$encoders = $this->findAndSortTaggedServices($this->encoderTag, $container)) { + throw new RuntimeException(sprintf('You must tag at least one service as "%s" to use the "%s" service.', $this->encoderTag, $this->serializerService)); + } + + $serializerDefinition->replaceArgument(1, $encoders); + } +} diff --git a/src/Symfony/Component/Serializer/Tests/DependencyInjection/SerializerPassTest.php b/src/Symfony/Component/Serializer/Tests/DependencyInjection/SerializerPassTest.php new file mode 100644 index 0000000000..761d401fa3 --- /dev/null +++ b/src/Symfony/Component/Serializer/Tests/DependencyInjection/SerializerPassTest.php @@ -0,0 +1,103 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\Serializer\Tests\DependencyInjection; + +use Symfony\Component\DependencyInjection\Reference; +use Symfony\Component\Serializer\DependencyInjection\SerializerPass; + +/** + * Tests for the SerializerPass class. + * + * @author Javier Lopez + */ +class SerializerPassTest extends \PHPUnit_Framework_TestCase +{ + public function testThrowExceptionWhenNoNormalizers() + { + $container = $this->getMockBuilder('Symfony\Component\DependencyInjection\ContainerBuilder')->setMethods(array('hasDefinition', 'findTaggedServiceIds'))->getMock(); + + $container->expects($this->once()) + ->method('hasDefinition') + ->with('serializer') + ->will($this->returnValue(true)); + + $container->expects($this->once()) + ->method('findTaggedServiceIds') + ->with('serializer.normalizer') + ->will($this->returnValue(array())); + + $this->setExpectedException('RuntimeException'); + + $serializerPass = new SerializerPass(); + $serializerPass->process($container); + } + + public function testThrowExceptionWhenNoEncoders() + { + $definition = $this->getMockBuilder('Symfony\Component\DependencyInjection\Definition')->getMock(); + $container = $this->getMockBuilder('Symfony\Component\DependencyInjection\ContainerBuilder')->setMethods(array('hasDefinition', 'findTaggedServiceIds', 'getDefinition'))->getMock(); + + $container->expects($this->once()) + ->method('hasDefinition') + ->with('serializer') + ->will($this->returnValue(true)); + + $container->expects($this->any()) + ->method('findTaggedServiceIds') + ->will($this->onConsecutiveCalls( + array('n' => array('serializer.normalizer')), + array() + )); + + $container->expects($this->once()) + ->method('getDefinition') + ->will($this->returnValue($definition)); + + $this->setExpectedException('RuntimeException'); + + $serializerPass = new SerializerPass(); + $serializerPass->process($container); + } + + public function testServicesAreOrderedAccordingToPriority() + { + $services = array( + 'n3' => array('tag' => array()), + 'n1' => array('tag' => array('priority' => 200)), + 'n2' => array('tag' => array('priority' => 100)), + ); + + $expected = array( + new Reference('n1'), + new Reference('n2'), + new Reference('n3'), + ); + + $container = $this->getMockBuilder('Symfony\Component\DependencyInjection\ContainerBuilder')->setMethods(array('findTaggedServiceIds'))->getMock(); + + $container->expects($this->any()) + ->method('findTaggedServiceIds') + ->will($this->returnValue($services)); + + $serializerPass = new SerializerPass(); + + $method = new \ReflectionMethod( + SerializerPass::class, + 'findAndSortTaggedServices' + ); + $method->setAccessible(true); + + $actual = $method->invoke($serializerPass, 'tag', $container); + + $this->assertEquals($expected, $actual); + } +} diff --git a/src/Symfony/Component/Serializer/composer.json b/src/Symfony/Component/Serializer/composer.json index 63569fdd04..e4259acb5b 100644 --- a/src/Symfony/Component/Serializer/composer.json +++ b/src/Symfony/Component/Serializer/composer.json @@ -26,6 +26,7 @@ "symfony/cache": "~3.1", "symfony/property-info": "~3.1", "doctrine/annotations": "~1.0", + "symfony/dependency-injection": "~3.2", "doctrine/cache": "~1.0", "phpdocumentor/reflection-docblock": "~3.0" },