TemplateLocator.php 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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\Loader;
  11. use Symfony\Component\Config\FileLocatorInterface;
  12. use Symfony\Component\Templating\TemplateReferenceInterface;
  13. /**
  14. * TemplateLocator locates templates in bundles.
  15. *
  16. * @author Fabien Potencier <fabien@symfony.com>
  17. */
  18. class TemplateLocator implements FileLocatorInterface
  19. {
  20. protected $locator;
  21. protected $path;
  22. protected $cache;
  23. /**
  24. * Constructor.
  25. *
  26. * @param FileLocatorInterface $locator A FileLocatorInterface instance
  27. * @param string $path A global fallback path
  28. */
  29. public function __construct(FileLocatorInterface $locator, $path)
  30. {
  31. $this->locator = $locator;
  32. $this->path = $path;
  33. $this->cache = array();
  34. }
  35. /**
  36. * Returns a full path for a given file.
  37. *
  38. * @param TemplateReferenceInterface $template A template
  39. * @param string $currentPath Unused
  40. * @param Boolean $first Unused
  41. *
  42. * @return string The full path for the file
  43. *
  44. * @throws \InvalidArgumentException When file is not found
  45. */
  46. public function locate($template, $currentPath = null, $first = true)
  47. {
  48. $key = $template->getSignature();
  49. if (isset($this->cache[$key])) {
  50. return $this->cache[$key];
  51. }
  52. try {
  53. return $this->cache[$key] = $this->locator->locate($template->getPath(), $this->path);
  54. } catch (\InvalidArgumentException $e) {
  55. throw new \InvalidArgumentException(sprintf('Unable to find template "%s" in "%s".', $template, $this->path), 0, $e);
  56. }
  57. }
  58. }