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 <j.boggiano@seld.be>
  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.         $curPath dirname($compiler->getDwoo()->getTemplate()->getResourceIdentifier()) DIRECTORY_SEPARATOR;
  43.         $curTpl $compiler->getDwoo()->getTemplate();
  44.  
  45.         while (!empty($file)) {
  46.             if ($file === '""' || $file === "''" || (substr($file01!== '"' && substr($file01!== '\'')) {
  47.                 throw new Dwoo_Compilation_Exception($compiler'Extends : The file name must be a non-empty string');
  48.                 return;
  49.             }
  50.  
  51.             if (preg_match('#^["\']([a-z]{2,}):(.*?)["\']$#i'$file$m)) {
  52.                 // resource:identifier given, extract them
  53.                 $resource $m[1];
  54.                 $identifier $m[2];
  55.             else {
  56.                 // get the current template's resource
  57.                 $resource $curTpl->getResourceName();
  58.                 $identifier substr($file1-1);
  59.             }
  60.  
  61.             try {
  62.                 $parent $compiler->getDwoo()->templateFactory($resource$identifiernullnullnull$curTpl);
  63.             catch (Dwoo_Security_Exception $e{
  64.                 throw new Dwoo_Compilation_Exception($compiler'Extends : Security restriction : '.$e->getMessage());
  65.             catch (Dwoo_Exception $e{
  66.                 throw new Dwoo_Compilation_Exception($compiler'Extends : '.$e->getMessage());
  67.             }
  68.  
  69.             if ($parent === null{
  70.                 throw new Dwoo_Compilation_Exception($compiler'Extends : Resource "'.$resource.':'.$identifier.'" not found.');
  71.             elseif ($parent === false{
  72.                 throw new Dwoo_Compilation_Exception($compiler'Extends : Resource "'.$resource.'" does not support extends.');
  73.             }
  74.  
  75.             $curTpl $parent;
  76.             $newParent array('source'=>$parent->getSource()'resource'=>$resource'identifier'=>$parent->getResourceIdentifier()'uid'=>$parent->getUid());
  77.             if (array_search($newParent$inheritanceTreetrue!== false{
  78.                 throw new Dwoo_Compilation_Exception($compiler'Extends : Recursive template inheritance detected');
  79.             }
  80.  
  81.             $inheritanceTree[$newParent;
  82.  
  83.             if (preg_match('/^'.self::$l.'extends\s+(?:file=)?\s*(\S+?|(["\']).+?\2)'.self::$r.'/i'$parent->getSource()$match)) {
  84.                 $curPath dirname($identifierDIRECTORY_SEPARATOR;
  85.                 $file (substr($match[1]01!== '"' && substr($match[1]01!== '"''"'.str_replace('"''\\"'$match[1]).'"' $match[1];
  86.             else {
  87.                 $file false;
  88.             }
  89.         }
  90.  
  91.         while (true{
  92.             $parent array_pop($inheritanceTree);
  93.             $child end($inheritanceTree);
  94.             self::$childSource $child['source'];
  95.             self::$lastReplacement count($inheritanceTree=== 1;
  96.             if (!isset($newSource)) {
  97.                 $newSource $parent['source'];
  98.             }
  99.             $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);
  100.  
  101.             $newSource $l.'do extendsCheck("'.$parent['resource'].':'.$parent['identifier'].'" "'.str_replace('"''\\"'$parent['uid']).'")'.$r.$newSource;
  102.  
  103.             if (self::$lastReplacement{
  104.                 break;
  105.             }
  106.         }
  107.  
  108.         $compiler->setTemplateSource($newSource);
  109.         $compiler->setPointer(0);
  110.     }
  111.  
  112.     protected static function replaceBlock(array $matches)
  113.     {
  114.         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)) {
  115.             $l stripslashes(self::$l);
  116.             $r stripslashes(self::$r);
  117.  
  118.             if (self::$lastReplacement{
  119.                 return preg_replace('/'.self::$l.'\$dwoo\.parent'.self::$r.'/is'$matches[3]$override[2]);
  120.             else {
  121.                 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;
  122.             }
  123.         else {
  124.             if (self::$lastReplacement{
  125.                 return $matches[3];
  126.             else {
  127.                 return $matches[0];
  128.             }
  129.         }
  130.     }
  131. }

Documentation generated on Sun, 07 Sep 2008 23:57:46 +0200 by phpDocumentor 1.4.0