feat : mise à jour de la structure du projet
This commit is contained in:
@@ -0,0 +1,81 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Shared\Infrastructure\ApiPlatform\State;
|
||||
|
||||
use ApiPlatform\Metadata\Operation;
|
||||
use ApiPlatform\State\ProviderInterface;
|
||||
use App\Shared\Infrastructure\ApiPlatform\Resource\SidebarResource;
|
||||
|
||||
/**
|
||||
* @implements ProviderInterface<object>
|
||||
*/
|
||||
class SidebarProvider implements ProviderInterface
|
||||
{
|
||||
/** @var list<string> */
|
||||
private readonly array $activeModuleIds;
|
||||
|
||||
/** @var list<array{label: string, icon: string, items: list<array{label: string, to: string, icon: string, module: string}>}> */
|
||||
private readonly array $sidebarConfig;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$configDir = dirname(__DIR__, 5).'/config';
|
||||
|
||||
// Load active modules
|
||||
$modulesFile = $configDir.'/modules.php';
|
||||
$moduleClasses = file_exists($modulesFile) ? require $modulesFile : [];
|
||||
|
||||
$ids = [];
|
||||
foreach ($moduleClasses as $moduleClass) {
|
||||
if (defined($moduleClass.'::ID')) {
|
||||
$ids[] = $moduleClass::ID;
|
||||
}
|
||||
}
|
||||
$this->activeModuleIds = $ids;
|
||||
|
||||
// Load sidebar config
|
||||
$sidebarFile = $configDir.'/sidebar.php';
|
||||
$this->sidebarConfig = file_exists($sidebarFile) ? require $sidebarFile : [];
|
||||
}
|
||||
|
||||
public function provide(Operation $operation, array $uriVariables = [], array $context = []): object
|
||||
{
|
||||
$sections = [];
|
||||
$disabledRoutes = [];
|
||||
|
||||
foreach ($this->sidebarConfig as $section) {
|
||||
$items = [];
|
||||
foreach ($section['items'] ?? [] as $item) {
|
||||
$isActive = in_array($item['module'] ?? null, $this->activeModuleIds, true);
|
||||
|
||||
if (!$isActive) {
|
||||
if (isset($item['to'])) {
|
||||
$disabledRoutes[] = $item['to'];
|
||||
}
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
$items[] = [
|
||||
'label' => $item['label'],
|
||||
'to' => $item['to'],
|
||||
'icon' => $item['icon'],
|
||||
];
|
||||
}
|
||||
|
||||
if ([] === $items) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$sections[] = [
|
||||
'label' => $section['label'],
|
||||
'icon' => $section['icon'],
|
||||
'items' => $items,
|
||||
];
|
||||
}
|
||||
|
||||
return new SidebarResource($sections, array_values(array_unique($disabledRoutes)));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user