Skip to content

Commit f2c5977

Browse files
author
Jérôme Deuchnord
committed
Fix BC break
1 parent 62f66fd commit f2c5977

File tree

6 files changed

+45
-15
lines changed

6 files changed

+45
-15
lines changed

src/EventListener/AddFormatListener.php

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ final class AddFormatListener
4141
/**
4242
* @throws InvalidArgumentException
4343
*/
44-
public function __construct(Negotiator $negotiator, /* FormatsProviderInterface */ $formatsProvider, EventDispatcherInterface $dispatcher)
44+
public function __construct(Negotiator $negotiator, /* FormatsProviderInterface */ $formatsProvider, EventDispatcherInterface $dispatcher = null)
4545
{
4646
$this->negotiator = $negotiator;
4747
if (\is_array($formatsProvider)) {
@@ -77,9 +77,16 @@ public function onKernelRequest(GetResponseEvent $event)
7777
}
7878

7979
$this->populateMimeTypes();
80-
$this->dispatcher->dispatch(PreAddFormatEvent::NAME, new PreAddFormatEvent($this->formats));
80+
81+
if (null !== $this->dispatcher) {
82+
$this->dispatcher->dispatch(PreAddFormatEvent::NAME, new PreAddFormatEvent($this->formats));
83+
}
84+
8185
$this->addRequestFormats($request, $this->formats);
82-
$this->dispatcher->dispatch(PostAddFormatEvent::NAME, new PostAddFormatEvent($this->formats));
86+
87+
if (null !== $this->dispatcher) {
88+
$this->dispatcher->dispatch(PostAddFormatEvent::NAME, new PostAddFormatEvent($this->formats));
89+
}
8390

8491
// Empty strings must be converted to null because the Symfony router doesn't support parameter typing before 3.2 (_format)
8592
if (null === $routeFormat = $request->attributes->get('_format') ?: null) {

src/EventListener/DeserializeListener.php

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ final class DeserializeListener
4242
/**
4343
* @throws InvalidArgumentException
4444
*/
45-
public function __construct(SerializerInterface $serializer, SerializerContextBuilderInterface $serializerContextBuilder, /* FormatsProviderInterface */ $formatsProvider, EventDispatcherInterface $dispatcher)
45+
public function __construct(SerializerInterface $serializer, SerializerContextBuilderInterface $serializerContextBuilder, /* FormatsProviderInterface */ $formatsProvider, EventDispatcherInterface $dispatcher = null)
4646
{
4747
$this->serializer = $serializer;
4848
$this->serializerContextBuilder = $serializerContextBuilder;
@@ -95,14 +95,20 @@ public function onKernelRequest(GetResponseEvent $event)
9595
$context[AbstractNormalizer::OBJECT_TO_POPULATE] = $data;
9696
}
9797

98-
$this->dispatcher->dispatch(PreDeserializeEvent::NAME, new PreDeserializeEvent($requestContent));
98+
if (null !== $this->dispatcher) {
99+
$this->dispatcher->dispatch(PreDeserializeEvent::NAME, new PreDeserializeEvent($requestContent));
100+
}
101+
99102
$request->attributes->set(
100103
'data',
101104
$this->serializer->deserialize(
102105
$requestContent, $attributes['input_class'], $format, $context
103106
)
104107
);
105-
$this->dispatcher->dispatch(PostDeserializeEvent::NAME, new PostDeserializeEvent($requestContent));
108+
109+
if (null !== $this->dispatcher) {
110+
$this->dispatcher->dispatch(PostDeserializeEvent::NAME, new PostDeserializeEvent($requestContent));
111+
}
106112
}
107113

108114
/**

src/EventListener/ReadListener.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,9 @@ public function onKernelRequest(GetResponseEvent $event)
9595
try {
9696
$identifiers = $this->extractIdentifiers($request->attributes->all(), $attributes);
9797

98-
$this->dispatcher->dispatch(PreReadEvent::NAME, new PreReadEvent($data));
98+
if (null !== $this->dispatcher) {
99+
$this->dispatcher->dispatch(PreReadEvent::NAME, new PreReadEvent($data));
100+
}
99101

100102
if (isset($attributes['item_operation_name'])) {
101103
$data = $this->getItemData($identifiers, $attributes, $context);
@@ -111,7 +113,9 @@ public function onKernelRequest(GetResponseEvent $event)
111113
$data = null;
112114
}
113115

114-
$this->dispatcher->dispatch(PostReadEvent::NAME, new PostReadEvent($data));
116+
if (null !== $this->dispatcher) {
117+
$this->dispatcher->dispatch(PostReadEvent::NAME, new PostReadEvent($data));
118+
}
115119

116120
if (null === $data) {
117121
throw new NotFoundHttpException('Not Found');

src/EventListener/RespondListener.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,14 +77,18 @@ public function onKernelView(GetResponseForControllerResultEvent $event)
7777
}
7878
}
7979

80-
$this->dispatcher->dispatch(PreRespondEvent::NAME, new PreRespondEvent($event));
80+
if (null !== $this->dispatcher) {
81+
$this->dispatcher->dispatch(PreRespondEvent::NAME, new PreRespondEvent($event));
82+
}
8183

8284
$event->setResponse(new Response(
8385
$controllerResult,
8486
self::METHOD_TO_CODE[$request->getMethod()] ?? Response::HTTP_OK,
8587
$headers
8688
));
8789

88-
$this->dispatcher->dispatch(PostRespondEvent::NAME, new PostRespondEvent($event));
90+
if (null !== $this->dispatcher) {
91+
$this->dispatcher->dispatch(PostRespondEvent::NAME, new PostRespondEvent($event));
92+
}
8993
}
9094
}

src/EventListener/SerializeListener.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ final class SerializeListener
3939
/** @var EventDispatcherInterface */
4040
private $dispatcher;
4141

42-
public function __construct(SerializerInterface $serializer, SerializerContextBuilderInterface $serializerContextBuilder, EventDispatcherInterface $dispatcher)
42+
public function __construct(SerializerInterface $serializer, SerializerContextBuilderInterface $serializerContextBuilder, EventDispatcherInterface $dispatcher = null)
4343
{
4444
$this->serializer = $serializer;
4545
$this->serializerContextBuilder = $serializerContextBuilder;
@@ -76,11 +76,15 @@ public function onKernelView(GetResponseForControllerResultEvent $event)
7676

7777
$request->attributes->set('_api_normalization_context', $context);
7878

79-
$this->dispatcher->dispatch(PreSerializeEvent::NAME, new PreSerializeEvent($controllerResult));
79+
if (null !== $this->dispatcher) {
80+
$this->dispatcher->dispatch(PreSerializeEvent::NAME, new PreSerializeEvent($controllerResult));
81+
}
8082

8183
$event->setControllerResult($this->serializer->serialize($controllerResult, $request->getRequestFormat(), $context));
8284

83-
$this->dispatcher->dispatch(PostSerializeEvent::NAME, new PostSerializeEvent($controllerResult));
85+
if (null !== $this->dispatcher) {
86+
$this->dispatcher->dispatch(PostSerializeEvent::NAME, new PostSerializeEvent($controllerResult));
87+
}
8488

8589
$request->attributes->set('_api_respond', true);
8690
$request->attributes->set('_resources', $request->attributes->get('_resources', []) + (array) $resources);

src/EventListener/WriteListener.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,14 +54,19 @@ public function onKernelView(GetResponseForControllerResultEvent $event)
5454
return;
5555
}
5656

57-
$this->dispatcher->dispatch(PreWriteEvent::NAME, new PreWriteEvent($request->getMethod(), $controllerResult));
57+
if (null !== $this->dispatcher) {
58+
$this->dispatcher->dispatch(PreWriteEvent::NAME, new PreWriteEvent($request->getMethod(), $controllerResult));
59+
}
5860

5961
switch ($request->getMethod()) {
6062
case 'PUT':
6163
case 'PATCH':
6264
case 'POST':
6365
$persistResult = $this->dataPersister->persist($controllerResult);
64-
$this->dispatcher->dispatch(PostWriteEvent::NAME, new PostWriteEvent($request->getMethod(), $controllerResult));
66+
67+
if (null !== $this->dispatcher) {
68+
$this->dispatcher->dispatch(PostWriteEvent::NAME, new PostWriteEvent($request->getMethod(), $controllerResult));
69+
}
6570

6671
if (null === $persistResult) {
6772
@trigger_error(sprintf('Returning void from %s::persist() is deprecated since API Platform 2.3 and will not be supported in API Platform 3, an object should always be returned.', DataPersisterInterface::class), E_USER_DEPRECATED);

0 commit comments

Comments
 (0)