00001 <?php 00002 /** 00003 * Magento 00004 * 00005 * NOTICE OF LICENSE 00006 * 00007 * This source file is subject to the Open Software License (OSL 3.0) 00008 * that is bundled with this package in the file LICENSE.txt. 00009 * It is also available through the world-wide-web at this URL: 00010 * http://opensource.org/licenses/osl-3.0.php 00011 * If you did not receive a copy of the license and are unable to 00012 * obtain it through the world-wide-web, please send an email 00013 * to license@magentocommerce.com so we can send you a copy immediately. 00014 * 00015 * DISCLAIMER 00016 * 00017 * Do not edit or add to this file if you wish to upgrade Magento to newer 00018 * versions in the future. If you wish to customize Magento for your 00019 * needs please refer to http://www.magentocommerce.com for more information. 00020 * 00021 * @category Varien 00022 * @package Varien_Event 00023 * @copyright Copyright (c) 2008 Irubin Consulting Inc. DBA Varien (http://www.varien.com) 00024 * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) 00025 */ 00026 00027 00028 /** 00029 * Collection of events 00030 * 00031 * @category Varien 00032 * @package Varien_Event 00033 * @author Magento Core Team <core@magentocommerce.com> 00034 */ 00035 class Varien_Event_Collection 00036 { 00037 /** 00038 * Array of events in the collection 00039 * 00040 * @var array 00041 */ 00042 protected $_events; 00043 00044 /** 00045 * Global observers 00046 * 00047 * For example regex observers will watch all events that 00048 * 00049 * @var Varien_Event_Observer_Collection 00050 */ 00051 protected $_observers; 00052 00053 /** 00054 * Initializes global observers collection 00055 * 00056 */ 00057 public function __construct() 00058 { 00059 $this->_events = array(); 00060 $this->_globalObservers = new Varien_Event_Observer_Collection(); 00061 } 00062 00063 /** 00064 * Returns all registered events in collection 00065 * 00066 * @return array 00067 */ 00068 public function getAllEvents() 00069 { 00070 return $this->_events; 00071 } 00072 00073 /** 00074 * Returns all registered global observers for the collection of events 00075 * 00076 * @return Varien_Event_Observer_Collection 00077 */ 00078 public function getGlobalObservers() 00079 { 00080 return $this->_globalObservers; 00081 } 00082 00083 /** 00084 * Returns event by its name 00085 * 00086 * If event doesn't exist creates new one and returns it 00087 * 00088 * @param string $eventName 00089 * @return Varien_Event 00090 */ 00091 public function getEventByName($eventName) 00092 { 00093 if (!isset($this->_events[$eventName])) { 00094 $this->addEvent(new Varien_Event(array('name'=>$eventName))); 00095 } 00096 return $this->_events[$eventName]; 00097 } 00098 00099 /** 00100 * Register an event for this collection 00101 * 00102 * @param Varien_Event $event 00103 * @return Varien_Event_Collection 00104 */ 00105 public function addEvent(Varien_Event $event) 00106 { 00107 $this->_events[$event->getName()] = $event; 00108 return $this; 00109 } 00110 00111 /** 00112 * Register an observer 00113 * 00114 * If observer has event_name property it will be regitered for this specific event. 00115 * If not it will be registered as global observer 00116 * 00117 * @param Varien_Event_Observer $observer 00118 * @return Varien_Event_Collection 00119 */ 00120 public function addObserver(Varien_Event_Observer $observer) 00121 { 00122 $eventName = $observer->getEventName(); 00123 if ($eventName) { 00124 $this->getEventByName($eventName)->addObserver($observer); 00125 } else { 00126 $this->getGlobalObservers()->addObserver($observer); 00127 } 00128 return $this; 00129 } 00130 00131 /** 00132 * Dispatch event name with optional data 00133 * 00134 * Will dispatch specific event and will try all global observers 00135 * 00136 * @param string $eventName 00137 * @param array $data 00138 * @return Varien_Event_Collection 00139 */ 00140 public function dispatch($eventName, array $data=array()) 00141 { 00142 $event = $this->getEventByName($eventName); 00143 $event->addData($data)->dispatch(); 00144 $this->getGlobalObservers()->dispatch($event); 00145 return $this; 00146 } 00147 }