PageRenderTime 26ms CodeModel.GetById 1ms RepoModel.GetById 0ms app.codeStats 0ms

/www/libs/nette-dev/Templates/Template.php

https://github.com/bazo/Mokuji
PHP | 156 lines | 72 code | 39 blank | 45 comment | 11 complexity | 577221738c6bdd5ee15ef67c1d6a1aa0 MD5 | raw file
Possible License(s): BSD-3-Clause, MIT
  1. <?php
  2. /**
  3. * Nette Framework
  4. *
  5. * @copyright Copyright (c) 2004, 2010 David Grudl
  6. * @license http://nettephp.com/license Nette license
  7. * @link http://nettephp.com
  8. * @category Nette
  9. * @package Nette\Templates
  10. */
  11. /**
  12. * Template stored in file.
  13. *
  14. * @copyright Copyright (c) 2004, 2010 David Grudl
  15. * @package Nette\Templates
  16. */
  17. class Template extends BaseTemplate implements IFileTemplate
  18. {
  19. /** @var int */
  20. public static $cacheExpire = FALSE;
  21. /** @var ICacheStorage */
  22. private static $cacheStorage;
  23. /** @var string */
  24. private $file;
  25. /**
  26. * Constructor.
  27. * @param string template file path
  28. */
  29. public function __construct($file = NULL)
  30. {
  31. if ($file !== NULL) {
  32. $this->setFile($file);
  33. }
  34. }
  35. /**
  36. * Sets the path to the template file.
  37. * @param string template file path
  38. * @return Template provides a fluent interface
  39. */
  40. public function setFile($file)
  41. {
  42. if (!is_file($file)) {
  43. throw new FileNotFoundException("Missing template file '$file'.");
  44. }
  45. $this->file = $file;
  46. return $this;
  47. }
  48. /**
  49. * Returns the path to the template file.
  50. * @return string template file path
  51. */
  52. public function getFile()
  53. {
  54. return $this->file;
  55. }
  56. /********************* rendering ****************d*g**/
  57. /**
  58. * Renders template to output.
  59. * @return void
  60. */
  61. public function render()
  62. {
  63. if ($this->file == NULL) { // intentionally ==
  64. throw new InvalidStateException("Template file name was not specified.");
  65. }
  66. $this->__set('template', $this);
  67. $shortName = str_replace(Environment::getVariable('appDir'), '', $this->file);
  68. $cache = new Cache($this->getCacheStorage(), 'Nette.Template');
  69. $key = trim(strtr($shortName, '\\/@', '.._'), '.') . '-' . md5($this->file);
  70. $cached = $content = $cache[$key];
  71. if ($content === NULL) {
  72. if (!$this->getFilters()) {
  73. $this->onPrepareFilters($this);
  74. }
  75. if (!$this->getFilters()) {
  76. LimitedScope::load($this->file, $this->getParams());
  77. return;
  78. }
  79. $content = $this->compile(file_get_contents($this->file), "file \xE2\x80\xA6$shortName");
  80. $cache->save(
  81. $key,
  82. $content,
  83. array(
  84. Cache::FILES => $this->file,
  85. Cache::EXPIRE => self::$cacheExpire,
  86. )
  87. );
  88. $cached = $cache[$key];
  89. }
  90. if ($cached !== NULL && self::$cacheStorage instanceof TemplateCacheStorage) {
  91. LimitedScope::load($cached['file'], $this->getParams());
  92. fclose($cached['handle']);
  93. } else {
  94. LimitedScope::evaluate($content, $this->getParams());
  95. }
  96. }
  97. /********************* caching ****************d*g**/
  98. /**
  99. * Set cache storage.
  100. * @param Cache
  101. * @return void
  102. */
  103. public static function setCacheStorage(ICacheStorage $storage)
  104. {
  105. self::$cacheStorage = $storage;
  106. }
  107. /**
  108. * @return ICacheStorage
  109. */
  110. public static function getCacheStorage()
  111. {
  112. if (self::$cacheStorage === NULL) {
  113. self::$cacheStorage = new TemplateCacheStorage(Environment::getVariable('tempDir'));
  114. }
  115. return self::$cacheStorage;
  116. }
  117. }