attach() 공개 메소드

Adds a new listener to an event. Listeners
public attach ( callback | CakeEventListener $callable, string $eventKey = null, array $options = [] ) : void
$callable callback | CakeEventListener PHP valid callback type or instance of CakeEventListener to be called when the event named with $eventKey is triggered. If a CakeEventListener instance is passed, then the `implementedEvents` method will be called on the object to register the declared events individually as methods to be managed by this class. It is possible to define multiple event handlers per event name.
$eventKey string The event unique identifier name with which the callback will be associated. If $callable is an instance of CakeEventListener this argument will be ignored
$options array used to set the `priority` and `passParams` flags to the listener. Priorities are handled like queues, and multiple attachments added to the same priority queue will be treated in the order of insertion. `passParams` means that the event data property will be converted to function arguments when the listener is called. If $called is an instance of CakeEventListener, this parameter will be ignored
리턴 void
 /**
  * Load a single event class attached to Crud
  *
  * @param string $name
  * @return void
  */
 protected function _loadListener($name)
 {
     $subject = $this->_getSubject();
     $config = $this->config(sprintf('listenerClassMap.%s', $name));
     list($plugin, $class) = pluginSplit($config, true);
     App::uses($class, $plugin . 'Controller/Event');
     // Make sure to cleanup duplicate events
     if (isset($this->_listeners[$name])) {
         $this->_eventManager->detach($this->_listeners[$name]);
         unset($this->_listeners[$name]);
     }
     $this->_listeners[$name] = new $class($subject);
     $this->_eventManager->attach($this->_listeners[$name]);
 }
예제 #2
0
 /**
  * Load a CrudAction instance.
  *
  * @param string $name The controller action name.
  * @return CrudAction
  * @throws CakeException If action is not mapped.
  */
 protected function _loadAction($name)
 {
     if (!isset($this->_actionInstances[$name])) {
         $config = $this->config('actions.' . $name);
         if (empty($config)) {
             throw new CakeException(sprintf('Action "%s" has not been mapped', $name));
         }
         list($plugin, $class) = pluginSplit($config['className'], true);
         $class = ucfirst($class);
         if (in_array($class, array('Index', 'View', 'Add', 'Edit', 'Delete'))) {
             if (!empty($plugin) && $plugin !== 'Crud.') {
                 throw new CakeException('The build-in CrudActions (Index, View, Add, Edit and Delete) must be loaded from the Crud plugin');
             }
             $plugin = 'Crud.';
         }
         $class .= 'CrudAction';
         App::uses($class, $plugin . 'Controller/Crud/Action');
         $subject = $this->getSubject(array('action' => $name));
         $this->_actionInstances[$name] = new $class($subject, $config);
         $this->_eventManager->attach($this->_actionInstances[$name]);
     }
     return $this->_actionInstances[$name];
 }
예제 #3
0
 /**
  * Adds a new listener to an event.
  * @see CakeEventManager::attach()
  * @return void
  */
 public function attach($callable, $eventKey = null, $options = array())
 {
     parent::attach($callable, $eventKey, $options);
     if (is_object($callable)) {
         $key = get_class($callable);
         $this->_listenersMap[$key] = $callable;
     }
 }
예제 #4
0
 /**
  * Returns the CakeEventManager manager instance that is handling any callbacks.
  * You can use this instance to register any new listeners or callbacks to the
  * controller events, or create your own events and trigger them at will.
  *
  * @return CakeEventManager
  */
 public function getEventManager()
 {
     if (empty($this->_eventManager)) {
         $this->_eventManager = new CakeEventManager();
         $this->_eventManager->attach($this->Components);
         $this->_eventManager->attach($this);
     }
     return $this->_eventManager;
 }
 /**
  * Attaches all event listeners for this dispatcher instance. Loads the
  * dispatcher filters from the configured locations.
  *
  * @param CakeEventManager $manager
  * @return void
  * @throws MissingDispatcherFilterException
  */
 protected function _attachFilters($manager)
 {
     $filters = Configure::read('Dispatcher.filters');
     if (empty($filters)) {
         return;
     }
     foreach ($filters as $filter) {
         if (is_string($filter)) {
             $filter = array('callable' => $filter);
         }
         if (is_string($filter['callable'])) {
             list($plugin, $callable) = pluginSplit($filter['callable'], true);
             App::uses($callable, $plugin . 'Routing/Filter');
             if (!class_exists($callable)) {
                 throw new MissingDispatcherFilterException($callable);
             }
             $manager->attach(new $callable());
         } else {
             $on = strtolower($filter['on']);
             $options = array();
             if (isset($filter['priority'])) {
                 $options = array('priority' => $filter['priority']);
             }
             $manager->attach($filter['callable'], 'Dispatcher.' . $on . 'Dispatch', $options);
         }
     }
 }
예제 #6
0
 /**
  * Returns the CakeEventManager manager instance that is handling any callbacks.
  * You can use this instance to register any new listeners or callbacks to the
  * controller events, or create your own events and trigger them at will.
  *
  * @return CakeEventManager
  */
 public function getEventManager()
 {
     if (empty($this->_eventManager)) {
         $this->_eventManager = new CakeEventManager();
     }
     if (!$this->_eventManagerConfigured) {
         $this->_eventManager->attach($this->Helpers);
         $this->_eventManagerConfigured = true;
     }
     return $this->_eventManager;
 }
예제 #7
0
 /**
  * Tests that stopping an event will not notify the rest of the listeners
  *
  * @return void
  */
 public function testStopPropagation()
 {
     $manager = new CakeEventManager();
     $listener = new CakeEventTestListener();
     $manager->attach(array($listener, 'listenerFunction'), 'fake.event');
     $manager->attach(array($listener, 'stopListener'), 'fake.event', array('priority' => 8));
     $manager->attach(array($listener, 'secondListenerFunction'), 'fake.event', array('priority' => 5));
     $event = new CakeEvent('fake.event');
     $manager->dispatch($event);
     $expected = array('secondListenerFunction');
     $this->assertEquals($expected, $listener->callStack);
 }
예제 #8
0
 /**
  * Test that events are dispatched properly when there are global and local
  * listeners at the same priority.
  *
  * @return void
  */
 public function testDispatchWithGlobalAndLocalEvents()
 {
     $listener = new CustomTestEventListener();
     CakeEventManager::instance()->attach($listener);
     $listener2 = new CakeEventTestListener();
     $manager = new CakeEventManager();
     $manager->attach(array($listener2, 'listenerFunction'), 'fake.event');
     $manager->dispatch(new CakeEvent('fake.event', $this));
     $this->assertEquals(array('listenerFunction'), $listener->callStack);
     $this->assertEquals(array('listenerFunction'), $listener2->callStack);
 }