-
Notifications
You must be signed in to change notification settings - Fork 71
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #22 from quadland/EventHandler
Event handler
- Loading branch information
Showing
10 changed files
with
339 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
<?php | ||
|
||
namespace EightPoints\Bundle\GuzzleBundle\DependencyInjection\Compiler; | ||
use \Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; | ||
use Symfony\Component\DependencyInjection\ContainerBuilder; | ||
|
||
/** | ||
* Class EventHandlerCompilerPass | ||
* | ||
* @package EightPoints\Bundle\GuzzleBundle\DependencyInjection\Compiler | ||
*/ | ||
class EventHandlerCompilerPass implements CompilerPassInterface | ||
{ | ||
/** | ||
* We tag handlers with specific services to listen too. | ||
* | ||
* We get all event tagged services from the container. | ||
* We then go through each event, and look for the value guzzle_bundle. | ||
* For each one we find, we check if the service key is set, and then | ||
* call setServiceName on each EventListener. | ||
* | ||
* @param \Symfony\Component\DependencyInjection\ContainerBuilder $container | ||
* | ||
* @api | ||
*/ | ||
public function process(ContainerBuilder $container) | ||
{ | ||
$taggedServices = $container->findTaggedServiceIds('kernel.event_listener'); | ||
|
||
foreach($taggedServices as $id => $tags) { | ||
foreach ($tags as $attributes) { | ||
if(strstr($attributes['event'], 'guzzle_bundle') !== false) { | ||
if(isset($attributes['service'])) { | ||
$container->getDefinition($id)->addMethodCall( | ||
'setServiceName', [$attributes['service']] | ||
); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<?php | ||
/** | ||
* Created by IntelliJ IDEA. | ||
* User: chris | ||
* Date: 9/16/15 | ||
* Time: 2:40 PM | ||
*/ | ||
|
||
namespace EightPoints\Bundle\GuzzleBundle\Events; | ||
|
||
|
||
interface GuzzleEventListenerInterface | ||
{ | ||
public function setServiceName($serviceName); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<?php | ||
namespace EightPoints\Bundle\GuzzleBundle\Events; | ||
|
||
/** | ||
* Class GuzzleEvents | ||
* | ||
* @package EightPoints\Bundle\GuzzleBundle\Events | ||
*/ | ||
final class GuzzleEvents | ||
{ | ||
const PRE_TRANSACTION = 'guzzle_bundle.pre_transaction'; | ||
const POST_TRANSACTION = 'guzzle_bundle.post_transaction'; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
<?php | ||
|
||
namespace EightPoints\Bundle\GuzzleBundle\Events; | ||
|
||
use Psr\Http\Message\ResponseInterface; | ||
use Symfony\Component\EventDispatcher\Event; | ||
|
||
/** | ||
* Class PostTransactionEvent | ||
* | ||
* @package EightPoints\Bundle\GuzzleBundle\Events | ||
*/ | ||
class PostTransactionEvent extends Event | ||
{ | ||
/** | ||
* @var ResponseInterface | ||
*/ | ||
protected $response; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
protected $serviceName; | ||
|
||
/** | ||
* PostTransactionEvent constructor. | ||
* | ||
* @param ResponseInterface $response | ||
*/ | ||
public function __construct(ResponseInterface $response, $serviceName) | ||
{ | ||
$this->response = $response; | ||
$this->serviceName = $serviceName; | ||
} | ||
|
||
|
||
/** | ||
* Get the transaction from the event. | ||
* | ||
* This returns the transaction we are working with. | ||
* | ||
* @return ResponseInterface | ||
*/ | ||
public function getTransaction() | ||
{ | ||
return $this->response; | ||
} | ||
|
||
/** | ||
* Sets the transaction inline with the event. | ||
* | ||
* @param ResponseInterface $response | ||
*/ | ||
public function setTransaction(ResponseInterface $response) | ||
{ | ||
$this->response = $response; | ||
} | ||
|
||
/** | ||
* @return string | ||
*/ | ||
public function getServiceName() | ||
{ | ||
return $this->serviceName; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
<?php | ||
namespace EightPoints\Bundle\GuzzleBundle\Events; | ||
use Psr\Http\Message\RequestInterface; | ||
use Symfony\Component\EventDispatcher\Event; | ||
|
||
/** | ||
* Class PreTransactionEvent | ||
* | ||
* @package EightPoints\Bundle\GuzzleBundle\Events | ||
* | ||
* @author Chris Warner([email protected]) | ||
*/ | ||
class PreTransactionEvent extends Event | ||
{ | ||
/** | ||
* @var RequestInterface | ||
*/ | ||
protected $requestTransaction; | ||
/** | ||
* @var string | ||
*/ | ||
protected $serviceName; | ||
|
||
/** | ||
* PreTransactionEvent constructor. | ||
* | ||
* @param RequestInterface $requestTransaction | ||
*/ | ||
public function __construct(RequestInterface $requestTransaction, $serviceName) | ||
{ | ||
$this->requestTransaction = $requestTransaction; | ||
$this->serviceName = $serviceName; | ||
} | ||
|
||
/** | ||
* Access the transaction from the Guzzle HTTP request | ||
* | ||
* This returns the actual Request Object from the Guzzle HTTP | ||
* Reqeust. This object will be modified by the event listener. | ||
* @return RequestInterface | ||
*/ | ||
public function getTransaction() | ||
{ | ||
return $this->requestTransaction; | ||
} | ||
|
||
/** | ||
* Replaces the transaction with the modified one. | ||
* | ||
* Guzzles transaction returns a modified request object, | ||
* so once it has been modified, we need to put it back on the | ||
* event so it can become part of the transaction. | ||
* | ||
* @param RequestInterface $requestTransaction | ||
*/ | ||
public function setTransaction(RequestInterface $requestTransaction) | ||
{ | ||
$this->requestTransaction = $requestTransaction; | ||
} | ||
|
||
/** | ||
* @return string | ||
*/ | ||
public function getServiceName() | ||
{ | ||
return $this->serviceName; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
<?php | ||
namespace EightPoints\Bundle\GuzzleBundle\Middleware; | ||
|
||
|
||
use EightPoints\Bundle\GuzzleBundle\Events\PostTransactionEvent; | ||
use Psr\Http\Message\RequestInterface; | ||
use Psr\Http\Message\ResponseInterface; | ||
use Symfony\Component\EventDispatcher\EventDispatcherInterface; | ||
use EightPoints\Bundle\GuzzleBundle\Events\GuzzleEvents; | ||
use EightPoints\Bundle\GuzzleBundle\Events\PreTransactionEvent; | ||
|
||
/** | ||
* Class EventDispatchMiddleware | ||
* | ||
* Dispatches an Event using the Symfony Event Dispatcher. | ||
* Dispatches a PRE_TRANSACTION event, before the transaction is sent | ||
* Dispatches a POST_TRANMSACTION event, when the remote hosts responds. | ||
* | ||
* @package EightPoints\Bundle\GuzzleBundle\Middleware | ||
*/ | ||
class EventDispatchMiddleware | ||
{ | ||
/** | ||
* @var EventDispatcherInterface | ||
*/ | ||
private $eventDispatcher; | ||
private $serviceName; | ||
|
||
/** | ||
* EventDispatchMiddleware constructor. | ||
* | ||
* @param EventDispatcherInterface $eventDispatcher | ||
*/ | ||
public function __construct(EventDispatcherInterface $eventDispatcher, $serviceName) | ||
{ | ||
$this->eventDispatcher = $eventDispatcher; | ||
$this->serviceName = $serviceName; | ||
} | ||
|
||
/** | ||
* @return \Closure | ||
*/ | ||
public function dispatchEvent() | ||
{ | ||
return function (callable $handler) { | ||
return function ( | ||
RequestInterface $request, | ||
array $options | ||
) use ( $handler ) { | ||
// Create the Pre Transaction event. | ||
$preTransactionEvent = new PreTransactionEvent($request, $this->serviceName); | ||
|
||
// Dispatch it through the symfony Dispatcher. | ||
$this->eventDispatcher->dispatch(GuzzleEvents::PRE_TRANSACTION, $preTransactionEvent); | ||
|
||
// Continue the handler chain. | ||
$promise = $handler($preTransactionEvent->getTransaction(), $options); | ||
// Handle the response form teh server. | ||
return $promise->then( | ||
function (ResponseInterface $response) { | ||
// Create hte Post Transaction event. | ||
$postTransactionEvent = new PostTransactionEvent($response, $this->serviceName); | ||
|
||
// Dispatch the event on the symfony event dispatcher. | ||
$this->eventDispatcher->dispatch(GuzzleEvents::POST_TRANSACTION, $postTransactionEvent); | ||
|
||
// Continue down the chain. | ||
return $postTransactionEvent->getTransaction(); | ||
} | ||
); | ||
}; | ||
}; | ||
} | ||
} |
Oops, something went wrong.