Skip to content

Commit 174e556

Browse files
author
Danny van Wijk
committed
Improve perf ValidationExceptionListener
1 parent af31ef1 commit 174e556

File tree

2 files changed

+17
-5
lines changed

2 files changed

+17
-5
lines changed

src/Bundle/Resources/config/services/listener.xml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,9 @@
2222
</service>
2323

2424
<service id="sylius.listener.exception.validation" class="Sylius\Resource\Symfony\Validator\EventListener\ValidationExceptionListener">
25-
<argument type="service" id="serializer" on-invalid="null" />
25+
<argument type="service" id="service_container" />
2626
<tag name="kernel.event_listener" event="kernel.exception" method="onKernelException" />
27+
<tag name="container.service_subscriber" />
2728
</service>
2829
</services>
2930
</container>

src/Component/src/Symfony/Validator/EventListener/ValidationExceptionListener.php

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,21 @@
1313

1414
namespace Sylius\Resource\Symfony\Validator\EventListener;
1515

16+
use Psr\Container\ContainerInterface;
1617
use Sylius\Resource\Symfony\Validator\Exception\ConstraintViolationListAwareExceptionInterface;
1718
use Symfony\Component\HttpFoundation\Response;
1819
use Symfony\Component\HttpKernel\Event\ExceptionEvent;
1920
use Symfony\Component\Serializer\SerializerInterface;
21+
use Symfony\Contracts\Service\ServiceSubscriberInterface;
2022

2123
/**
2224
* Handles validation errors.
2325
*
2426
* @experimental
2527
*/
26-
final class ValidationExceptionListener
28+
final class ValidationExceptionListener implements ServiceSubscriberInterface
2729
{
28-
public function __construct(private ?SerializerInterface $serializer = null)
30+
public function __construct(private ContainerInterface $container)
2931
{
3032
}
3133

@@ -40,7 +42,9 @@ public function onKernelException(ExceptionEvent $event): void
4042
return;
4143
}
4244

43-
if (null === $this->serializer) {
45+
/** @var SerializerInterface|null $serializer */
46+
$serializer = $this->container->get('serializer');
47+
if (null === $serializer) {
4448
throw new \LogicException('The Symfony Serializer is not available. Try running "composer require symfony/serializer".');
4549
}
4650

@@ -53,7 +57,7 @@ public function onKernelException(ExceptionEvent $event): void
5357
$mimeType = $request->getMimeType($format);
5458

5559
$event->setResponse(new Response(
56-
$this->serializer->serialize($exception->getConstraintViolationList(), $format),
60+
$serializer->serialize($exception->getConstraintViolationList(), $format),
5761
Response::HTTP_UNPROCESSABLE_ENTITY,
5862
[
5963
'Content-Type' => sprintf('%s; charset=utf-8', $mimeType),
@@ -62,4 +66,11 @@ public function onKernelException(ExceptionEvent $event): void
6266
],
6367
));
6468
}
69+
70+
public static function getSubscribedServices(): array
71+
{
72+
return [
73+
'serializer' => '?' . SerializerInterface::class,
74+
];
75+
}
6576
}

0 commit comments

Comments
 (0)