feat : mise à jour de la structure du projet
This commit is contained in:
@@ -0,0 +1,24 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Shared\Infrastructure\ApiPlatform\Resource;
|
||||
|
||||
use ApiPlatform\Metadata\ApiResource;
|
||||
use ApiPlatform\Metadata\Get;
|
||||
use App\Shared\Infrastructure\ApiPlatform\State\AppVersionProvider;
|
||||
|
||||
#[ApiResource(
|
||||
operations: [
|
||||
new Get(
|
||||
uriTemplate: '/version',
|
||||
provider: AppVersionProvider::class,
|
||||
),
|
||||
],
|
||||
)]
|
||||
class AppVersion
|
||||
{
|
||||
public function __construct(
|
||||
public readonly string $version,
|
||||
) {}
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Shared\Infrastructure\ApiPlatform\Resource;
|
||||
|
||||
use ApiPlatform\Metadata\ApiResource;
|
||||
use ApiPlatform\Metadata\Get;
|
||||
use App\Shared\Infrastructure\ApiPlatform\State\ModulesProvider;
|
||||
|
||||
#[ApiResource(
|
||||
operations: [
|
||||
new Get(
|
||||
uriTemplate: '/modules',
|
||||
provider: ModulesProvider::class,
|
||||
),
|
||||
],
|
||||
)]
|
||||
class ModulesResource
|
||||
{
|
||||
/** @var list<string> */
|
||||
public array $modules = [];
|
||||
|
||||
/** @param list<string> $modules */
|
||||
public function __construct(array $modules = [])
|
||||
{
|
||||
$this->modules = $modules;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Shared\Infrastructure\ApiPlatform\Resource;
|
||||
|
||||
use ApiPlatform\Metadata\ApiResource;
|
||||
use ApiPlatform\Metadata\Get;
|
||||
use App\Shared\Infrastructure\ApiPlatform\State\SidebarProvider;
|
||||
|
||||
#[ApiResource(
|
||||
operations: [
|
||||
new Get(
|
||||
uriTemplate: '/sidebar',
|
||||
provider: SidebarProvider::class,
|
||||
),
|
||||
],
|
||||
)]
|
||||
class SidebarResource
|
||||
{
|
||||
/** @var list<array{label: string, icon: string, items: list<array{label: string, to: string, icon: string}>}> */
|
||||
public array $sections = [];
|
||||
|
||||
/** @var list<string> */
|
||||
public array $disabledRoutes = [];
|
||||
|
||||
/**
|
||||
* @param list<array{label: string, icon: string, items: list<array{label: string, to: string, icon: string}>}> $sections
|
||||
* @param list<string> $disabledRoutes
|
||||
*/
|
||||
public function __construct(array $sections = [], array $disabledRoutes = [])
|
||||
{
|
||||
$this->sections = $sections;
|
||||
$this->disabledRoutes = $disabledRoutes;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
<?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\AppVersion;
|
||||
use Symfony\Component\DependencyInjection\Attribute\Autowire;
|
||||
|
||||
/**
|
||||
* @implements ProviderInterface<object>
|
||||
*/
|
||||
class AppVersionProvider implements ProviderInterface
|
||||
{
|
||||
public function __construct(
|
||||
#[Autowire(param: 'app.version')]
|
||||
private readonly string $appVersion,
|
||||
) {}
|
||||
|
||||
public function provide(Operation $operation, array $uriVariables = [], array $context = []): object
|
||||
{
|
||||
return new AppVersion($this->appVersion);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
<?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\ModulesResource;
|
||||
|
||||
/**
|
||||
* @implements ProviderInterface<object>
|
||||
*/
|
||||
class ModulesProvider implements ProviderInterface
|
||||
{
|
||||
/** @var list<string> */
|
||||
private readonly array $activeModuleIds;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$configPath = dirname(__DIR__, 5).'/config/modules.php';
|
||||
$moduleClasses = file_exists($configPath) ? require $configPath : [];
|
||||
|
||||
$ids = [];
|
||||
foreach ($moduleClasses as $moduleClass) {
|
||||
if (defined($moduleClass.'::ID')) {
|
||||
$ids[] = $moduleClass::ID;
|
||||
}
|
||||
}
|
||||
|
||||
$this->activeModuleIds = $ids;
|
||||
}
|
||||
|
||||
public function provide(Operation $operation, array $uriVariables = [], array $context = []): object
|
||||
{
|
||||
return new ModulesResource($this->activeModuleIds);
|
||||
}
|
||||
}
|
||||
@@ -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