TemplateLocator.php 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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. */
  28. public function __construct(FileLocatorInterface $locator)
  29. {
  30. $this->locator = $locator;
  31. $this->cache = array();
  32. }
  33. /**
  34. * Returns a full path for a given file.
  35. *
  36. * @param TemplateReferenceInterface $template A template
  37. * @param string $currentPath Unused
  38. * @param Boolean $first Unused
  39. *
  40. * @return string The full path for the file
  41. *
  42. * @throws \InvalidArgumentException When the template is not an instance of TemplateReferenceInterface
  43. * @throws \InvalidArgumentException When the template file can not be found
  44. */
  45. public function locate($template, $currentPath = null, $first = true)
  46. {
  47. if (!$template instanceof TemplateReferenceInterface) {
  48. throw new \InvalidArgumentException("The template must be an instance of TemplateReferenceInterface.");
  49. }
  50. $key = $template->getSignature();
  51. if (isset($this->cache[$key])) {
  52. return $this->cache[$key];
  53. }
  54. try {
  55. return $this->cache[$key] = $this->locator->locate($template->getPath(), $currentPath);
  56. } catch (\InvalidArgumentException $e) {
  57. throw new \InvalidArgumentException(sprintf('Unable to find template "%s" in "%s".', $template, $this->path), 0, $e);
  58. }
  59. }
  60. }