Array.php 2.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. * Adds or overrides a template.
  40. *
  41. * @param string $name The template name
  42. * @param string $template The template source
  43. */
  44. public function setTemplate($name, $template)
  45. {
  46. $this->templates[(string) $name] = $template;
  47. }
  48. /**
  49. * Gets the source code of a template, given its name.
  50. *
  51. * @param string $name The name of the template to load
  52. *
  53. * @return string The template source code
  54. */
  55. public function getSource($name)
  56. {
  57. $name = (string) $name;
  58. if (!isset($this->templates[$name])) {
  59. throw new Twig_Error_Loader(sprintf('Template "%s" is not defined.', $name));
  60. }
  61. return $this->templates[$name];
  62. }
  63. /**
  64. * Gets the cache key to use for the cache for a given template name.
  65. *
  66. * @param string $name The name of the template to load
  67. *
  68. * @return string The cache key
  69. */
  70. public function getCacheKey($name)
  71. {
  72. $name = (string) $name;
  73. if (!isset($this->templates[$name])) {
  74. throw new Twig_Error_Loader(sprintf('Template "%s" is not defined.', $name));
  75. }
  76. return $this->templates[$name];
  77. }
  78. /**
  79. * Returns true if the template is still fresh.
  80. *
  81. * @param string $name The template name
  82. * @param timestamp $time The last modification time of the cached template
  83. */
  84. public function isFresh($name, $time)
  85. {
  86. $name = (string) $name;
  87. if (!isset($this->templates[$name])) {
  88. throw new Twig_Error_Loader(sprintf('Template "%s" is not defined.', $name));
  89. }
  90. return true;
  91. }
  92. }