Source for file extends.php
Documentation is available at extends.php
* Extends another template, read more about template inheritance at {@link http://wiki.dwoo.org/index.php/TemplateInheritance}
* * file : the template to extend
* This software is provided 'as-is', without any express or implied warranty.
* In no event will the authors be held liable for any damages arising from the use of this software.
* This file is released under the LGPL
* "GNU Lesser General Public License"
* More information can be found here:
* {@link http://www.gnu.org/copyleft/lesser.html}
* @copyright Copyright (c) 2008, Jordi Boggiano
* @license http://www.gnu.org/copyleft/lesser.html GNU Lesser General Public License
protected static $childSource;
protected static $lastReplacement;
public static function compile(Dwoo_Compiler $compiler, $file)
list
($l, $r) =
$compiler->getDelimiters();
self::$l =
preg_quote($l,'/');
self::$r =
preg_quote($r,'/');
if ($compiler->getLooseOpeningHandling()) {
self::$r =
'\s*'.
self::$r;
$inheritanceTree =
array(array('source'=>
$compiler->getTemplateSource()));
if ($file ===
'""' ||
$file ===
"''" ||
(substr($file, 0, 1) !==
'"' &&
substr($file, 0, 1) !==
'\'')) {
if (preg_match('#^["\']([a-z]{2,}):(.*?)["\']$#i', $file, $m)) {
$identifier =
substr($file, 1, -
1);
if ($resource ===
'file' &&
$policy =
$compiler->getSecurityPolicy()) {
if (preg_match('{^([a-z]+?)://}i', $identifier)) {
$dirs =
$policy->getAllowedDirectories();
foreach ($dirs as $dir=>
$dummy) {
if (strpos($identifier, $dir) ===
0) {
$parent =
$compiler->getDwoo()->templateFactory($resource, $identifier);
throw
new Dwoo_Compilation_Exception($compiler, 'Extends : Resource <em>'.
$resource.
'</em> was not added to Dwoo, can not include <em>'.
$identifier.
'</em>');
} elseif ($parent ===
false) {
throw
new Dwoo_Compilation_Exception($compiler, 'Extends : Extending "'.
$resource.
':'.
$identifier.
'" was not allowed for an unknown reason.');
$newParent =
array('source'=>
$parent->getSource(), 'resource'=>
$resource, 'identifier'=>
$identifier, 'uid'=>
$parent->getUid());
if (array_search($newParent, $inheritanceTree, true) !==
false) {
$inheritanceTree[] =
$newParent;
if (preg_match('/^'.
self::$l.
'extends\s+(?:file=)?\s*(\S+?)'.
self::$r.
'/i', $parent->getSource(), $match)) {
$file =
(substr($match[1], 0, 1) !==
'"' &&
substr($match[1], 0, 1) !==
'"') ?
'"'.
str_replace('"', '\\"', $match[1]).
'"' :
$match[1];
$child =
end($inheritanceTree);
self::$childSource =
$child['source'];
self::$lastReplacement =
count($inheritanceTree) ===
1;
if (!isset
($newSource)) {
$newSource =
$parent['source'];
$newSource =
preg_replace_callback('/'.
self::$l.
'block (["\']?)(.+?)\1'.
self::$r.
'(?:\r?\n?)(.*?)(?:\r?\n?)'.
self::$l.
'\/block'.
self::$r.
'/is', array('Dwoo_Plugin_extends', 'replaceBlock'), $newSource);
$newSource =
$l.
'do extendsCheck("'.
$parent['resource'].
':'.
$parent['identifier'].
'" "'.
str_replace('"', '\\"', $parent['uid']).
'")'.
$r.
$newSource;
if (self::$lastReplacement) {
$compiler->setTemplateSource($newSource);
$compiler->setPointer(0);
if (preg_match('/'.
self::$l.
'block (["\']?)'.
preg_quote($matches[2],'/').
'\1'.
self::$r.
'(?:\r?\n?)(.*?)(?:\r?\n?)'.
self::$l.
'\/block'.
self::$r.
'/is', self::$childSource, $override)) {
$l =
stripslashes(self::$l);
$r =
stripslashes(self::$r);
if (self::$lastReplacement) {
return preg_replace('/'.
self::$l.
'\$dwoo\.parent'.
self::$r.
'/is', $matches[3], $override[2]);
return $l.
'block '.
$matches[1].
$matches[2].
$matches[1].
$r.
preg_replace('/'.
self::$l.
'\$dwoo\.parent'.
self::$r.
'/is', $matches[3], $override[2]).
$l.
'/block'.
$r;
if (self::$lastReplacement) {
Documentation generated on Sun, 03 Aug 2008 15:12:34 +0200 by phpDocumentor 1.4.0