src/App/Component/ViewLayer/ViewHandlers/HtmlViewHandler.php line 31

Open in your IDE?
  1. <?php
  2. namespace App\Component\ViewLayer\ViewHandlers;
  3. use App\Component\Dom\Nodes\RawNode;
  4. use App\Component\ViewLayer\AbstractView;
  5. use App\Component\ViewLayer\AbstractViewHandler;
  6. use App\Component\ViewLayer\Views\AbstractHtmlView;
  7. use Symfony\Component\Serializer\NameConverter\CamelCaseToSnakeCaseNameConverter;
  8. use Twig\Environment;
  9. /**
  10.  * Class HtmlViewHandler
  11.  * @package App\Component\ViewLayer\ViewHandlers
  12.  */
  13. final class HtmlViewHandler extends AbstractViewHandler
  14. {
  15.     /**
  16.      * @var Environment
  17.      */
  18.     protected Environment $twig;
  19.     /**
  20.      * @param Environment $twig
  21.      */
  22.     public function __construct(
  23.         Environment $twig
  24.     )
  25.     {
  26.         $this->twig $twig;
  27.         $this->snakecaser = new CamelCaseToSnakeCaseNameConverter();
  28.     }
  29.     /**
  30.      * {@inheritDoc}
  31.      */
  32.     public function supports(AbstractView $view): bool
  33.     {
  34.         return ($view instanceof AbstractHtmlView);
  35.     }
  36.     /**
  37.      * {@inheritDoc}
  38.      * @param AbstractHtmlView $view
  39.      */
  40.     public function prepare(AbstractView $view): void
  41.     {
  42.         // see if we don't have a template given; if not generate a default one
  43.         if (empty($view->getTemplate())) {
  44.             // get the routing information
  45.             $route $view->getRequest()->attributes->get('_route');
  46.             // determine the controller
  47.             $controller $view->getRequest()->attributes->get('_controller');
  48.             $controller substr($controller0strpos($controller'::'));
  49.             // determine the alias
  50.             if (str_starts_with($controller'App\\')) {
  51.                 $alias 'App';
  52.             } else {
  53.                 $alias preg_replace('/^([^\\\\]+?)\\\\(.+?)Bundle\\\\.*$/''$1$2'$controller);
  54.             }
  55.             // get the parts that make up the route; currently ignoring the first two
  56.             // TODO: need to make this a bit more flexible as the naming convention might not be consistent?
  57.             $paths array_slice(explode('.'$route), 2);
  58.             // need to rip the last one off as that is the file
  59.             $file array_pop($paths);
  60.             // set the template path
  61.             $view->setTemplate(call_user_func_array(
  62.                 'sprintf',
  63.                 array_merge(
  64.                     [
  65.                         '@%s/%s/%s.html.twig',
  66.                         $alias,
  67.                     ],
  68.                     [implode('/'$paths)],
  69.                     [$file]
  70.                 )
  71.             ));
  72.         }
  73.     }
  74.     /**
  75.      * {@inheritDoc}
  76.      * @param AbstractHtmlView $view
  77.      */
  78.     public function handle(AbstractView $view): string
  79.     {
  80.         // render the main content area and set it into the dom
  81.         $content $this->twig->render(
  82.             $view->getTemplate(),
  83.             array_merge(
  84.                 $view->getParameters(),
  85.                 []
  86.             )
  87.         );
  88.         // generate the final content
  89.         return $this->twig->render(
  90.             sprintf(
  91.                 '@App/view_layer/%s.html.twig',
  92.                 strtolower(preg_replace(
  93.                     '/^.+\\\\(.+?)HtmlView$/',
  94.                     '$1',
  95.                     get_class($view)
  96.                 ))
  97.             ),
  98.             array_merge(
  99.                 $view->getDom()->toArray(),
  100.                 [
  101.                     'content' => (new RawNode())->setContent($content),
  102.                 ]
  103.             )
  104.         );
  105.     }
  106. }