Skip to content

Commit 93a06c0

Browse files
authored
Merge pull request #1879 from xabbuh/issue-1806
prefer decoration over parameter replacements
2 parents 18870fe + 096e471 commit 93a06c0

File tree

3 files changed

+30
-13
lines changed

3 files changed

+30
-13
lines changed

DependencyInjection/Compiler/JMSFormErrorHandlerPass.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,26 @@
1414
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
1515
use Symfony\Component\DependencyInjection\ContainerBuilder;
1616
use FOS\RestBundle\Serializer\Normalizer\FormErrorHandler;
17+
use Symfony\Component\DependencyInjection\Reference;
1718

1819
/**
19-
* Changes the JMS FormError handler.
20+
* Decorates the JMS FormErrorHandler.
2021
*
2122
* @author Guilhem Niot <[email protected]>
23+
* @author Christian Flothmann <[email protected]>
2224
*
2325
* @internal
2426
*/
2527
final class JMSFormErrorHandlerPass implements CompilerPassInterface
2628
{
2729
public function process(ContainerBuilder $container)
2830
{
29-
$container->setParameter('jms_serializer.form_error_handler.class', FormErrorHandler::class);
31+
if (!$container->has('jms_serializer.form_error_handler')) {
32+
return;
33+
}
34+
35+
$container->register('fos_rest.serializer.form_error_handler', FormErrorHandler::class)
36+
->setDecoratedService('jms_serializer.form_error_handler')
37+
->addArgument(new Reference('fos_rest.serializer.form_error_handler.inner'));
3038
}
3139
}

Serializer/Normalizer/FormErrorHandler.php

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,23 +13,27 @@
1313

1414
use JMS\Serializer\Context;
1515
use JMS\Serializer\Handler\FormErrorHandler as JMSFormErrorHandler;
16+
use JMS\Serializer\Handler\SubscribingHandlerInterface;
1617
use JMS\Serializer\JsonSerializationVisitor;
1718
use JMS\Serializer\XmlSerializationVisitor;
1819
use Symfony\Component\Form\Form;
1920
use JMS\Serializer\YamlSerializationVisitor;
20-
use Symfony\Component\Translation\TranslatorInterface;
2121

2222
/**
2323
* Extend the JMS FormErrorHandler to include more informations when using the ViewHandler.
2424
*/
25-
class FormErrorHandler extends JMSFormErrorHandler
25+
class FormErrorHandler implements SubscribingHandlerInterface
2626
{
27-
private $translator;
27+
private $formErrorHandler;
2828

29-
public function __construct(TranslatorInterface $translator)
29+
public function __construct(JMSFormErrorHandler $formErrorHandler)
3030
{
31-
$this->translator = $translator;
32-
parent::__construct($translator);
31+
$this->formErrorHandler = $formErrorHandler;
32+
}
33+
34+
public static function getSubscribingMethods()
35+
{
36+
return JMSFormErrorHandler::getSubscribingMethods();
3337
}
3438

3539
public function serializeFormToXml(XmlSerializationVisitor $visitor, Form $form, array $type, Context $context = null)
@@ -52,20 +56,20 @@ public function serializeFormToXml(XmlSerializationVisitor $visitor, Form $form,
5256
$errorsNode = $visitor->document->createElement('errors');
5357
$visitor->getCurrentNode()->appendChild($errorsNode);
5458
$visitor->setCurrentNode($errorsNode);
55-
parent::serializeFormToXml($visitor, $form, $type);
59+
$this->formErrorHandler->serializeFormToXml($visitor, $form, $type);
5660
$visitor->revertCurrentNode();
5761

5862
return;
5963
}
6064
}
6165

62-
return parent::serializeFormToXml($visitor, $form, $type);
66+
return $this->formErrorHandler->serializeFormToXml($visitor, $form, $type);
6367
}
6468

6569
public function serializeFormToJson(JsonSerializationVisitor $visitor, Form $form, array $type, Context $context = null)
6670
{
6771
$isRoot = null === $visitor->getRoot();
68-
$result = $this->adaptFormArray(parent::serializeFormToJson($visitor, $form, $type), $context);
72+
$result = $this->adaptFormArray($this->formErrorHandler->serializeFormToJson($visitor, $form, $type), $context);
6973

7074
if ($isRoot) {
7175
$visitor->setRoot($result);
@@ -77,7 +81,7 @@ public function serializeFormToJson(JsonSerializationVisitor $visitor, Form $for
7781
public function serializeFormToYml(YamlSerializationVisitor $visitor, Form $form, array $type, Context $context = null)
7882
{
7983
$isRoot = null === $visitor->getRoot();
80-
$result = $this->adaptFormArray(parent::serializeFormToYml($visitor, $form, $type), $context);
84+
$result = $this->adaptFormArray($this->formErrorHandler->serializeFormToYml($visitor, $form, $type), $context);
8185

8286
if ($isRoot) {
8387
$visitor->setRoot($result);
@@ -86,6 +90,11 @@ public function serializeFormToYml(YamlSerializationVisitor $visitor, Form $form
8690
return $result;
8791
}
8892

93+
public function __call($name, $arguments)
94+
{
95+
return call_user_func_array([$this->formErrorHandler, $name], $arguments);
96+
}
97+
8998
private function adaptFormArray(\ArrayObject $serializedForm, Context $context = null)
9099
{
91100
$statusCode = $this->getStatusCode($context);

Tests/Functional/DependencyInjectionTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public function testSerializerRelatedServicesAreNotRemovedWhenJmsSerializerBundl
2929
self::bootKernel();
3030
$container = self::$kernel->getContainer();
3131

32-
$this->assertSame(FormErrorHandler::class, $container->getParameter('jms_serializer.form_error_handler.class'));
32+
$this->assertInstanceOf(FormErrorHandler::class, $container->get('jms_serializer.form_error_handler'));
3333
$this->assertInstanceOf(JMSHandlerRegistry::class, $container->get('test.jms_serializer.handler_registry'));
3434
}
3535

0 commit comments

Comments
 (0)