Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
88.89% |
8 / 9 |
|
75.00% |
3 / 4 |
CRAP | |
0.00% |
0 / 1 |
dispatcher | |
88.89% |
8 / 9 |
|
75.00% |
3 / 4 |
6.05 | |
0.00% |
0 / 1 |
trigger_event | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
2 | |||
dispatch | |
66.67% |
2 / 3 |
|
0.00% |
0 / 1 |
2.15 | |||
disable | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
enable | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 |
1 | <?php |
2 | /** |
3 | * |
4 | * This file is part of the phpBB Forum Software package. |
5 | * |
6 | * @copyright (c) phpBB Limited <https://www.phpbb.com> |
7 | * @license GNU General Public License, version 2 (GPL-2.0) |
8 | * |
9 | * For full copyright and license information, please see |
10 | * the docs/CREDITS.txt file. |
11 | * |
12 | */ |
13 | |
14 | namespace phpbb\event; |
15 | |
16 | use Symfony\Component\EventDispatcher\EventDispatcher; |
17 | |
18 | /** |
19 | * Extension of the Symfony EventDispatcher |
20 | * |
21 | * It provides an additional `trigger_event` method, which |
22 | * gives some syntactic sugar for dispatching events. Instead |
23 | * of creating the event object, the method will do that for |
24 | * you. |
25 | * |
26 | * Example: |
27 | * |
28 | * $vars = array('page_title'); |
29 | * extract($phpbb_dispatcher->trigger_event('core.index', compact($vars))); |
30 | * |
31 | */ |
32 | class dispatcher extends EventDispatcher implements dispatcher_interface |
33 | { |
34 | /** |
35 | * @var bool |
36 | */ |
37 | protected $disabled = false; |
38 | |
39 | /** |
40 | * {@inheritdoc} |
41 | */ |
42 | public function trigger_event($eventName, $data = []) |
43 | { |
44 | $event = new \phpbb\event\data($data); |
45 | foreach ((array) $eventName as $name) |
46 | { |
47 | $this->dispatch($event, $name); |
48 | } |
49 | return $event->get_data_filtered(array_keys($data)); |
50 | } |
51 | |
52 | /** |
53 | * {@inheritdoc} |
54 | */ |
55 | public function dispatch(object $event, string $eventName = null) : object |
56 | { |
57 | if ($this->disabled) |
58 | { |
59 | return $event; |
60 | } |
61 | |
62 | return parent::dispatch($event, $eventName); |
63 | } |
64 | |
65 | /** |
66 | * {@inheritdoc} |
67 | */ |
68 | public function disable() |
69 | { |
70 | $this->disabled = true; |
71 | } |
72 | |
73 | /** |
74 | * {@inheritdoc} |
75 | */ |
76 | public function enable() |
77 | { |
78 | $this->disabled = false; |
79 | } |
80 | } |