-
Notifications
You must be signed in to change notification settings - Fork 1
/
EventDispatcher.php
65 lines (53 loc) · 1.43 KB
/
EventDispatcher.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
<?php
/**
* Event Dispatcher implementation.
*
* @author Iron Bound Designs
* @since 1.0
* @copyright 2019 (c) Iron Bound Designs.
* @license GPLv2
*/
declare(strict_types=1);
namespace IronBound\Psr14WP;
use Psr\EventDispatcher\EventDispatcherInterface;
class EventDispatcher implements EventDispatcherInterface
{
public const DEFAULT_PREFIX = 'ironbound.psr14wp.';
/** @var string */
private $prefix;
/**
* EventDispatcher constructor.
*
* @param string $prefix
*/
public function __construct(string $prefix = self::DEFAULT_PREFIX)
{
$this->prefix = $prefix;
}
/**
* Dispatches an Event to WordPress actions.
*
* Will fire separate actions for the event's class,
* then all parent classes in ascending order up the hierarchy chain,
* then all interfaces in ascending order up the hierarchy chain.
*
* @param object $event
*
* @return object
*/
public function dispatch(object $event)
{
$this->call(get_class($event), $event);
foreach (class_parents($event) as $parent) {
$this->call($parent, $event);
}
foreach (class_implements($event) as $interface) {
$this->call($interface, $event);
}
return $event;
}
protected function call(string $class, object $event): void
{
do_action($this->prefix . $class, $event);
}
}