Skip to content

Commit

Permalink
feat: Create different operation names for each Entity with WebResponse.
Browse files Browse the repository at this point in the history
BREAKING CHANGE: Regenerate your api platform resource YAML files, or rename `getByPath` operation to `%entity%_get_by_path`
  • Loading branch information
ambroisemaupate committed Feb 12, 2024
1 parent 256acf0 commit 21fdc5c
Show file tree
Hide file tree
Showing 14 changed files with 133 additions and 58 deletions.
10 changes: 6 additions & 4 deletions config/api_resources/nsarticle.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ App\GeneratedEntity\NSArticle:
types:
- Article
operations:
ApiPlatform\Metadata\GetCollection:
article_get_collection:
method: GET
class: ApiPlatform\Metadata\GetCollection
shortName: Article
normalizationContext:
enable_max_depth: true
Expand All @@ -16,7 +17,7 @@ App\GeneratedEntity\NSArticle:
- document_display
- document_thumbnails
- document_display_sources
_api_article_archives:
article_archives_collection:
method: GET
class: ApiPlatform\Metadata\GetCollection
shortName: Article
Expand All @@ -25,8 +26,9 @@ App\GeneratedEntity\NSArticle:
archive_enabled: true
openapiContext:
summary: 'Retrieve all Article ressources archives months and years'
ApiPlatform\Metadata\Get:
article_get:
method: GET
class: ApiPlatform\Metadata\Get
shortName: Article
normalizationContext:
groups:
Expand All @@ -38,7 +40,7 @@ App\GeneratedEntity\NSArticle:
- document_display
- document_thumbnails
- document_display_sources
getByPath:
article_get_by_path:
method: GET
class: ApiPlatform\Metadata\Get
uriTemplate: /web_response_by_path
Expand Down
8 changes: 5 additions & 3 deletions config/api_resources/nsarticlecontainer.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ App\GeneratedEntity\NSArticleContainer:
types:
- ArticleContainer
operations:
ApiPlatform\Metadata\GetCollection:
articlecontainer_get_collection:
method: GET
class: ApiPlatform\Metadata\GetCollection
shortName: ArticleContainer
normalizationContext:
enable_max_depth: true
Expand All @@ -16,8 +17,9 @@ App\GeneratedEntity\NSArticleContainer:
- document_display
- document_thumbnails
- document_display_sources
ApiPlatform\Metadata\Get:
articlecontainer_get:
method: GET
class: ApiPlatform\Metadata\Get
shortName: ArticleContainer
normalizationContext:
groups:
Expand All @@ -29,7 +31,7 @@ App\GeneratedEntity\NSArticleContainer:
- document_display
- document_thumbnails
- document_display_sources
getByPath:
articlecontainer_get_by_path:
method: GET
class: ApiPlatform\Metadata\Get
uriTemplate: /web_response_by_path
Expand Down
3 changes: 2 additions & 1 deletion config/api_resources/nsarticlefeedblock.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ App\GeneratedEntity\NSArticleFeedBlock:
types:
- ArticleFeedBlock
operations:
ApiPlatform\Metadata\Get:
articlefeedblock_get:
method: GET
class: ApiPlatform\Metadata\Get
shortName: ArticleFeedBlock
normalizationContext:
groups:
Expand Down
4 changes: 3 additions & 1 deletion config/api_resources/nsbasicblock.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@ App\GeneratedEntity\NSBasicBlock:
types:
- BasicBlock
operations:
ApiPlatform\Metadata\Get:
basicblock_get:
method: GET
class: ApiPlatform\Metadata\Get
shortName: BasicBlock
normalizationContext:
groups:
- nodes_sources
- node_listing
- urls
- tag_base
- translation_base
Expand Down
4 changes: 3 additions & 1 deletion config/api_resources/nsgroupblock.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@ App\GeneratedEntity\NSGroupBlock:
types:
- GroupBlock
operations:
ApiPlatform\Metadata\Get:
groupblock_get:
method: GET
class: ApiPlatform\Metadata\Get
shortName: GroupBlock
normalizationContext:
groups:
- nodes_sources
- node_listing
- urls
- tag_base
- translation_base
Expand Down
4 changes: 3 additions & 1 deletion config/api_resources/nsmenu.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@ App\GeneratedEntity\NSMenu:
types:
- Menu
operations:
ApiPlatform\Metadata\Get:
menu_get:
method: GET
class: ApiPlatform\Metadata\Get
shortName: Menu
normalizationContext:
groups:
- nodes_sources
- node_listing
- urls
- tag_base
- translation_base
Expand Down
4 changes: 3 additions & 1 deletion config/api_resources/nsmenulink.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@ App\GeneratedEntity\NSMenuLink:
types:
- MenuLink
operations:
ApiPlatform\Metadata\Get:
menulink_get:
method: GET
class: ApiPlatform\Metadata\Get
shortName: MenuLink
normalizationContext:
groups:
- nodes_sources
- node_listing
- urls
- tag_base
- translation_base
Expand Down
4 changes: 3 additions & 1 deletion config/api_resources/nsneutral.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@ App\GeneratedEntity\NSNeutral:
types:
- Neutral
operations:
ApiPlatform\Metadata\Get:
neutral_get:
method: GET
class: ApiPlatform\Metadata\Get
shortName: Neutral
normalizationContext:
groups:
- nodes_sources
- node_listing
- urls
- tag_base
- translation_base
Expand Down
10 changes: 7 additions & 3 deletions config/api_resources/nsoffer.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ App\GeneratedEntity\NSOffer:
types:
- Offer
operations:
ApiPlatform\Metadata\GetCollection:
offer_get_collection:
method: GET
class: ApiPlatform\Metadata\GetCollection
shortName: Offer
normalizationContext:
enable_max_depth: true
Expand All @@ -16,19 +17,21 @@ App\GeneratedEntity\NSOffer:
- document_display
- document_thumbnails
- document_display_sources
ApiPlatform\Metadata\Get:
offer_get:
method: GET
class: ApiPlatform\Metadata\Get
shortName: Offer
normalizationContext:
groups:
- nodes_sources
- node_listing
- urls
- tag_base
- translation_base
- document_display
- document_thumbnails
- document_display_sources
getByPath:
offer_get_by_path:
method: GET
class: ApiPlatform\Metadata\Get
uriTemplate: /web_response_by_path
Expand All @@ -39,6 +42,7 @@ App\GeneratedEntity\NSOffer:
enable_max_depth: true
groups:
- nodes_sources
- node_listing
- urls
- tag_base
- translation_base
Expand Down
13 changes: 9 additions & 4 deletions config/api_resources/nspage.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ App\GeneratedEntity\NSPage:
types:
- Page
operations:
ApiPlatform\Metadata\GetCollection:
page_get_collection:
method: GET
class: ApiPlatform\Metadata\GetCollection
shortName: Page
normalizationContext:
enable_max_depth: true
Expand All @@ -18,7 +19,8 @@ App\GeneratedEntity\NSPage:
- document_display_sources
- nodes_sources_images
- nodes_sources_boolean
_api_page_archives:
- nodes_sources_geo
page_archives_collection:
method: GET
class: ApiPlatform\Metadata\GetCollection
shortName: Page
Expand All @@ -27,8 +29,9 @@ App\GeneratedEntity\NSPage:
archive_enabled: true
openapiContext:
summary: 'Retrieve all Page ressources archives months and years'
ApiPlatform\Metadata\Get:
page_get:
method: GET
class: ApiPlatform\Metadata\Get
shortName: Page
normalizationContext:
groups:
Expand All @@ -42,7 +45,8 @@ App\GeneratedEntity\NSPage:
- document_display_sources
- nodes_sources_images
- nodes_sources_boolean
getByPath:
- nodes_sources_geo
page_get_by_path:
method: GET
class: ApiPlatform\Metadata\Get
uriTemplate: /web_response_by_path
Expand All @@ -62,6 +66,7 @@ App\GeneratedEntity\NSPage:
- document_display_sources
- nodes_sources_images
- nodes_sources_boolean
- nodes_sources_geo
- web_response
- walker
- walker_level
Expand Down
2 changes: 1 addition & 1 deletion config/api_resources/web_response.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ RZ\Roadiz\CoreBundle\Api\Model\WebResponse:
- tag_base
- translation_base
- document_display
#- node_attributes
- node_attributes
- document_display_sources
openapiContext:
summary: Get a resource by its path wrapped in a WebResponse object
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,59 +6,65 @@

use ApiPlatform\Api\IriConverterInterface;
use ApiPlatform\Exception\InvalidArgumentException;
use ApiPlatform\Exception\OperationNotFoundException;
use ApiPlatform\Exception\ResourceClassNotFoundException;
use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface;
use Psr\Log\LoggerInterface;
use RZ\Roadiz\Core\AbstractEntities\PersistableInterface;
use RZ\Roadiz\CoreBundle\Api\DataTransformer\WebResponseDataTransformerInterface;
use RZ\Roadiz\CoreBundle\Api\Model\WebResponseInterface;
use RZ\Roadiz\CoreBundle\Entity\Redirection;
use RZ\Roadiz\CoreBundle\NodeType\ApiResourceOperationNameGenerator;
use RZ\Roadiz\CoreBundle\Preview\PreviewResolverInterface;
use RZ\Roadiz\CoreBundle\Routing\PathResolverInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Exception\ResourceNotFoundException;
use Symfony\Component\String\UnicodeString;

final class GetWebResponseByPathController extends AbstractController
{
public function __construct(
private readonly RequestStack $requestStack,
private readonly PathResolverInterface $pathResolver,
private readonly WebResponseDataTransformerInterface $webResponseDataTransformer,
private readonly IriConverterInterface $iriConverter,
private readonly PreviewResolverInterface $previewResolver
private readonly PreviewResolverInterface $previewResolver,
private readonly ApiResourceOperationNameGenerator $apiResourceOperationNameGenerator,
) {
}

public function __invoke(): ?WebResponseInterface
public function __invoke(?Request $request): ?WebResponseInterface
{
try {
if (
null === $this->requestStack->getMainRequest() ||
empty($this->requestStack->getMainRequest()->query->get('path'))
null === $request ||
empty($request->query->get('path'))
) {
throw new InvalidArgumentException('path query parameter is mandatory');
}
$resource = $this->normalizeResourcePath(
(string) $this->requestStack->getMainRequest()->query->get('path')
$request,
(string) $request->query->get('path')
);
$this->requestStack->getMainRequest()->attributes->set('data', $resource);
$this->requestStack->getMainRequest()->attributes->set('id', $resource->getId());
$request->attributes->set('data', $resource);
$request->attributes->set('id', $resource->getId());
/*
* Force API Platform to look for real resource configuration and serialization
* context. You must define "itemOperations.getByPath" for your API resource configuration.
* context. You must define "%entity%_get_by_path" operation for your API resource configuration.
*/
$this->requestStack->getMainRequest()->attributes->set('_api_resource_class', get_class($resource));
$resourceClass = get_class($resource);
$operationName = $this->apiResourceOperationNameGenerator->generateGetByPath($resourceClass);

$request->attributes->set('_api_operation_name', $operationName);
$request->attributes->set('_api_resource_class', $resourceClass);
$request->attributes->set('_stateless', true);
return $this->webResponseDataTransformer->transform($resource, WebResponseInterface::class);
} catch (ResourceNotFoundException $exception) {
throw new NotFoundHttpException($exception->getMessage(), $exception);
throw $this->createNotFoundException($exception->getMessage(), $exception);
}
}

/**
* @param string $path
* @return PersistableInterface
*/
protected function normalizeResourcePath(string $path): PersistableInterface
protected function normalizeResourcePath(?Request $request, string $path): PersistableInterface
{
/*
* Serve any PersistableInterface Resource by implementing
Expand Down Expand Up @@ -93,21 +99,20 @@ protected function normalizeResourcePath(string $path): PersistableInterface
* Recursive call to normalize path coming from Redirection if redirected path
* is internal (starting with /)
*/
return $this->normalizeResourcePath($resource->getRedirectUri());
return $this->normalizeResourcePath($request, $resource->getRedirectUri());
}
}

$this->addResourceToCacheTags($resource);
$this->addResourceToCacheTags($request, $resource);

/*
* Or plain entity
*/
return $resource;
}

protected function addResourceToCacheTags(PersistableInterface $resource): void
protected function addResourceToCacheTags(?Request $request, PersistableInterface $resource): void
{
$request = $this->requestStack->getMainRequest();
if (null !== $request) {
$iri = $this->iriConverter->getIriFromResource($resource);
$request->attributes->set('_resources', $request->attributes->get('_resources', []) + [ $iri => $iri ]);
Expand Down
Loading

0 comments on commit 21fdc5c

Please sign in to comment.