/** * Indirectly test CroogoEventManager::detachPluginSubscribers() * triggerred by calling CroogoPlugin::unload(null) */ public function testDetachPluginSubscribers() { $eventManager = CroogoEventManager::instance(); $loaded = CakePlugin::loaded('Shops'); $this->assertNotEmpty($loaded); $eventName = 'Controller.Users.activationFailure'; $event = Croogo::dispatchEvent($eventName, $this->Users); $this->assertTrue($event->result, sprintf('Event: %s', $eventName)); CroogoPlugin::unload('Shops'); $eventName = 'Controller.Users.activationFailure'; $event = Croogo::dispatchEvent($eventName, $this->Users); $this->assertNull($event->result, sprintf('Event: %s', $eventName)); }
/** * Test Reuse the same Event Listener class */ public function testAliasingEventListener() { $eventManager = CroogoEventManager::instance(); $listeners = $eventManager->listeners('Controller.Nodes.afterAdd'); foreach ($listeners as $listener) { $eventManager->detach($listener['callable']); } $handlers = array('Shops.ShopsNodesEventHandler', 'CustomShopsNodesEventHandler' => array('options' => array('className' => 'Shops.ShopsNodesEventHandler'))); Configure::write('EventHandlers', $handlers); Cache::delete('EventHandlers', 'cached_settings'); CroogoEventManager::loadListeners(); $listeners = $eventManager->listeners('Controller.Nodes.afterAdd'); foreach ($listeners as $listener) { $this->assertInstanceOf('ShopsNodesEventHandler', $listener['callable'][0]); } }
/** * Load Event Handlers during bootstrap. * * Plugins can add their own custom EventHandler in Config/events.php * with the following format: * * $config = array( * 'EventHandlers' => array( * 'Example.ExampleEventHandler' => array( * 'eventKey' => null, * 'options' => array( * 'priority' => 1, * 'passParams' => false, * 'className' => 'Plugin.ClassName', * ))); * * @return void */ public static function loadListeners() { $eventManager = CroogoEventManager::instance(); $cached = Cache::read('EventHandlers', 'cached_settings'); if ($cached === false) { $eventHandlers = Configure::read('EventHandlers'); $validKeys = array('eventKey' => null, 'options' => array()); $cached = array(); if (!empty($eventHandlers) && is_array($eventHandlers)) { foreach ($eventHandlers as $eventHandler => $eventOptions) { $eventKey = null; if (is_numeric($eventHandler)) { $eventHandler = $eventOptions; $eventOptions = array(); } list($plugin, $class) = pluginSplit($eventHandler); if (!empty($eventOptions)) { extract(array_intersect_key($eventOptions, $validKeys)); } if (isset($eventOptions['options']['className'])) { list($plugin, $class) = pluginSplit($eventOptions['options']['className']); } App::uses($class, $plugin . '.Event'); if (class_exists($class)) { $cached[] = compact('plugin', 'class', 'eventKey', 'eventOptions'); } else { CakeLog::error(__d('croogo', 'EventHandler %s not found in plugin %s', $class, $plugin)); } } Cache::write('EventHandlers', $cached, 'cached_settings'); } } foreach ($cached as $cache) { extract($cache); if (CakePlugin::loaded($plugin)) { App::uses($class, $plugin . '.Event'); $settings = isset($eventOptions['options']) ? $eventOptions['options'] : array(); $listener = new $class($settings); $eventManager->attach($listener, $eventKey, $eventOptions); } } }
/** * Forgets a loaded plugin or all of them if first parameter is null * * This method is identical to CakePlugin::load() with extra functionality * that unregister event listeners when a plugin in unloaded. * * @see CakePlugin::unload() * @param string $plugin name of the plugin to forget * @return void */ public static function unload($plugin) { $eventManager = CroogoEventManager::instance(); if ($eventManager instanceof CroogoEventManager) { if ($plugin == null) { $activePlugins = CakePlugin::loaded(); foreach ($activePlugins as $activePlugin) { $eventManager->detachPluginSubscribers($activePlugin); } } else { $eventManager->detachPluginSubscribers($plugin); } } CakePlugin::unload($plugin); Cache::delete('EventHandlers', 'cached_settings'); }
/** * Forgets a loaded plugin or all of them if first parameter is null * * This method is identical to CakePlugin::load() with extra functionality * that unregister event listeners when a plugin in unloaded. * * @see CakePlugin::unload() * @param string $plugin name of the plugin to forget * @return void */ public static function unload($plugin) { $eventManager = CroogoEventManager::instance(); if ($eventManager instanceof CroogoEventManager) { if ($plugin == null) { $activePlugins = CakePlugin::loaded(); foreach ($activePlugins as $activePlugin) { $eventManager->detachPluginSubscribers($activePlugin); } } else { $eventManager->detachPluginSubscribers($plugin); } } CakePlugin::unload($plugin); }