-
Notifications
You must be signed in to change notification settings - Fork 439
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 #129 from php-enqueue/bundle-do-not-use-container-…
…aware-event-dispatcher [bundle] Extend EventDispatcher instead of container aware one.
- Loading branch information
Showing
55 changed files
with
1,296 additions
and
372 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
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,112 @@ | ||
# Async event dispatcher (Symfony) | ||
|
||
The doc shows how you can setup async event dispatching in plain PHP. | ||
If you are looking for the ways to use it in Symfony application [read this post instead](../bundle/async_events.md) | ||
|
||
* [Installation](#installation) | ||
* [Configuration](#configuration) | ||
* [Dispatch event](#dispatch-event) | ||
* [Process async events](#process-async-events) | ||
|
||
## Installation | ||
|
||
You need the async dispatcher library and a one of [the supported transports](../transport) | ||
|
||
```bash | ||
$ composer require enqueue/async-event-dispatcher enqueue/fs | ||
``` | ||
|
||
## Configuration | ||
|
||
```php | ||
<?php | ||
|
||
// config.php | ||
|
||
use Enqueue\AsyncEventDispatcher\AsyncListener; | ||
use Enqueue\AsyncEventDispatcher\AsyncProcessor; | ||
use Enqueue\AsyncEventDispatcher\PhpSerializerEventTransformer; | ||
use Enqueue\AsyncEventDispatcher\AsyncEventDispatcher; | ||
use Enqueue\AsyncEventDispatcher\SimpleRegistry; | ||
use Enqueue\Fs\FsConnectionFactory; | ||
use Symfony\Component\EventDispatcher\EventDispatcher; | ||
|
||
require_once __DIR__.'/vendor/autoload.php'; | ||
|
||
// it could be any other enqueue/psr-queue compatible context. | ||
$context = (new FsConnectionFactory('file://'.__DIR__.'/queues'))->createContext(); | ||
$eventQueue = $context->createQueue('symfony_events'); | ||
|
||
$registry = new SimpleRegistry( | ||
['the_event' => 'default'], | ||
['default' => new PhpSerializerEventTransformer($context, true)] | ||
); | ||
|
||
$asyncListener = new AsyncListener($context, $registry, $eventQueue); | ||
|
||
$dispatcher = new EventDispatcher(); | ||
|
||
// the listener sends even as a message through MQ | ||
$dispatcher->addListener('the_event', $asyncListener); | ||
|
||
$asyncDispatcher = new AsyncEventDispatcher($dispatcher, $asyncListener); | ||
|
||
// the listener is executed on consumer side. | ||
$asyncDispatcher->addListener('the_event', function() { | ||
}); | ||
|
||
$asyncProcessor = new AsyncProcessor($registry, $asyncDispatcher); | ||
``` | ||
|
||
## Dispatch event | ||
|
||
```php | ||
<?php | ||
|
||
// send.php | ||
|
||
use Symfony\Component\EventDispatcher\GenericEvent; | ||
|
||
require_once __DIR__.'/vendor/autoload.php'; | ||
|
||
include __DIR__.'/config.php'; | ||
|
||
$dispatcher->dispatch('the_event', new GenericEvent('theSubject')); | ||
``` | ||
|
||
## Process async events | ||
|
||
```php | ||
<?php | ||
|
||
// consume.php | ||
|
||
use Enqueue\Psr\PsrProcessor; | ||
|
||
require_once __DIR__.'/vendor/autoload.php'; | ||
include __DIR__.'/config.php'; | ||
|
||
$consumer = $context->createConsumer($eventQueue); | ||
|
||
while (true) { | ||
if ($message = $consumer->receive(5000)) { | ||
$result = $asyncProcessor->process($message, $context); | ||
|
||
switch ((string) $result) { | ||
case PsrProcessor::ACK: | ||
$consumer->acknowledge($message); | ||
break; | ||
case PsrProcessor::REJECT: | ||
$consumer->reject($message); | ||
break; | ||
case PsrProcessor::REQUEUE: | ||
$consumer->reject($message, true); | ||
break; | ||
default: | ||
throw new \LogicException('Result is not supported'); | ||
} | ||
} | ||
} | ||
``` | ||
|
||
[back to index](../index.md) |
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
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 |
---|---|---|
@@ -1,3 +1,10 @@ | ||
parameters: | ||
excludes_analyse: | ||
- pkg/enqueue/Util/UUID.php | ||
- pkg/enqueue/Util/UUID.php | ||
- pkg/enqueue-bundle/Tests/Functional/App | ||
- pkg/job-queue/Test/JobRunner.php | ||
- pkg/job-queue/Tests/Functional/app/AppKernel.php | ||
- pkg/redis/PhpRedis.php | ||
- pkg/redis/RedisConnectionFactory.php | ||
- pkg/gearman | ||
- pkg/amqp-ext/AmqpConsumer.php |
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,7 @@ | ||
*~ | ||
/composer.lock | ||
/composer.phar | ||
/phpunit.xml | ||
/vendor/ | ||
/.idea/ | ||
Tests/Functional/queues |
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,21 @@ | ||
sudo: false | ||
|
||
git: | ||
depth: 1 | ||
|
||
language: php | ||
|
||
php: | ||
- '5.6' | ||
- '7.0' | ||
|
||
cache: | ||
directories: | ||
- $HOME/.composer/cache | ||
|
||
install: | ||
- composer self-update | ||
- composer install --prefer-source --ignore-platform-reqs | ||
|
||
script: | ||
- vendor/bin/phpunit --exclude-group=functional |
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,57 @@ | ||
<?php | ||
|
||
namespace Enqueue\AsyncEventDispatcher; | ||
|
||
use Symfony\Component\EventDispatcher\Event; | ||
use Symfony\Component\EventDispatcher\EventDispatcher; | ||
use Symfony\Component\EventDispatcher\EventDispatcherInterface; | ||
|
||
class AsyncEventDispatcher extends EventDispatcher | ||
{ | ||
/** | ||
* @var EventDispatcherInterface | ||
*/ | ||
private $trueEventDispatcher; | ||
|
||
/** | ||
* @var AsyncListener | ||
*/ | ||
private $asyncListener; | ||
|
||
/** | ||
* @param EventDispatcherInterface $trueEventDispatcher | ||
* @param AsyncListener $asyncListener | ||
*/ | ||
public function __construct(EventDispatcherInterface $trueEventDispatcher, AsyncListener $asyncListener) | ||
{ | ||
$this->trueEventDispatcher = $trueEventDispatcher; | ||
$this->asyncListener = $asyncListener; | ||
} | ||
|
||
/** | ||
* This method dispatches only those listeners that were marked as async. | ||
* | ||
* @param string $eventName | ||
* @param Event|null $event | ||
*/ | ||
public function dispatchAsyncListenersOnly($eventName, Event $event = null) | ||
{ | ||
try { | ||
$this->asyncListener->syncMode($eventName); | ||
|
||
parent::dispatch($eventName, $event); | ||
} finally { | ||
$this->asyncListener->resetSyncMode(); | ||
} | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function dispatch($eventName, Event $event = null) | ||
{ | ||
parent::dispatch($eventName, $event); | ||
|
||
$this->trueEventDispatcher->dispatch($eventName, $event); | ||
} | ||
} |
Oops, something went wrong.