-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
1 parent
866cadc
commit d5929e8
Showing
1 changed file
with
71 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |