Skip to content

Commit

Permalink
✨ Adding locale in context of DependencyBuilder.php
Browse files Browse the repository at this point in the history
  • Loading branch information
intraordinaire committed Feb 9, 2023
1 parent d5929e8 commit 1a5ed51
Showing 1 changed file with 40 additions and 14 deletions.
54 changes: 40 additions & 14 deletions src/DependencyBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,47 +22,73 @@ public function __construct($module)
/**
* Build the dependencies data array to be given to the CDC
*
* @return array
* @return array{
* "module_display_name": string,
* "module_name": string,
* "module_version": string,
* "ps_version": string,
* "php_version": string,
* "locale": string,
* "dependencies": array{
* "min_version"?: string,
* "current_version"?: string,
* "installed"?: bool,
* "enabled"?: bool
* }
* }
*/
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,
'module_display_name' => (string) $this->module->displayName,
'module_name' => (string) $this->module->name,
'module_version' => (string) $this->module->version,
'ps_version' => (string) _PS_VERSION_,
'php_version' => (string) PHP_VERSION,
'dependencies' => [],
];

$context = \ContextCore::getContext();
if ($context !== null && $context->employee !== null) {
$locale = \DbCore::getInstance()->getValue('SELECT `locale` FROM `' . _DB_PREFIX_ . 'lang` WHERE `id_lang`=' . pSQL((string) $context->employee->id_lang));
}

if (empty($locale)) {
$locale = 'en-GB';
}

$data['locale'] = (string) $locale;

$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) {
if ($fileContent = file_get_contents($dependencyFile)) {
$dependenciesContent = json_decode($fileContent, true);
}
if (!isset($dependenciesContent) || json_last_error() != JSON_ERROR_NONE) {
throw new \Exception(self::DEPENDENCY_FILENAME . ' file may be malformatted.');
}

if (empty($dependenciesContent['dependencies'])) {
if (!is_array($dependenciesContent) || empty($dependenciesContent['dependencies']) || !is_array($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) . '"');
$dependencyData = \DbCore::getInstance()->getRow('SELECT `id_module`, `active`, `version` FROM `' . _DB_PREFIX_ . 'module` WHERE `name` = "' . pSQL((string) $dependencyName) . '"');
if (!$dependencyData) {
$data['dependencies'][$dependencyName] = [
'min_version' => $dependencyMinVersion,
'min_version' => (string) $dependencyMinVersion,
'installed' => false,
];
continue;
}
$data['dependencies'][$dependencyName] = [
'min_version' => $dependencyMinVersion,
'min_version' => (string) $dependencyMinVersion,
'installed' => true,
'enabled' => (bool) $dependencyData['active'],
'current_version' => $dependencyData['version'],
'enabled' => isset($dependencyData['active']) && (bool) $dependencyData['active'],
'current_version' => isset($dependencyData['version']) ? $dependencyData['version'] : null,
];
}

Expand Down

0 comments on commit 1a5ed51

Please sign in to comment.