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 Cake\Core\App; |
19
|
|
|
use Core\Core\Plugin; |
20
|
|
|
use Cake\Event\EventManager as CakeEventManager; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Class Manager |
24
|
|
|
* |
25
|
|
|
* @package Core\Event |
26
|
|
|
*/ |
27
|
|
|
class EventManager extends CakeEventManager |
28
|
|
|
{ |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Load Event Handlers during bootstrap. |
32
|
|
|
* |
33
|
|
|
* Plugins can add their own custom EventHandler in Config/events.php |
34
|
|
|
* with the following format: |
35
|
|
|
* |
36
|
|
|
* 'events' => [ |
37
|
|
|
* 'Core.CoreEventHandler' => [ |
38
|
|
|
* 'options' = [ |
39
|
|
|
* 'callable' => '', |
40
|
|
|
* 'priority' => '', |
41
|
|
|
* ] |
42
|
|
|
* ] |
43
|
|
|
* ] |
44
|
|
|
* |
45
|
|
|
* @return void |
46
|
|
|
*/ |
47
|
|
|
public static function loadListeners() |
48
|
|
|
{ |
49
|
|
|
$manager = self::instance(); |
50
|
|
|
$plugins = Plugin::loaded(); |
51
|
|
|
|
52
|
|
|
foreach ($plugins as $plugin) { |
|
|
|
|
53
|
|
|
$events = Plugin::getData($plugin, 'events')->getArrayCopy(); |
54
|
|
|
foreach ($events as $name => $config) { |
55
|
|
|
if (is_numeric($name)) { |
56
|
|
|
$name = $config; |
57
|
|
|
$config = []; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
$class = App::className($name, 'Event'); |
61
|
|
|
$config = (array) $config; |
62
|
|
|
|
63
|
|
|
if ($class !== false) { |
64
|
|
|
$listener = new $class($config); |
65
|
|
|
$manager->on($listener, $config); |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Emits an event. |
73
|
|
|
* |
74
|
|
|
* @param string $name |
75
|
|
|
* @param object|null $subject |
76
|
|
|
* @param array|null $data |
77
|
|
|
* @return Event |
78
|
|
|
*/ |
79
|
|
|
public static function trigger($name, $subject = null, $data = null) |
80
|
|
|
{ |
81
|
|
|
$event = new Event($name, $subject, $data); |
82
|
|
|
if (is_object($subject)) { |
83
|
|
|
return $subject->getEventManager()->dispatch($event); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
return EventManager::instance()->dispatch($event); |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|
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.