1
|
|
|
<?php |
2
|
|
|
namespace Ffcms\Core\Managers; |
3
|
|
|
|
4
|
|
|
use Ffcms\Core\App; |
5
|
|
|
use Ffcms\Core\Helper\Type\Obj; |
6
|
|
|
use Ffcms\Core\Helper\Type\Str; |
7
|
|
|
use Ffcms\Core\Helper\FileSystem\Directory; |
8
|
|
|
use Ffcms\Core\Helper\FileSystem\File; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Class EventManager. Control and run events. |
12
|
|
|
* @package \Ffcms\Core\Event |
13
|
|
|
* @author zenn |
14
|
|
|
*/ |
15
|
|
|
class EventManager |
16
|
|
|
{ |
17
|
|
|
/** @var array $events */ |
18
|
|
|
private $events; |
19
|
|
|
private $runned; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* EventManager constructor. Get always initiated data from memory storage. |
23
|
|
|
*/ |
24
|
|
|
public function __construct() |
25
|
|
|
{ |
26
|
|
|
// get events from memory object saver |
27
|
|
|
$this->events = App::$Memory->get('events.catched.save'); |
|
|
|
|
28
|
|
|
$this->runned = App::$Memory->get('events.runned.save'); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** Catch the event if it occurred after this initiation of interception |
32
|
|
|
* @param string|array $event |
33
|
|
|
* @param \Closure $callback |
34
|
|
|
*/ |
35
|
|
|
public function on($event, \Closure $callback) |
36
|
|
|
{ |
37
|
|
|
// check if event is a single string and parse it to array single item |
38
|
|
|
if (!Obj::isArray($event)) { |
39
|
|
|
$event = [$event]; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
foreach ($event as $item) { |
|
|
|
|
43
|
|
|
$this->events[$item][] = $callback; |
44
|
|
|
} |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Catch the event if it occurred before the initiation of interception |
49
|
|
|
* @param string|array $event |
50
|
|
|
* @param \Closure $callback |
51
|
|
|
* @return mixed |
52
|
|
|
*/ |
53
|
|
|
public function listen($event, \Closure $callback) |
54
|
|
|
{ |
55
|
|
|
// check if $event is a single string and set it as array with one item |
56
|
|
|
if (!Obj::isArray($event)) { |
57
|
|
|
$event = [$event]; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
// each every one event in array |
61
|
|
|
foreach ($event as $item) { |
|
|
|
|
62
|
|
|
if (Obj::isArray($this->runned) && array_key_exists($item, $this->runned)) { |
63
|
|
|
return call_user_func_array($callback, $this->runned[$item]); |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
return false; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Initialize event on happend |
72
|
|
|
* @return mixed |
73
|
|
|
*/ |
74
|
|
|
public function run() |
75
|
|
|
{ |
76
|
|
|
// dynamicly parse input params |
77
|
|
|
$args = func_get_args(); |
78
|
|
|
|
79
|
|
|
if (count($args) < 1) { |
80
|
|
|
return false; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
// get event name |
84
|
|
|
$eventName = array_shift($args); |
85
|
|
|
// get event args as array if passed |
86
|
|
|
$eventArgs = @array_shift($args); |
87
|
|
|
|
88
|
|
|
// if event is registered |
89
|
|
|
if (isset($this->events[$eventName]) && Obj::isArray($this->events[$eventName])) { |
90
|
|
|
foreach ($this->events[$eventName] as $callback) { |
91
|
|
|
// call anonymous function with args if passed |
92
|
|
|
return call_user_func_array($callback, $eventArgs); |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
// set to post runned actions |
97
|
|
|
$this->runned[$eventName] = $eventArgs; |
98
|
|
|
return false; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Save events data in memory to prevent any sh@ts ;D |
103
|
|
|
*/ |
104
|
|
|
public function __destruct() |
105
|
|
|
{ |
106
|
|
|
App::$Memory->set('events.catched.save', $this->events); |
107
|
|
|
App::$Memory->set('events.runned.save', $this->runned); |
108
|
|
|
|
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
} |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..