1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Inji core |
4
|
|
|
* |
5
|
|
|
* @author Alexey Krupskiy <[email protected]> |
6
|
|
|
* @link http://inji.ru/ |
7
|
|
|
* @copyright 2015 Alexey Krupskiy |
8
|
|
|
* @license https://github.com/injitools/cms-Inji/blob/master/LICENSE |
9
|
|
|
*/ |
10
|
|
|
class Inji { |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Static storage for core object |
14
|
|
|
* |
15
|
|
|
* @var Inji |
16
|
|
|
*/ |
17
|
|
|
public static $inst = null; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Dynamic events listeners |
21
|
|
|
* |
22
|
|
|
* @var array |
23
|
|
|
*/ |
24
|
|
|
private $_listeners = []; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Core config |
28
|
|
|
* |
29
|
|
|
* @var array |
30
|
|
|
*/ |
31
|
|
|
public static $config = []; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Static storage for anything |
35
|
|
|
* |
36
|
|
|
* @var array |
37
|
|
|
*/ |
38
|
|
|
public static $storage = []; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Stop executing code if this true after use Inji::$inst->stop() constuction in code |
42
|
|
|
* |
43
|
|
|
* @var boolean |
44
|
|
|
*/ |
45
|
|
|
public $exitOnStop = true; |
46
|
|
|
public $parallelLockFileStream = null; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Add event listener |
50
|
|
|
* |
51
|
|
|
* @param string $eventName |
52
|
|
|
* @param string $listenCode |
53
|
|
|
* @param array|string|closure $callback |
54
|
|
|
* @param boolean $save |
55
|
|
|
*/ |
56
|
4 |
|
public function listen($eventName, $listenCode, $callback, $save = false) { |
57
|
4 |
|
if ($save) { |
58
|
1 |
|
$config = Inji\Config::custom(Inji\App::$primary->path . '/config/events.php'); |
59
|
1 |
|
$config[$eventName][$listenCode] = serialize($callback); |
60
|
1 |
|
Inji\Config::save(Inji\App::$primary->path . '/config/events.php', $config); |
61
|
|
|
} else { |
62
|
3 |
|
$this->_listeners[$eventName][$listenCode] = $callback; |
63
|
|
|
} |
64
|
4 |
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Throw event |
68
|
|
|
* |
69
|
|
|
* @param string $eventName |
70
|
|
|
* @param mixed $eventObject |
71
|
|
|
* @return mixed |
72
|
|
|
*/ |
73
|
10 |
|
public function event($eventName, $eventObject = null) { |
74
|
|
|
$event = [ |
75
|
10 |
|
'eventName' => $eventName, |
76
|
10 |
|
'eventObject' => $eventObject, |
77
|
|
|
]; |
78
|
|
|
|
79
|
10 |
|
$listeners = []; |
80
|
10 |
|
if (!empty($this->_listeners[$eventName])) { |
81
|
3 |
|
$listeners = $this->_listeners[$eventName]; |
82
|
|
|
} |
83
|
10 |
|
$config = Inji\Config::custom(Inji\App::$primary->path . '/config/events.php'); |
84
|
10 |
|
if (!empty($config[$eventName])) { |
85
|
1 |
|
foreach ($config[$eventName] as $listenCode => $callback) { |
86
|
1 |
|
$listeners[$listenCode] = (@unserialize($callback) !== false) ? unserialize($callback) : $callback; |
87
|
|
|
} |
88
|
|
|
} |
89
|
10 |
|
if ($listeners) { |
90
|
4 |
|
$iteration = 0; |
91
|
4 |
|
$calledBefore = []; |
92
|
4 |
|
foreach ($listeners as $listenCode => $callback) { |
93
|
4 |
|
$event['iteration'] = ++$iteration; |
94
|
4 |
|
$event['calledBefore'] = $calledBefore; |
95
|
4 |
|
if (is_callable($callback)) { |
96
|
3 |
|
$event['eventObject'] = $callback($event); |
97
|
1 |
|
} elseif (is_array($callback) && isset($callback['callback'])) { |
98
|
1 |
|
$event['eventObject'] = $callback['callback']($event, $callback); |
99
|
|
|
} else { |
100
|
|
|
$event['eventObject'] = Inji\App::$cur->{$callback['module']}->{$callback['method']}($event, $callback); |
101
|
|
|
} |
102
|
4 |
|
$calledBefore[$iteration] = $listenCode; |
103
|
|
|
} |
104
|
|
|
} |
105
|
10 |
|
return $event['eventObject']; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Unlisten event |
110
|
|
|
* |
111
|
|
|
* @param string $eventName |
112
|
|
|
* @param string $listenCode |
113
|
|
|
* @param boolean $save |
114
|
|
|
*/ |
115
|
2 |
|
public function unlisten($eventName, $listenCode, $save = false) { |
116
|
2 |
|
if ($save) { |
117
|
1 |
|
$config = Inji\Config::custom(Inji\App::$primary->path . '/config/events.php'); |
118
|
1 |
|
if (!empty($config[$eventName][$listenCode])) { |
119
|
1 |
|
unset($config[$eventName][$listenCode]); |
120
|
1 |
|
Inji\Config::save(Inji\App::$primary->path . '/config/events.php', $config); |
121
|
|
|
} |
122
|
|
|
} |
123
|
2 |
|
if (!empty($this->_listeners[$eventName][$listenCode])) { |
124
|
1 |
|
unset($this->_listeners[$eventName][$listenCode]); |
125
|
|
|
} |
126
|
2 |
|
} |
127
|
|
|
|
128
|
|
|
public function stop() { |
129
|
|
|
if ($this->exitOnStop) { |
130
|
|
|
exit(); |
|
|
|
|
131
|
|
|
} |
132
|
|
|
return false; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
public function blockParallel() { |
136
|
|
|
$this->parallelLockFileStream = fopen('lock.file', 'w+'); |
137
|
|
|
if (!flock($this->parallelLockFileStream, LOCK_EX | LOCK_NB)) { |
|
|
|
|
138
|
|
|
return false; |
139
|
|
|
} |
140
|
|
|
return true; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
public function unBlockParallel() { |
144
|
|
|
if (is_resource($this->parallelLockFileStream)) { |
145
|
|
|
flock($this->parallelLockFileStream, LOCK_UN); |
146
|
|
|
fclose($this->parallelLockFileStream); |
147
|
|
|
} |
148
|
|
|
} |
149
|
|
|
} |
In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.