|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* CakeCMS Core |
|
4
|
|
|
* |
|
5
|
|
|
* This file is part of the of the simple cms based on CakePHP 3. |
|
6
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
7
|
|
|
* file that was distributed with this source code. |
|
8
|
|
|
* |
|
9
|
|
|
* @package Core |
|
10
|
|
|
* @license MIT |
|
11
|
|
|
* @copyright MIT License http://www.opensource.org/licenses/mit-license.php |
|
12
|
|
|
* @link https://github.com/CakeCMS/Core". |
|
13
|
|
|
* @author Sergey Kalistratov <[email protected]> |
|
14
|
|
|
*/ |
|
15
|
|
|
|
|
16
|
|
|
namespace Core\Event; |
|
17
|
|
|
|
|
18
|
|
|
use Core\Plugin; |
|
19
|
|
|
use Cake\Core\App; |
|
20
|
|
|
use Cake\Event\Event; |
|
21
|
|
|
use Cake\Event\EventManager as CakeEventManager; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* Class Manager |
|
25
|
|
|
* |
|
26
|
|
|
* @package Core\Event |
|
27
|
|
|
*/ |
|
28
|
|
|
class EventManager extends CakeEventManager |
|
29
|
|
|
{ |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* Load Event Handlers during bootstrap. |
|
33
|
|
|
* |
|
34
|
|
|
* Plugins can add their own custom EventHandler in Config/events.php |
|
35
|
|
|
* with the following format: |
|
36
|
|
|
* |
|
37
|
|
|
* 'events' => [ |
|
38
|
|
|
* 'Core.CoreEventHandler' => [ |
|
39
|
|
|
* 'options' = [ |
|
40
|
|
|
* 'callable' => '', |
|
41
|
|
|
* 'priority' => '', |
|
42
|
|
|
* ] |
|
43
|
|
|
* ] |
|
44
|
|
|
* ] |
|
45
|
|
|
* |
|
46
|
|
|
* @return void |
|
47
|
|
|
*/ |
|
48
|
|
|
public static function loadListeners() |
|
49
|
|
|
{ |
|
50
|
|
|
$manager = self::instance(); |
|
51
|
|
|
$plugins = Plugin::loaded(); |
|
52
|
|
|
|
|
53
|
|
|
foreach ($plugins as $plugin) { |
|
|
|
|
|
|
54
|
|
|
$events = Plugin::getData($plugin, 'events')->getArrayCopy(); |
|
55
|
|
|
foreach ($events as $name => $config) { |
|
56
|
|
|
if (is_numeric($name)) { |
|
57
|
|
|
$name = $config; |
|
58
|
|
|
$config = []; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
$class = App::className($name, 'Event'); |
|
62
|
|
|
$config = (array) $config; |
|
63
|
|
|
|
|
64
|
|
|
if ($class !== false) { |
|
65
|
|
|
$listener = new $class($config); |
|
66
|
|
|
$manager->on($listener, $config); |
|
67
|
|
|
} |
|
68
|
|
|
} |
|
69
|
|
|
} |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* Emits an event. |
|
74
|
|
|
* |
|
75
|
|
|
* @param string $name |
|
76
|
|
|
* @param object|null $subject |
|
77
|
|
|
* @param array|null $data |
|
78
|
|
|
* @return Event |
|
79
|
|
|
*/ |
|
80
|
|
|
public static function trigger($name, $subject = null, $data = null) |
|
81
|
|
|
{ |
|
82
|
|
|
$event = new Event($name, $subject, $data); |
|
83
|
|
|
if (is_object($subject)) { |
|
84
|
|
|
return $subject->eventManager()->dispatch($event); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
return EventManager::instance()->dispatch($event); |
|
88
|
|
|
} |
|
89
|
|
|
} |
|
90
|
|
|
|
There are different options of fixing this problem.
If you want to be on the safe side, you can add an additional type-check:
If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:
Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.