Пример #1
0
 /**
  * Set an object directly into the registry by name.
  *
  * If this collection implements events, the passed object will
  * be attached into the event manager
  *
  * @param string $objectName The name of the object to set in the registry.
  * @param object $object instance to store in the registry
  * @return void
  */
 public function set($objectName, $object)
 {
     list($plugin, $name) = pluginSplit($objectName);
     $this->unload($objectName);
     if (isset($this->_eventManager)) {
         $this->_eventManager->attach($object);
     }
     $this->_loaded[$name] = $object;
 }
Пример #2
0
 /**
  * Create the behavior instance.
  *
  * Part of the template method for Cake\Utility\ObjectRegistry::load()
  * Enabled behaviors will be registered with the event manager.
  *
  * @param string $class The classname that is missing.
  * @param string $alias The alias of the object.
  * @param array $config An array of config to use for the behavior.
  * @return Behavior The constructed behavior class.
  */
 protected function _create($class, $alias, $config)
 {
     $instance = new $class($this->_table, $config);
     $enable = isset($config['enabled']) ? $config['enabled'] : true;
     if ($enable) {
         $this->_eventManager->attach($instance);
     }
     $methods = $this->_getMethods($instance, $class, $alias);
     $this->_methodMap += $methods['methods'];
     $this->_finderMap += $methods['finders'];
     return $instance;
 }
Пример #3
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();
     EventManager::instance()->attach($listener);
     $listener2 = new EventTestListener();
     $manager = new EventManager();
     $manager->attach(array($listener2, 'listenerFunction'), 'fake.event');
     $manager->dispatch(new Event('fake.event', $this));
     $this->assertEquals(array('listenerFunction'), $listener->callStack);
     $this->assertEquals(array('listenerFunction'), $listener2->callStack);
 }
Пример #4
0
 /**
  * Attaches all event listeners for this dispatcher instance. Loads the
  * dispatcher filters from the configured locations.
  *
  * @param \Cake\Event\EventManager $manager
  * @return void
  * @throws \Cake\Routing\Error\MissingDispatcherFilterException
  */
 protected function _attachFilters($manager)
 {
     $filters = Configure::read('Dispatcher.filters');
     if (empty($filters)) {
         return;
     }
     foreach ($filters as $index => $filter) {
         $settings = array();
         if (is_array($filter) && !is_int($index)) {
             $settings = $filter;
             $filter = $index;
         }
         if (is_string($filter)) {
             $filter = array('callable' => $filter);
         }
         if (is_string($filter['callable'])) {
             $callable = App::classname($filter['callable'], 'Routing/Filter');
             if (!$callable) {
                 throw new Error\MissingDispatcherFilterException($filter['callable']);
             }
             $manager->attach(new $callable($settings));
         } else {
             $on = strtolower($filter['on']);
             $options = array();
             if (isset($filter['priority'])) {
                 $options = array('priority' => $filter['priority']);
             }
             $manager->attach($filter['callable'], 'Dispatcher.' . $on . 'Dispatch', $options);
         }
     }
 }
Пример #5
0
 /**
  * Initializes a new instance
  *
  * The $config array understands the following keys:
  *
  * - table: Name of the database table to represent
  * - alias: Alias to be assigned to this table (default to table name)
  * - connection: The connection instance to use
  * - entityClass: The fully namespaced class name of the entity class that will
  *   represent rows in this table.
  * - schema: A \Cake\Database\Schema\Table object or an array that can be
  *   passed to it.
  * - eventManager: An instance of an event manager to use for internal events
  * - behaviors: A BehaviorRegistry. Generally not used outside of tests.
  * - associations: An Associations instance.
  *
  * @param array $config List of options for this table
  */
 public function __construct(array $config = [])
 {
     if (!empty($config['table'])) {
         $this->table($config['table']);
     }
     if (!empty($config['alias'])) {
         $this->alias($config['alias']);
     }
     if (!empty($config['connection'])) {
         $this->connection($config['connection']);
     }
     if (!empty($config['schema'])) {
         $this->schema($config['schema']);
     }
     if (!empty($config['entityClass'])) {
         $this->entityClass($config['entityClass']);
     }
     $eventManager = $behaviors = $associations = null;
     if (!empty($config['eventManager'])) {
         $eventManager = $config['eventManager'];
     }
     if (!empty($config['behaviors'])) {
         $behaviors = $config['behaviors'];
     }
     if (!empty($config['associations'])) {
         $associations = $config['associations'];
     }
     $this->_eventManager = $eventManager ?: new EventManager();
     $this->_behaviors = $behaviors ?: new BehaviorRegistry($this);
     $this->_associations = $associations ?: new Associations();
     $this->initialize($config);
     $this->_eventManager->attach($this);
 }