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.  * @author     Jordi Boggiano <j.boggiano@seld.be>
  12.  * @copyright  Copyright (c) 2008, Jordi Boggiano
  13.  * @license    http://dwoo.org/LICENSE   Modified BSD License
  14.  * @link       http://dwoo.org/
  15.  * @version    1.1.0
  16.  * @date       2009-07-18
  17.  * @package    Dwoo
  18.  */
  19. class Dwoo_Plugin_extends extends Dwoo_Plugin implements Dwoo_ICompilable
  20. {
  21.     protected static $childSource;
  22.     protected static $l;
  23.     protected static $r;
  24.     protected static $lastReplacement;
  25.  
  26.     public static function compile(Dwoo_Compiler $compiler, $file)
  27.     {
  28.         list($l, $r) = $compiler->getDelimiters();
  29.         self::$l = preg_quote($l,'/');
  30.         self::$r = preg_quote($r,'/');
  31.  
  32.         if ($compiler->getLooseOpeningHandling()) {
  33.             self::$l .= '\s*';
  34.             self::$r = '\s*'.self::$r;
  35.         }
  36.         $inheritanceTree = array(array('source'=>$compiler->getTemplateSource()));
  37.         $curPath = dirname($compiler->getDwoo()->getTemplate()->getResourceIdentifier()) . DIRECTORY_SEPARATOR;
  38.         $curTpl = $compiler->getDwoo()->getTemplate();
  39.  
  40.         while (!empty($file)) {
  41.             if ($file === '""' || $file === "''" || (substr($file, 0, 1) !== '"' && substr($file, 0, 1) !== '\'')) {
  42.                 throw new Dwoo_Compilation_Exception($compiler, 'Extends : The file name must be a non-empty string');
  43.                 return;
  44.             }
  45.  
  46.             if (preg_match('#^["\']([a-z]{2,}):(.*?)["\']$#i', $file, $m)) {
  47.                 // resource:identifier given, extract them
  48.                 $resource = $m[1];
  49.                 $identifier = $m[2];
  50.             } else {
  51.                 // get the current template's resource
  52.                 $resource = $curTpl->getResourceName();
  53.                 $identifier = substr($file, 1, -1);
  54.             }
  55.  
  56.             try {
  57.                 $parent = $compiler->getDwoo()->templateFactory($resource, $identifier, null, null, null, $curTpl);
  58.             } catch (Dwoo_Security_Exception $e) {
  59.                 throw new Dwoo_Compilation_Exception($compiler, 'Extends : Security restriction : '.$e->getMessage());
  60.             } catch (Dwoo_Exception $e) {
  61.                 throw new Dwoo_Compilation_Exception($compiler, 'Extends : '.$e->getMessage());
  62.             }
  63.  
  64.             if ($parent === null) {
  65.                 throw new Dwoo_Compilation_Exception($compiler, 'Extends : Resource "'.$resource.':'.$identifier.'" not found.');
  66.             } elseif ($parent === false) {
  67.                 throw new Dwoo_Compilation_Exception($compiler, 'Extends : Resource "'.$resource.'" does not support extends.');
  68.             }
  69.  
  70.             $curTpl = $parent;
  71.             $newParent = array('source'=>$parent->getSource(), 'resource'=>$resource, 'identifier'=>$parent->getResourceIdentifier(), 'uid'=>$parent->getUid());
  72.             if (array_search($newParent, $inheritanceTree, true) !== false) {
  73.                 throw new Dwoo_Compilation_Exception($compiler, 'Extends : Recursive template inheritance detected');
  74.             }
  75.             $inheritanceTree[] = $newParent;
  76.  
  77.             if (preg_match('/^'.self::$l.'extends\s+(?:file=)?\s*(\S+?|(["\']).+?\2)'.self::$r.'/i', $parent->getSource(), $match)) {
  78.                 $curPath = dirname($identifier) . DIRECTORY_SEPARATOR;
  79.                 $file = (substr($match[1], 0, 1) !== '"' && substr($match[1], 0, 1) !== '"') ? '"'.str_replace('"', '\\"', $match[1]).'"' : $match[1];
  80.             } else {
  81.                 $file = false;
  82.             }
  83.         }
  84.  
  85.         while (true) {
  86.             $parent = array_pop($inheritanceTree);
  87.             $child = end($inheritanceTree);
  88.             self::$childSource = $child['source'];
  89.             self::$lastReplacement = count($inheritanceTree) === 1;
  90.             if (!isset($newSource)) {
  91.                 $newSource = $parent['source'];
  92.             }
  93.  
  94.             // TODO parse blocks tree for child source and new source
  95.             // TODO replace blocks that are found in the child and in the parent recursively
  96.             $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);
  97.  
  98.             $newSource = $l.'do extendsCheck("'.$parent['resource'].':'.$parent['identifier'].'")'.$r.$newSource;
  99.  
  100.             if (self::$lastReplacement) {
  101.                 break;
  102.             }
  103.         }
  104.  
  105.         $compiler->setTemplateSource($newSource);
  106.         $compiler->recompile();
  107.     }
  108.  
  109.     protected static function replaceBlock(array $matches)
  110.     {
  111.         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)) {
  112.             $l = stripslashes(self::$l);
  113.             $r = stripslashes(self::$r);
  114.  
  115.             if (self::$lastReplacement) {
  116.                 return preg_replace('/'.self::$l.'\$dwoo\.parent'.self::$r.'/is', $matches[3], $override[2]);
  117.             } else {
  118.                 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;
  119.             }
  120.         } else {
  121.             if (self::$lastReplacement) {
  122.                 return $matches[3];
  123.             } else {
  124.                 return $matches[0];
  125.             }
  126.         }
  127.     }
  128. }

Documentation generated on Sat, 18 Jul 2009 21:05:00 +0200 by phpDocumentor 1.4.0