Skip to content

Commit

Permalink
feat: Set Request attribute data as WebResponse instead of WebRespo…
Browse files Browse the repository at this point in the history
…nse item. Changed LinkHeader and AddHeaders event subscribers to check if `data` is WebResponseInterface
  • Loading branch information
roadiz-ci committed Mar 26, 2024
1 parent cb44b25 commit b8b7408
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/Api/Controller/GetWebResponseByPathController.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,10 @@ public function __invoke(?Request $request): ?WebResponseInterface
$request->attributes->set('_locale', $resource->getTranslation()->getPreferredLocale());
}

return $this->webResponseDataTransformer->transform($resource, WebResponseInterface::class);
$data = $this->webResponseDataTransformer->transform($resource, WebResponseInterface::class);
$request->attributes->set('data', $data);

return $data;
} catch (ResourceNotFoundException | ResourceClassNotFoundException $exception) {
throw $this->createNotFoundException($exception->getMessage(), $exception);
}
Expand Down
6 changes: 6 additions & 0 deletions src/EventSubscriber/NodesSourcesAddHeadersSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace RZ\Roadiz\CoreBundle\EventSubscriber;

use ApiPlatform\Util\RequestAttributesExtractor;
use RZ\Roadiz\CoreBundle\Api\Model\WebResponseInterface;
use RZ\Roadiz\CoreBundle\Entity\NodesSources;
use RZ\Roadiz\CoreBundle\Preview\PreviewResolverInterface;
use Symfony\Bundle\SecurityBundle\Security;
Expand Down Expand Up @@ -55,6 +56,11 @@ public function onKernelResponse(ResponseEvent $event): void
$resourceCacheHeaders = $attributes['cache_headers'] ?? [];
$data = $request->attributes->get('data');

// Work with WebResponse item instead of WebResponse itself
if ($data instanceof WebResponseInterface) {
$data = $data->getItem();
}

// if the public-property is defined and not yet set; apply it to the response
$public = $resourceCacheHeaders['public'] ?? null;
if (null !== $public && !$response->headers->hasCacheControlDirective('public')) {
Expand Down
6 changes: 6 additions & 0 deletions src/EventSubscriber/NodesSourcesLinkHeaderEventSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use Doctrine\Persistence\ManagerRegistry;
use Psr\Link\EvolvableLinkProviderInterface;
use RZ\Roadiz\CoreBundle\Api\Model\WebResponseInterface;
use RZ\Roadiz\CoreBundle\Entity\NodesSources;
use Symfony\Cmf\Component\Routing\RouteObjectInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
Expand Down Expand Up @@ -38,6 +39,11 @@ public function onKernelView(ViewEvent $event): void
$resources = $request->attributes->get('data');
$linkProvider = $request->attributes->get('_links', new GenericLinkProvider());

// Work with WebResponse item instead of WebResponse itself
if ($resources instanceof WebResponseInterface) {
$resources = $resources->getItem();
}

if (!$resources instanceof NodesSources || !$linkProvider instanceof EvolvableLinkProviderInterface) {
return;
}
Expand Down

0 comments on commit b8b7408

Please sign in to comment.