_registry = $registry; $this->setConfig($config); if ($this->components) { $this->_componentMap = $registry->normalizeArray($this->components); } $this->initialize($config); } /** * Get the controller this component is bound to. * * @return \Cake\Controller\Controller The bound controller. */ public function getController(): Controller { return $this->_registry->getController(); } /** * Constructor hook method. * * Implement this method to avoid having to overwrite * the constructor and call parent. * * @param array $config The configuration settings provided to this component. * @return void */ public function initialize(array $config): void { } /** * Magic method for lazy loading $components. * * @param string $name Name of component to get. * @return mixed A Component object or null. */ public function __get(string $name) { if (isset($this->_componentMap[$name]) && !isset($this->{$name})) { $config = (array)$this->_componentMap[$name]['config'] + ['enabled' => false]; $this->{$name} = $this->_registry->load($this->_componentMap[$name]['class'], $config); } if (!isset($this->{$name})) { return null; } return $this->{$name}; } /** * Get the Controller callbacks this Component is interested in. * * Uses Conventions to map controller events to standard component * callback method names. By defining one of the callback methods a * component is assumed to be interested in the related event. * * Override this method if you need to add non-conventional event listeners. * Or if you want components to listen to non-standard events. * * @return array */ public function implementedEvents(): array { $eventMap = [ 'Controller.initialize' => 'beforeFilter', 'Controller.startup' => 'startup', 'Controller.beforeRender' => 'beforeRender', 'Controller.beforeRedirect' => 'beforeRedirect', 'Controller.shutdown' => 'shutdown', ]; $events = []; foreach ($eventMap as $event => $method) { if (method_exists($this, $method)) { $events[$event] = $method; } } return $events; } /** * Returns an array that can be used to describe the internal state of this * object. * * @return array */ public function __debugInfo(): array { return [ 'components' => $this->components, 'implementedEvents' => $this->implementedEvents(), '_config' => $this->getConfig(), ]; } }