Skip to content

Commit 92f655c

Browse files
authored
Merge pull request #2977 from teohhanhui/merge-2.4
Merge 2.4 into master
2 parents 092818a + 3958b81 commit 92f655c

File tree

6 files changed

+70
-25
lines changed

6 files changed

+70
-25
lines changed

features/graphql/filters.feature

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,8 +107,8 @@ Feature: Collections filtering
107107
}
108108
}
109109
"""
110-
Then the JSON node "data.dummies.edges[0].node.relatedDummies.edges" should have 0 element
111-
And the JSON node "data.dummies.edges[1].node.relatedDummies.edges" should have 0 element
110+
Then the JSON node "data.dummies.edges[0].node.relatedDummies.edges" should have 0 elements
111+
And the JSON node "data.dummies.edges[1].node.relatedDummies.edges" should have 0 elements
112112
And the JSON node "data.dummies.edges[2].node.relatedDummies.edges" should have 1 element
113113
And the JSON node "data.dummies.edges[2].node.relatedDummies.edges[0].node.name" should be equal to "RelatedDummy13"
114114

src/Bridge/Doctrine/EventListener/PublishMercureUpdatesListener.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use ApiPlatform\Core\Api\IriConverterInterface;
1717
use ApiPlatform\Core\Api\ResourceClassResolverInterface;
1818
use ApiPlatform\Core\Api\UrlGeneratorInterface;
19+
use ApiPlatform\Core\Bridge\Symfony\Messenger\DispatchTrait;
1920
use ApiPlatform\Core\Exception\InvalidArgumentException;
2021
use ApiPlatform\Core\Exception\RuntimeException;
2122
use ApiPlatform\Core\Metadata\Resource\Factory\ResourceMetadataFactoryInterface;
@@ -35,12 +36,12 @@
3536
*/
3637
final class PublishMercureUpdatesListener
3738
{
39+
use DispatchTrait;
3840
use ResourceClassInfoTrait;
3941

4042
private $iriConverter;
4143
private $resourceMetadataFactory;
4244
private $serializer;
43-
private $messageBus;
4445
private $publisher;
4546
private $expressionLanguage;
4647
private $createdEntities;
@@ -180,6 +181,6 @@ private function publishUpdate($entity, array $targets): void
180181
}
181182

182183
$update = new Update($iri, $data, $targets);
183-
$this->messageBus ? $this->messageBus->dispatch($update) : ($this->publisher)($update);
184+
$this->messageBus ? $this->dispatch($update) : ($this->publisher)($update);
184185
}
185186
}

src/Bridge/Symfony/Messenger/DataPersister.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,9 @@
3232
final class DataPersister implements ContextAwareDataPersisterInterface
3333
{
3434
use ClassInfoTrait;
35+
use DispatchTrait;
3536

3637
private $resourceMetadataFactory;
37-
private $messageBus;
3838

3939
public function __construct(ResourceMetadataFactoryInterface $resourceMetadataFactory, MessageBusInterface $messageBus)
4040
{
@@ -75,7 +75,7 @@ public function supports($data, array $context = []): bool
7575
*/
7676
public function persist($data, array $context = [])
7777
{
78-
$envelope = $this->messageBus->dispatch($data);
78+
$envelope = $this->dispatch($data);
7979

8080
$handledStamp = $envelope->last(HandledStamp::class);
8181
if (!$handledStamp instanceof HandledStamp) {
@@ -90,7 +90,7 @@ public function persist($data, array $context = [])
9090
*/
9191
public function remove($data, array $context = [])
9292
{
93-
$this->messageBus->dispatch(
93+
$this->dispatch(
9494
(new Envelope($data))
9595
->with(new RemoveStamp())
9696
);
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the API Platform project.
5+
*
6+
* (c) Kévin Dunglas <[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+
declare(strict_types=1);
13+
14+
namespace ApiPlatform\Core\Bridge\Symfony\Messenger;
15+
16+
use Symfony\Component\Messenger\Envelope;
17+
use Symfony\Component\Messenger\Exception\HandlerFailedException;
18+
use Symfony\Component\Messenger\MessageBusInterface;
19+
20+
/**
21+
* @internal
22+
*/
23+
trait DispatchTrait
24+
{
25+
/**
26+
* @var MessageBusInterface|null
27+
*/
28+
private $messageBus;
29+
30+
/**
31+
* @param object|Envelope $message
32+
*/
33+
private function dispatch($message)
34+
{
35+
if (!$this->messageBus instanceof MessageBusInterface) {
36+
throw new \InvalidArgumentException('The message bus is not set.');
37+
}
38+
39+
if (!class_exists(HandlerFailedException::class)) {
40+
return $this->messageBus->dispatch($message);
41+
}
42+
43+
try {
44+
return $this->messageBus->dispatch($message);
45+
} catch (HandlerFailedException $e) {
46+
// unwrap the exception thrown in handler for Symfony Messenger >= 4.3
47+
while ($e instanceof HandlerFailedException) {
48+
/** @var \Throwable $e */
49+
$e = $e->getPrevious();
50+
}
51+
52+
throw $e;
53+
}
54+
}
55+
}

src/EventListener/ExceptionListener.php

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
use Psr\Log\LoggerInterface;
1818
use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
1919
use Symfony\Component\HttpKernel\EventListener\ExceptionListener as BaseExceptionListener;
20-
use Symfony\Component\Messenger\Exception\HandlerFailedException;
2120

2221
/**
2322
* Handles requests errors.
@@ -45,18 +44,6 @@ public function onKernelException(GetResponseForExceptionEvent $event): void
4544
return;
4645
}
4746

48-
// unwrap the exception thrown in handler for Symfony Messenger >= 4.3
49-
$exception = $event->getException();
50-
if ($exception instanceof HandlerFailedException) {
51-
/** @var \Throwable $previousException */
52-
$previousException = $exception->getPrevious();
53-
if (!$previousException instanceof \Exception) {
54-
throw $previousException;
55-
}
56-
57-
$event->setException($previousException);
58-
}
59-
6047
$this->exceptionListener->onKernelException($event);
6148
}
6249
}

tests/EventListener/ExceptionListenerTest.php

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,9 @@ public function testOnKernelException(Request $request)
3535
$kernel->handle(Argument::type(Request::class), HttpKernelInterface::SUB_REQUEST, false)->willReturn(new Response())->shouldBeCalled();
3636

3737
$eventProphecy = $this->prophesize(GetResponseForExceptionEvent::class);
38-
$eventProphecy->getRequest()->willReturn($request)->shouldBeCalled();
39-
$eventProphecy->getException()->willReturn(new \Exception())->shouldBeCalled();
40-
$eventProphecy->getKernel()->willReturn($kernel)->shouldBeCalled();
38+
$eventProphecy->getRequest()->willReturn($request);
39+
$eventProphecy->getException()->willReturn(new \Exception());
40+
$eventProphecy->getKernel()->willReturn($kernel);
4141
$eventProphecy->setResponse(Argument::type(Response::class))->shouldBeCalled();
4242

4343
$listener = new ExceptionListener('foo:bar');
@@ -55,7 +55,8 @@ public function getRequest()
5555
public function testDoNothingWhenNotAnApiCall()
5656
{
5757
$eventProphecy = $this->prophesize(GetResponseForExceptionEvent::class);
58-
$eventProphecy->getRequest()->willReturn(new Request())->shouldBeCalled();
58+
$eventProphecy->getRequest()->willReturn(new Request());
59+
$eventProphecy->setResponse(Argument::type(Response::class))->shouldNotBeCalled();
5960

6061
$listener = new ExceptionListener('foo:bar');
6162
$listener->onKernelException($eventProphecy->reveal());
@@ -67,7 +68,8 @@ public function testDoNothingWhenHtmlRequested()
6768
$request->setRequestFormat('html');
6869

6970
$eventProphecy = $this->prophesize(GetResponseForExceptionEvent::class);
70-
$eventProphecy->getRequest()->willReturn($request)->shouldBeCalled();
71+
$eventProphecy->getRequest()->willReturn($request);
72+
$eventProphecy->setResponse(Argument::type(Response::class))->shouldNotBeCalled();
7173

7274
$listener = new ExceptionListener('foo:bar');
7375
$listener->onKernelException($eventProphecy->reveal());

0 commit comments

Comments
 (0)