/** * Loads and registers plugin's namespace and loads its event listeners classes. * * This is used to allow plugins being installed to respond to events before * they are integrated to the system. Events such as `beforeInstall`, * `afterInstall`, etc. * * @param string $plugin Name of the plugin for which attach listeners * @param string $path Path to plugin's root directory (which contains "src") * @throws \Cake\Error\FatalErrorException On illegal usage of this method */ protected function _attachListeners($plugin, $path) { $path = normalizePath("{$path}/"); $eventsPath = normalizePath("{$path}/src/Event/"); if (is_readable($eventsPath) && is_dir($eventsPath)) { $EventManager = EventManager::instance(); $eventsFolder = new Folder($eventsPath); Plugin::load($plugin, ['autoload' => true, 'bootstrap' => false, 'routes' => false, 'path' => $path, 'classBase' => 'src', 'ignoreMissing' => true]); foreach ($eventsFolder->read(false, false, true)[1] as $classPath) { $className = preg_replace('/\\.php$/i', '', basename($classPath)); $fullClassName = implode('\\', [$plugin, 'Event', $className]); if (class_exists($fullClassName)) { $handler = new $fullClassName(); $this->_listeners[] = $handler; $EventManager->on($handler); } } } }
} $filter = $plugin->status; if ($plugin->isTheme) { $filter = $filter && in_array($plugin->name, [option('front_theme'), option('back_theme')]); } if (!$filter) { return; } if (!in_array("{$plugin->name}\\", array_keys($classLoader->getPrefixesPsr4()))) { $classLoader->addPsr4("{$plugin->name}\\", normalizePath("{$plugin->path}/src/"), true); } if (!in_array("{$plugin->name}\\Test\\", array_keys($classLoader->getPrefixesPsr4()))) { $classLoader->addPsr4("{$plugin->name}\\Test\\", normalizePath("{$plugin->path}/tests/"), true); } $info = ['autoload' => false, 'bootstrap' => true, 'routes' => true, 'path' => normalizePath("{$plugin->path}/"), 'classBase' => 'src', 'ignoreMissing' => true]; Plugin::load($plugin->name, $info); foreach ($plugin->eventListeners as $fullClassName) { if (class_exists($fullClassName)) { if (str_ends_with($fullClassName, 'Shortcode')) { EventDispatcher::instance('Shortcode')->eventManager()->on(new $fullClassName()); } else { EventDispatcher::instance()->eventManager()->on(new $fullClassName()); } } } $pluginsPath[] = $info['path']; }); if (empty($pluginsPath)) { die("Ops, something went wrong. Try to clear your site's snapshot and verify write permissions on /tmp directory."); } /**