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
|
|
|
* @package Phossa2\Event |
26
|
|
|
* @author Hong Zhang <[email protected]> |
27
|
|
|
* @version 2.0.0 |
28
|
|
|
* @since 2.0.0 added |
29
|
|
|
*/ |
30
|
|
|
class StaticEventDispatcher extends StaticAbstract |
31
|
|
|
{ |
32
|
|
|
/** |
33
|
|
|
* slave event manager |
34
|
|
|
* |
35
|
|
|
* @var EventManagerInterface[] |
36
|
|
|
* @access protected |
37
|
|
|
* @staticvar |
38
|
|
|
*/ |
39
|
|
|
protected static $event_manager = []; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Provides a static interface for event dispatcher's dynamic methods |
43
|
|
|
* |
44
|
|
|
* @param string $name method name |
45
|
|
|
* @param array $arguments arguments |
46
|
|
|
* @return mixed |
47
|
|
|
* @access public |
48
|
|
|
* @static |
49
|
|
|
* @internal |
50
|
|
|
*/ |
51
|
|
|
public static function __callStatic($name, array $arguments) |
52
|
|
|
{ |
53
|
|
|
$mgr = static::getEventManager(); |
54
|
|
|
if (method_exists($mgr, $name)) { |
55
|
|
|
return call_user_func_array([$mgr, $name], $arguments); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
throw new BadMethodCallException( |
59
|
|
|
Message::get( |
60
|
|
|
Message::MSG_METHOD_NOTFOUND, |
61
|
|
|
$name, |
62
|
|
|
get_called_class() |
63
|
|
|
), |
64
|
|
|
Message::MSG_METHOD_NOTFOUND |
65
|
|
|
); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Set the inner event manager |
70
|
|
|
* |
71
|
|
|
* @param EventManagerInterface $eventManager |
72
|
|
|
* @access public |
73
|
|
|
* @api |
74
|
|
|
* @static |
75
|
|
|
*/ |
76
|
|
|
public static function setEventManager(EventManagerInterface $eventManager) |
77
|
|
|
{ |
78
|
|
|
self::$event_manager[get_called_class()] = $eventManager; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Get the inner event manager |
83
|
|
|
* |
84
|
|
|
* @return EventManagerInterface $eventManager |
85
|
|
|
* @access public |
86
|
|
|
* @api |
87
|
|
|
* @static |
88
|
|
|
*/ |
89
|
|
|
public static function getEventManager() |
90
|
|
|
{ |
91
|
|
|
if (!isset(self::$event_manager[get_called_class()])) { |
92
|
|
|
self::$event_manager[get_called_class()] = |
93
|
|
|
EventDispatcher::getShareable('__STATIC__'); |
94
|
|
|
} |
95
|
|
|
return self::$event_manager[get_called_class()]; |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|