Skip to content

Commit

Permalink
Add Zend\EventManager\ProvidesEvents trait
Browse files Browse the repository at this point in the history
- Cannot use in core until 3.0
- Comments added to classes that could use it
  • Loading branch information
EvanDotPro committed Nov 10, 2011
1 parent adac457 commit 8182346
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 0 deletions.
79 changes: 79 additions & 0 deletions library/Zend/EventManager/ProvidesEvents.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<?php
/**
* Zend Framework
*
* LICENSE
*
* This source file is subject to the new BSD license that is bundled
* with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://framework.zend.com/license/new-bsd
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to [email protected] so we can send you a copy immediately.
*
* @category Zend
* @package Zend_EventManager
* @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/

/**
* @namespace
*/
namespace Zend\EventManager;

use Traversable;

/**
* A trait for objects that provide events
*
* @category Zend
* @package Zend_EventManager
* @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
trait ProvidesEvents
{
/**
* @var EventCollection
*/
protected $events;

/**
* Set the event manager instance used by this context
*
* @param EventCollection $events
* @return mixed
*/
public function setEventManager(EventCollection $events)
{
$this->events = $events;
return $this;
}

/**
* Retrieve the event manager
*
* Lazy-loads an EventManager instance if none registered.
*
* @return EventCollection
*/
public function events()
{
if (!$this->events instanceof EventCollection) {
$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($identifiers + (array) $this->eventIdentifier);
}
// silently ignore invalid eventIdentifier types
}
$this->setEventManager(new EventManager($identifiers));
}
return $this->events;
}
}
2 changes: 2 additions & 0 deletions library/Zend/Mvc/Controller/ActionController.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
*/
abstract class ActionController implements Dispatchable, InjectApplicationEvent, LocatorAware, Pluggable
{
//use \Zend\EventManager\ProvidesEvents;

protected $broker;
protected $event;
protected $events;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@
*/
class Application
{
//use \Zend\EventManager\ProvidesEvents;

/**
* Autoloader to use
*
Expand Down

0 comments on commit 8182346

Please sign in to comment.