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; |
16
|
|
|
|
17
|
|
|
use Phossa2\Event\Message\Message; |
18
|
|
|
use Phossa2\Shared\Base\StaticAbstract; |
19
|
|
|
use Phossa2\Event\Exception\BadMethodCallException; |
20
|
|
|
use Phossa2\Event\Interfaces\EventManagerInterface; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* StaticEventDispatcher |
24
|
|
|
* |
25
|
|
|
* A static wrapper of EventDispatcher |
26
|
|
|
* |
27
|
|
|
* ```php |
28
|
|
|
* // attach |
29
|
|
|
* StaticEventDispatcher::attach('test', function() { echo 'test'; }); |
30
|
|
|
* |
31
|
|
|
* // trigger |
32
|
|
|
* StaticEventDispatcher::trigger('test'); |
33
|
|
|
* ``` |
34
|
|
|
* |
35
|
|
|
* @package Phossa2\Event |
36
|
|
|
* @author Hong Zhang <[email protected]> |
37
|
|
|
* @version 2.1.0 |
38
|
|
|
* @since 2.0.0 added |
39
|
|
|
* @since 2.1.0 updated |
40
|
|
|
*/ |
41
|
|
|
class StaticEventDispatcher extends StaticAbstract |
42
|
|
|
{ |
43
|
|
|
/** |
44
|
|
|
* slave event manager |
45
|
|
|
* |
46
|
|
|
* @var EventManagerInterface[] |
47
|
|
|
* @access protected |
48
|
|
|
* @staticvar |
49
|
|
|
*/ |
50
|
|
|
protected static $event_manager = []; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* default static scope |
54
|
|
|
* |
55
|
|
|
* @var string |
56
|
|
|
* @access protected |
57
|
|
|
* @staticvar |
58
|
|
|
*/ |
59
|
|
|
protected static $static_scope = '__STATIC__'; |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Provides a static interface for event dispatcher's dynamic methods |
63
|
|
|
* |
64
|
|
|
* @param string $name method name |
65
|
|
|
* @param array $arguments arguments |
66
|
|
|
* @return mixed |
67
|
|
|
* @throws BadMethodCallException if method not found |
68
|
|
|
* @access public |
69
|
|
|
* @static |
70
|
|
|
* @internal |
71
|
|
|
*/ |
72
|
|
|
public static function __callStatic($name, array $arguments) |
73
|
|
|
{ |
74
|
|
|
$mgr = static::getEventManager(); |
75
|
|
|
if (method_exists($mgr, $name)) { |
76
|
|
|
return call_user_func_array([$mgr, $name], $arguments); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
throw new BadMethodCallException( |
80
|
|
|
Message::get( |
81
|
|
|
Message::MSG_METHOD_NOTFOUND, |
82
|
|
|
$name, |
83
|
|
|
get_called_class() |
84
|
|
|
), |
85
|
|
|
Message::MSG_METHOD_NOTFOUND |
86
|
|
|
); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Set the inner event manager |
91
|
|
|
* |
92
|
|
|
* @param EventManagerInterface $eventManager |
93
|
|
|
* @access public |
94
|
|
|
* @api |
95
|
|
|
* @static |
96
|
|
|
*/ |
97
|
|
|
public static function setEventManager(EventManagerInterface $eventManager) |
98
|
|
|
{ |
99
|
|
|
self::$event_manager[get_called_class()] = $eventManager; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Get the inner event manager |
104
|
|
|
* |
105
|
|
|
* @return EventManagerInterface $eventManager |
106
|
|
|
* @access public |
107
|
|
|
* @api |
108
|
|
|
* @static |
109
|
|
|
*/ |
110
|
|
|
public static function getEventManager() |
111
|
|
|
{ |
112
|
|
|
if (!isset(self::$event_manager[get_called_class()])) { |
113
|
|
|
self::$event_manager[get_called_class()] = |
114
|
|
|
EventDispatcher::getShareable(static::$static_scope); |
115
|
|
|
} |
116
|
|
|
return self::$event_manager[get_called_class()]; |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
|