Source of file EventManagerAwareTrait.php
Size: 2,497 Bytes - Last Modified: 2014-03-12T23:21:18+01:00
/home/theseer/Downloads/ZendFramework-2.3.0/library/Zend/EventManager/EventManagerAwareTrait.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778 | <?php /** * Zend Framework (http://framework.zend.com/) * * @link http://github.com/zendframework/zf2 for the canonical source repository * @copyright Copyright (c) 2005-2014 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License */ namespace Zend\EventManager; use Traversable; /** * A trait for objects that provide events. * * If you use this trait in an object, you will probably want to also implement * EventManagerAwareInterface, which will make it so the default initializer in * a ZF2 MVC application will automatically inject an instance of the * EventManager into your object when it is pulled from the ServiceManager. * * @see Zend\Mvc\Service\ServiceManagerConfig */ trait EventManagerAwareTrait { /** * @var EventManagerInterface */ protected $events; /** * Set the event manager instance used by this context. * * For convenience, this method will also set the class name / LSB name as * identifiers, in addition to any string or array of strings set to the * $this->eventIdentifier property. * * @param EventManagerInterface $events * @return mixed */ public function setEventManager(EventManagerInterface $events) { $identifiers = array(__CLASS__, get_class($this)); if (isset($this->eventIdentifier)) { if ((is_string($this->eventIdentifier)) || (is_array($this->eventIdentifier)) || ($this->eventIdentifier instanceof Traversable) ) { $identifiers = array_unique(array_merge($identifiers, (array) $this->eventIdentifier)); } elseif (is_object($this->eventIdentifier)) { $identifiers[] = $this->eventIdentifier; } // silently ignore invalid eventIdentifier types } $events->setIdentifiers($identifiers); $this->events = $events; if (method_exists($this, 'attachDefaultListeners')) { $this->attachDefaultListeners(); } return $this; } /** * Retrieve the event manager * * Lazy-loads an EventManager instance if none registered. * * @return EventManagerInterface */ public function getEventManager() { if (!$this->events instanceof EventManagerInterface) { $this->setEventManager(new EventManager()); } return $this->events; } } |