-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathEvent.php
More file actions
146 lines (127 loc) · 3.01 KB
/
Event.php
File metadata and controls
146 lines (127 loc) · 3.01 KB
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
<?php
namespace Foolz\Plugin;
/**
* An event is an action that is run on a Hook with the same key
*
* @author Foolz <support@foolz.us>
* @package Foolz\Plugin
* @license http://www.apache.org/licenses/LICENSE-2.0.html Apache License 2.0
*/
class Event
{
/**
* Array of all the registered events
*
* @var array the key is the Hook key, value is the Event object
*/
protected static $events = array();
/**
* The Hook key
*
* @var null|string|array
*/
protected $key = null;
/**
* Priority to order the execution of plugins on the same hook. Lower runs earlier, negative allowed.
*
* @var int
*/
protected $priority = 5;
/**
* The string to call a static class method or a Closure
*
* @var null|string|Closure
*/
protected $callable = null;
/**
* Takes the Hook key and stores the object in the static::$events array
*
* @param string|array $key the key or the array of keys
*/
public function __construct($key)
{
$this->key = $key;
if (is_array($key)) {
foreach ($key as $k) {
static::$events[$k][] = $this;
}
} else {
static::$events[$key][] = $this;
}
}
/**
* Shorthand for the construct for PHP 5.3 to allow chaining
*
* @param string $key
* @return \Foolz\Plugin\Event
*/
public static function forge($key)
{
return new static($key);
}
/**
* Removes all the events with the key
*
* @param string $key
*/
public static function clear($key)
{
unset(static::$events[$key]);
}
/**
* Returns all the events ordered by ascending priority number
*
* @param string $key
* @return array empty if no hooks present, ordered by ascending priority number
*/
public static function getByKey($key)
{
if (!isset(static::$events[$key])) {
return array();
}
$events = static::$events[$key];
// sort by ascending priority
usort($events, function($a, $b) { return $a->getPriority() - $b->getPriority(); });
return $events;
}
/**
* Returns the priority
*
* @return int
*/
public function getPriority()
{
return $this->priority;
}
/**
* Sets the priority
*
* @param int $priority
* @return \Foolz\Plugin\Event
*/
public function setPriority($priority)
{
$this->priority = $priority;
return $this;
}
/**
* Gets the method or the Closure to run
*
* @return string|Callable
*/
public function getCall()
{
return $this->callable;
}
/**
* Sets the method or Closure to run
*
* @param string|Closure $callable
* @return \Foolz\Plugin\Event
*/
public function setCall($callable)
{
$this->callable = $callable;
return $this;
}
}