src/App/Component/ViewLayer/ViewLayerService.php line 120

Open in your IDE?
  1. <?php
  2. namespace App\Component\ViewLayer;
  3. use App\Twig\ViewLayerGlobal;
  4. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  5. use Symfony\Component\HttpFoundation\Request;
  6. use Symfony\Component\HttpFoundation\Response;
  7. use Symfony\Component\HttpKernel\Event\ViewEvent;
  8. use Symfony\Component\HttpKernel\KernelEvents;
  9. /**
  10.  * Class ViewLayerService
  11.  * @package App\Component\ViewLayer
  12.  */
  13. final class ViewLayerService implements EventSubscriberInterface
  14. {
  15.     /**
  16.      * @var ViewLayerGlobal
  17.      */
  18.     protected ViewLayerGlobal $global;
  19.     /**
  20.      * @var array|AbstractViewHandler[]
  21.      */
  22.     protected array $handlers = [];
  23.     /**
  24.      * @param ViewLayerGlobal $global
  25.      */
  26.     public function __construct(ViewLayerGlobal $global)
  27.     {
  28.         $this->global $global;
  29.     }
  30.     /**
  31.      * @param AbstractViewHandler $handler
  32.      * @return $this
  33.      */
  34.     public function registerHandler(AbstractViewHandler $handler): self
  35.     {
  36.         if ( ! in_array($handler$this->handlerstrue)) {
  37.             $this->handlers[] = $handler;
  38.         }
  39.         return $this;
  40.     }
  41.     /**
  42.      * @param AbstractView $view
  43.      * @return AbstractViewHandler
  44.      */
  45.     protected function determineHandler(AbstractView $view): AbstractViewHandler
  46.     {
  47.         foreach ($this->handlers as $handler) {
  48.             if ($handler->supports($view)) {
  49.                 return $handler;
  50.             }
  51.         }
  52.         throw new \Exception();
  53.     }
  54.     /**
  55.      * {@inheritDoc}
  56.      */
  57.     public static function getSubscribedEvents(): array
  58.     {
  59.         return [
  60.             KernelEvents::VIEW => [
  61.                 ['onKernelView'PHP_INT_MIN],
  62.             ],
  63.         ];
  64.     }
  65.     /**
  66.      * @param AbstractView $view
  67.      * @param Request|null $request
  68.      * @return Response
  69.      */
  70.     public function handle(AbstractView $view, ?Request $request null): Response
  71.     {
  72.         // set up the globals
  73.         $this->global->setup($view);
  74.         // attach the request
  75.         if ($request) {
  76.             $view->setRequest($request);
  77.         }
  78.         if ( ! $view->getRequest()) {
  79.             throw new \Exception();
  80.         }
  81.         // find the handler for this type
  82.         $handler $this->determineHandler($view);
  83.         // do prepare
  84.         $handler->prepare($view);
  85.         // handle the view
  86.         $content $handler->handle($view);
  87.         // generate the content for the response and finish the response
  88.         $view->getResponse()
  89.             ->setContent($content);
  90.         $view->getResponse()->headers->add([
  91.             'Content-Type' => $view->getHttpContentType(),
  92.         ]);
  93.         // finalize
  94.         $handler->finalize($view);
  95.         // clear the globals
  96.         $this->global->dismantle();
  97.         return $view->getResponse();
  98.     }
  99.     /**
  100.      * @param ViewEvent $event
  101.      */
  102.     public function onKernelView(ViewEvent $event): void
  103.     {
  104.         // obtain the view
  105.         $view $event->getControllerResult();
  106.         // make sure it is of proper type, return early if not there is nothing to do
  107.         if ( ! $view instanceof AbstractView) {
  108.             return;
  109.         }
  110.         // set the response on the event
  111.         $event->setResponse($this->handle(
  112.             $view,
  113.             $event->getRequest()
  114.         ));
  115.     }
  116. }