diff --git a/lib/src/CvPlugins.php b/lib/src/CvPlugins.php index 4f9079e..d769bc1 100644 --- a/lib/src/CvPlugins.php +++ b/lib/src/CvPlugins.php @@ -33,6 +33,8 @@ class CvPlugins { * Ex: ['appName' => 'cv', 'appVersion' => '0.3.50'] */ public function init(array $pluginEnv) { + require_once __DIR__ . '/cvplugin_loader.php'; + $this->pluginEnv = $pluginEnv; if (getenv('CV_PLUGIN_PATH')) { $this->paths = explode(PATH_SEPARATOR, getenv('CV_PLUGIN_PATH')); @@ -68,11 +70,28 @@ public function init(array $pluginEnv) { $this->loadAll($plugins); } + /** + * Like CvPlugins::init(), this searches for and loads plugins. This is effectively + * the second phase of plugin-loading. It focuses on CiviCRM extensions + * which embed extra plugins. + */ + public function initExtensions(): void { + $plugins = []; + $event = GenericHookEvent::create([ + 'plugins' => &$plugins, + 'pluginEnv' => $this->pluginEnv + ['protocol' => self::PROTOCOL_VERSION], + ]); + \Civi::dispatcher()->dispatch('civi.cv-lib.plugins', $event); + + $this->loadAll($plugins); + } + /** * @param array $plugins * Ex: ['helloworld' => '/etc/cv/plugin/helloworld.php'] + * @internal */ - protected function loadAll(array $plugins): void { + public function loadAll(array $plugins): void { ksort($plugins); foreach ($plugins as $pluginName => $pluginFile) { $this->load($this->pluginEnv + [ @@ -90,9 +109,21 @@ protected function loadAll(array $plugins): void { * - version: Protocol version (ex: "1") * - name: Basenemae of the plugin (eg `hello.php`) * - file: Logic filename (eg `/etc/cv/plugin/hello.php`) + * * @return void + * @internal */ - protected function load(array $CV_PLUGIN) { + public function load(array $CV_PLUGIN) { + if (isset($this->plugins[$CV_PLUGIN['name']])) { + if ($this->plugins[$CV_PLUGIN['name']] === $CV_PLUGIN['file']) { + return; + } + else { + fprintf(STDERR, "WARNING: Plugin %s has already been loaded from %s. Ignore duplicate %s.\n", + $CV_PLUGIN['name'], $this->plugins[$CV_PLUGIN['name']], $CV_PLUGIN['file']); + return; + } + } $this->plugins[$CV_PLUGIN['name']] = $CV_PLUGIN['file']; include $CV_PLUGIN['file']; } diff --git a/lib/src/cvplugin_loader.php b/lib/src/cvplugin_loader.php new file mode 100644 index 0000000..4813357 --- /dev/null +++ b/lib/src/cvplugin_loader.php @@ -0,0 +1,27 @@ +initExtensions(); + } + } +}