From c148989dbafd4674838ef1d284f9253000af745a Mon Sep 17 00:00:00 2001 From: aljawaid <10233708+aljawaid@users.noreply.github.com> Date: Sun, 21 May 2023 04:30:35 +0100 Subject: [PATCH] FIX: Code Cleanup - `PluginManagerController.php` - fix comments - fix code syntax - move `show` page codeblocks to top - fix language strings --- Controller/PluginManagerController.php | 111 +++++++++++++++---------- 1 file changed, 67 insertions(+), 44 deletions(-) diff --git a/Controller/PluginManagerController.php b/Controller/PluginManagerController.php index 129cfdb..9d6576f 100644 --- a/Controller/PluginManagerController.php +++ b/Controller/PluginManagerController.php @@ -9,10 +9,11 @@ use ZipArchive; /** - * Class PluginManager + * Plugin PluginManager + * Class PluginManagerController * * @author aljawaid - * + * @author alfredbuehler */ class PluginManagerController extends \Kanboard\Controller\PluginController @@ -31,7 +32,36 @@ public function show() } /** - * Install a plugin. + * Display the Plugin Info Page + * + * @access public + */ + + public function showPluginInfo() + { + $this->response->html($this->helper->layout->plugin('pluginManager:info/plugin-info', array( + 'title' => t('Plugin Manager') . ' ⥂ ' . t('Plugin Info'), + 'available_plugins' => Directory::getInstance($this->container)->getAvailablePlugins() + ))); + } + + /** + * Display the Manual Plugins Page + * + * @access public + */ + + public function showManualPlugins() + { + $this->response->html($this->helper->layout->plugin('pluginManager:plugin/manual-plugins', array( + 'title' => t('Plugin Manager') . ' ⥂ ' . t('Manual Plugins'), + ))); + } + + /** + * Install a plugin + * + * @author alfredbuehler */ public function installPlugin() { @@ -51,10 +81,11 @@ public function installPlugin() } /** - * Install a plugin from URL. + * Install a plugin from URL * * @param string $archiveUrl * @return bool + * @author alfredbuehler */ private function installByURL(string $archiveUrl): bool { @@ -64,6 +95,7 @@ private function installByURL(string $archiveUrl): bool $this->installByFile($archiveFile); } catch (PluginInstallerException $e) { $this->flash->failure($e->getMessage()); + return false; } @@ -71,10 +103,11 @@ private function installByURL(string $archiveUrl): bool } /** - * Install a plugin from file. + * Install a plugin from file * * @param string $archiveFile * @return bool + * @author alfredbuehler */ private function installByFile(string $archiveFile): bool { @@ -83,7 +116,7 @@ private function installByFile(string $archiveFile): bool try { if ($zip->open($archiveFile) !== true) { - throw new PluginInstallerException(t('Unable to open plugin archive')); + throw new PluginInstallerException(t('Unable to open the plugin archive')); } $dirname = getPluginDir($zip); @@ -91,80 +124,62 @@ private function installByFile(string $archiveFile): bool $namespace = getPluginNamespace($plugin); if ($dirname != "$namespace/") { - throw new PluginInstallerException(t("The directory name ($dirname) doesn't match with the namespace ($namespace).")); + throw new PluginInstallerException(t('The directory name (%s) does not match with the namespace (%s)', $dirname, $namespace)); } $pluginName = getPluginName($plugin); if ($pluginName != $namespace) { - throw new PluginInstallerException(t("The plugin name ($pluginName) doesn't match with the namespace ($namespace).")); + throw new PluginInstallerException(t('The plugin name (%s) does not match with the namespace (%s)', $pluginName, $namespace)); } if (!$zip->extractTo(PLUGINS_DIR)) { $zip->close(); - throw new PluginInstallerException(t('Unable to extract plugin archive.')); + throw new PluginInstallerException(t('Unable to extract the plugin archive')); } // Success - $zip->close(); unlink($archiveFile); $this->flash->success(t('Plugin installed successfully')); } catch (PluginInstallerException $e) { unlink($archiveFile); $this->flash->failure($e->getMessage()); + return false; } } else { $this->flash->failure(t('Plugin archive file not found')); + return false; } return true; } - - /** - * Display the Plugin Info Page - * - * @access public - */ - - public function showPluginInfo() - { - $this->response->html($this->helper->layout->plugin('pluginManager:info/plugin-info', array( - 'title' => t('Plugin Manager') . ' ⥂ ' . t('Plugin Info'), - 'available_plugins' => Directory::getInstance($this->container)->getAvailablePlugins() - ))); - } - - /** - * Display the Manual Plugins Page - * - * @access public - */ - - public function showManualPlugins() - { - $this->response->html($this->helper->layout->plugin('pluginManager:plugin/manual-plugins', array( - 'title' => t('Plugin Manager') . ' ⥂ ' . t('Manual Plugins'), - ))); - } } // phpcs:disable PSR1.Classes.ClassDeclaration.MultipleClasses +/** + * Different class in file + * Added by @author + * + * @author alfredbuehler + */ class Installer extends \Kanboard\Core\Plugin\Installer { /** * Download archive * - * @param string $archiveUrl - * @return string Downloaded $archiveFile + * @param string $archiveUrl + * @return string Downloaded $archiveFile + * @author alfredbuehler */ public function downloadPluginArchive($archiveUrl): string { $zip = parent::downloadPluginArchive($archiveUrl); // zip is open! $archiveFile = $zip->filename; $zip->close(); + return $archiveFile; } } @@ -175,13 +190,15 @@ public function downloadPluginArchive($archiveUrl): string * * @param ZipArchive open zip * @return string directory name in archive + * @author alfredbuehler */ function getPluginDir(ZipArchive $zip): string { $dirname = $zip->getNameIndex(0); if ($dirname === false) { - throw new PluginInstallerException(t('Plugin directory not found.')); + throw new PluginInstallerException(t('Plugin directory name was not found')); } + return $dirname; } @@ -191,13 +208,15 @@ function getPluginDir(ZipArchive $zip): string * @param ZipArchive open zip * @param string directory name in archive * @return string content of Plugin.php + * @author alfredbuehler */ function getPluginFile(ZipArchive $zip, string $dirname): string { $plugin = $zip->getFromName($dirname . 'Plugin.php'); if ($plugin === false) { - throw new PluginInstallerException(t('File Plugin.php could not get extracted.')); + throw new PluginInstallerException(t('File \'Plugin.php\' could not get extracted')); } + return $plugin; } @@ -206,6 +225,7 @@ function getPluginFile(ZipArchive $zip, string $dirname): string * * @param string content of Plugin.php * @return string namespace of plugin + * @author alfredbuehler */ function getPluginNamespace(string $plugin): string { @@ -213,17 +233,19 @@ function getPluginNamespace(string $plugin): string $rc = preg_match("/^namespace Kanboard\\\\Plugin\\\\(\w{1,});/m", $plugin, $match); if ($rc == false || $rc != 1) { - throw new PluginInstallerException('The namespace was not found.'); + throw new PluginInstallerException('The namespace was not found'); } $namespace = $match[1]; + return $namespace; } /** - * Get the plugins name + * Get the plugin's name * * @param string content of Plugin.php * @return string name of plugin + * @author alfredbuehler */ function getPluginName(string $plugin): string { @@ -231,8 +253,9 @@ function getPluginName(string $plugin): string $rc = preg_match('/(public function getPluginName\(\))(.|\n)*return ["\'](.*)["\']/U', $plugin, $match); if ($rc == false || $rc != 1) { - throw new PluginInstallerException(t('The plugin name was not found.')); + throw new PluginInstallerException(t('The plugin name was not found')); } $pluginName = $match[3]; + return $pluginName; }