|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Knp\RadBundle\DomainEvent\Dispatcher; |
|
4
|
|
|
|
|
5
|
|
|
use Doctrine\Common\EventSubscriber; |
|
6
|
|
|
use Symfony\Component\DependencyInjection\ContainerInterface; |
|
7
|
|
|
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException; |
|
8
|
|
|
use Symfony\Component\DependencyInjection\Exception\ParameterNotFoundException; |
|
9
|
|
|
|
|
10
|
|
|
class Delayed implements EventSubscriber |
|
11
|
|
|
{ |
|
12
|
|
|
private $container; |
|
13
|
|
|
private $delayedEventNames; |
|
14
|
|
|
private $events = array(); |
|
15
|
|
|
|
|
16
|
|
|
public function __construct(ContainerInterface $container, array $delayedEventNames = []) |
|
17
|
|
|
{ |
|
18
|
|
|
$this->container = $container; |
|
19
|
|
|
$this->delayedEventNames = $delayedEventNames; |
|
20
|
|
|
} |
|
21
|
|
|
|
|
22
|
|
|
public function __call($method, array $arguments) |
|
23
|
|
|
{ |
|
24
|
|
|
if (!in_array(substr($method, 2), $this->delayedEventNames)) { |
|
25
|
|
|
throw new \BadMethodCallException; |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
$this->events[] = $arguments[0]; |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
public function getSubscribedEvents() |
|
32
|
|
|
{ |
|
33
|
|
|
return $this->prepareEventNames($this->delayedEventNames); |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
public function dispatchDelayedDomainEvents() |
|
37
|
|
|
{ |
|
38
|
|
|
$eventManager = $this->container->get('doctrine.dbal.default_connection.event_manager'); |
|
39
|
|
|
foreach ($this->events as $event) { |
|
40
|
|
|
$eventManager->dispatchEvent('onDelayed' . $event->getName(), $event); |
|
41
|
|
|
} |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
private function prepareEventNames(array $events) |
|
45
|
|
|
{ |
|
46
|
|
|
return array_map(function ($element) { |
|
47
|
|
|
return sprintf('on%s', $element); |
|
48
|
|
|
}, $events); |
|
49
|
|
|
} |
|
50
|
|
|
} |
|
51
|
|
|
|