Skip to content

Commit

Permalink
Split events v7 (#287)
Browse files Browse the repository at this point in the history
#287 Split events, deprecate GuzzleEventListenerInterface, adjust tests
  • Loading branch information
rpkamp authored and gregurco committed Dec 31, 2019
1 parent b2e56e1 commit b61e820
Show file tree
Hide file tree
Showing 9 changed files with 387 additions and 106 deletions.
12 changes: 12 additions & 0 deletions src/DependencyInjection/Compiler/EventHandlerCompilerPass.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,13 @@

namespace EightPoints\Bundle\GuzzleBundle\DependencyInjection\Compiler;

use EightPoints\Bundle\GuzzleBundle\Events\GuzzleEventListenerInterface;
use EightPoints\Bundle\GuzzleBundle\Events\GuzzleEvents;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use function error_log;
use function sprintf;
use function trigger_error;

class EventHandlerCompilerPass implements CompilerPassInterface
{
Expand All @@ -29,6 +33,14 @@ public function process(ContainerBuilder $container)
foreach ($taggedServices as $id => $tags) {
foreach ($tags as $attributes) {
if (isset($attributes['service']) && in_array($attributes['event'], GuzzleEvents::EVENTS, true)) {
@trigger_error(
sprintf(
'Using interface "%s" is deprecated and will be removed in EightPointsGuzzleBundle version 8',
GuzzleEventListenerInterface::class
),
E_USER_DEPRECATED
);

$container->getDefinition($id)->addMethodCall(
'setServiceName',
[$attributes['service']]
Expand Down
7 changes: 7 additions & 0 deletions src/Events/GuzzleEventListenerInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@

namespace EightPoints\Bundle\GuzzleBundle\Events;

use function trigger_error;

@trigger_error(
sprintf('Interface "%s" is deprecated and will be removed in EightPointsGuzzleBundle version 8', GuzzleEventListenerInterface::class),
E_USER_DEPRECATED
);

interface GuzzleEventListenerInterface
{
/**
Expand Down
12 changes: 12 additions & 0 deletions src/Events/GuzzleEvents.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace EightPoints\Bundle\GuzzleBundle\Events;

use function sprintf;

final class GuzzleEvents
{
const PRE_TRANSACTION = 'eight_points_guzzle.pre_transaction';
Expand All @@ -12,4 +14,14 @@ final class GuzzleEvents
self::PRE_TRANSACTION,
self::POST_TRANSACTION,
];

public static function preTransactionFor(string $serviceName): string
{
return sprintf('%s.%s', self::PRE_TRANSACTION, $serviceName);
}

public static function postTransactionFor(string $serviceName): string
{
return sprintf('%s.%s', self::POST_TRANSACTION, $serviceName);
}
}
38 changes: 21 additions & 17 deletions src/Middleware/EventDispatchMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,16 @@

namespace EightPoints\Bundle\GuzzleBundle\Middleware;

use EightPoints\Bundle\GuzzleBundle\Events\Event;
use EightPoints\Bundle\GuzzleBundle\Events\GuzzleEvents;
use EightPoints\Bundle\GuzzleBundle\Events\PostTransactionEvent;
use EightPoints\Bundle\GuzzleBundle\Events\PreTransactionEvent;
use Exception;
use GuzzleHttp\Exception\RequestException;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface as ContractsEventDispatcherInterface;
use EightPoints\Bundle\GuzzleBundle\Events\GuzzleEvents;
use EightPoints\Bundle\GuzzleBundle\Events\PreTransactionEvent;

/**
* Dispatches an Event using the Symfony Event Dispatcher.
Expand Down Expand Up @@ -50,11 +51,8 @@ public function dispatchEvent() : \Closure
$preTransactionEvent = new PreTransactionEvent($request, $this->serviceName);

// Dispatch it through the symfony Dispatcher.
if ($this->eventDispatcher instanceof ContractsEventDispatcherInterface) {
$this->eventDispatcher->dispatch($preTransactionEvent, GuzzleEvents::PRE_TRANSACTION);
} else {
$this->eventDispatcher->dispatch(GuzzleEvents::PRE_TRANSACTION, $preTransactionEvent);
}
$this->doDispatch($preTransactionEvent, GuzzleEvents::PRE_TRANSACTION);
$this->doDispatch($preTransactionEvent, GuzzleEvents::preTransactionFor($this->serviceName));

// Continue the handler chain.
$promise = $handler($preTransactionEvent->getTransaction(), $options);
Expand All @@ -66,11 +64,8 @@ function (ResponseInterface $response) {
$postTransactionEvent = new PostTransactionEvent($response, $this->serviceName);

// Dispatch the event on the symfony event dispatcher.
if ($this->eventDispatcher instanceof ContractsEventDispatcherInterface) {
$this->eventDispatcher->dispatch($postTransactionEvent, GuzzleEvents::POST_TRANSACTION);
} else {
$this->eventDispatcher->dispatch(GuzzleEvents::POST_TRANSACTION, $postTransactionEvent);
}
$this->doDispatch($postTransactionEvent, GuzzleEvents::POST_TRANSACTION);
$this->doDispatch($postTransactionEvent, GuzzleEvents::postTransactionFor($this->serviceName));

// Continue down the chain.
return $postTransactionEvent->getTransaction();
Expand All @@ -83,11 +78,8 @@ function (Exception $reason) {
$postTransactionEvent = new PostTransactionEvent($response, $this->serviceName);

// Dispatch the event on the symfony event dispatcher.
if ($this->eventDispatcher instanceof ContractsEventDispatcherInterface) {
$this->eventDispatcher->dispatch($postTransactionEvent, GuzzleEvents::POST_TRANSACTION);
} else {
$this->eventDispatcher->dispatch(GuzzleEvents::POST_TRANSACTION, $postTransactionEvent);
}
$this->doDispatch($postTransactionEvent, GuzzleEvents::POST_TRANSACTION);
$this->doDispatch($postTransactionEvent, GuzzleEvents::postTransactionFor($this->serviceName));

// Continue down the chain.
return \GuzzleHttp\Promise\rejection_for($reason);
Expand All @@ -96,4 +88,16 @@ function (Exception $reason) {
};
};
}

private function doDispatch(Event $event, string $name)
{
if ($this->eventDispatcher instanceof ContractsEventDispatcherInterface) {
$this->eventDispatcher->dispatch($event, $name);

return;
}

// BC compatibility
$this->eventDispatcher->dispatch($name, $event);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ class EventHandlerCompilerPassTest extends TestCase
* Test the case when compiler pass added method call to proper service.
*
* @covers \EightPoints\Bundle\GuzzleBundle\DependencyInjection\Compiler\EventHandlerCompilerPass::process
*
* This functionality will be removed in EightPointGuzzleBundle version 8
* @group legacy
*/
public function testProcessAddedMethodCall()
{
Expand Down
Loading

0 comments on commit b61e820

Please sign in to comment.