diff --git a/src/DependencyBuilder.php b/src/DependencyBuilder.php index fc90d09..341fc8f 100644 --- a/src/DependencyBuilder.php +++ b/src/DependencyBuilder.php @@ -64,8 +64,13 @@ public function areDependenciesMet() { $dependencies = $this->getDependencies(false); - foreach ($dependencies as $dependency) { - if (false === $dependency['installed'] || false === $dependency['enabled']) { + foreach ($dependencies as $dependencyName => $dependency) { + if ( + !array_key_exists('installed', $dependency) + || !array_key_exists('enabled', $dependency) + || false === $dependency['installed'] + || false === $dependency['enabled'] + ) { return false; } } @@ -176,10 +181,14 @@ protected function buildRoutesForModule($moduleName) { $urls = []; foreach (['install', 'enable', 'upgrade'] as $action) { - $urls[$action] = $this->router->generate('admin_module_manage_action', [ + $route = $this->router->generate('admin_module_manage_action', [ 'action' => $action, 'module_name' => $moduleName, ]); + + if (is_string($route)) { + $urls[$action] = $route; + } } return $urls; @@ -210,11 +219,18 @@ protected function buildRouter() } /** - * @return array|null + * @param bool $addRoutes + * + * @return array{ + * "name": string, + * "installed": bool, + * "enabled": bool, + * "current_version": string, + * }|non-empty-array|null * * @throws \Exception */ - protected function addMboInDependencies() + protected function addMboInDependencies($addRoutes = false) { if (!$this->isMboNeeded()) { return null; @@ -222,18 +238,21 @@ protected function addMboInDependencies() $mboStatus = (new Presenter())->present(); - if ((bool) $mboStatus['isEnabled']) { - return null; - } - - $mboRoutes = $this->buildRoutesForModule(Installer::MODULE_NAME); - - return array_merge([ + $specification = [ 'current_version' => (string) $mboStatus['version'], 'installed' => (bool) $mboStatus['isInstalled'], - 'enabled' => false, + 'enabled' => (bool) $mboStatus['isEnabled'], 'name' => Installer::MODULE_NAME, - ], $mboRoutes); + ]; + + if (!$addRoutes) { + return $specification; + } + + return array_merge( + $specification, + $this->buildRoutesForModule(Installer::MODULE_NAME) + ); } /** @@ -247,7 +266,12 @@ protected function isMboNeeded() /** * @param bool $addRoutes * - * @return array{}|array{ps_mbo: array} + * @return array|array> * * @throws \Exception */ @@ -256,8 +280,13 @@ private function getDependencies($addRoutes = false) $dependenciesContent = $this->getDependenciesSpecification(); if (empty($dependenciesContent['dependencies'])) { + $mboDependency = $this->addMboInDependencies($addRoutes); + if (null === $mboDependency) { + return []; + } + return [ - Installer::MODULE_NAME => $this->addMboInDependencies(), + Installer::MODULE_NAME => $mboDependency, ]; } @@ -269,6 +298,10 @@ private function getDependencies($addRoutes = false) $dependencies = []; foreach ($dependenciesContent['dependencies'] as $dependency) { + if (!is_array($dependency) || !array_key_exists('name', $dependency)) { + continue; + } + $dependencyData = \DbCore::getInstance()->getRow( sprintf( 'SELECT `id_module`, `active`, `version` FROM `%smodule` WHERE `name` = "%s"', @@ -303,7 +336,7 @@ private function getDependencies($addRoutes = false) /** * @return array{ - * "dependencies": array{} + * "dependencies": array * } * * @throws \Exception @@ -318,7 +351,12 @@ private function getDependenciesSpecification() if ($fileContent = file_get_contents($dependencyFile)) { $dependenciesContent = json_decode($fileContent, true); } - if (!isset($dependenciesContent) || !is_array($dependenciesContent) || json_last_error() != JSON_ERROR_NONE) { + if ( + !isset($dependenciesContent) + || !is_array($dependenciesContent) + || !array_key_exists('dependencies', $dependenciesContent) + || json_last_error() != JSON_ERROR_NONE + ) { throw new \Exception(self::DEPENDENCY_FILENAME . ' file may be malformed.'); }