From fdbb4dac6df7aedb68512d1a3c8b6cef53fd8a47 Mon Sep 17 00:00:00 2001 From: bzick Date: Mon, 4 Mar 2013 12:40:32 +0400 Subject: [PATCH] Done extends --- src/Aspect.php | 14 ++++ src/Aspect/Compiler.php | 89 ++++++++++++---------- src/Aspect/Template.php | 2 +- tests/cases/Aspect/ExtendsTemplateTest.php | 4 +- 4 files changed, 65 insertions(+), 44 deletions(-) diff --git a/src/Aspect.php b/src/Aspect.php index 3ee3739..1fc1a62 100644 --- a/src/Aspect.php +++ b/src/Aspect.php @@ -320,6 +320,10 @@ class Aspect { return $this; } + public function addCompilerSmart($class) { + return $this; + } + /** * Add block compiler * @@ -339,6 +343,16 @@ class Aspect { return $this; } + /** + * @param string $class + * @param array $tags + * @param array $floats + * @return Aspect + */ + public function addBlockCompilerSmart($class, array $tags, array $floats = array()) { + return $this; + } + /** * @param string $function * @param callable $callback diff --git a/src/Aspect/Compiler.php b/src/Aspect/Compiler.php index e283cb2..fc96cb7 100644 --- a/src/Aspect/Compiler.php +++ b/src/Aspect/Compiler.php @@ -375,7 +375,9 @@ class Compiler { throw new ImproperUseException("Only one {extends} allowed"); } $tpl_name = $tpl->parseFirstArg($tokens, $name); - $tpl->addPostCompile(__CLASS__."::extendBody"); + if(empty($tpl->_extended)) { + $tpl->addPostCompile(__CLASS__."::extendBody"); + } if($name) { // static extends $tpl->_extends = $tpl->getStorage()->getRawTemplate()->load($name, false); $tpl->_compatible = &$tpl->_extends->_compatible; @@ -388,44 +390,32 @@ class Compiler { } /** - * Post compile method for {extends ...} tag + * Post compile action for {extends ...} tag * @param $body * @param Template $tpl */ public static function extendBody(&$body, $tpl) { - if(isset($tpl->_extends)) { // is child - if(is_object($tpl->_extends)) { // static extends - /* @var Template $t */ - $tpl->_extends->_extended = true; - $tpl->_extends->blocks = &$tpl->blocks; - $tpl->_extends->compile(); - if($tpl->_compatible) { - $body .= $tpl->_extends->_body; + $t = $tpl; + while(isset($t->_extends)) { + $t = $t->_extends; + if(is_object($t)) { + $t->_extended = true; + $t->_compatible = &$tpl->_compatible; + $t->blocks = &$tpl->blocks; + $t->compile(); + if(!isset($t->_extends)) { // last item => parent + if(empty($tpl->_compatible)) { + $body = $t->getBody(); + } else { + $body = ''.$body.''.$t->getBody(); + } + return; } else { - $body = $tpl->_extends->_body; + $body .= $t->getBody(); } - /*if(empty($tpl->_dynamic)) { - do { - $t->_blocks = &$tpl->_blocks; - $t->compile(); - $tpl->addDepend($t); - if(!empty($t->_dynamic)) { - $body = ''.$body.''.$t->_body; - return; - } else { - $body .= $t->_body; - } - } while(isset($t->_extends) && $t = $t->_extends); - $body = $t->_body; - } else { - $t->_blocks = &$tpl->_blocks; - $t->_dyn = &$tpl->_dynamic; - $t->compile(); - $tpl->addDepend($t); - $body = ''.$body.''.$t->_body; - }*/ - } else { // dynamic extends + } else { $body = ''.$body.'b = &$tpl->b; $parent->display((array)$tpl); unset($tpl->b, $parent->b); ?>'; + return; } } } @@ -433,11 +423,27 @@ class Compiler { /** * @param Tokenizer $tokens * @param Template $tpl + * @throws ImproperUseException + * @return string */ public static function tagUse(Tokenizer $tokens, Template $tpl) { - $p = $tpl->parseFirstArg($tokens, $scalar); - if(!$scalar) { - $tpl->_static = false; + $tpl->parseFirstArg($tokens, $name); + if($name) { + $donor = $tpl->getStorage()->getRawTemplate()->load($name, false); + $donor->_extended = true; + $tpl->_compatible = &$donor->_compatible; + $donor->compile(); + if(empty($tpl->_compatible)) { + $tpl->blocks += $donor->blocks; + } + return '?>'.$donor->getBody().'getStorage()->getTemplate('.$cname.'); '; + + //$tpl->_compatible = true; + //$tpl->_ = false; } } @@ -470,15 +476,15 @@ class Compiler { 'blocks['.$scope["cname"].'])) { '. '$tpl->b['.$scope["cname"].'] = function($tpl) { ?>'.PHP_EOL. $scope->getContent(). - "};". - "".PHP_EOL + "".PHP_EOL ); } else { $tpl->blocks[ $scope["name"] ] = $scope->getContent(); $scope->replaceContent( 'b['.$scope["cname"].'] = function($tpl) { ?>'.PHP_EOL. $scope->getContent(). - "".PHP_EOL ); } } @@ -488,8 +494,8 @@ class Compiler { 'b['.$scope["cname"].'])) { '. '$tpl->b['.$scope["cname"].'] = function($tpl) { ?>'.PHP_EOL. $scope->getContent(). - "};". - "".PHP_EOL + "".PHP_EOL ); } } else { // is parent @@ -497,9 +503,10 @@ class Compiler { if($tpl->_compatible) { // compatible mode enabled $scope->replaceContent( 'b['.$scope["cname"].'])) { echo $tpl->b['.$scope["cname"].']->__invoke($tpl); } else {?>'.PHP_EOL. - $tpl->blocks[ $scope["body"] ]. + $tpl->blocks[ $scope["name"] ]. ''.PHP_EOL ); + } else { $scope->replaceContent($tpl->blocks[ $scope["name"] ]); } diff --git a/src/Aspect/Template.php b/src/Aspect/Template.php index dac63d1..345cdb9 100644 --- a/src/Aspect/Template.php +++ b/src/Aspect/Template.php @@ -195,7 +195,7 @@ class Template extends Render { call_user_func_array($cb, array(&$this->_body, $this)); } } - $this->_body = str_replace(array('?>'.PHP_EOL.'_body); + /*$this->_body = str_replace(array('?>'.PHP_EOL.'_body);*/ } /** diff --git a/tests/cases/Aspect/ExtendsTemplateTest.php b/tests/cases/Aspect/ExtendsTemplateTest.php index e6b2667..d0d6ceb 100644 --- a/tests/cases/Aspect/ExtendsTemplateTest.php +++ b/tests/cases/Aspect/ExtendsTemplateTest.php @@ -43,10 +43,10 @@ class ExtendsTemplateTest extends TestCase { * @param $vars * @param $result */ - public function _testDynamicExtends($name, $code, $vars, $result) { + public function testDynamicExtends($name, $code, $vars, $result) { static $i = 0; $vars["iteration"] = $i++; - $this->execTpl($name, $code, $vars, $result); + $this->execTpl($name, $code, $vars, $result, 0); } /**