vendor/symfony/ux-twig-component/src/ComponentTemplateFinder.php line 34

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of the Symfony package.
  4.  *
  5.  * (c) Fabien Potencier <fabien@symfony.com>
  6.  *
  7.  * For the full copyright and license information, please view the LICENSE
  8.  * file that was distributed with this source code.
  9.  */
  10. namespace Symfony\UX\TwigComponent;
  11. use Twig\Environment;
  12. use Twig\Loader\LoaderInterface;
  13. /**
  14.  * @author Matheo Daninos <matheo.daninos@gmail.com>
  15.  */
  16. final class ComponentTemplateFinder implements ComponentTemplateFinderInterface
  17. {
  18.     private readonly LoaderInterface $loader;
  19.     public function __construct(
  20.         Environment|LoaderInterface $loader,
  21.         private readonly ?string $directory null,
  22.     ) {
  23.         if ($loader instanceof Environment) {
  24.             trigger_deprecation('symfony/ux-twig-component''2.13''The "%s()" method will require "%s $loader" as first argument in 3.0. Passing an "Environment" instance is deprecated.'__METHOD__LoaderInterface::class);
  25.             $loader $loader->getLoader();
  26.         }
  27.         $this->loader $loader;
  28.         if (null === $this->directory) {
  29.             trigger_deprecation('symfony/ux-twig-component''2.13''The "%s()" method will require "string $directory" argument in 3.0. Not defining it or passing null is deprecated.'__METHOD__);
  30.         }
  31.     }
  32.     public function findAnonymousComponentTemplate(string $name): ?string
  33.     {
  34.         $loader $this->loader;
  35.         $componentPath rtrim(str_replace(':''/'$name));
  36.         // Legacy auto-naming rules < 2.13
  37.         if (null === $this->directory) {
  38.             if ($loader->exists('components/'.$componentPath.'.html.twig')) {
  39.                 return 'components/'.$componentPath.'.html.twig';
  40.             }
  41.             if ($loader->exists($componentPath.'.html.twig')) {
  42.                 return $componentPath.'.html.twig';
  43.             }
  44.             if ($loader->exists('components/'.$componentPath)) {
  45.                 return 'components/'.$componentPath;
  46.             }
  47.             if ($loader->exists($componentPath)) {
  48.                 return $componentPath;
  49.             }
  50.             return null;
  51.         }
  52.         $template rtrim($this->directory'/').'/'.$componentPath.'.html.twig';
  53.         if ($loader->exists($template)) {
  54.             return $template;
  55.         }
  56.         $parts explode('/'$componentPath2);
  57.         if (\count($parts) < 2) {
  58.             return null;
  59.         }
  60.         $template '@'.$parts[0].'/components/'.$parts[1].'.html.twig';
  61.         if ($loader->exists($template)) {
  62.             return $template;
  63.         }
  64.         return null;
  65.     }
  66. }