Skip to content

Commit

Permalink
✨ Adding routes to be called in context
Browse files Browse the repository at this point in the history
  • Loading branch information
intraordinaire committed Feb 10, 2023
1 parent 1368820 commit 120891d
Showing 1 changed file with 59 additions and 7 deletions.
66 changes: 59 additions & 7 deletions src/DependencyBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Prestashop\ModuleLibMboInstaller;

use Symfony\Component\Routing\Router;

class DependencyBuilder
{
const DEPENDENCY_FILENAME = 'ps_dependencies.json';
Expand All @@ -11,12 +13,20 @@ class DependencyBuilder
*/
protected $module;

/**
* @var Router
*/
protected $router;

/**
* @param \ModuleCore $module
*
* @throws \Exception
*/
public function __construct($module)
{
$this->module = $module;
$this->buildRouter();
}

/**
Expand All @@ -36,6 +46,8 @@ public function __construct($module)
* "enabled"?: bool
* }>
* }
*
* @throws \Exception
*/
public function buildDependencies()
{
Expand Down Expand Up @@ -77,21 +89,61 @@ public function buildDependencies()

foreach ($dependenciesContent['dependencies'] as $dependencyName => $dependencyMinVersion) {
$dependencyData = \DbCore::getInstance()->getRow('SELECT `id_module`, `active`, `version` FROM `' . _DB_PREFIX_ . 'module` WHERE `name` = "' . pSQL((string) $dependencyName) . '"');

$data['dependencies'][$dependencyName] = array_merge(['min_version' => (string) $dependencyMinVersion], $this->buildRoutesForModule($dependencyName));
if (!$dependencyData) {
$data['dependencies'][$dependencyName] = [
'min_version' => (string) $dependencyMinVersion,
'installed' => false,
];
$data['dependencies'][$dependencyName]['installed'] = false;
continue;
}
$data['dependencies'][$dependencyName] = [
'min_version' => (string) $dependencyMinVersion,
$data['dependencies'][$dependencyName] = array_merge($data['dependencies'][$dependencyName], [
'installed' => true,
'enabled' => isset($dependencyData['active']) && (bool) $dependencyData['active'],
'current_version' => isset($dependencyData['version']) ? $dependencyData['version'] : null,
];
]);
}

return $data;
}

/**
* @param string $moduleName
*
* @return array<string, string>
*/
protected function buildRoutesForModule($moduleName)
{
$urls = [];
foreach (['install', 'enable', 'upgrade'] as $action) {
$urls[$action] = $this->router->generate('admin_module_manage_action', [
'action' => $action,
'module_name' => $moduleName,
]);
}

return $urls;
}

/**
* @return void
*
* @throws \Exception
*/
protected function buildRouter()
{
global $kernel;
if (!$kernel instanceof \AppKernel) {
throw new \Exception('Unable to retrieve Symfony AppKernel.');
}

$container = $kernel->getContainer();
if (!$container instanceof \Symfony\Component\DependencyInjection\ContainerInterface) {
throw new \Exception('Unable to retrieve Symfony ContainerInterface.');
}

$router = $container->get('router');
if (!$router instanceof Router) {
throw new \Exception('Unable to retrieve Symfony Router.');
}
$this->router = $router;
}
}

0 comments on commit 120891d

Please sign in to comment.