82 lines
2.4 KiB
PHP
82 lines
2.4 KiB
PHP
<?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)));
|
|
}
|
|
}
|