Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use ApiPlatform\Core\Api\IriConverterInterface;
use ApiPlatform\Core\Api\ResourceClassResolverInterface;
use ApiPlatform\Core\Api\UrlGeneratorInterface;
use ApiPlatform\Core\Bridge\Symfony\Messenger\DispatchTrait;
use ApiPlatform\Core\Exception\InvalidArgumentException;
use ApiPlatform\Core\Exception\RuntimeException;
use ApiPlatform\Core\Metadata\Resource\Factory\ResourceMetadataFactoryInterface;
Expand All @@ -35,12 +36,12 @@
*/
final class PublishMercureUpdatesListener
{
use DispatchTrait;
use ResourceClassInfoTrait;

private $iriConverter;
private $resourceMetadataFactory;
private $serializer;
private $messageBus;
private $publisher;
private $expressionLanguage;
private $createdEntities;
Expand Down Expand Up @@ -180,6 +181,6 @@ private function publishUpdate($entity, array $targets): void
}

$update = new Update($iri, $data, $targets);
$this->messageBus ? $this->messageBus->dispatch($update) : ($this->publisher)($update);
$this->messageBus ? $this->dispatch($update) : ($this->publisher)($update);
}
}
6 changes: 3 additions & 3 deletions src/Bridge/Symfony/Messenger/DataPersister.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@
final class DataPersister implements ContextAwareDataPersisterInterface
{
use ClassInfoTrait;
use DispatchTrait;

private $resourceMetadataFactory;
private $messageBus;

public function __construct(ResourceMetadataFactoryInterface $resourceMetadataFactory, MessageBusInterface $messageBus)
{
Expand Down Expand Up @@ -75,7 +75,7 @@ public function supports($data, array $context = []): bool
*/
public function persist($data, array $context = [])
{
$envelope = $this->messageBus->dispatch($data);
$envelope = $this->dispatch($data);

$handledStamp = $envelope->last(HandledStamp::class);
if (!$handledStamp instanceof HandledStamp) {
Expand All @@ -90,7 +90,7 @@ public function persist($data, array $context = [])
*/
public function remove($data, array $context = [])
{
$this->messageBus->dispatch(
$this->dispatch(
(new Envelope($data))
->with(new RemoveStamp())
);
Expand Down
55 changes: 55 additions & 0 deletions src/Bridge/Symfony/Messenger/DispatchTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

/*
* This file is part of the API Platform project.
*
* (c) Kévin Dunglas <dunglas@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace ApiPlatform\Core\Bridge\Symfony\Messenger;

use Symfony\Component\Messenger\Envelope;
use Symfony\Component\Messenger\Exception\HandlerFailedException;
use Symfony\Component\Messenger\MessageBusInterface;

/**
* @internal
*/
trait DispatchTrait
{
/**
* @var MessageBusInterface|null
*/
private $messageBus;

/**
* @param object|Envelope $message
*/
private function dispatch($message)
{
if (!$this->messageBus instanceof MessageBusInterface) {
throw new \InvalidArgumentException('The message bus is not set.');
}

if (!class_exists(HandlerFailedException::class)) {
return $this->messageBus->dispatch($message);
}

try {
return $this->messageBus->dispatch($message);
} catch (HandlerFailedException $e) {
// unwrap the exception thrown in handler for Symfony Messenger >= 4.3
while ($e instanceof HandlerFailedException) {
/** @var \Throwable $e */
$e = $e->getPrevious();
}

throw $e;
}
}
}
14 changes: 0 additions & 14 deletions src/EventListener/ExceptionListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
use Psr\Log\LoggerInterface;
use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
use Symfony\Component\HttpKernel\EventListener\ExceptionListener as BaseExceptionListener;
use Symfony\Component\Messenger\Exception\HandlerFailedException;

/**
* Handles requests errors.
Expand Down Expand Up @@ -45,19 +44,6 @@ public function onKernelException(GetResponseForExceptionEvent $event): void
return;
}

$exception = $event->getException();

// unwrap the exception thrown in handler for Symfony Messenger >= 4.3
while ($exception instanceof HandlerFailedException) {
/** @var \Throwable $exception */
$exception = $exception->getPrevious();
if (!$exception instanceof \Exception) {
throw $exception;
}
}

$event->setException($exception);

$this->exceptionListener->onKernelException($event);
}
}
1 change: 0 additions & 1 deletion tests/EventListener/ExceptionListenerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ public function testOnKernelException(Request $request)
$eventProphecy = $this->prophesize(GetResponseForExceptionEvent::class);
$eventProphecy->getRequest()->willReturn($request);
$eventProphecy->getException()->willReturn(new \Exception());
$eventProphecy->setException(Argument::type(\Exception::class))->will(function () {});
$eventProphecy->getKernel()->willReturn($kernel);
$eventProphecy->setResponse(Argument::type(Response::class))->shouldBeCalled();

Expand Down