[limb-svn] r6310 - in 3.x/trunk/limb/macro: src src/tags tests/cases tests/cases/tags
svn at limb-project.com
svn at limb-project.com
Tue Sep 18 11:34:45 MSD 2007
Author: pachanga
Date: 2007-09-18 11:34:44 +0400 (Tue, 18 Sep 2007)
New Revision: 6310
URL: http://fisheye.limb-project.com/changelog/limb/?cs=6310
Added:
3.x/trunk/limb/macro/src/lmbMacroPreprocessor.class.php
Modified:
3.x/trunk/limb/macro/src/lmbMacroParser.class.php
3.x/trunk/limb/macro/src/lmbMacroTemplate.class.php
3.x/trunk/limb/macro/src/lmbMacroTemplateExecutor.class.php
3.x/trunk/limb/macro/src/tags/slot.tag.php
3.x/trunk/limb/macro/src/tags/wrap.tag.php
3.x/trunk/limb/macro/tests/cases/lmbMacroTemplateTest.class.php
3.x/trunk/limb/macro/tests/cases/tags/lmbMacroWrapTagTest.class.php
Log:
-- wrapped children with <%wrap%> tag have local vars fully isolated from parent vars
-- dynamically wrapped children have access to global variables set in parent(a bit hackish, but ok for now)
-- added preprocessing stage where short PHP tags become full tags(<?= becomes <?php echo), variables like $#foo become $this->foo
Modified: 3.x/trunk/limb/macro/src/lmbMacroParser.class.php
===================================================================
--- 3.x/trunk/limb/macro/src/lmbMacroParser.class.php 2007-09-18 06:12:13 UTC (rev 6309)
+++ 3.x/trunk/limb/macro/src/lmbMacroParser.class.php 2007-09-18 07:34:44 UTC (rev 6310)
@@ -8,6 +8,7 @@
*/
lmb_require('limb/macro/src/lmbMacroTokenizer.class.php');
+lmb_require('limb/macro/src/lmbMacroPreprocessor.class.php');
lmb_require('limb/macro/src/lmbMacroTokenizerListener.interface.php');
lmb_require('limb/macro/src/lmbMacroLiteralParsingState.class.php');
lmb_require('limb/macro/src/lmbMacroTagParsingState.class.php');
@@ -39,6 +40,7 @@
function __construct($tree_builder, $template_locator, $tag_dictionary)
{
$this->tokenizer = new lmbMacroTokenizer($this);
+ $this->preprocessor = new lmbMacroPreprocessor();
$this->tree_builder = $tree_builder;
$this->template_locator = $template_locator;
@@ -86,6 +88,8 @@
$content = $this->template_locator->readTemplateFile($source_file_path);
+ $this->preprocessor->process($content);
+
$this->tokenizer->parse($content, $source_file_path);
if($tags_before_parse != $this->tree_builder->getExpectedTagCount())
Added: 3.x/trunk/limb/macro/src/lmbMacroPreprocessor.class.php
===================================================================
--- 3.x/trunk/limb/macro/src/lmbMacroPreprocessor.class.php (rev 0)
+++ 3.x/trunk/limb/macro/src/lmbMacroPreprocessor.class.php 2007-09-18 07:34:44 UTC (rev 6310)
@@ -0,0 +1,25 @@
+<?php
+/*
+ * Limb PHP Framework
+ *
+ * @link http://limb-project.com
+ * @copyright Copyright © 2004-2007 BIT(http://bit-creative.com)
+ * @license LGPL http://www.gnu.org/copyleft/lesser.html
+ */
+
+/**
+ * class lmbMacroPreprocessor
+ *
+ * @package macro
+ * @version $Id$
+ */
+
+class lmbMacroPreprocessor
+{
+ function process(&$contents)
+ {
+ $contents = str_replace('<?=', '<?php echo ', $contents);
+ $contents = preg_replace('~<\?(?!php|=)~', '<?php ', $contents);
+ $contents = str_replace('$#', '$this->', $contents);
+ }
+}
Modified: 3.x/trunk/limb/macro/src/lmbMacroTemplate.class.php
===================================================================
--- 3.x/trunk/limb/macro/src/lmbMacroTemplate.class.php 2007-09-18 06:12:13 UTC (rev 6309)
+++ 3.x/trunk/limb/macro/src/lmbMacroTemplate.class.php 2007-09-18 07:34:44 UTC (rev 6310)
@@ -22,6 +22,7 @@
protected $file;
protected $cache_dir;
protected $vars = array();
+ protected $child_executor;
function __construct($file, $cache_dir = null, $locator = null)
{
@@ -46,6 +47,11 @@
$this->vars[$name] = $value;
}
+ function setChildExecutor($executor)
+ {
+ $this->child_executor = $executor;
+ }
+
function render($vars = array())
{
if(!$source_file = $this->locator->locateSourceTemplate($this->file))
@@ -53,7 +59,7 @@
$compiled_file = $this->locator->locateCompiledTemplate($this->file);
- $class = 'MacroTemplateExecutor' . uniqid();//???
+ $class = 'MacroTemplateExecutor' . uniqid();//think about evaling this instance
$compiler = $this->_createCompiler();
$compiler->compile($source_file, $compiled_file, $class, 'render');
@@ -61,6 +67,10 @@
include($compiled_file);
$executor = new $class($this->vars, $this->cache_dir, $this->locator);
+ //in case of dynamic wrapping we need to ask parent for all unknown variables
+ if($this->child_executor)
+ $this->child_executor->setContext($executor);
+
ob_start();
$executor->render($vars);
$out = ob_get_contents();
Modified: 3.x/trunk/limb/macro/src/lmbMacroTemplateExecutor.class.php
===================================================================
--- 3.x/trunk/limb/macro/src/lmbMacroTemplateExecutor.class.php 2007-09-18 06:12:13 UTC (rev 6309)
+++ 3.x/trunk/limb/macro/src/lmbMacroTemplateExecutor.class.php 2007-09-18 07:34:44 UTC (rev 6310)
@@ -17,6 +17,7 @@
{
protected $cache_dir;
protected $locator;
+ protected $context;
function __construct($vars = array(), $cache_dir = null, $locator = null)
{
@@ -32,8 +33,18 @@
$this->$name = $value;
}
+ function setContext(lmbMacroTemplateExecutor $context)
+ {
+ $this->context = $context;
+ }
+
function __get($name)
{
+ //we can have parent variable context which should be consulted for all missing variables
+ //actually, it's quite a dirty hack for a deeper problem which should be addressed later
+ if($this->context)
+ return $this->context->$name;
+
//we definitely want to supress warnings, make it some sort of a NullObject?
return '';
}
@@ -56,6 +67,7 @@
$template->setVars(get_object_vars($this));//global template vars
foreach($slots_handlers as $name => $handler)
$template->set('__slot_handler_' . $name, $handler);
+ $template->setChildExecutor($this);//from now we consider the wrapper to be a master variable context
echo $template->render();
}
}
Modified: 3.x/trunk/limb/macro/src/tags/slot.tag.php
===================================================================
--- 3.x/trunk/limb/macro/src/tags/slot.tag.php 2007-09-18 06:12:13 UTC (rev 6309)
+++ 3.x/trunk/limb/macro/src/tags/slot.tag.php 2007-09-18 07:34:44 UTC (rev 6310)
@@ -30,7 +30,15 @@
$code->writePHP('call_user_func_array($this->__slot_handler_' . $slot . ', array());');
$code->writePHP('}');
- parent :: generateContents($code);
+ //we need to isolate statically wrapped template variables via method call
+ //in case of dynamic call we don't have children, hence the check
+ if($this->children)
+ {
+ $method = $code->beginMethod('__slotHandler' . uniqid());
+ parent :: generateContents($code);
+ $code->endMethod();
+ $code->writePHP('$this->' . $method . '()');
+ }
}
}
Modified: 3.x/trunk/limb/macro/src/tags/wrap.tag.php
===================================================================
--- 3.x/trunk/limb/macro/src/tags/wrap.tag.php 2007-09-18 06:12:13 UTC (rev 6309)
+++ 3.x/trunk/limb/macro/src/tags/wrap.tag.php 2007-09-18 07:34:44 UTC (rev 6310)
@@ -95,14 +95,14 @@
{
foreach($intos as $into)
{
- $methods[$into->get('slot')] = $code->beginMethod('__slotHandler'. mt_rand());
+ $methods[$into->get('slot')] = $code->beginMethod('__slotHandler'. uniqid());
$into->generateContents($code);
$code->endMethod();
}
}
else
{
- $methods[$this->get('into')] = $code->beginMethod('__slotHandler'. mt_rand());
+ $methods[$this->get('into')] = $code->beginMethod('__slotHandler'. uniqid());
parent :: generateContents($code);
$code->endMethod();
}
Modified: 3.x/trunk/limb/macro/tests/cases/lmbMacroTemplateTest.class.php
===================================================================
--- 3.x/trunk/limb/macro/tests/cases/lmbMacroTemplateTest.class.php 2007-09-18 06:12:13 UTC (rev 6309)
+++ 3.x/trunk/limb/macro/tests/cases/lmbMacroTemplateTest.class.php 2007-09-18 07:34:44 UTC (rev 6310)
@@ -26,6 +26,23 @@
$this->assertEqual($view->render(), 'Hello, Bob');
}
+ function testShortTagsPreprocessor()
+ {
+ if(ini_get('short_open_tag') == 1)
+ echo __METHOD__ . " does not check anything, since short tags are On anyway\n";
+
+ $view = $this->_createView('Hello, <?=$this->name?>');
+ $view->set('name', 'Bob');
+ $this->assertEqual($view->render(), 'Hello, Bob');
+ }
+
+ function testGlobalVarsPreprocessor()
+ {
+ $view = $this->_createView('Hello, <?=$#name?>');
+ $view->set('name', 'Bob');
+ $this->assertEqual($view->render(), 'Hello, Bob');
+ }
+
function _createView($tpl)
{
$file = $this->_createTemplate($tpl);
Modified: 3.x/trunk/limb/macro/tests/cases/tags/lmbMacroWrapTagTest.class.php
===================================================================
--- 3.x/trunk/limb/macro/tests/cases/tags/lmbMacroWrapTagTest.class.php 2007-09-18 06:12:13 UTC (rev 6309)
+++ 3.x/trunk/limb/macro/tests/cases/tags/lmbMacroWrapTagTest.class.php 2007-09-18 07:34:44 UTC (rev 6310)
@@ -162,7 +162,7 @@
$this->assertEqual($out, '<p>Hello, Bob</p>');
}
- function TODO_testDynamicallyWrappedChildAccessesParentData()
+ function testDynamicallyWrappedChildAccessesParentData()
{
$bar = '<%wrap with="$this->layout" into="slot1"%><?php echo $this->bob?><%/wrap%>';
$foo = '<?php $this->bob = "Bob";?><p>Hello, <%slot id="slot1"/%></p>';
@@ -177,6 +177,35 @@
$this->assertEqual($out, '<p>Hello, Bob</p>');
}
+ function testStaticallyWrappedChildLocalVarsAreIsolated()
+ {
+ $bar = '<%wrap with="foo.html" into="slot1"%><?php $foo = "Todd";?><%/wrap%>';
+ $foo = '<?php $foo = "Bob";?><%slot id="slot1"/%><?php echo $foo;?>';
+
+ $bar_tpl = $this->_createTemplate($bar, 'bar.html');
+ $foo_tpl = $this->_createTemplate($foo, 'foo.html');
+
+ $macro = $this->_createMacro($bar_tpl);
+
+ $out = $macro->render();
+ $this->assertEqual($out, 'Bob');
+ }
+
+ function testDynamicallyWrappedChildLocalVarsAreIsolated()
+ {
+ $bar = '<%wrap with="$this->layout" into="slot1"%><?php $foo = "Todd";?><%/wrap%>';
+ $foo = '<?php $foo = "Bob";?><%slot id="slot1"/%><?php echo $foo;?>';
+
+ $bar_tpl = $this->_createTemplate($bar, 'bar.html');
+ $foo_tpl = $this->_createTemplate($foo, 'foo.html');
+
+ $macro = $this->_createMacro($bar_tpl);
+ $macro->set('layout', 'foo.html');
+
+ $out = $macro->render();
+ $this->assertEqual($out, 'Bob');
+ }
+
protected function _createMacro($file)
{
$base_dir = LIMB_VAR_DIR . '/tpl';
More information about the limb-svn
mailing list