From 56145de156231692d90fd6e7cf049b9a5f5660d9 Mon Sep 17 00:00:00 2001 From: mel Date: Mon, 13 Apr 2026 14:24:46 -0700 Subject: [PATCH] Add CLI command to set theme (#2319) --- lib/task/tools/atomPluginsTask.class.php | 106 ++++++++++++++++++++--- 1 file changed, 93 insertions(+), 13 deletions(-) diff --git a/lib/task/tools/atomPluginsTask.class.php b/lib/task/tools/atomPluginsTask.class.php index d52086342b..e705219a94 100644 --- a/lib/task/tools/atomPluginsTask.class.php +++ b/lib/task/tools/atomPluginsTask.class.php @@ -43,24 +43,72 @@ public function execute($arguments = [], $options = []) switch ($arguments['action']) { case 'add': - $plugins[] = $arguments['plugin']; - - // Save changes - $setting->setValue(serialize(array_unique($plugins)), ['sourceCulture' => true]); - $setting->save(); + $this->addPlugin($arguments['plugin'], $plugins); + $this->savePlugins($setting, $plugins); break; case 'delete': - if (false !== $key = array_search($arguments['plugin'], $plugins)) { - unset($plugins[$key]); - } else { - throw new sfException('Plugin could not be found.'); + $this->deletePlugin($arguments['plugin'], $plugins); + $this->savePlugins($setting, $plugins); + + break; + + case 'set-theme': + $themePlugins = []; + $configuration = ProjectConfiguration::getActive(); + $pluginPaths = $configuration->getAllPluginPaths(); + + // Get list of theme plugins + foreach (sfPluginAdminPluginConfiguration::$pluginNames as $name) { + unset($pluginPaths[$name]); } - // Save changes - $setting->setValue(serialize(array_unique($plugins)), ['sourceCulture' => true]); - $setting->save(); + foreach ($pluginPaths as $name => $path) { + $className = $name.'Configuration'; + if (sfConfig::get('sf_plugins_dir') == substr($path, 0, strlen(sfConfig::get('sf_plugins_dir'))) && is_readable($classPath = $path.'/config/'.$className.'.class.php')) { + $this->installPluginAssets($name, $path); + + require_once $classPath; + + $class = new $className($configuration); + + // Build a list of themes + if (isset($class::$summary) && 1 === preg_match('/theme/i', $class::$summary)) { + $themePlugins[] = $name; + } + } + } + + // Get current theme plugin + $currentTheme = null; + $criteria = new Criteria(); + $criteria->add(QubitSetting::NAME, 'plugins'); + if (1 == count($query = QubitSetting::get($criteria))) { + $setting = $query[0]; + + foreach (unserialize($setting->getValue(['sourceCulture' => true])) as $plugin) { + if (in_array($plugin, $themePlugins)) { + $currentTheme = $plugin; + + break; + } + } + } + + // Check if the new plugin is a theme plugin + if (in_array($arguments['plugin'], $themePlugins)) { + // Delete current theme plugin + $this->deletePlugin($currentTheme, $plugins); + + // Add new theme plugin + $this->addPlugin($arguments['plugin'], $plugins); + + // Save new plugins array + $this->savePlugins($setting, $plugins); + } else { + throw new sfException(sprintf('%s is not a theme plugin.', $arguments['plugin'])); + } break; @@ -76,10 +124,21 @@ public function execute($arguments = [], $options = []) } } + // Copied from sfPluginPublishAssetsTask + protected function installPluginAssets($name, $path) + { + $webDir = $path.'/web'; + + if (is_dir($webDir)) { + $filesystem = new sfFilesystem(); + $filesystem->relativeSymlink($webDir, sfConfig::get('sf_web_dir').'/'.$name, true); + } + } + protected function configure() { $this->addArguments([ - new sfCommandArgument('action', sfCommandArgument::REQUIRED, 'The action (add, delete or list).'), + new sfCommandArgument('action', sfCommandArgument::REQUIRED, 'The action (add, delete, set-theme or list).'), new sfCommandArgument('plugin', sfCommandArgument::OPTIONAL, 'The plugin name.'), ]); @@ -98,7 +157,28 @@ protected function configure() Manage AtoM plugins stored in the database. Examples: - symfony atom-plugins add arFoobarPlugin - symfony atom-plugins delete arFoobarPlugin + - symfony atom-plugins set-theme arFoobarPlugin - symfony atom-plugins list EOF; } + + private function deletePlugin($plugin, &$plugins) + { + if (false !== $key = array_search($plugin, $plugins)) { + unset($plugins[$key]); + } else { + throw new sfException('Plugin could not be found.'); + } + } + + private function addPlugin($plugin, &$plugins) + { + $plugins[] = $plugin; + } + + private function savePlugins($setting, &$plugins) + { + $setting->setValue(serialize(array_unique($plugins)), ['sourceCulture' => true]); + $setting->save(); + } }