feature #21293 [FrameworkBundle][Serializer] Move SerializerPass to the Serializer (chalasr)
This PR was merged into the 3.3-dev branch.
Discussion
----------
[FrameworkBundle][Serializer] Move SerializerPass to the Serializer
| Q | A
| ------------- | ---
| Branch? | master
| Bug fix? | no
| New feature? | yes
| BC breaks? | no
| Deprecations? | yes
| Tests pass? | yes
| Fixed tickets | n/a
| License | MIT
| Doc PR | n/a
Part of #21284
Commits
-------
95cf5084c0
[FrameworkBundle][Serializer] Move SerializerPass to the Serializer
This commit is contained in:
commit
846d24ae36
@ -57,6 +57,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
|
||||
-----------
|
||||
|
||||
|
@ -176,6 +176,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
|
||||
--------------
|
||||
|
||||
|
@ -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
|
||||
-----
|
||||
|
@ -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 <f12loalf@gmail.com>
|
||||
*/
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
@ -33,11 +33,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;
|
||||
@ -95,7 +95,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);
|
||||
|
@ -17,6 +17,8 @@ use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\SerializerPass;
|
||||
/**
|
||||
* Tests for the SerializerPass class.
|
||||
*
|
||||
* @group legacy
|
||||
*
|
||||
* @author Javier Lopez <f12loalf@gmail.com>
|
||||
*/
|
||||
class SerializerPassTest extends \PHPUnit_Framework_TestCase
|
||||
|
@ -46,7 +46,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",
|
||||
@ -60,7 +60,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",
|
||||
|
@ -1,6 +1,11 @@
|
||||
CHANGELOG
|
||||
=========
|
||||
|
||||
3.3.0
|
||||
-----
|
||||
|
||||
* added `SerializerPass`
|
||||
|
||||
3.1.0
|
||||
-----
|
||||
|
||||
|
@ -0,0 +1,60 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Symfony package.
|
||||
*
|
||||
* (c) Fabien Potencier <fabien@symfony.com>
|
||||
*
|
||||
* 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 <f12loalf@gmail.com>
|
||||
* @author Robin Chalas <robin.chalas@gmail.com>
|
||||
*/
|
||||
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);
|
||||
}
|
||||
}
|
@ -0,0 +1,103 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Symfony package.
|
||||
*
|
||||
* (c) Fabien Potencier <fabien@symfony.com>
|
||||
*
|
||||
* 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 <f12loalf@gmail.com>
|
||||
*/
|
||||
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);
|
||||
}
|
||||
}
|
@ -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"
|
||||
},
|
||||
|
Reference in New Issue
Block a user