PageRenderTime 47ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/Croogo/Event/CroogoEventManager.php

https://github.com/kareypowell/croogo
PHP | 140 lines | 80 code | 8 blank | 52 comment | 14 complexity | 6587143510270c77b5a4abbd600336a2 MD5 | raw file
  1. <?php
  2. App::uses('CakeEventManager', 'Event');
  3. /**
  4. * Croogo Event Manager class
  5. *
  6. * Descendant of CakeEventManager, customized to map event listener objects
  7. *
  8. * @since 1.4
  9. * @package Croogo.Croogo.Event
  10. * @see CakeEventManager
  11. * @author Rachman Chavik <rchavik@xintesa.com>
  12. * @license http://www.opensource.org/licenses/mit-license.php The MIT License
  13. * @link http://www.croogo.org
  14. */
  15. class CroogoEventManager extends CakeEventManager {
  16. /**
  17. * A map of registered event listeners
  18. */
  19. protected $_listenersMap = array();
  20. /**
  21. * Returns the globally available instance of a CroogoEventManager
  22. * @return CroogoEventManager the global event manager
  23. */
  24. public static function instance($manager = null) {
  25. if (empty(self::$_generalManager)) {
  26. return parent::instance(new CroogoEventManager());
  27. }
  28. return parent::instance($manager);
  29. }
  30. /**
  31. * Load Event Handlers during bootstrap.
  32. *
  33. * Plugins can add their own custom EventHandler in Config/events.php
  34. * with the following format:
  35. *
  36. * $config = array(
  37. * 'EventHandlers' => array(
  38. * 'Example.ExampleEventHandler' => array(
  39. * 'eventKey' => null,
  40. * 'options' => array(
  41. * 'priority' => 1,
  42. * 'passParams' => false,
  43. * 'className' => 'Plugin.ClassName',
  44. * )));
  45. *
  46. * @return void
  47. */
  48. public static function loadListeners() {
  49. $eventManager = CroogoEventManager::instance();
  50. $cached = Cache::read('EventHandlers', 'cached_settings');
  51. if ($cached === false) {
  52. $eventHandlers = Configure::read('EventHandlers');
  53. $validKeys = array('eventKey' => null, 'options' => array());
  54. $cached = array();
  55. if (!empty($eventHandlers) && is_array($eventHandlers)) {
  56. foreach ($eventHandlers as $eventHandler => $eventOptions) {
  57. $eventKey = null;
  58. if (is_numeric($eventHandler)) {
  59. $eventHandler = $eventOptions;
  60. $eventOptions = array();
  61. }
  62. list($plugin, $class) = pluginSplit($eventHandler);
  63. if (!empty($eventOptions)) {
  64. extract(array_intersect_key($eventOptions, $validKeys));
  65. }
  66. if (isset($eventOptions['options']['className'])) {
  67. list($plugin, $class) = pluginSplit($eventOptions['options']['className']);
  68. }
  69. App::uses($class, $plugin . '.Event');
  70. if (class_exists($class)) {
  71. $cached[] = compact('plugin', 'class', 'eventKey', 'eventOptions');
  72. } else {
  73. CakeLog::error(__d('croogo', 'EventHandler %s not found in plugin %s', $class, $plugin));
  74. }
  75. }
  76. Cache::write('EventHandlers', $cached, 'cached_settings');
  77. }
  78. }
  79. foreach ($cached as $cache) {
  80. extract($cache);
  81. if (CakePlugin::loaded($plugin)) {
  82. App::uses($class, $plugin . '.Event');
  83. $settings = isset($eventOptions['options']) ? $eventOptions['options'] : array();
  84. $listener = new $class($settings);
  85. $eventManager->attach($listener, $eventKey, $eventOptions);
  86. }
  87. }
  88. }
  89. /**
  90. * Adds a new listener to an event.
  91. * @see CakeEventManager::attach()
  92. * @return void
  93. */
  94. public function attach($callable, $eventKey = null, $options = array()) {
  95. parent::attach($callable, $eventKey, $options);
  96. if (is_object($callable)) {
  97. $key = get_class($callable);
  98. $this->_listenersMap[$key] = $callable;
  99. }
  100. }
  101. /**
  102. * Removes a listener from the active listeners.
  103. * @see CakeEventManager::detach()
  104. * @return void
  105. */
  106. public function detach($callable, $eventKey = null) {
  107. if (is_object($callable)) {
  108. $key = get_class($callable);
  109. unset($this->_listenersMap[$key]);
  110. }
  111. parent::detach($callable, $eventKey);
  112. }
  113. /**
  114. * Detach all listener objects belonging to a plugin
  115. * @param $plugin string
  116. * @return void
  117. */
  118. public function detachPluginSubscribers($plugin) {
  119. $eventHandlers = Configure::read('EventHandlers');
  120. if (empty($eventHandlers)) {
  121. return;
  122. }
  123. $eventHandlers = array_keys($eventHandlers);
  124. $eventHandlers = preg_grep('/^' . $plugin . '/', $eventHandlers);
  125. foreach ($eventHandlers as $eventHandler) {
  126. list(, $class) = pluginSplit($eventHandler);
  127. if (isset($this->_listenersMap[$class])) {
  128. $this->detach($this->_listenersMap[$class]);
  129. }
  130. }
  131. }
  132. }