vendor/pimcore/pimcore/bundles/CoreBundle/Templating/LegacyTemplateGuesser.php line 81

Open in your IDE?
  1. <?php
  2. /**
  3.  * Pimcore
  4.  *
  5.  * This source file is available under two different licenses:
  6.  * - GNU General Public License version 3 (GPLv3)
  7.  * - Pimcore Commercial License (PCL)
  8.  * Full copyright and license information is available in
  9.  * LICENSE.md which is distributed with this source code.
  10.  *
  11.  *  @copyright  Copyright (c) Pimcore GmbH (http://www.pimcore.org)
  12.  *  @license    http://www.pimcore.org/license     GPLv3 and PCL
  13.  */
  14. namespace Pimcore\Bundle\CoreBundle\Templating;
  15. use Doctrine\Persistence\Proxy;
  16. use Pimcore\Controller\Configuration\TemplatePhp;
  17. use Sensio\Bundle\FrameworkExtraBundle\Templating\TemplateGuesser as BaseTemplateGuesser;
  18. use Symfony\Bundle\FrameworkBundle\Templating\DelegatingEngine;
  19. use Symfony\Bundle\FrameworkBundle\Templating\TemplateReference;
  20. use Symfony\Component\HttpFoundation\Request;
  21. use Symfony\Component\HttpKernel\KernelInterface;
  22. /**
  23.  * @deprecated
  24.  * Provides backward compatibility for camelCase template names and PHP engine support
  25.  */
  26. class LegacyTemplateGuesser extends BaseTemplateGuesser
  27. {
  28.     /**
  29.      * @var KernelInterface
  30.      */
  31.     protected $kernel;
  32.     /**
  33.      * @var DelegatingEngine
  34.      */
  35.     protected $templateEngine;
  36.     /**
  37.      * @var string[]
  38.      */
  39.     private $controllerPatterns;
  40.     public function __construct(KernelInterface $kernelDelegatingEngine $templateEngine$controllerPatterns = [])
  41.     {
  42.         $controllerPatterns[] = '/Controller\\\(.+)Controller$/';
  43.         $this->kernel $kernel;
  44.         $this->controllerPatterns $controllerPatterns;
  45.         $this->templateEngine $templateEngine;
  46.         parent::__construct($kernel$controllerPatterns);
  47.     }
  48.     /**
  49.      * @inheritdoc
  50.      */
  51.     public function guessTemplateName($controllerRequest $request$engine 'twig')
  52.     {
  53.         if ($request->attributes->get('_template') instanceof TemplatePhp) {
  54.             $engine 'php';
  55.         }
  56.         //first lookup for new template name(snake_case)
  57.         //if not found then use legacy guesser template name(camelCase)
  58.         $templateReference parent::guessTemplateName($controller$request);
  59.         // Only AppBundle should use templates inside app folder
  60.         if (=== strpos($templateReference'@') && substr(explode('/'$templateReference)[0], 1) === 'App') {
  61.             $templateReference str_replace('@App/'''$templateReference);
  62.         }
  63.         //update view file format(not supported by Sensio), if engine is php
  64.         if ($engine == 'php') {
  65.             $templateReference str_replace('.twig''.php'$templateReference);
  66.         }
  67.         if ($this->templateEngine->exists($templateReference)) {
  68.             return $templateReference;
  69.         }
  70.         if (is_object($controller) && method_exists($controller'__invoke')) {
  71.             $controller = [$controller'__invoke'];
  72.         } elseif (!is_array($controller)) {
  73.             throw new \InvalidArgumentException(sprintf('First argument of %s must be an array callable or an object defining the magic method __invoke. "%s" given.'__METHOD__gettype($controller)));
  74.         }
  75.         $className $this->getRealClass(\get_class($controller[0]));
  76.         $matchController null;
  77.         foreach ($this->controllerPatterns as $pattern) {
  78.             if (preg_match($pattern$className$tempMatch)) {
  79.                 $matchController $tempMatch;
  80.                 break;
  81.             }
  82.         }
  83.         if (null === $matchController) {
  84.             throw new \InvalidArgumentException(sprintf('The "%s" class does not look like a controller class (its FQN must match one of the following regexps: "%s")', \get_class($controller[0]), implode('", "'$this->controllerPatterns)));
  85.         }
  86.         if ($controller[1] === '__invoke') {
  87.             $matchAction $matchController;
  88.             $matchController null;
  89.         } elseif (!preg_match('/^(.+)Action$/'$controller[1], $matchAction)) {
  90.             $matchAction = [null$controller[1]];
  91.         }
  92.         $bundle $this->getBundleForClass($className);
  93.         if ($bundle) {
  94.             while ($bundleName $bundle->getName()) {
  95.                 if (!method_exists($bundle'getParent') || (null === $parentBundleName $bundle->getParent())) {
  96.                     $bundleName $bundle->getName();
  97.                     break;
  98.                 }
  99.                 $bundle $this->kernel->getBundle($parentBundleName);
  100.             }
  101.         } else {
  102.             $bundleName null;
  103.         }
  104.         $legacyTemplateReference = new TemplateReference($bundleName$matchController[1], $matchAction[1], $request->getRequestFormat(), $engine);
  105.         // Only AppBundle should use templates inside app folder
  106.         if ($legacyTemplateReference->get('bundle') === 'AppBundle') {
  107.             $legacyTemplateReference->set('bundle''');
  108.         }
  109.         if ($this->templateEngine->exists($legacyTemplateReference->getLogicalName())) {
  110.             return $legacyTemplateReference;
  111.         }
  112.         return $templateReference;
  113.     }
  114.     /**
  115.      * @inheritdoc
  116.      */
  117.     protected function getBundleForClass($class)
  118.     {
  119.         $reflectionClass = new \ReflectionClass($class);
  120.         $bundles $this->kernel->getBundles();
  121.         do {
  122.             $namespace $reflectionClass->getNamespaceName();
  123.             foreach ($bundles as $bundle) {
  124.                 if (=== strpos($namespace$bundle->getNamespace())) {
  125.                     return $bundle;
  126.                 }
  127.             }
  128.             $reflectionClass $reflectionClass->getParentClass();
  129.         } while ($reflectionClass);
  130.     }
  131.     private static function getRealClass(string $class): string
  132.     {
  133.         if (false === $pos strrpos($class'\\'.Proxy::MARKER.'\\')) {
  134.             return $class;
  135.         }
  136.         return substr($class$pos Proxy::MARKER_LENGTH 2);
  137.     }
  138. }