Source for file extends.php

Documentation is available at extends.php

  1. <?php
  2.  
  3. /**
  4.  * Extends another template, read more about template inheritance at {@link http://wiki.dwoo.org/index.php/TemplateInheritance}
  5.  * <pre>
  6.  *  * file : the template to extend
  7.  * </pre>
  8.  * This software is provided 'as-is', without any express or implied warranty.
  9.  * In no event will the authors be held liable for any damages arising from the use of this software.
  10.  *
  11.  * This file is released under the LGPL
  12.  * "GNU Lesser General Public License"
  13.  * More information can be found here:
  14.  * {@link http://www.gnu.org/copyleft/lesser.html}
  15.  *
  16.  * @author     Jordi Boggiano <[email protected]>
  17.  * @copyright  Copyright (c) 2008, Jordi Boggiano
  18.  * @license    http://www.gnu.org/copyleft/lesser.html  GNU Lesser General Public License
  19.  * @link       http://dwoo.org/
  20.  * @version    0.9.1
  21.  * @date       2008-05-30
  22.  * @package    Dwoo
  23.  */
  24. class Dwoo_Plugin_extends extends Dwoo_Plugin implements Dwoo_ICompilable
  25. {
  26.     protected static $childSource;
  27.     protected static $l;
  28.     protected static $r;
  29.     protected static $lastReplacement;
  30.  
  31.     public static function compile(Dwoo_Compiler $compiler$file)
  32.     {
  33.         list($l$r$compiler->getDelimiters();
  34.         self::$l preg_quote($l,'/');
  35.         self::$r preg_quote($r,'/');
  36.  
  37.         if ($compiler->getLooseOpeningHandling()) {
  38.             self::$l .= '\s*';
  39.             self::$r '\s*'.self::$r;
  40.         }
  41.         $inheritanceTree array(array('source'=>$compiler->getTemplateSource()));
  42.  
  43.         while (!empty($file)) {
  44.             if ($file === '""' || $file === "''" || (substr($file01!== '"' && substr($file01!== '\'')) {
  45.                 throw new Dwoo_Compilation_Exception($compiler'Extends : The file name must be a non-empty string');
  46.                 return;
  47.             }
  48.  
  49.             if (preg_match('#^["\']([a-z]{2,}):(.*?)["\']$#i'$file$m)) {
  50.                 $resource $m[1];
  51.                 $identifier $m[2];
  52.             else {
  53.                 $resource 'file';
  54.                 $identifier substr($file1-1);
  55.             }
  56.  
  57.             if ($resource === 'file' && $policy $compiler->getSecurityPolicy()) {
  58.                 while (true{
  59.                     if (preg_match('{^([a-z]+?)://}i'$identifier)) {
  60.                         throw new Dwoo_Security_Exception('The security policy prevents you to read files from external sources.');
  61.                     }
  62.  
  63.                     $identifier realpath($identifier);
  64.                     $dirs $policy->getAllowedDirectories();
  65.                     foreach ($dirs as $dir=>$dummy{
  66.                         if (strpos($identifier$dir=== 0{
  67.                             break 2;
  68.                         }
  69.                     }
  70.                     throw new Dwoo_Security_Exception('The security policy prevents you to read <em>'.$identifier.'</em>');
  71.                 }
  72.             }
  73.  
  74.             try {
  75.                 $parent $compiler->getDwoo()->templateFactory($resource$identifier);
  76.             catch (Dwoo_Exception $e{
  77.                 throw new Dwoo_Compilation_Exception($compiler'Extends : Resource <em>'.$resource.'</em> was not added to Dwoo, can not include <em>'.$identifier.'</em>');
  78.             }
  79.  
  80.             if ($parent === null{
  81.                 throw new Dwoo_Compilation_Exception($compiler'Extends : Resource "'.$resource.':'.$identifier.'" was not found.');
  82.             elseif ($parent === false{
  83.                 throw new Dwoo_Compilation_Exception($compiler'Extends : Extending "'.$resource.':'.$identifier.'" was not allowed for an unknown reason.');
  84.             }
  85.  
  86.             $newParent array('source'=>$parent->getSource()'resource'=>$resource'identifier'=>$identifier'uid'=>$parent->getUid());
  87.             if (array_search($newParent$inheritanceTreetrue!== false{
  88.                 throw new Dwoo_Compilation_Exception($compiler'Extends : Recursive template inheritance detected');
  89.             }
  90.  
  91.             $inheritanceTree[$newParent;
  92.  
  93.             if (preg_match('/^'.self::$l.'extends\s+(?:file=)?\s*(\S+?)'.self::$r.'/i'$parent->getSource()$match)) {
  94.                 $file (substr($match[1]01!== '"' && substr($match[1]01!== '"''"'.str_replace('"''\\"'$match[1]).'"' $match[1];
  95.             else {
  96.                 $file false;
  97.             }
  98.         }
  99.  
  100.         while (true{
  101.             $parent array_pop($inheritanceTree);
  102.             $child end($inheritanceTree);
  103.             self::$childSource $child['source'];
  104.             self::$lastReplacement count($inheritanceTree=== 1;
  105.             if (!isset($newSource)) {
  106.                 $newSource $parent['source'];
  107.             }
  108.             $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);
  109.  
  110.             $newSource $l.'do extendsCheck("'.$parent['resource'].':'.$parent['identifier'].'" "'.str_replace('"''\\"'$parent['uid']).'")'.$r.$newSource;
  111.  
  112.             if (self::$lastReplacement{
  113.                 break;
  114.             }
  115.         }
  116.  
  117.         $compiler->setTemplateSource($newSource);
  118.         $compiler->setPointer(0);
  119.     }
  120.  
  121.     protected static function replaceBlock(array $matches)
  122.     {
  123.         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)) {
  124.             $l stripslashes(self::$l);
  125.             $r stripslashes(self::$r);
  126.  
  127.             if (self::$lastReplacement{
  128.                 return preg_replace('/'.self::$l.'\$dwoo\.parent'.self::$r.'/is'$matches[3]$override[2]);
  129.             else {
  130.                 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;
  131.             }
  132.         else {
  133.             if (self::$lastReplacement{
  134.                 return $matches[3];
  135.             else {
  136.                 return $matches[0];
  137.             }
  138.         }
  139.     }
  140. }

Documentation generated on Sun, 03 Aug 2008 15:12:34 +0200 by phpDocumentor 1.4.0