-
Notifications
You must be signed in to change notification settings - Fork 2
/
EventManager.php
executable file
·65 lines (61 loc) · 1.83 KB
/
EventManager.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
<?php
/**
* EventManager class file.
* @copyright (c) 2013, Galament
* @license http://www.opensource.org/licenses/bsd-license.php
*/
namespace bariew\eventManager;
use yii\base\Component;
use yii\base\Event;
/**
* Attaches events to all app models.
*
* @author Pavel Bariev <[email protected]>
*/
class EventManager extends Component
{
/**
* System wide models events settings -
* an array with structure: [
* $eventSenderClassName => [
* $eventName => [
* [$handlerClassName, $handlerMethodName]
* ]
* ]
* ]
*
* @since 1.3.0 handler can also keep additional data and $append boolean as for Event::on() method eg:
* ... [[$handlerClassName, $handlerMethodName], ['myData'], false]
*
* @var array events settings
*/
public $events = [];
/**
* @inheritdoc
*/
public function init()
{
parent::init();
$this->attachEvents($this->events);
}
/**
* attaches all events to all classNames
* @param array $eventConfig commonly $this->events config
*/
public function attachEvents($eventConfig)
{
foreach ($eventConfig as $className => $events) {
foreach ($events as $eventName => $handlers) {
foreach ($handlers as $handler) {
if (is_array($handler) && is_callable($handler[0])) {
$data = isset($handler[1]) ? array_pop($handler) : null;
$append = isset($handler[2]) ? array_pop($handler) : null;
Event::on($className, $eventName, $handler[0], $data, $append);
} else if (is_callable($handler)){
Event::on($className, $eventName, $handler);
}
}
}
}
}
}