From d5929e838010081f4deab385c1c44022a654590f Mon Sep 17 00:00:00 2001 From: Vincent Garcia Date: Thu, 9 Feb 2023 16:30:49 +0100 Subject: [PATCH] :sparkles: Adding DependencyBuilder In order to correctly build the dependencies variable to instanciate the CDC, add an helper to ease the work of module devs. --- src/DependencyBuilder.php | 71 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 71 insertions(+) create mode 100644 src/DependencyBuilder.php diff --git a/src/DependencyBuilder.php b/src/DependencyBuilder.php new file mode 100644 index 0000000..4cd6de9 --- /dev/null +++ b/src/DependencyBuilder.php @@ -0,0 +1,71 @@ +module = $module; + } + + /** + * Build the dependencies data array to be given to the CDC + * + * @return array + */ + public function buildDependencies() + { + $data = [ + 'module_display_name' => $this->module->displayName, + 'module_name' => $this->module->name, + 'module_version' => $this->module->version, + 'ps_version' => _PS_VERSION_, + 'php_version' => PHP_VERSION, + 'dependencies' => [], + ]; + + $dependencyFile = $this->module->getLocalPath() . self::DEPENDENCY_FILENAME; + if (!file_exists($dependencyFile)) { + throw new \Exception(self::DEPENDENCY_FILENAME . ' file is not found in ' . $this->module->getLocalPath()); + } + + $dependenciesContent = json_decode(file_get_contents($dependencyFile), true); + if (json_last_error() != JSON_ERROR_NONE) { + throw new \Exception(self::DEPENDENCY_FILENAME . ' file may be malformatted.'); + } + + if (empty($dependenciesContent['dependencies'])) { + return $data; + } + + foreach ($dependenciesContent['dependencies'] as $dependencyName => $dependencyMinVersion) { + $dependencyData = \DbCore::getInstance()->getRow('SELECT `id_module`, `active`, `version` FROM `' . _DB_PREFIX_ . 'module` WHERE `name` = "' . pSQL($dependencyName) . '"'); + if (!$dependencyData) { + $data['dependencies'][$dependencyName] = [ + 'min_version' => $dependencyMinVersion, + 'installed' => false, + ]; + continue; + } + $data['dependencies'][$dependencyName] = [ + 'min_version' => $dependencyMinVersion, + 'installed' => true, + 'enabled' => (bool) $dependencyData['active'], + 'current_version' => $dependencyData['version'], + ]; + } + + return $data; + } +}