Skip to content

Commit

Permalink
Stop extending from the SensioFrameworkExtraBundle's Template annotat…
Browse files Browse the repository at this point in the history
…ion class
  • Loading branch information
mbabker committed Dec 29, 2023
1 parent e01be81 commit 82500c7
Show file tree
Hide file tree
Showing 10 changed files with 235 additions and 49 deletions.
31 changes: 24 additions & 7 deletions Controller/Annotations/View.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,22 @@

namespace FOS\RestBundle\Controller\Annotations;

use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;

/**
* View annotation class.
*
* @Annotation
* @Target({"METHOD","CLASS"})
*/
#[\Attribute(\Attribute::TARGET_CLASS | \Attribute::TARGET_METHOD)]
class View extends Template
class View
{
/**
* The controller (+action) this annotation is set to.
*
* @var array
*/
protected $owner = [];

/**
* @var int|null
*/
Expand All @@ -42,21 +47,33 @@ class View extends Template
*/
public function __construct(
$data = [],
array $vars = [],
bool $isStreamable = false,
array $owner = [],
?int $statusCode = null,
array $serializerGroups = [],
bool $serializerEnableMaxDepthChecks = false
) {
parent::__construct($data, $vars, $isStreamable, $owner);

$values = is_array($data) ? $data : [];
$this->owner = $values['owner'] ?? $owner;
$this->statusCode = $values['statusCode'] ?? $statusCode;
$this->serializerGroups = $values['serializerGroups'] ?? $serializerGroups;
$this->serializerEnableMaxDepthChecks = $values['serializerEnableMaxDepthChecks'] ?? $serializerEnableMaxDepthChecks;
}

public function setOwner(array $owner)
{
$this->owner = $owner;
}

/**
* The controller (+action) this annotation is attached to.
*
* @return array
*/
public function getOwner()
{
return $this->owner;
}

/**
* @param int $statusCode
*/
Expand Down
131 changes: 123 additions & 8 deletions EventListener/ViewResponseListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,31 +11,112 @@

namespace FOS\RestBundle\EventListener;

use Doctrine\Common\Annotations\Reader;
use Doctrine\Persistence\Proxy;
use FOS\RestBundle\Controller\Annotations\View as ViewAnnotation;
use FOS\RestBundle\FOSRestBundle;
use FOS\RestBundle\View\View;
use FOS\RestBundle\View\ViewHandlerInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Event\ControllerEvent;
use Symfony\Component\HttpKernel\Event\ViewEvent;
use Symfony\Component\HttpKernel\KernelEvents;

/**
* The ViewResponseListener class handles the View core event as well as the "@extra:Template" annotation.
* The ViewResponseListener class handles the kernel.view event and creates a {@see Response} for a {@see View} provided by the controller result.
*
* @author Lukas Kahwe Smith <smith@pooteeweet.org>
*
* @internal
*/
class ViewResponseListener implements EventSubscriberInterface
{
/**
* @var ViewHandlerInterface
*/
private $viewHandler;

private $forceView;

public function __construct(ViewHandlerInterface $viewHandler, bool $forceView)
/**
* @var Reader|null
*/
private $annotationReader;

public function __construct(ViewHandlerInterface $viewHandler, bool $forceView, ?Reader $annotationReader = null)
{
$this->viewHandler = $viewHandler;
$this->forceView = $forceView;
$this->annotationReader = $annotationReader;
}

/**
* Extracts configuration for a {@see ViewAnnotation} from the controller if present.
*/
public function onKernelController(ControllerEvent $event)
{
$request = $event->getRequest();

if (!$request->attributes->get(FOSRestBundle::ZONE_ATTRIBUTE, true)) {
return;
}

$controller = $event->getController();

if (!\is_array($controller) && method_exists($controller, '__invoke')) {
$controller = [$controller, '__invoke'];
}

if (!\is_array($controller)) {
return;
}

$className = $this->getRealClass(\get_class($controller[0]));
$object = new \ReflectionClass($className);
$method = $object->getMethod($controller[1]);

/** @var ViewAnnotation|null $classConfiguration */
$classConfiguration = null;

/** @var ViewAnnotation|null $methodConfiguration */
$methodConfiguration = null;

if (null !== $this->annotationReader) {
$classConfiguration = $this->getViewConfiguration($this->annotationReader->getClassAnnotations($object));
$methodConfiguration = $this->getViewConfiguration($this->annotationReader->getMethodAnnotations($method));
}

if (80000 <= \PHP_VERSION_ID) {
if (null === $classConfiguration) {
$classAttributes = array_map(
function (\ReflectionAttribute $attribute) {
return $attribute->newInstance();
},
$object->getAttributes(ViewAnnotation::class, \ReflectionAttribute::IS_INSTANCEOF)
);

$classConfiguration = $this->getViewConfiguration($classAttributes);
}

if (null === $methodConfiguration) {
$methodAttributes = array_map(
function (\ReflectionAttribute $attribute) {
return $attribute->newInstance();
},
$method->getAttributes(ViewAnnotation::class, \ReflectionAttribute::IS_INSTANCEOF)
);

$methodConfiguration = $this->getViewConfiguration($methodAttributes);
}
}

// An annotation/attribute on the method takes precedence over the class level
if (null !== $methodConfiguration) {
$request->attributes->set(FOSRestBundle::VIEW_ATTRIBUTE, $methodConfiguration);
} elseif (null !== $classConfiguration) {
$request->attributes->set(FOSRestBundle::VIEW_ATTRIBUTE, $classConfiguration);
}
}

public function onKernelView(ViewEvent $event): void
Expand All @@ -46,7 +127,8 @@ public function onKernelView(ViewEvent $event): void
return;
}

$configuration = $request->attributes->get('_template');
/** @var ViewAnnotation|null $configuration */
$configuration = $request->attributes->get(FOSRestBundle::VIEW_ATTRIBUTE);

$view = $event->getControllerResult();
if (!$view instanceof View) {
Expand Down Expand Up @@ -81,16 +163,49 @@ public function onKernelView(ViewEvent $event): void
$view->setFormat($request->getRequestFormat());
}

$response = $this->viewHandler->handle($view, $request);

$event->setResponse($response);
$event->setResponse($this->viewHandler->handle($view, $request));
}

public static function getSubscribedEvents(): array
{
// Must be executed before SensioFrameworkExtraBundle's listener
return [
KernelEvents::VIEW => ['onKernelView', 30],
KernelEvents::CONTROLLER => 'onKernelController',
KernelEvents::VIEW => ['onKernelView', -128],
];
}

/**
* @param object[] $annotations
*/
private function getViewConfiguration(array $annotations): ?ViewAnnotation
{
$viewAnnotation = null;

foreach ($annotations as $annotation) {
if (!$annotation instanceof ViewAnnotation) {
continue;
}

if (null === $viewAnnotation) {
$viewAnnotation = $annotation;
} else {
throw new \LogicException('Multiple "view" annotations are not allowed.');
}
}

return $viewAnnotation;
}

private function getRealClass(string $class): string
{
if (class_exists(Proxy::class)) {
if (false === $pos = strrpos($class, '\\'.Proxy::MARKER.'\\')) {
return $class;
}

return substr($class, $pos + Proxy::MARKER_LENGTH + 2);
}

return $class;
}
}
1 change: 1 addition & 0 deletions FOSRestBundle.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
*/
class FOSRestBundle extends Bundle
{
const VIEW_ATTRIBUTE = '_fos_rest_view';
const ZONE_ATTRIBUTE = '_fos_rest_zone';

/**
Expand Down
1 change: 1 addition & 0 deletions Resources/config/view_response_listener.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
<tag name="kernel.event_subscriber" />
<argument type="service" id="fos_rest.view_handler" />
<argument /> <!-- force view -->
<argument type="service" id="annotation_reader" on-invalid="null"/>
</service>

</services>
Expand Down
Loading

0 comments on commit 82500c7

Please sign in to comment.