vendor/sylius/theme-bundle/src/Twig/Locator/LegacyNamespacedTemplateLocator.php line 33

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of the Sylius package.
  4.  *
  5.  * (c) Paweł Jędrzejewski
  6.  *
  7.  * For the full copyright and license information, please view the LICENSE
  8.  * file that was distributed with this source code.
  9.  */
  10. declare(strict_types=1);
  11. namespace Sylius\Bundle\ThemeBundle\Twig\Locator;
  12. use Sylius\Bundle\ThemeBundle\Model\ThemeInterface;
  13. use Symfony\Component\Filesystem\Filesystem;
  14. /**
  15.  * Handles templates like "@Acme/template.html.twig".
  16.  *
  17.  * @deprecated Deprecated since Sylius/ThemeBundle 2.0 and will be removed in 3.0.
  18.  */
  19. final class LegacyNamespacedTemplateLocator implements TemplateLocatorInterface
  20. {
  21.     private Filesystem $filesystem;
  22.     public function __construct(Filesystem $filesystem)
  23.     {
  24.         @trigger_error(sprintf(
  25.             '"%s" is deprecated since Sylius/ThemeBundle 2.0 and will be removed in 3.0.',
  26.             self::class,
  27.         ), \E_USER_DEPRECATED);
  28.         $this->filesystem $filesystem;
  29.     }
  30.     public function locate(string $templateThemeInterface $theme): string
  31.     {
  32.         $this->assertResourcePathIsValid($template);
  33.         $twigNamespace substr($template1, (int) strpos($template'/') - 1);
  34.         $resourceName substr($template, (int) strpos($template'/') + 1);
  35.         $path sprintf('%s/%s/views/%s'$theme->getPath(), $this->getBundleOrPluginName($twigNamespace), $resourceName);
  36.         if ($this->filesystem->exists($path)) {
  37.             return $path;
  38.         }
  39.         throw new TemplateNotFoundException($template, [$theme]);
  40.     }
  41.     public function supports(string $template): bool
  42.     {
  43.         return strpos($template'@') === && strpos($template'Resources/views/') === false;
  44.     }
  45.     private function assertResourcePathIsValid(string $template): void
  46.     {
  47.         if (strpos($template'..') !== false) {
  48.             throw new \InvalidArgumentException(sprintf('File name "%s" contains invalid characters (..).'$template));
  49.         }
  50.     }
  51.     private function getBundleOrPluginName(string $twigNamespace): string
  52.     {
  53.         if (substr($twigNamespace, -6) === 'Plugin') {
  54.             return $twigNamespace;
  55.         }
  56.         return $twigNamespace 'Bundle';
  57.     }
  58. }