vendor/pimcore/pimcore/lib/Controller/Config/ConfigNormalizer.php line 168

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4.  * Pimcore
  5.  *
  6.  * This source file is available under two different licenses:
  7.  * - GNU General Public License version 3 (GPLv3)
  8.  * - Pimcore Enterprise License (PEL)
  9.  * Full copyright and license information is available in
  10.  * LICENSE.md which is distributed with this source code.
  11.  *
  12.  * @copyright  Copyright (c) Pimcore GmbH (http://www.pimcore.org)
  13.  * @license    http://www.pimcore.org/license     GPLv3 and PEL
  14.  */
  15. namespace Pimcore\Controller\Config;
  16. use Doctrine\Common\Inflector\Inflector;
  17. use Symfony\Component\HttpKernel\KernelInterface;
  18. /**
  19.  * This service exists as integration point between legacy module/controller/action <-> new bundle/controller/action and
  20.  * to handle default bundle/controller/action in case it is not configured.
  21.  *
  22.  * Most of the normalizations here could be removed when we do not need to support legacy ZF1 notations (e.g. DB was
  23.  * fully migrated).
  24.  *
  25.  * TODO use a config switch to enable/disable ZF1 compatibility
  26.  */
  27. class ConfigNormalizer
  28. {
  29.     /**
  30.      * @var KernelInterface
  31.      */
  32.     private $kernel;
  33.     /**
  34.      * @var array
  35.      */
  36.     private $routingDefaults = [];
  37.     /**
  38.      * @var array
  39.      */
  40.     private $bundleCache = [];
  41.     /**
  42.      * @param KernelInterface $kernel
  43.      */
  44.     public function __construct(KernelInterface $kernel)
  45.     {
  46.         $this->kernel $kernel;
  47.     }
  48.     public function setRoutingDefaults(array $routingDefaults)
  49.     {
  50.         $this->routingDefaults $routingDefaults;
  51.     }
  52.     /**
  53.      * Transform parent/controller/action into a controller reference string
  54.      *
  55.      * @param string|null $bundle Bundle or (legacy) module
  56.      * @param string|null $controller
  57.      * @param string|null $action
  58.      *
  59.      * @return string
  60.      */
  61.     public function formatControllerReference(string $bundle nullstring $controller nullstring $action null): string
  62.     {
  63.         $action $this->normalizeActionName($action);
  64.         // check if controller is a service (prefixed with @)
  65.         if (null !== $controller && === strpos($controller'@')) {
  66.             return sprintf(
  67.                 '%s:%sAction',
  68.                 substr($controller1),
  69.                 $action
  70.             );
  71.         } else {
  72.             $bundle $this->normalizeBundleName($bundle);
  73.             $controller $this->normalizeControllerName($controller);
  74.             return sprintf(
  75.                 '%s:%s:%s',
  76.                 $bundle,
  77.                 $controller,
  78.                 $action
  79.             );
  80.         }
  81.     }
  82.     /**
  83.      * Normalize module/bundle name (App -> AppBundle, module -> ModuleBundle)
  84.      *
  85.      * @param string|null $bundle
  86.      *
  87.      * @return string
  88.      */
  89.     public function normalizeBundleName(string $bundle null): string
  90.     {
  91.         if (empty($bundle)) {
  92.             return $this->routingDefaults['bundle'];
  93.         }
  94.         $originalBundle $bundle;
  95.         // bundle name contains Bundle - we assume it's properly formatted
  96.         if (false !== strpos($bundle'Bundle')) {
  97.             return $bundle;
  98.         }
  99.         // App -> AppBundle
  100.         $bundle strtolower($bundle);
  101.         if (isset($this->bundleCache[$bundle])) {
  102.             return $this->bundleCache[$bundle];
  103.         }
  104.         foreach ($this->kernel->getBundles() as $bundleInstance) {
  105.             if ($bundle 'bundle' === strtolower($bundleInstance->getName())) {
  106.                 $this->bundleCache[$bundle] = $bundleInstance->getName();
  107.                 return $this->bundleCache[$bundle];
  108.             }
  109.         }
  110.         throw new \RuntimeException(sprintf('Unable to normalize string "%s" into a valid bundle name'$originalBundle));
  111.     }
  112.     /**
  113.      * Normalize controller name from category_controller to Category/Controller
  114.      *
  115.      * @param string|null $controller
  116.      *
  117.      * @return string
  118.      */
  119.     public function normalizeControllerName(string $controller null): string
  120.     {
  121.         if (empty($controller)) {
  122.             return $this->routingDefaults['controller'];
  123.         }
  124.         // split submodules with _ and uppercase first character
  125.         $controllerParts array_map(function ($part) {
  126.             return ucfirst($part);
  127.         }, explode('_'$controller));
  128.         $controller implode('/'$controllerParts);
  129.         return $controller;
  130.     }
  131.     /**
  132.      * Normalize action name form action-name to actionName
  133.      *
  134.      * @param string|null $action
  135.      *
  136.      * @return string
  137.      */
  138.     public function normalizeActionName(string $action null): string
  139.     {
  140.         if (empty($action)) {
  141.             return $this->routingDefaults['action'];
  142.         }
  143.         return Inflector::camelize($action);
  144.     }
  145.     /**
  146.      * Normalize template from .php to .html.php and remove leading slash
  147.      *
  148.      * @param string|null $template
  149.      *
  150.      * @return string|null
  151.      */
  152.     public function normalizeTemplateName(string $template null)
  153.     {
  154.         // we just return the original value as template could be null
  155.         // do NOT use a string return type for this method!
  156.         if (empty($template)) {
  157.             return $template;
  158.         }
  159.         // if we find colons in the template name we assume it's properly formatted for Symfony
  160.         if (false !== strpos($template':')) {
  161.             return $template;
  162.         }
  163.         // if we find Bundle in the template name we assume it's properly formatted for Symfony
  164.         if (false !== strpos($template'Bundle')) {
  165.             return $template;
  166.         }
  167.         // replace .php with .html.php
  168.         $suffixPattern '/(?<!\.html)\.php$/i';
  169.         if (preg_match($suffixPattern$template)) {
  170.             $template preg_replace($suffixPattern'.html.php'$template);
  171.         }
  172.         // split template into path and filename
  173.         if (substr($template01) === '/') {
  174.             $template substr($template1);
  175.         }
  176.         $path '';
  177.         if (false !== strpos($template'/')) {
  178.             $parts explode('/'$template);
  179.             $template array_pop($parts);
  180.             $path implode('/'$parts);
  181.         }
  182.         return sprintf('%s/%s'$path$template);
  183.     }
  184. }