1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* GpsLab component. |
4
|
|
|
* |
5
|
|
|
* @author Peter Gribanov <[email protected]> |
6
|
|
|
* @copyright Copyright (c) 2016, Peter Gribanov |
7
|
|
|
* @license http://opensource.org/licenses/MIT |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace GpsLab\Domain\Event\Aggregator; |
11
|
|
|
|
12
|
|
|
use GpsLab\Domain\Event\EventInterface; |
13
|
|
|
use GpsLab\Domain\Event\NameResolver\EventNameResolverInterface; |
14
|
|
|
use GpsLab\Domain\Event\NameResolver\NameResolverContainer; |
15
|
|
|
|
16
|
|
|
trait AggregateEventsRaiseInSelfTrait |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* @var EventInterface[] |
20
|
|
|
*/ |
21
|
|
|
private $events = []; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @deprecated It will be removed in 2.0 |
25
|
|
|
* |
26
|
|
|
* @var EventNameResolverInterface |
27
|
|
|
*/ |
28
|
|
|
private $resolver; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @deprecated It will be removed in 2.0. If you want change the event name resolver, you must override getMethodNameFromEvent() method. |
32
|
|
|
* @see AggregateEventsRaiseInSelfTrait::getMethodNameFromEvent() |
33
|
|
|
* |
34
|
|
|
* @param EventNameResolverInterface $resolver |
35
|
|
|
*/ |
36
|
|
|
protected function changeEventNameResolver(EventNameResolverInterface $resolver) |
37
|
|
|
{ |
38
|
|
|
trigger_error('It will be removed in 2.0. If you want change the event name resolver, you must override getMethodNameFromEvent() method.', E_USER_DEPRECATED); |
39
|
|
|
|
40
|
|
|
$this->resolver = $resolver; |
|
|
|
|
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @param EventInterface $event |
45
|
|
|
*/ |
46
|
3 |
View Code Duplication |
private function raiseInSelf(EventInterface $event) |
|
|
|
|
47
|
|
|
{ |
48
|
3 |
|
$method = $this->getMethodNameFromEvent($event); |
49
|
|
|
|
50
|
|
|
// if method is not exists is not a critical error |
51
|
3 |
|
if (method_exists($this, $method)) { |
52
|
2 |
|
call_user_func([$this, $method], $event); |
53
|
2 |
|
} |
54
|
3 |
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @param EventInterface $event |
58
|
|
|
*/ |
59
|
3 |
|
protected function raise(EventInterface $event) |
60
|
|
|
{ |
61
|
3 |
|
$this->events[] = $event; |
62
|
3 |
|
$this->raiseInSelf($event); |
63
|
3 |
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @return EventInterface[] |
67
|
|
|
*/ |
68
|
2 |
|
public function pullEvents() |
69
|
|
|
{ |
70
|
2 |
|
$events = $this->events; |
71
|
2 |
|
$this->events = []; |
72
|
|
|
|
73
|
2 |
|
return $events; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Get handler method name from event. |
78
|
|
|
* |
79
|
|
|
* Override this method if you want to change algorithm to generate the handler method name. |
80
|
|
|
* |
81
|
|
|
* @param EventInterface $event |
82
|
|
|
* |
83
|
|
|
* @return string |
84
|
|
|
*/ |
85
|
3 |
|
protected function getMethodNameFromEvent(EventInterface $event) |
86
|
|
|
{ |
87
|
|
|
// BC: use custom event name resolver if exists |
88
|
3 |
|
if ($this->resolver instanceof EventNameResolverInterface) { |
|
|
|
|
89
|
1 |
|
return 'on'.$this->resolver->getEventName($event); |
|
|
|
|
90
|
|
|
} |
91
|
|
|
|
92
|
2 |
|
return 'on'.NameResolverContainer::getResolver()->getEventName($event); |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|
This property has been deprecated. The supplier of the class has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the property will be removed from the class and what other property to use instead.