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

Open in your IDE?
  1. <?php
  2. namespace Pimcore\Bundle\CoreBundle\Templating;
  3. use Doctrine\Common\Persistence\Proxy;
  4. use Pimcore\Controller\Configuration\TemplatePhp;
  5. use Sensio\Bundle\FrameworkExtraBundle\Templating\TemplateGuesser as BaseTemplateGuesser;
  6. use Symfony\Bundle\FrameworkBundle\Templating\DelegatingEngine;
  7. use Symfony\Bundle\FrameworkBundle\Templating\TemplateReference;
  8. use Symfony\Component\HttpFoundation\Request;
  9. use Symfony\Component\HttpKernel\KernelInterface;
  10. /**
  11.  * @deprecated
  12.  * Provides backward compatibility for camelCase template names and PHP engine support
  13.  */
  14. class LegacyTemplateGuesser extends BaseTemplateGuesser
  15. {
  16.     /**
  17.      * @var KernelInterface
  18.      */
  19.     protected $kernel;
  20.     /**
  21.      * @var $templateEngine
  22.      */
  23.     protected $templateEngine;
  24.     /**
  25.      * @var string[]
  26.      */
  27.     private $controllerPatterns;
  28.     public function __construct(KernelInterface $kernelDelegatingEngine $templateEngine$controllerPatterns = [])
  29.     {
  30.         $controllerPatterns[] = '/Controller\\\(.+)Controller$/';
  31.         $this->kernel $kernel;
  32.         $this->controllerPatterns $controllerPatterns;
  33.         $this->templateEngine $templateEngine;
  34.         parent::__construct($kernel$controllerPatterns);
  35.     }
  36.     /**
  37.      * @inheritdoc
  38.      */
  39.     public function guessTemplateName($controllerRequest $request$engine 'twig')
  40.     {
  41.         if ($request->attributes->get('_template') instanceof TemplatePhp) {
  42.             $engine 'php';
  43.         }
  44.         //first lookup for new template name(snake_case)
  45.         //if not found then use legacy guesser template name(camelCase)
  46.         $templateReference parent::guessTemplateName($controller$request);
  47.         // Only AppBundle should use templates inside app folder
  48.         if (=== strpos($templateReference'@') && substr(explode('/'$templateReference)[0], 1) === 'App') {
  49.             $templateReference str_replace('@App/'''$templateReference);
  50.         }
  51.         //update view file format(not supported by Sensio), if engine is php
  52.         if ($engine == 'php') {
  53.             $templateReference str_replace('.twig''.php'$templateReference);
  54.         }
  55.         if ($this->templateEngine->exists($templateReference)) {
  56.             return $templateReference;
  57.         }
  58.         if (is_object($controller) && method_exists($controller'__invoke')) {
  59.             $controller = [$controller'__invoke'];
  60.         } elseif (!is_array($controller)) {
  61.             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)));
  62.         }
  63.         $className $this->getRealClass(\get_class($controller[0]));
  64.         $matchController null;
  65.         foreach ($this->controllerPatterns as $pattern) {
  66.             if (preg_match($pattern$className$tempMatch)) {
  67.                 $matchController $tempMatch;
  68.                 break;
  69.             }
  70.         }
  71.         if (null === $matchController) {
  72.             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)));
  73.         }
  74.         if ($controller[1] === '__invoke') {
  75.             $matchAction $matchController;
  76.             $matchController null;
  77.         } elseif (!preg_match('/^(.+)Action$/'$controller[1], $matchAction)) {
  78.             $matchAction = [null$controller[1]];
  79.         }
  80.         $bundle $this->getBundleForClass($className);
  81.         if ($bundle) {
  82.             while ($bundleName $bundle->getName()) {
  83.                 if (!method_exists($bundle'getParent') || (null === $parentBundleName $bundle->getParent())) {
  84.                     $bundleName $bundle->getName();
  85.                     break;
  86.                 }
  87.                 $bundle $this->kernel->getBundle($parentBundleName);
  88.             }
  89.         } else {
  90.             $bundleName null;
  91.         }
  92.         $legacyTemplateReference = new TemplateReference($bundleName$matchController[1], $matchAction[1], $request->getRequestFormat(), $engine);
  93.         // Only AppBundle should use templates inside app folder
  94.         if ($legacyTemplateReference->get('bundle') === 'AppBundle') {
  95.             $legacyTemplateReference->set('bundle''');
  96.         }
  97.         if ($this->templateEngine->exists($legacyTemplateReference->getLogicalName())) {
  98.             return $legacyTemplateReference;
  99.         }
  100.         return $templateReference;
  101.     }
  102.     /**
  103.      * @inheritdoc
  104.      */
  105.     protected function getBundleForClass($class)
  106.     {
  107.         $reflectionClass = new \ReflectionClass($class);
  108.         $bundles $this->kernel->getBundles();
  109.         do {
  110.             $namespace $reflectionClass->getNamespaceName();
  111.             foreach ($bundles as $bundle) {
  112.                 if (=== strpos($namespace$bundle->getNamespace())) {
  113.                     return $bundle;
  114.                 }
  115.             }
  116.             $reflectionClass $reflectionClass->getParentClass();
  117.         } while ($reflectionClass);
  118.     }
  119.     private static function getRealClass(string $class): string
  120.     {
  121.         if (false === $pos strrpos($class'\\'.Proxy::MARKER.'\\')) {
  122.             return $class;
  123.         }
  124.         return substr($class$pos Proxy::MARKER_LENGTH 2);
  125.     }
  126. }