FilesystemLoader.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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\TwigBundle\Loader;
  11. use Symfony\Component\Config\FileLocatorInterface;
  12. use Symfony\Component\Templating\TemplateNameParserInterface;
  13. use Symfony\Component\Templating\TemplateReferenceInterface;
  14. /**
  15. * FilesystemLoader extends the default Twig filesystem loader
  16. * to work with the Symfony paths and template references.
  17. *
  18. * @author Fabien Potencier <fabien@symfony.com>
  19. */
  20. class FilesystemLoader extends \Twig_Loader_Filesystem
  21. {
  22. protected $locator;
  23. protected $parser;
  24. /**
  25. * Constructor.
  26. *
  27. * @param FileLocatorInterface $locator A FileLocatorInterface instance
  28. * @param TemplateNameParserInterface $parser A TemplateNameParserInterface instance
  29. * @param string|null $rootPath The root path common to all relative paths (null for getcwd())
  30. */
  31. public function __construct(FileLocatorInterface $locator, TemplateNameParserInterface $parser, $rootPath = null)
  32. {
  33. parent::__construct(array(), $rootPath);
  34. $this->locator = $locator;
  35. $this->parser = $parser;
  36. }
  37. /**
  38. * {@inheritdoc}
  39. *
  40. * The name parameter might also be a TemplateReferenceInterface.
  41. */
  42. public function exists($name)
  43. {
  44. return parent::exists((string) $name);
  45. }
  46. /**
  47. * Returns the path to the template file.
  48. *
  49. * The file locator is used to locate the template when the naming convention
  50. * is the symfony one (i.e. the name can be parsed).
  51. * Otherwise the template is located using the locator from the twig library.
  52. *
  53. * @param string|TemplateReferenceInterface $template The template
  54. * @param bool $throw When true, a \Twig_Error_Loader exception will be thrown if a template could not be found
  55. *
  56. * @return string The path to the template file
  57. *
  58. * @throws \Twig_Error_Loader if the template could not be found
  59. */
  60. protected function findTemplate($template, $throw = true)
  61. {
  62. $logicalName = (string) $template;
  63. if (isset($this->cache[$logicalName])) {
  64. return $this->cache[$logicalName];
  65. }
  66. $file = null;
  67. $previous = null;
  68. try {
  69. $file = parent::findTemplate($logicalName);
  70. } catch (\Twig_Error_Loader $e) {
  71. $twigLoaderException = $e;
  72. // for BC
  73. try {
  74. $template = $this->parser->parse($template);
  75. $file = $this->locator->locate($template);
  76. } catch (\Exception $e) {
  77. }
  78. }
  79. if (false === $file || null === $file) {
  80. if ($throw) {
  81. throw $twigLoaderException;
  82. }
  83. return false;
  84. }
  85. return $this->cache[$logicalName] = $file;
  86. }
  87. }