Array.php 2.2KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. <?php
  2. /*
  3. * This file is part of Twig.
  4. *
  5. * (c) 2009 Fabien Potencier
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. /**
  11. * Loads a template from an array.
  12. *
  13. * When using this loader with a cache mechanism, you should know that a new cache
  14. * key is generated each time a template content "changes" (the cache key being the
  15. * source code of the template). If you don't want to see your cache grows out of
  16. * control, you need to take care of clearing the old cache file by yourself.
  17. *
  18. * @package twig
  19. * @author Fabien Potencier <fabien@symfony.com>
  20. */
  21. class Twig_Loader_Array implements Twig_LoaderInterface
  22. {
  23. protected $templates;
  24. /**
  25. * Constructor.
  26. *
  27. * @param array $templates An array of templates (keys are the names, and values are the source code)
  28. *
  29. * @see Twig_Loader
  30. */
  31. public function __construct(array $templates)
  32. {
  33. $this->templates = array();
  34. foreach ($templates as $name => $template) {
  35. $this->templates[$name] = $template;
  36. }
  37. }
  38. /**
  39. * Gets the source code of a template, given its name.
  40. *
  41. * @param string $name The name of the template to load
  42. *
  43. * @return string The template source code
  44. */
  45. public function getSource($name)
  46. {
  47. if (!isset($this->templates[$name])) {
  48. throw new Twig_Error_Loader(sprintf('Template "%s" is not defined.', $name));
  49. }
  50. return $this->templates[$name];
  51. }
  52. /**
  53. * Gets the cache key to use for the cache for a given template name.
  54. *
  55. * @param string $name The name of the template to load
  56. *
  57. * @return string The cache key
  58. */
  59. public function getCacheKey($name)
  60. {
  61. if (!isset($this->templates[$name])) {
  62. throw new Twig_Error_Loader(sprintf('Template "%s" is not defined.', $name));
  63. }
  64. return $this->templates[$name];
  65. }
  66. /**
  67. * Returns true if the template is still fresh.
  68. *
  69. * @param string $name The template name
  70. * @param timestamp $time The last modification time of the cached template
  71. */
  72. public function isFresh($name, $time)
  73. {
  74. return true;
  75. }
  76. }