本文整理匯總了PHP中CakeEventManager::attach方法的典型用法代碼示例。如果您正苦於以下問題:PHP CakeEventManager::attach方法的具體用法?PHP CakeEventManager::attach怎麽用?PHP CakeEventManager::attach使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類CakeEventManager
的用法示例。
在下文中一共展示了CakeEventManager::attach方法的8個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的PHP代碼示例。
示例1: _loadListener
/**
* 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: _loadAction
/**
* 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: attach
/**
* 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: getEventManager
/**
* 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;
}
示例5: _attachFilters
/**
* 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: getEventManager
/**
* 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: testStopPropagation
/**
* 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: testDispatchWithGlobalAndLocalEvents
/**
* 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);
}