/usr/share/php/Nette/Templating/Template.php is in php-nette 2.1.0-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 | <?php
/**
* This file is part of the Nette Framework (http://nette.org)
* Copyright (c) 2004 David Grudl (http://davidgrudl.com)
*/
namespace Nette\Templating;
use Nette,
Nette\Caching,
Nette\Utils\Callback;
/**
* Template.
*
* @author David Grudl
*/
class Template extends Nette\Object implements ITemplate
{
/** @var array of function(Template $sender); Occurs before a template is compiled - implement to customize the filters */
public $onPrepareFilters = array();
/** @var string */
private $source;
/** @var array */
private $params = array();
/** @var array compile-time filters */
private $filters = array();
/** @var array run-time helpers */
private $helpers = array();
/** @var array */
private $helperLoaders = array();
/** @var Nette\Caching\IStorage */
private $cacheStorage;
/**
* Sets template source code.
* @param string
* @return self
*/
public function setSource($source)
{
$this->source = $source;
return $this;
}
/**
* Returns template source code.
* @return source
*/
public function getSource()
{
return $this->source;
}
/********************* rendering ****************d*g**/
/**
* Renders template to output.
* @return void
*/
public function render()
{
$cache = new Caching\Cache($storage = $this->getCacheStorage(), 'Nette.Template');
$cached = $compiled = $cache->load($this->source);
if ($compiled === NULL) {
$compiled = $this->compile();
$cache->save($this->source, $compiled, array(Caching\Cache::CONSTS => 'Nette\Framework::REVISION'));
$cached = $cache->load($this->source);
}
if ($cached !== NULL && $storage instanceof Caching\Storages\PhpFileStorage) {
Nette\Utils\LimitedScope::load($cached['file'], $this->getParameters());
} else {
Nette\Utils\LimitedScope::evaluate($compiled, $this->getParameters());
}
}
/**
* Renders template to file.
* @param string
* @return void
*/
public function save($file)
{
if (file_put_contents($file, $this->__toString(TRUE)) === FALSE) {
throw new Nette\IOException("Unable to save file '$file'.");
}
}
/**
* Renders template to string.
* @param bool can throw exceptions? (hidden parameter)
* @return string
*/
public function __toString()
{
ob_start();
try {
$this->render();
return ob_get_clean();
} catch (\Exception $e) {
ob_end_clean();
if (func_get_args() && func_get_arg(0)) {
throw $e;
} else {
trigger_error("Exception in " . __METHOD__ . "(): {$e->getMessage()} in {$e->getFile()}:{$e->getLine()}", E_USER_ERROR);
}
}
}
/**
* Applies filters on template content.
* @return string
*/
public function compile()
{
if (!$this->filters) {
$this->onPrepareFilters($this);
}
$code = $this->getSource();
foreach ($this->filters as $filter) {
$code = self::extractPhp($code, $blocks);
$code = call_user_func($filter, $code);
$code = strtr($code, $blocks); // put PHP code back
}
return Helpers::optimizePhp($code);
}
/********************* template filters & helpers ****************d*g**/
/**
* Registers callback as template compile-time filter.
* @param callable
* @return self
*/
public function registerFilter($callback)
{
$this->filters[] = Callback::check($callback);
return $this;
}
/**
* Returns all registered compile-time filters.
* @return array
*/
public function getFilters()
{
return $this->filters;
}
/**
* Registers callback as template run-time helper.
* @param string
* @param callable
* @return self
*/
public function registerHelper($name, $callback)
{
$this->helpers[strtolower($name)] = $callback;
return $this;
}
/**
* Registers callback as template run-time helpers loader.
* @param callable
* @return self
*/
public function registerHelperLoader($callback)
{
array_unshift($this->helperLoaders, $callback);
return $this;
}
/**
* Returns all registered run-time helpers.
* @return array
*/
public function getHelpers()
{
return $this->helpers;
}
/**
* Returns all registered template run-time helper loaders.
* @return array
*/
public function getHelperLoaders()
{
return $this->helperLoaders;
}
/**
* Call a template run-time helper. Do not call directly.
* @param string helper name
* @param array arguments
* @return mixed
*/
public function __call($name, $args)
{
$lname = strtolower($name);
if (!isset($this->helpers[$lname])) {
foreach ($this->helperLoaders as $loader) {
$helper = Callback::invoke($loader, $lname);
if ($helper) {
$this->registerHelper($lname, $helper);
return Callback::invokeArgs($this->helpers[$lname], $args);
}
}
return parent::__call($name, $args);
}
return Callback::invokeArgs($this->helpers[$lname], $args);
}
/**
* Sets translate adapter.
* @return self
*/
public function setTranslator(Nette\Localization\ITranslator $translator = NULL)
{
$this->registerHelper('translate', $translator === NULL ? NULL : array($translator, 'translate'));
return $this;
}
/********************* template parameters ****************d*g**/
/**
* Adds new template parameter.
* @return self
*/
public function add($name, $value)
{
if (array_key_exists($name, $this->params)) {
throw new Nette\InvalidStateException("The variable '$name' already exists.");
}
$this->params[$name] = $value;
return $this;
}
/**
* Sets all parameters.
* @param array
* @return self
*/
public function setParameters(array $params)
{
$this->params = $params + $this->params;
return $this;
}
/**
* Returns array of all parameters.
* @return array
*/
public function getParameters()
{
$this->params['template'] = $this;
return $this->params;
}
/**
* Sets a template parameter. Do not call directly.
* @return void
*/
public function __set($name, $value)
{
$this->params[$name] = $value;
}
/**
* Returns a template parameter. Do not call directly.
* @return mixed value
*/
public function &__get($name)
{
if (!array_key_exists($name, $this->params)) {
trigger_error("The variable '$name' does not exist in template.", E_USER_NOTICE);
}
return $this->params[$name];
}
/**
* Determines whether parameter is defined. Do not call directly.
* @return bool
*/
public function __isset($name)
{
return isset($this->params[$name]);
}
/**
* Removes a template parameter. Do not call directly.
* @param string name
* @return void
*/
public function __unset($name)
{
unset($this->params[$name]);
}
/********************* caching ****************d*g**/
/**
* Set cache storage.
* @return self
*/
public function setCacheStorage(Caching\IStorage $storage)
{
$this->cacheStorage = $storage;
return $this;
}
/**
* @return Nette\Caching\IStorage
*/
public function getCacheStorage()
{
if ($this->cacheStorage === NULL) {
return new Caching\Storages\DevNullStorage;
}
return $this->cacheStorage;
}
/********************* tools ****************d*g**/
/**
* Extracts all blocks of PHP code.
* @param string
* @param array
* @return string
*/
private static function extractPhp($source, & $blocks)
{
$res = '';
$blocks = array();
$tokens = token_get_all($source);
foreach ($tokens as $n => $token) {
if (is_array($token)) {
if ($token[0] === T_INLINE_HTML) {
$res .= $token[1];
continue;
} elseif ($token[0] === T_CLOSE_TAG) {
if ($php !== $res) { // not <?xml
$res .= str_repeat("\n", substr_count($php, "\n"));
}
$res .= $token[1];
continue;
} elseif ($token[0] === T_OPEN_TAG && $token[1] === '<?' && isset($tokens[$n+1][1]) && $tokens[$n+1][1] === 'xml') {
$php = & $res;
$token[1] = '<<?php ?>?';
} elseif ($token[0] === T_OPEN_TAG || $token[0] === T_OPEN_TAG_WITH_ECHO) {
$res .= $id = "<?php \x01@php:p" . count($blocks) . "@\x02";
$php = & $blocks[$id];
}
$php .= $token[1];
} else {
$php .= $token;
}
}
return $res;
}
}
|