1 | <?php |
||
51 | class RuntimeManager |
||
52 | { |
||
53 | const EVENT = 'event'; |
||
54 | const FILTER = 'filter'; |
||
55 | |||
56 | /** |
||
57 | * @access protected |
||
58 | * @var Framework |
||
59 | */ |
||
60 | protected $framework; |
||
61 | |||
62 | /** |
||
63 | * @access protected |
||
64 | * @var ObjectBackendAbstract |
||
65 | */ |
||
66 | protected $handlerObjectBackend; |
||
67 | |||
68 | /** |
||
69 | * @access protected |
||
70 | * @var array |
||
71 | */ |
||
72 | protected $handlers = array(); |
||
73 | |||
74 | /** |
||
75 | * Constructs the object |
||
76 | * |
||
77 | * @access public |
||
78 | * @param \Zepi\Turbo\Framework $framework |
||
79 | * @param \Zepi\Turbo\Backend\ObjectBackendAbstract $handlerObjectBackend |
||
80 | */ |
||
81 | 17 | public function __construct(Framework $framework, ObjectBackendAbstract $handlerObjectBackend) |
|
86 | |||
87 | /** |
||
88 | * Initializes the event system. The function loads all saved |
||
89 | * events from the object backend. |
||
90 | * |
||
91 | * @access public |
||
92 | */ |
||
93 | 17 | public function initializeManager() |
|
102 | |||
103 | /** |
||
104 | * Executes the events for the given event name |
||
105 | * |
||
106 | * @access public |
||
107 | * @param string $eventName |
||
108 | */ |
||
109 | 2 | public function executeEvent($eventName) |
|
113 | |||
114 | /** |
||
115 | * Executes the filter for the given filter name |
||
116 | * |
||
117 | * @access public |
||
118 | * @param string $eventName |
||
119 | * @param mixed $value |
||
120 | */ |
||
121 | 2 | public function executeFilter($eventName, $value = null) |
|
125 | |||
126 | /** |
||
127 | * Executes the given event handlers. Every event handler will |
||
128 | * be initialized and executed. |
||
129 | * |
||
130 | * @access protected |
||
131 | * @param string $type |
||
132 | * @param string $name |
||
133 | * @param mixed $value |
||
134 | * @return mixed |
||
135 | */ |
||
136 | 2 | protected function executeItems($type, $name, $value = null) |
|
137 | { |
||
138 | 2 | if (!isset($this->handlers[$type][$name])) { |
|
139 | 2 | return $value; |
|
140 | } |
||
141 | |||
142 | 2 | $request = $this->framework->getRequest(); |
|
143 | |||
144 | 2 | foreach ($this->filterHandlers($type, $name, $request) as $handlerName) { |
|
145 | 2 | $handler = $this->framework->getInstance($handlerName); |
|
146 | |||
147 | 2 | $response = $this->framework->getResponse(); |
|
148 | 2 | $response->setData('_executedType', $type); |
|
149 | 2 | $response->setData('_executedName', $name); |
|
150 | |||
151 | // Execute the handler |
||
152 | 2 | $handlerResult = $handler->execute( |
|
153 | 2 | $this->framework, |
|
154 | 2 | $this->framework->getRequest(), |
|
155 | 2 | $response, |
|
156 | 2 | $value |
|
157 | ); |
||
158 | |||
159 | // Save the handler result to the variable if this is an filter handler |
||
160 | 2 | if ($type === self::FILTER) { |
|
161 | 2 | $value = $handlerResult; |
|
162 | } |
||
163 | } |
||
164 | |||
165 | // Return the value if this is an filter handler |
||
166 | 2 | if ($type === self::FILTER) { |
|
167 | return $value; |
||
168 | } |
||
169 | 2 | } |
|
170 | |||
171 | /** |
||
172 | * Filters the handler and returns an single array with |
||
173 | * all queued handlers |
||
174 | * |
||
175 | * @access protected |
||
176 | * @param string $type |
||
177 | * @param string $name |
||
178 | * @param RequestAbstract $request |
||
179 | * @return array |
||
180 | */ |
||
181 | 2 | protected function filterHandlers($type, $name, RequestAbstract $request) |
|
197 | |||
198 | /** |
||
199 | * Returns true if the event handler and the request are from the same interface, |
||
200 | * e.g. both are from cli. If the event handler is neutral return true. Return false |
||
201 | * if the event handler isn't neutral and the request is not from the same interface. |
||
202 | * |
||
203 | * @access protected |
||
204 | * @param RequestAbstract $request |
||
205 | * @param string $handlerName |
||
206 | * @return boolean |
||
207 | */ |
||
208 | 2 | protected function compareRequestWithInterface(RequestAbstract $request, $handlerName) |
|
228 | |||
229 | /** |
||
230 | * Adds an event handler |
||
231 | * |
||
232 | * @access public |
||
233 | * @param string $eventName |
||
234 | * @param string $eventHandlerName |
||
235 | * @param integer $priority |
||
236 | */ |
||
237 | 2 | public function addEventHandler($eventName, $eventHandlerName, $priority = 50) |
|
241 | |||
242 | /** |
||
243 | * Adds an filter handler |
||
244 | * |
||
245 | * @access public |
||
246 | * @param string $filterName |
||
247 | * @param string $filterHandlerName |
||
248 | * @param integer $priority |
||
249 | */ |
||
250 | public function addFilterHandler($filterName, $filterHandlerName, $priority = 50) |
||
254 | |||
255 | /** |
||
256 | * Adds a new handler |
||
257 | * |
||
258 | * @access protected |
||
259 | * @param string $type |
||
260 | * @param string $name |
||
261 | * @param string $handlerName |
||
262 | * @param integer $priority |
||
263 | */ |
||
264 | 2 | protected function addHandler($type, $name, $handlerName, $priority) |
|
282 | |||
283 | /** |
||
284 | * Removes an event handler |
||
285 | * |
||
286 | * @access public |
||
287 | * @param string $eventName |
||
288 | * @param string $eventHandlerName |
||
289 | * @param integer $priority |
||
290 | */ |
||
291 | public function removeEventHandler($eventName, $eventHandlerName, $priority = 50) |
||
295 | |||
296 | /** |
||
297 | * Removes an filter handler |
||
298 | * |
||
299 | * @access public |
||
300 | * @param string $filterName |
||
301 | * @param string $filterHandlerName |
||
302 | * @param integer $priority |
||
303 | */ |
||
304 | public function removeFilterHandler($filterName, $filterHandlerName, $priority = 50) |
||
308 | |||
309 | /** |
||
310 | * Removes an handler |
||
311 | * |
||
312 | * @access protected |
||
313 | * @param string $type |
||
314 | * @param string $name |
||
315 | * @param string $handlerName |
||
316 | * @param string $priority |
||
317 | */ |
||
318 | protected function removeHandler($type, $name, $handlerName, $priority) |
||
335 | |||
336 | /** |
||
337 | * Clears the handlers cache and reactivates the modules |
||
338 | * to rebuild the cache. |
||
339 | * |
||
340 | * @access public |
||
341 | * @param boolean $reactivateModules |
||
342 | */ |
||
343 | public function clearCache($reactivateModules = true) |
||
351 | |||
352 | /** |
||
353 | * Saves the registred handlers in the object backend. |
||
354 | * |
||
355 | * @access protected |
||
356 | */ |
||
357 | 2 | protected function saveHandlers() |
|
361 | } |
||
362 |