feat(central) : ajoute le pilotage de maintenance et le deploiement prod
This commit is contained in:
0
src/ApiResource/.gitignore
vendored
Normal file
0
src/ApiResource/.gitignore
vendored
Normal file
51
src/ApiResource/ManagedApplication.php
Normal file
51
src/ApiResource/ManagedApplication.php
Normal file
@@ -0,0 +1,51 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\ApiResource;
|
||||
|
||||
use ApiPlatform\Metadata\ApiProperty;
|
||||
use ApiPlatform\Metadata\ApiResource;
|
||||
use ApiPlatform\Metadata\Get;
|
||||
use ApiPlatform\Metadata\GetCollection;
|
||||
use ApiPlatform\Metadata\Post;
|
||||
use App\State\ManagedApplicationProvider;
|
||||
use App\State\MaintenanceToggleProcessor;
|
||||
use Symfony\Component\Serializer\Attribute\Groups;
|
||||
|
||||
#[ApiResource(
|
||||
operations: [
|
||||
new GetCollection(
|
||||
uriTemplate: '/applications',
|
||||
normalizationContext: ['groups' => ['app:read']],
|
||||
security: "is_granted('ROLE_ADMIN')",
|
||||
provider: ManagedApplicationProvider::class,
|
||||
),
|
||||
new Get(
|
||||
uriTemplate: '/applications/{slug}',
|
||||
normalizationContext: ['groups' => ['app:read']],
|
||||
security: "is_granted('ROLE_ADMIN')",
|
||||
provider: ManagedApplicationProvider::class,
|
||||
),
|
||||
new Post(
|
||||
uriTemplate: '/applications/{slug}/maintenance',
|
||||
normalizationContext: ['groups' => ['app:read']],
|
||||
denormalizationContext: ['groups' => ['app:write']],
|
||||
security: "is_granted('ROLE_ADMIN')",
|
||||
provider: ManagedApplicationProvider::class,
|
||||
processor: MaintenanceToggleProcessor::class,
|
||||
),
|
||||
],
|
||||
)]
|
||||
final class ManagedApplication
|
||||
{
|
||||
#[ApiProperty(identifier: true)]
|
||||
#[Groups(['app:read'])]
|
||||
public string $slug = '';
|
||||
|
||||
#[Groups(['app:read'])]
|
||||
public string $name = '';
|
||||
|
||||
#[Groups(['app:read', 'app:write'])]
|
||||
public bool $maintenance = false;
|
||||
}
|
||||
0
src/Controller/.gitignore
vendored
Normal file
0
src/Controller/.gitignore
vendored
Normal file
0
src/Entity/.gitignore
vendored
Normal file
0
src/Entity/.gitignore
vendored
Normal file
0
src/Repository/.gitignore
vendored
Normal file
0
src/Repository/.gitignore
vendored
Normal file
63
src/State/MaintenanceToggleProcessor.php
Normal file
63
src/State/MaintenanceToggleProcessor.php
Normal file
@@ -0,0 +1,63 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\State;
|
||||
|
||||
use ApiPlatform\Metadata\Operation;
|
||||
use ApiPlatform\State\ProcessorInterface;
|
||||
use App\ApiResource\ManagedApplication;
|
||||
use Symfony\Component\DependencyInjection\Attribute\Autowire;
|
||||
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
|
||||
|
||||
final readonly class MaintenanceToggleProcessor implements ProcessorInterface
|
||||
{
|
||||
/**
|
||||
* @param list<array{name: string, slug: string, maintenance_path: string}> $managedApplications
|
||||
*/
|
||||
public function __construct(
|
||||
#[Autowire('%app.managed_applications%')]
|
||||
private array $managedApplications,
|
||||
) {}
|
||||
|
||||
/**
|
||||
* @param ManagedApplication $data
|
||||
*/
|
||||
public function process(mixed $data, Operation $operation, array $uriVariables = [], array $context = []): ManagedApplication
|
||||
{
|
||||
$slug = $uriVariables['slug'] ?? '';
|
||||
$appConfig = null;
|
||||
|
||||
foreach ($this->managedApplications as $app) {
|
||||
if ($app['slug'] === $slug) {
|
||||
$appConfig = $app;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (null === $appConfig) {
|
||||
throw new NotFoundHttpException(sprintf('Application "%s" not found.', $slug));
|
||||
}
|
||||
|
||||
$maintenancePath = $appConfig['maintenance_path'];
|
||||
|
||||
if ($data->maintenance) {
|
||||
$directory = dirname($maintenancePath);
|
||||
|
||||
if (!is_dir($directory)) {
|
||||
mkdir($directory, 0755, true);
|
||||
}
|
||||
|
||||
touch($maintenancePath);
|
||||
} elseif (file_exists($maintenancePath)) {
|
||||
unlink($maintenancePath);
|
||||
}
|
||||
|
||||
$dto = new ManagedApplication();
|
||||
$dto->slug = $appConfig['slug'];
|
||||
$dto->name = $appConfig['name'];
|
||||
$dto->maintenance = file_exists($maintenancePath);
|
||||
|
||||
return $dto;
|
||||
}
|
||||
}
|
||||
56
src/State/ManagedApplicationProvider.php
Normal file
56
src/State/ManagedApplicationProvider.php
Normal file
@@ -0,0 +1,56 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\State;
|
||||
|
||||
use ApiPlatform\Metadata\GetCollection;
|
||||
use ApiPlatform\Metadata\Operation;
|
||||
use ApiPlatform\State\ProviderInterface;
|
||||
use App\ApiResource\ManagedApplication;
|
||||
use Symfony\Component\DependencyInjection\Attribute\Autowire;
|
||||
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
|
||||
|
||||
final readonly class ManagedApplicationProvider implements ProviderInterface
|
||||
{
|
||||
/**
|
||||
* @param list<array{name: string, slug: string, maintenance_path: string}> $managedApplications
|
||||
*/
|
||||
public function __construct(
|
||||
#[Autowire('%app.managed_applications%')]
|
||||
private array $managedApplications,
|
||||
) {}
|
||||
|
||||
public function provide(Operation $operation, array $uriVariables = [], array $context = []): ManagedApplication|array
|
||||
{
|
||||
if ($operation instanceof GetCollection) {
|
||||
return array_map(
|
||||
fn (array $app) => $this->buildDto($app),
|
||||
$this->managedApplications,
|
||||
);
|
||||
}
|
||||
|
||||
$slug = $uriVariables['slug'] ?? '';
|
||||
|
||||
foreach ($this->managedApplications as $app) {
|
||||
if ($app['slug'] === $slug) {
|
||||
return $this->buildDto($app);
|
||||
}
|
||||
}
|
||||
|
||||
throw new NotFoundHttpException(sprintf('Application "%s" not found.', $slug));
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array{name: string, slug: string, maintenance_path: string} $app
|
||||
*/
|
||||
private function buildDto(array $app): ManagedApplication
|
||||
{
|
||||
$dto = new ManagedApplication();
|
||||
$dto->slug = $app['slug'];
|
||||
$dto->name = $app['name'];
|
||||
$dto->maintenance = file_exists($app['maintenance_path']);
|
||||
|
||||
return $dto;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user