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

Skip to content

Commit ee29ffa

Browse files
committed
add class for serialization context
1 parent a611a10 commit ee29ffa

File tree

8 files changed

+86
-46
lines changed

8 files changed

+86
-46
lines changed

src/Symfony/Bundle/FrameworkBundle/Resources/config/schema/symfony-1.0.xsd

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@
230230
<xsd:complexType name="serializer">
231231
<xsd:choice minOccurs="0" maxOccurs="unbounded">
232232
<xsd:element name="mapping" type="file_mapping" />
233-
<xsd:element name="default-context" type="metadata" minOccurs="0" maxOccurs="1" />
233+
<xsd:element name="default-context" type="serializer_default_context" />
234234
</xsd:choice>
235235
<xsd:attribute name="enabled" type="xsd:boolean" />
236236
<xsd:attribute name="enable-annotations" type="xsd:boolean" />
@@ -243,6 +243,10 @@
243243
<xsd:attribute name="enabled" type="xsd:boolean" />
244244
</xsd:complexType>
245245

246+
<xsd:complexType name="serializer_default_context">
247+
<xsd:attribute name="enable_max_depth" type="xsd:boolean"/>
248+
</xsd:complexType>
249+
246250
<xsd:complexType name="cache">
247251
<xsd:sequence>
248252
<xsd:element name="app" type="xsd:string" minOccurs="0" maxOccurs="1" />

src/Symfony/Bundle/FrameworkBundle/Resources/config/serializer.xml

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,13 @@
3434

3535
<!-- Normalizer -->
3636
<service id="serializer.normalizer.constraint_violation_list" class="Symfony\Component\Serializer\Normalizer\ConstraintViolationListNormalizer">
37-
<argument>%serializer.default_context%</argument>
37+
<argument type="service" id="serializer_context" />
3838
<!-- Run before serializer.normalizer.object -->
3939
<tag name="serializer.normalizer" priority="-915" />
4040
</service>
4141

4242
<service id="serializer.normalizer.dateinterval" class="Symfony\Component\Serializer\Normalizer\DateIntervalNormalizer">
43-
<argument>%serializer.default_context%</argument>
43+
<argument type="service" id="serializer_context" />
4444
<!-- Run before serializer.normalizer.object -->
4545
<tag name="serializer.normalizer" priority="-915" />
4646
</service>
@@ -52,15 +52,15 @@
5252
</service>
5353

5454
<service id="serializer.normalizer.datetime" class="Symfony\Component\Serializer\Normalizer\DateTimeNormalizer">
55-
<argument>%serializer.default_context%</argument>
55+
<argument type="service" id="serializer_context" />
5656
<!-- Run before serializer.normalizer.object -->
5757
<tag name="serializer.normalizer" priority="-910" />
5858
</service>
5959

6060
<service id="serializer.normalizer.json_serializable" class="Symfony\Component\Serializer\Normalizer\JsonSerializableNormalizer">
6161
<argument>null</argument>
6262
<argument>null</argument>
63-
<argument>%serializer.default_context%</argument>
63+
<argument type="service" id="serializer_context" />
6464
<!-- Run before serializer.normalizer.object -->
6565
<tag name="serializer.normalizer" priority="-900" />
6666
</service>
@@ -78,7 +78,7 @@
7878
<argument type="service" id="property_info" on-invalid="ignore" />
7979
<argument type="service" id="serializer.mapping.class_discriminator_resolver" on-invalid="ignore" />
8080
<argument>null</argument>
81-
<argument>%serializer.default_context%</argument>
81+
<argument type="service" id="serializer_context" />
8282

8383
<!-- Run after all custom normalizers -->
8484
<tag name="serializer.normalizer" priority="-1000" />
@@ -123,7 +123,7 @@
123123

124124
<!-- Encoders -->
125125
<service id="serializer.encoder.xml" class="Symfony\Component\Serializer\Encoder\XmlEncoder">
126-
<argument>%serializer.default_context%</argument>
126+
<argument type="service" id="serializer_context" />
127127
<tag name="serializer.encoder" />
128128
</service>
129129

@@ -134,12 +134,12 @@
134134
<service id="serializer.encoder.yaml" class="Symfony\Component\Serializer\Encoder\YamlEncoder">
135135
<argument>null</argument>
136136
<argument>null</argument>
137-
<argument>%serializer.default_context%</argument>
137+
<argument type="service" id="serializer_context" />
138138
<tag name="serializer.encoder" />
139139
</service>
140140

141141
<service id="serializer.encoder.csv" class="Symfony\Component\Serializer\Encoder\CsvEncoder">
142-
<argument>%serializer.default_context%</argument>
142+
<argument type="service" id="serializer_context" />
143143
<tag name="serializer.encoder" />
144144
</service>
145145

@@ -156,5 +156,9 @@
156156

157157
<tag name="property_info.list_extractor" priority="-999" />
158158
</service>
159+
160+
<service id="serializer_default_context" class="Symfony\Component\Serializer\DefaultContext" alias="serializer_context">
161+
<argument>%serializer.default_context%</argument>
162+
</service>
159163
</services>
160164
</container>
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
namespace Symfony\Component\Serializer\Context;
4+
5+
/*
6+
* This file is part of the Symfony package.
7+
*
8+
* (c) Fabien Potencier <[email protected]>
9+
*
10+
* For the full copyright and license information, please view the LICENSE
11+
* file that was distributed with this source code.
12+
*/
13+
interface ContextInterface
14+
{
15+
public function toArray(): array;
16+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
namespace Symfony\Component\Serializer\Context;
4+
5+
/*
6+
* This file is part of the Symfony package.
7+
*
8+
* (c) Fabien Potencier <[email protected]>
9+
*
10+
* For the full copyright and license information, please view the LICENSE
11+
* file that was distributed with this source code.
12+
*/
13+
class DefaultContext implements ContextInterface
14+
{
15+
private $context;
16+
17+
public function __construct(array $context)
18+
{
19+
$this->context = $context;
20+
}
21+
22+
public function toArray(): array
23+
{
24+
return $this->context;
25+
}
26+
}

src/Symfony/Component/Serializer/DependencyInjection/SerializerPass.php

Lines changed: 1 addition & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
1515
use Symfony\Component\DependencyInjection\Compiler\PriorityTaggedServiceTrait;
1616
use Symfony\Component\DependencyInjection\ContainerBuilder;
17+
use Symfony\Component\DependencyInjection\Definition;
1718
use Symfony\Component\DependencyInjection\Exception\RuntimeException;
1819

1920
/**
@@ -50,39 +51,10 @@ public function process(ContainerBuilder $container)
5051

5152
$serializerDefinition = $container->getDefinition($this->serializerService);
5253
$serializerDefinition->replaceArgument(0, $normalizers);
53-
$this->addDefaultContextParameter($normalizers, $container);
5454

5555
if (!$encoders = $this->findAndSortTaggedServices($this->encoderTag, $container)) {
5656
throw new RuntimeException(sprintf('You must tag at least one service as "%s" to use the "%s" service.', $this->encoderTag, $this->serializerService));
5757
}
58-
5958
$serializerDefinition->replaceArgument(1, $encoders);
60-
$this->addDefaultContextParameter($encoders, $container);
61-
}
62-
63-
private function addDefaultContextParameter($services, $container)
64-
{
65-
foreach ($services as $service) {
66-
$definition = $container->getDefinition($service);
67-
if (!$definition->isAutowired()) {
68-
continue;
69-
}
70-
71-
if (null === $class = $definition->getClass()) {
72-
continue;
73-
}
74-
75-
$reflection = new \ReflectionClass($class);
76-
77-
if (null === $constructor = $reflection->getConstructor()) {
78-
continue;
79-
}
80-
81-
foreach ($constructor->getParameters() as $arg) {
82-
if ('defaultContext' === $arg->name) {
83-
$definition->setArgument('$defaultContext', '%serializer.default_context%');
84-
}
85-
}
86-
}
8759
}
8860
}

src/Symfony/Component/Serializer/Encoder/JsonDecode.php

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

1212
namespace Symfony\Component\Serializer\Encoder;
1313

14+
use Symfony\Component\Serializer\Context\ContextInterface;
1415
use Symfony\Component\Serializer\Exception\NotEncodableValueException;
1516

1617
/**

src/Symfony/Component/Serializer/Normalizer/ConstraintViolationListNormalizer.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111

1212
namespace Symfony\Component\Serializer\Normalizer;
1313

14+
use Symfony\Component\Serializer\Context\ContextInterface;
15+
use Symfony\Component\Serializer\Context\DefaultContext;
1416
use Symfony\Component\Serializer\NameConverter\NameConverterInterface;
1517
use Symfony\Component\Validator\ConstraintViolationListInterface;
1618

@@ -32,8 +34,12 @@ class ConstraintViolationListNormalizer implements NormalizerInterface, Cacheabl
3234
private $defaultContext;
3335
private $nameConverter;
3436

35-
public function __construct($defaultContext = [], NameConverterInterface $nameConverter = null)
37+
public function __construct(/** ContextInterface */ $defaultContext = null, NameConverterInterface $nameConverter = null)
3638
{
39+
if(!$defaultContext instanceof ContextInterface && is_array($defaultContext)){
40+
$defaultContext = new DefaultContext($defaultContext);
41+
@trigger_error("you must pass an object of type ". ContextInterface::class .", the passage of an array is depreciated", E_USER_DEPRECATED);
42+
}
3743
$this->defaultContext = $defaultContext;
3844
$this->nameConverter = $nameConverter;
3945
}

src/Symfony/Component/Serializer/Tests/DependencyInjection/SerializerPassTest.php

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,10 @@
1414
use PHPUnit\Framework\TestCase;
1515
use Symfony\Component\DependencyInjection\ContainerBuilder;
1616
use Symfony\Component\DependencyInjection\Reference;
17+
use Symfony\Component\Serializer\Context\ContextInterface;
18+
use Symfony\Component\Serializer\Context\DefaultContext;
1719
use Symfony\Component\Serializer\DependencyInjection\SerializerPass;
20+
use Symfony\Component\Serializer\Normalizer\JsonSerializableNormalizer;
1821
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
1922

2023
/**
@@ -69,17 +72,25 @@ public function testServicesAreOrderedAccordingToPriority()
6972
$this->assertEquals($expected, $definition->getArgument(0));
7073
$this->assertEquals($expected, $definition->getArgument(1));
7174
}
72-
73-
public function testServiceHasDefaultContextParameterArgument()
75+
public function testServiceHasDefaultContextInterface()
7476
{
7577
$container = new ContainerBuilder();
76-
77-
$definition = $container->register('serializer')->setClass(ObjectNormalizer::class)->setArguments([null, null, null, null, null, null, null])->addTag('serializer.normalizer')->addTag('serializer.encoder');
78+
$container->register('serializer_context', DefaultContext::class)->addArgument(['enable_max_depth' => true])->addTag(ContextInterface::class);
79+
$definition = $container->register('serializer')->setClass(ObjectNormalizer::class)->setArguments([null, null, null, null, null, null, $container->get('serializer_context')])->addTag('serializer.normalizer')->addTag('serializer.encoder');
7880
$definition->setAutowired(true);
79-
8081
$serializerPass = new SerializerPass();
8182
$serializerPass->process($container);
82-
83-
$this->assertEquals('%serializer.default_context%', $definition->getArgument('$defaultContext'));
83+
$this->assertInstanceOf(ContextInterface::class, $definition->getArgument(6));
84+
}
85+
public function testServiceHasDefaultNonContextInterface()
86+
{
87+
$container = new ContainerBuilder();
88+
$container->register('serializer_context', DefaultContext::class)->addArgument(['enable_max_depth' => true])->addTag(ContextInterface::class);
89+
$definition = $container->register('serializer')->setClass(JsonSerializableNormalizer::class)->setArguments([null, null, ['enable_max_depth' => true] ])->addTag('serializer.normalizer')->addTag('serializer.encoder');
90+
$definition->setAutowired(true);
91+
$serializerPass = new SerializerPass();
92+
$serializerPass->process($container);
93+
94+
$this->assertEquals(['enable_max_depth' => true], $definition->getArgument(2));
8495
}
8596
}

0 commit comments

Comments
 (0)