fix : use custom EnvironmentCreateProcessor to properly add environments
API Platform's default sub-resource POST was replacing instead of adding. Custom processor with read:false + Link + manual persist fixes this. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -10,6 +10,7 @@ use ApiPlatform\Metadata\Link;
|
|||||||
use ApiPlatform\Metadata\Patch;
|
use ApiPlatform\Metadata\Patch;
|
||||||
use ApiPlatform\Metadata\Post;
|
use ApiPlatform\Metadata\Post;
|
||||||
use App\Repository\EnvironmentRepository;
|
use App\Repository\EnvironmentRepository;
|
||||||
|
use App\State\EnvironmentCreateProcessor;
|
||||||
use App\State\MaintenanceToggleProcessor;
|
use App\State\MaintenanceToggleProcessor;
|
||||||
use Doctrine\Common\Collections\ArrayCollection;
|
use Doctrine\Common\Collections\ArrayCollection;
|
||||||
use Doctrine\Common\Collections\Collection;
|
use Doctrine\Common\Collections\Collection;
|
||||||
@@ -23,7 +24,9 @@ use Symfony\Component\Serializer\Attribute\Groups;
|
|||||||
uriVariables: [
|
uriVariables: [
|
||||||
'slug' => new Link(toProperty: 'application', fromClass: Application::class, identifiers: ['slug']),
|
'slug' => new Link(toProperty: 'application', fromClass: Application::class, identifiers: ['slug']),
|
||||||
],
|
],
|
||||||
|
read: false,
|
||||||
security: "is_granted('ROLE_ADMIN')",
|
security: "is_granted('ROLE_ADMIN')",
|
||||||
|
processor: EnvironmentCreateProcessor::class,
|
||||||
),
|
),
|
||||||
new Patch(
|
new Patch(
|
||||||
security: "is_granted('ROLE_ADMIN')",
|
security: "is_granted('ROLE_ADMIN')",
|
||||||
|
|||||||
44
src/State/EnvironmentCreateProcessor.php
Normal file
44
src/State/EnvironmentCreateProcessor.php
Normal file
@@ -0,0 +1,44 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace App\State;
|
||||||
|
|
||||||
|
use ApiPlatform\Metadata\Operation;
|
||||||
|
use ApiPlatform\State\ProcessorInterface;
|
||||||
|
use App\Entity\Environment;
|
||||||
|
use App\Repository\ApplicationRepository;
|
||||||
|
use Doctrine\ORM\EntityManagerInterface;
|
||||||
|
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
|
||||||
|
|
||||||
|
final readonly class EnvironmentCreateProcessor implements ProcessorInterface
|
||||||
|
{
|
||||||
|
public function __construct(
|
||||||
|
private ApplicationRepository $applicationRepository,
|
||||||
|
private EntityManagerInterface $entityManager,
|
||||||
|
) {}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param Environment $data
|
||||||
|
*/
|
||||||
|
public function process(mixed $data, Operation $operation, array $uriVariables = [], array $context = []): Environment
|
||||||
|
{
|
||||||
|
$slug = $uriVariables['slug']
|
||||||
|
?? $context['request']?->attributes->get('slug')
|
||||||
|
?? $context['request']?->attributes->get('_route_params')['slug']
|
||||||
|
?? '';
|
||||||
|
|
||||||
|
$application = $this->applicationRepository->findOneBy(['slug' => $slug]);
|
||||||
|
|
||||||
|
if (null === $application) {
|
||||||
|
throw new NotFoundHttpException(sprintf('Application "%s" not found.', $slug));
|
||||||
|
}
|
||||||
|
|
||||||
|
$data->setApplication($application);
|
||||||
|
|
||||||
|
$this->entityManager->persist($data);
|
||||||
|
$this->entityManager->flush();
|
||||||
|
|
||||||
|
return $data;
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user