<?php
 namespace Moto\EventManager; use ArrayAccess; use Zend; use Moto; class EventManager extends Zend\EventManager\EventManager { protected $_eventNameSpaceSeparator = '.'; public function getListeners($event) { $listener = parent::getListeners($event); if (strpos($event, $this->_eventNameSpaceSeparator) > 0) { if (preg_match('/^(.*)' . '\\' . $this->_eventNameSpaceSeparator . '([^' . '\\' . $this->_eventNameSpaceSeparator . ']*)$/', $event, $match)) { if ($match[2] != '*') { $_eventName = $match[1] . $this->_eventNameSpaceSeparator . '*'; $_listener = $this->getListeners($_eventName); if ($_listener->count()) { $this->insertListeners($listener, $_listener); } } } } return $listener; } public function filter($eventName, $value = null, $argv = array()) { if ($eventName instanceof Zend\EventManager\EventInterface) { $event = $eventName; $eventName = $event->getName(); } elseif ($value instanceof Zend\EventManager\EventInterface) { $event = $value; $event->setName($eventName); } elseif ($argv instanceof Zend\EventManager\EventInterface) { $event = $argv; $event->setName($eventName); $event->setTarget($value); } else { $event = new $this->eventClass(); $event->setName($eventName); $event->setTarget($value); $event->setParams($argv); } $event->stopPropagation(false); return $this->_applyFilters($eventName, $event); } protected function _applyFilters($eventName, Zend\EventManager\EventInterface $event) { $response = $event->getTarget(); $listeners = $this->getListeners($eventName); $sharedListeners = $this->getSharedListeners($eventName); $sharedWildcardListeners = $this->getSharedListeners('*'); $wildcardListeners = $this->getListeners('*'); if (count($sharedListeners) || count($sharedWildcardListeners) || count($wildcardListeners)) { $listeners = clone $listeners; $this->insertListeners($listeners, $sharedListeners); $this->insertListeners($listeners, $sharedWildcardListeners); $this->insertListeners($listeners, $wildcardListeners); } foreach ($listeners as $listener) { $listenerCallback = $listener->getCallback(); try { $event->setTarget($response); $args = $event->getParams(); array_unshift($args, $event, $response); $result = call_user_func_array($listenerCallback, $args); if (!is_object($response) && $result !== null) { $response = $result; } } catch (Moto\EventManager\EventException $e) { throw $e; } catch (\Exception $e) { Moto\System\Log::error('Moto\EventManager::filter', array('hook' => $eventName, 'code' => $e->getCode(), 'message' => $e->getMessage())); } if ($event->propagationIsStopped()) { break; } } return $response; } } 