src/Hitso/Bundle/CommonBundle/Helper/Templating/TemplatingHelper.php line 39

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace Hitso\Bundle\CommonBundle\Helper\Templating;
  4. use Hitso\Bundle\CommonBundle\Controller\Controller;
  5. use ReflectionClass;
  6. use Symfony\Bundle\FrameworkBundle\Templating\EngineInterface;
  7. use Symfony\Component\HttpFoundation\Response;
  8. use Symfony\Component\HttpKernel\Bundle\BundleInterface;
  9. use Symfony\Component\HttpKernel\KernelInterface;
  10. use Twig\Environment;
  11. class TemplatingHelper implements TemplatingHelperInterface
  12. {
  13.     /**
  14.      * @var EngineInterface
  15.      */
  16.     protected $engine;
  17.     /**
  18.      * @var KernelInterface
  19.      */
  20.     protected $kernel;
  21.     /**
  22.      * @var Environment
  23.      */
  24.     protected $environment;
  25.     public function __construct(EngineInterface $engineEnvironment $environmentKernelInterface $kernel)
  26.     {
  27.         $this->engine      $engine;
  28.         $this->environment $environment;
  29.         $this->kernel      $kernel;
  30.     }
  31.     public function render(string $name, array $parameters = []): string
  32.     {
  33.         return $this->engine->render($name$parameters);
  34.     }
  35.     public function renderFromString(string $content, array $parameters = []): string
  36.     {
  37.         $template $this->environment->createTemplate($content);
  38.         return $template->render($parameters);
  39.     }
  40.     public function renderControllerResponse(Controller $controllerstring $templateName, array $parameters = []): Response
  41.     {
  42.         $template $this->resolveControllerTemplate($controller$templateName);
  43.         return $this->engine->renderResponse($template$parameters);
  44.     }
  45.     public function resolveControllerTemplate(Controller $controllerstring $templateName): string
  46.     {
  47.         $reflectionClass = new ReflectionClass($controller);
  48.         $controllerName  $this->getControllerLogicalName($reflectionClass);
  49.         $bundleName      $this->getBundleName($reflectionClass);
  50.         return sprintf('%s:%s:%s.html.twig'$bundleName$controllerName$templateName);
  51.     }
  52.     protected function getControllerLogicalName(ReflectionClass $reflectionClass): string
  53.     {
  54.         $className $reflectionClass->getName();
  55.         preg_match('/Controller\\\(.+)Controller$/'$className$matchController);
  56.         return $matchController[1];
  57.     }
  58.     protected function getBundleName(ReflectionClass $reflectionClass): string
  59.     {
  60.         $currentBundle $this->getBundleForClass($reflectionClass);
  61.         return $currentBundle->getName();
  62.     }
  63.     protected function getBundleForClass(ReflectionClass $reflectionClass): BundleInterface
  64.     {
  65.         $bundles $this->kernel->getBundles();
  66.         do {
  67.             $namespace $reflectionClass->getNamespaceName();
  68.             foreach ($bundles as $bundle) {
  69.                 if (=== strpos($namespace$bundle->getNamespace())) {
  70.                     return $bundle;
  71.                 }
  72.             }
  73.             $reflectionClass $reflectionClass->getParentClass();
  74.         } while ($reflectionClass);
  75.     }
  76. }