vendor/easycorp/easyadmin-bundle/src/EventListener/CrudResponseListener.php line 26

Open in your IDE?
  1. <?php
  2. namespace EasyCorp\Bundle\EasyAdminBundle\EventListener;
  3. use EasyCorp\Bundle\EasyAdminBundle\Config\KeyValueStore;
  4. use EasyCorp\Bundle\EasyAdminBundle\Provider\AdminContextProvider;
  5. use Symfony\Component\Form\FormInterface;
  6. use Symfony\Component\HttpFoundation\Response;
  7. use Symfony\Component\HttpKernel\Event\ViewEvent;
  8. use Twig\Environment;
  9. /**
  10.  * @author Javier Eguiluz <javier.eguiluz@gmail.com>
  11.  */
  12. final class CrudResponseListener
  13. {
  14.     private $adminContextProvider;
  15.     private $twig;
  16.     public function __construct(AdminContextProvider $adminContextProviderEnvironment $twig)
  17.     {
  18.         $this->adminContextProvider $adminContextProvider;
  19.         $this->twig $twig;
  20.     }
  21.     public function onKernelView(ViewEvent $event)
  22.     {
  23.         $responseParameters $event->getControllerResult();
  24.         if (null === $responseParameters || !$responseParameters instanceof KeyValueStore) {
  25.             return;
  26.         }
  27.         if (!$responseParameters->has('templateName') && !$responseParameters->has('templatePath')) {
  28.             throw new \RuntimeException('The KeyValueStore object returned by CrudController actions must include either a "templateName" or a "templatePath" parameter to define the template used to render the action result.');
  29.         }
  30.         $templateParameters $responseParameters->all();
  31.         $templatePath \array_key_exists('templatePath'$templateParameters)
  32.             ? $templateParameters['templatePath']
  33.             : $this->adminContextProvider->getContext()->getTemplatePath($templateParameters['templateName']);
  34.         // to make parameters easier to modify, we pass around FormInterface objects
  35.         // so we must convert those values to FormView before rendering the template
  36.         foreach ($templateParameters as $paramName => $paramValue) {
  37.             if ($paramValue instanceof FormInterface) {
  38.                 $templateParameters[$paramName] = $paramValue->createView();
  39.             }
  40.         }
  41.         $event->setResponse(new Response($this->twig->render($templatePath$templateParameters)));
  42.     }
  43. }