Skip to content

Commit

Permalink
✨ Adding DependencyBuilder
Browse files Browse the repository at this point in the history
In order to correctly build the dependencies variable to instanciate the CDC, add an helper to ease the work of module devs.
  • Loading branch information
intraordinaire committed Feb 9, 2023
1 parent 866cadc commit d5929e8
Showing 1 changed file with 71 additions and 0 deletions.
71 changes: 71 additions & 0 deletions src/DependencyBuilder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?php

namespace Prestashop\ModuleLibMboInstaller;

class DependencyBuilder
{
const DEPENDENCY_FILENAME = 'ps_dependencies.json';

/**
* @var \ModuleCore
*/
protected $module;

/**
* @param \ModuleCore $module
*/
public function __construct($module)
{
$this->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;
}
}

0 comments on commit d5929e8

Please sign in to comment.