Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit a328e75

Browse files
committed
[FrameworkBundle][Serializer] Move SerializerPass to the Serializer
1 parent 42c3d4f commit a328e75

File tree

11 files changed

+183
-33
lines changed

11 files changed

+183
-33
lines changed

UPGRADE-3.3.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@ FrameworkBundle
3636

3737
* The `Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\AddConsoleCommandPass` has been deprecated. Use `Symfony\Component\Console\DependencyInjection\AddConsoleCommandPass` instead.
3838

39+
* The `Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\SerializerPass` class has been
40+
deprecated and will be removed in 4.0.
41+
Use the `Symfony\Component\Serializer\DependencyInjection\SerializerPass` class instead.
42+
3943
HttpKernel
4044
-----------
4145

UPGRADE-4.0.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,9 @@ FrameworkBundle
157157

158158
* The `Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\AddConsoleCommandPass` has been removed. Use `Symfony\Component\Console\DependencyInjection\AddConsoleCommandPass` instead.
159159

160+
* The `Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\SerializerPass` class has been removed.
161+
Use the `Symfony\Component\Serializer\DependencyInjection\SerializerPass` class instead.
162+
160163
SecurityBundle
161164
--------------
162165

src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ CHANGELOG
1212
is disabled.
1313
* Added `GlobalVariables::getToken()`
1414
* Deprecated `Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\AddConsoleCommandPass`. Use `Symfony\Component\Console\DependencyInjection\AddConsoleCommandPass` instead.
15+
* Deprecated `SerializerPass`, use `Symfony\Component\Serializer\DependencyInjection\SerializerPass` instead.
1516

1617
3.2.0
1718
-----

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

Lines changed: 6 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -11,40 +11,18 @@
1111

1212
namespace Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler;
1313

14-
use Symfony\Component\DependencyInjection\Compiler\PriorityTaggedServiceTrait;
15-
use Symfony\Component\DependencyInjection\ContainerBuilder;
16-
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
17-
use Symfony\Component\DependencyInjection\Exception\RuntimeException;
14+
@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);
15+
16+
use Symfony\Component\Serializer\DependencyInjection\SerializerPass as BaseSerializerPass;
1817

1918
/**
2019
* Adds all services with the tags "serializer.encoder" and "serializer.normalizer" as
2120
* encoders and normalizers to the Serializer service.
2221
*
22+
* @deprecated since version 3.3, to be removed in 4.0. Use {@link BaseSerializerPass} instead.
23+
*
2324
* @author Javier Lopez <[email protected]>
2425
*/
25-
class SerializerPass implements CompilerPassInterface
26+
class SerializerPass extends BaseSerializerPass
2627
{
27-
use PriorityTaggedServiceTrait;
28-
29-
public function process(ContainerBuilder $container)
30-
{
31-
if (!$container->hasDefinition('serializer')) {
32-
return;
33-
}
34-
35-
// Looks for all the services tagged "serializer.normalizer" and adds them to the Serializer service
36-
$normalizers = $this->findAndSortTaggedServices('serializer.normalizer', $container);
37-
38-
if (empty($normalizers)) {
39-
throw new RuntimeException('You must tag at least one service as "serializer.normalizer" to use the Serializer service');
40-
}
41-
$container->getDefinition('serializer')->replaceArgument(0, $normalizers);
42-
43-
// Looks for all the services tagged "serializer.encoders" and adds them to the Serializer service
44-
$encoders = $this->findAndSortTaggedServices('serializer.encoder', $container);
45-
if (empty($encoders)) {
46-
throw new RuntimeException('You must tag at least one service as "serializer.encoder" to use the Serializer service');
47-
}
48-
$container->getDefinition('serializer')->replaceArgument(1, $encoders);
49-
}
5028
}

src/Symfony/Bundle/FrameworkBundle/FrameworkBundle.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,11 @@
3131
use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\CompilerDebugDumpPass;
3232
use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\TranslationExtractorPass;
3333
use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\TranslationDumperPass;
34-
use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\SerializerPass;
3534
use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\UnusedTagsPass;
3635
use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\ConfigCachePass;
3736
use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\ValidateWorkflowsPass;
3837
use Symfony\Component\Console\DependencyInjection\AddConsoleCommandPass;
38+
use Symfony\Component\Serializer\DependencyInjection\SerializerPass;
3939
use Symfony\Component\Debug\ErrorHandler;
4040
use Symfony\Component\DependencyInjection\ContainerBuilder;
4141
use Symfony\Component\DependencyInjection\Compiler\PassConfig;
@@ -92,7 +92,9 @@ public function build(ContainerBuilder $container)
9292
$container->addCompilerPass(new TranslationExtractorPass());
9393
$container->addCompilerPass(new TranslationDumperPass());
9494
$container->addCompilerPass(new FragmentRendererPass(), PassConfig::TYPE_AFTER_REMOVING);
95-
$container->addCompilerPass(new SerializerPass());
95+
if (class_exists(SerializerPass::class)) {
96+
$container->addCompilerPass(new SerializerPass());
97+
}
9698
$container->addCompilerPass(new PropertyInfoPass());
9799
$container->addCompilerPass(new ControllerArgumentValueResolverPass());
98100
$container->addCompilerPass(new CachePoolPass(), PassConfig::TYPE_BEFORE_OPTIMIZATION, 32);

src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Compiler/SerializerPassTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
/**
1818
* Tests for the SerializerPass class.
1919
*
20+
* @group legacy
21+
*
2022
* @author Javier Lopez <[email protected]>
2123
*/
2224
class SerializerPassTest extends \PHPUnit_Framework_TestCase

src/Symfony/Bundle/FrameworkBundle/composer.json

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,20 +44,21 @@
4444
"symfony/process": "~2.8|~3.0",
4545
"symfony/security-core": "~3.2",
4646
"symfony/security-csrf": "~2.8|~3.0",
47-
"symfony/serializer": "~2.8|~3.0",
47+
"symfony/serializer": "~3.3",
4848
"symfony/translation": "~2.8|~3.0",
4949
"symfony/templating": "~2.8|~3.0",
5050
"symfony/validator": "~3.2",
5151
"symfony/yaml": "~3.2",
52-
"symfony/property-info": "~2.8|~3.0",
52+
"symfony/property-info": "~3.1",
5353
"doctrine/annotations": "~1.0",
5454
"phpdocumentor/reflection-docblock": "^3.0",
5555
"twig/twig": "~1.26|~2.0"
5656
},
5757
"conflict": {
5858
"phpdocumentor/reflection-docblock": "<3.0",
5959
"phpdocumentor/type-resolver": "<0.2.0",
60-
"symfony/console": "<3.3"
60+
"symfony/console": "<3.3",
61+
"symfony/serializer": "<3.3"
6162
},
6263
"suggest": {
6364
"ext-apcu": "For best performance of the system caches",

src/Symfony/Component/Serializer/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
CHANGELOG
22
=========
33

4+
3.3.0
5+
-----
6+
7+
* added `SerializerPass`
8+
49
3.1.0
510
-----
611

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
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\Serializer\DependencyInjection;
13+
14+
use Symfony\Component\DependencyInjection\Compiler\PriorityTaggedServiceTrait;
15+
use Symfony\Component\DependencyInjection\ContainerBuilder;
16+
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
17+
use Symfony\Component\DependencyInjection\Exception\RuntimeException;
18+
19+
/**
20+
* Adds all services with the tags "serializer.encoder" and "serializer.normalizer" as
21+
* encoders and normalizers to the "serializer" service.
22+
*
23+
* @author Javier Lopez <[email protected]>
24+
*/
25+
class SerializerPass implements CompilerPassInterface
26+
{
27+
use PriorityTaggedServiceTrait;
28+
29+
public function process(ContainerBuilder $container)
30+
{
31+
if (!$container->hasDefinition('serializer')) {
32+
return;
33+
}
34+
35+
// Looks for all the services tagged "serializer.normalizer" and adds them to the Serializer service
36+
$normalizers = $this->findAndSortTaggedServices('serializer.normalizer', $container);
37+
38+
if (empty($normalizers)) {
39+
throw new RuntimeException('You must tag at least one service as "serializer.normalizer" to use the Serializer service');
40+
}
41+
$container->getDefinition('serializer')->replaceArgument(0, $normalizers);
42+
43+
// Looks for all the services tagged "serializer.encoders" and adds them to the Serializer service
44+
$encoders = $this->findAndSortTaggedServices('serializer.encoder', $container);
45+
if (empty($encoders)) {
46+
throw new RuntimeException('You must tag at least one service as "serializer.encoder" to use the Serializer service');
47+
}
48+
$container->getDefinition('serializer')->replaceArgument(1, $encoders);
49+
}
50+
}
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
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\Serializer\Tests\DependencyInjection;
13+
14+
use Symfony\Component\DependencyInjection\Reference;
15+
use Symfony\Component\Serializer\DependencyInjection\SerializerPass;
16+
17+
/**
18+
* Tests for the SerializerPass class.
19+
*
20+
* @author Javier Lopez <[email protected]>
21+
*/
22+
class SerializerPassTest extends \PHPUnit_Framework_TestCase
23+
{
24+
public function testThrowExceptionWhenNoNormalizers()
25+
{
26+
$container = $this->getMockBuilder('Symfony\Component\DependencyInjection\ContainerBuilder')->setMethods(array('hasDefinition', 'findTaggedServiceIds'))->getMock();
27+
28+
$container->expects($this->once())
29+
->method('hasDefinition')
30+
->with('serializer')
31+
->will($this->returnValue(true));
32+
33+
$container->expects($this->once())
34+
->method('findTaggedServiceIds')
35+
->with('serializer.normalizer')
36+
->will($this->returnValue(array()));
37+
38+
$this->setExpectedException('RuntimeException');
39+
40+
$serializerPass = new SerializerPass();
41+
$serializerPass->process($container);
42+
}
43+
44+
public function testThrowExceptionWhenNoEncoders()
45+
{
46+
$definition = $this->getMockBuilder('Symfony\Component\DependencyInjection\Definition')->getMock();
47+
$container = $this->getMockBuilder('Symfony\Component\DependencyInjection\ContainerBuilder')->setMethods(array('hasDefinition', 'findTaggedServiceIds', 'getDefinition'))->getMock();
48+
49+
$container->expects($this->once())
50+
->method('hasDefinition')
51+
->with('serializer')
52+
->will($this->returnValue(true));
53+
54+
$container->expects($this->any())
55+
->method('findTaggedServiceIds')
56+
->will($this->onConsecutiveCalls(
57+
array('n' => array('serializer.normalizer')),
58+
array()
59+
));
60+
61+
$container->expects($this->once())
62+
->method('getDefinition')
63+
->will($this->returnValue($definition));
64+
65+
$this->setExpectedException('RuntimeException');
66+
67+
$serializerPass = new SerializerPass();
68+
$serializerPass->process($container);
69+
}
70+
71+
public function testServicesAreOrderedAccordingToPriority()
72+
{
73+
$services = array(
74+
'n3' => array('tag' => array()),
75+
'n1' => array('tag' => array('priority' => 200)),
76+
'n2' => array('tag' => array('priority' => 100)),
77+
);
78+
79+
$expected = array(
80+
new Reference('n1'),
81+
new Reference('n2'),
82+
new Reference('n3'),
83+
);
84+
85+
$container = $this->getMockBuilder('Symfony\Component\DependencyInjection\ContainerBuilder')->setMethods(array('findTaggedServiceIds'))->getMock();
86+
87+
$container->expects($this->any())
88+
->method('findTaggedServiceIds')
89+
->will($this->returnValue($services));
90+
91+
$serializerPass = new SerializerPass();
92+
93+
$method = new \ReflectionMethod(
94+
SerializerPass::class,
95+
'findAndSortTaggedServices'
96+
);
97+
$method->setAccessible(true);
98+
99+
$actual = $method->invoke($serializerPass, 'tag', $container);
100+
101+
$this->assertEquals($expected, $actual);
102+
}
103+
}

src/Symfony/Component/Serializer/composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
"symfony/cache": "~3.1",
2727
"symfony/property-info": "~3.1",
2828
"doctrine/annotations": "~1.0",
29+
"symfony/dependency-injection": "~3.2",
2930
"doctrine/cache": "~1.0",
3031
"phpdocumentor/reflection-docblock": "~3.0"
3132
},

0 commit comments

Comments
 (0)