PageRenderTime 37ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/Nette/Templates/Template.php

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