1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Phossa Project |
4
|
|
|
* |
5
|
|
|
* PHP version 5.4 |
6
|
|
|
* |
7
|
|
|
* @category Library |
8
|
|
|
* @package Phossa2\Event |
9
|
|
|
* @copyright Copyright (c) 2016 phossa.com |
10
|
|
|
* @license http://mit-license.org/ MIT License |
11
|
|
|
* @link http://www.phossa.com/ |
12
|
|
|
*/ |
13
|
|
|
/*# declare(strict_types=1); */ |
14
|
|
|
|
15
|
|
|
namespace Phossa2\Event\Traits; |
16
|
|
|
|
17
|
|
|
use Phossa2\Event\Event; |
18
|
|
|
use Phossa2\Event\EventDispatcher; |
19
|
|
|
use Phossa2\Event\Interfaces\ListenerInterface; |
20
|
|
|
use Phossa2\Event\Interfaces\EventManagerInterface; |
21
|
|
|
use Phossa2\Event\Interfaces\ListenerAwareInterface; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Implementation of EventCapableInterface |
25
|
|
|
* |
26
|
|
|
* @package Phossa2\Event |
27
|
|
|
* @author Hong Zhang <[email protected]> |
28
|
|
|
* @see EventCapableInterface |
29
|
|
|
* @version 2.1.1 |
30
|
|
|
* @since 2.0.0 added |
31
|
|
|
* @since 2.1.1 updated |
32
|
|
|
*/ |
33
|
|
|
trait EventCapableTrait |
34
|
|
|
{ |
35
|
|
|
use EventPrototypeTrait; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* event manager or dispatcher |
39
|
|
|
* |
40
|
|
|
* @var EventManagerInterface |
41
|
|
|
* @access protected |
42
|
|
|
*/ |
43
|
|
|
protected $event_manager; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* {@inheritDoc} |
47
|
|
|
*/ |
48
|
|
|
public function setEventManager( |
49
|
|
|
EventManagerInterface $eventManager |
50
|
|
|
) { |
51
|
|
|
$this->event_manager = $eventManager; |
52
|
|
|
|
53
|
|
|
// attach events from $this |
54
|
|
|
if ($eventManager instanceof ListenerAwareInterface && |
55
|
|
|
$this instanceof ListenerInterface |
56
|
|
|
) { |
57
|
|
|
$eventManager->attachListener($this); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
return $this; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* {@inheritDoc} |
65
|
|
|
*/ |
66
|
|
|
public function getEventManager()/*# : EventManagerInterface */ |
67
|
|
|
{ |
68
|
|
|
// create the default slave |
69
|
|
|
if (is_null($this->event_manager)) { |
70
|
|
|
// add classname as a scope |
71
|
|
|
$this->setEventManager(new EventDispatcher(get_class($this))); |
72
|
|
|
} |
73
|
|
|
return $this->event_manager; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* {@inheritDoc} |
78
|
|
|
*/ |
79
|
|
|
public function trigger( |
80
|
|
|
/*# string */ $eventName, |
81
|
|
|
array $parameters = [] |
82
|
|
|
)/*# : bool */ { |
83
|
|
|
$evt = $this->newEvent($eventName, $this, $parameters); |
84
|
|
|
return $this->getEventManager()->trigger($evt); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* {@inheritDoc} |
89
|
|
|
*/ |
90
|
|
|
public function triggerEvent( |
91
|
|
|
/*# string */ $eventName, |
92
|
|
|
array $parameters = [] |
93
|
|
|
)/*# : EventInterface */ { |
94
|
|
|
$evt = $this->newEvent($eventName, $this, $parameters); |
95
|
|
|
$this->getEventManager()->trigger($evt); |
96
|
|
|
return $evt; |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|