TemplateFilenameParser.php 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  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\Bundle\FrameworkBundle\Templating;
  11. use Symfony\Component\Templating\TemplateNameParserInterface;
  12. use Symfony\Component\Templating\TemplateReferenceInterface;
  13. /**
  14. * TemplateFilenameParser converts template filenames to
  15. * TemplateReferenceInterface instances.
  16. *
  17. * @author Fabien Potencier <fabien@symfony.com>
  18. */
  19. class TemplateFilenameParser implements TemplateNameParserInterface
  20. {
  21. /**
  22. * {@inheritdoc}
  23. */
  24. public function parse($name)
  25. {
  26. if ($name instanceof TemplateReferenceInterface) {
  27. return $name;
  28. }
  29. $parts = explode('/', str_replace('\\', '/', $name));
  30. $elements = explode('.', array_pop($parts));
  31. if (3 > \count($elements)) {
  32. return false;
  33. }
  34. $engine = array_pop($elements);
  35. $format = array_pop($elements);
  36. return new TemplateReference('', implode('/', $parts), implode('.', $elements), $format, $engine);
  37. }
  38. }