[limb-svn] r6260 - in 3.x/trunk/limb/macro: src tests/cases
svn at limb-project.com
svn at limb-project.com
Wed Sep 5 02:35:49 MSD 2007
Author: pachanga
Date: 2007-09-05 02:35:49 +0400 (Wed, 05 Sep 2007)
New Revision: 6260
URL: http://fisheye.limb-project.com/changelog/limb/?cs=6260
Modified:
3.x/trunk/limb/macro/src/lmbMacroCodeWriter.class.php
3.x/trunk/limb/macro/src/lmbMacroCompiler.class.php
3.x/trunk/limb/macro/src/lmbMacroTemplateExecutor.class.php
3.x/trunk/limb/macro/tests/cases/lmbMacroCodeWriterTest.class.php
Log:
-- refactoring lmbMacroCodeWriter, it now allows to have nested methods declarations
Modified: 3.x/trunk/limb/macro/src/lmbMacroCodeWriter.class.php
===================================================================
--- 3.x/trunk/limb/macro/src/lmbMacroCodeWriter.class.php 2007-09-04 13:46:13 UTC (rev 6259)
+++ 3.x/trunk/limb/macro/src/lmbMacroCodeWriter.class.php 2007-09-04 22:35:49 UTC (rev 6260)
@@ -18,27 +18,49 @@
const MODE_PHP = 1;
const MODE_HTML = 2;
- protected $current_mode = self :: MODE_HTML;
+ protected $class;
+ protected $parent;
+
+ protected $current_mode = self :: MODE_PHP;
+
+ protected $current_method;
+
protected $code = '';
+ protected $methods = array();
+
+ protected $methods_stack = array();
+
protected $include_list = array();
protected $temp_var_name = 1;
- function reset()
+ function __construct($class, $parent = 'lmbMacroTemplateExecutor')
{
- $this->code = '';
- $this->current_mode = self :: MODE_HTML;
- $this->include_list = array();
+ $this->class = $class;
+ $this->parent = $parent;
+
+ $this->registerInclude('limb/macro/src/lmbMacroTemplateExecutor.class.php');
+ $this->beginMethod('render');
}
+ function getClass()
+ {
+ return $this->class;
+ }
+
+ function getRenderMethod()
+ {
+ return 'render';
+ }
+
protected function switchToPHP()
{
if($this->current_mode == self :: MODE_HTML)
{
$this->current_mode = self :: MODE_PHP;
- $this->code .= '<?php ';
+ $this->_append('<?php ');
}
}
@@ -48,16 +70,16 @@
{
$this->current_mode = self :: MODE_HTML;
if($context === "\n")
- $this->code .= " ?>\n";
+ $this->_append(" ?>\n");
else
- $this->code .= ' ?>';
+ $this->_append(' ?>');
}
}
function writePHP($text)
{
$this->switchToPHP();
- $this->code .= $text;
+ $this->_append($text);
}
function writePHPLiteral($text, $escape_text = true)
@@ -65,9 +87,9 @@
$this->switchToPHP();
if($escape_text)
- $this->code .= "'" . $this->escapeLiteral($text) . "'";
+ $this->_append("'" . $this->escapeLiteral($text) . "'");
else
- $this->code .= "'" . $text . "'";
+ $this->_append("'" . $text . "'");
}
function escapeLiteral($text)
@@ -81,21 +103,23 @@
function writeHTML($text)
{
$this->switchToHTML(substr($text,0,1));
- $this->code .= $text;
+ $this->_append($text);
}
function writeRaw($text)
{
- $this->code .= $text;
+ $this->_append($text);
}
function renderCode()
{
- $this->switchToHTML();
+ $this->endMethod();
- $this->_prependIncludeListToCode();
-
- return $this->code;
+ return "<?php\n" .
+ $this->_renderIncludeList() .
+ "class {$this->class} " . ($this->parent ? "extends {$this->parent} " : '') . "{\n" .
+ $this->_renderMethods() .
+ "\n}";
}
function getCode()
@@ -103,21 +127,6 @@
return $this->code;
}
- function setCode($code)
- {
- $this->code = $code;
- }
-
- protected function _prependIncludeListToCode()
- {
- $include_code = '';
- foreach($this->include_list as $include_file)
- $include_code .= "require_once('$include_file');\n";
-
- if(!empty($include_code))
- $this->code = '<?php ' . $include_code . '?>' . $this->code;
- }
-
function getMode()
{
return $this->current_mode;
@@ -136,8 +145,8 @@
function beginFunction($name, $param_list = array())
{
- $this->writePHP('function ' . $name . '(' . implode(',', $param_list) .") {\n");
- return $name;
+ $this->writePHP('function ' . $name . '(' . implode(',', $param_list) .") {\n");
+ return $name;
}
function endFunction()
@@ -145,15 +154,18 @@
$this->writePHP(" }\n");
}
- function beginClass($name, $parent = null)
+ function beginMethod($name, $param_list = array())
{
- $this->writePHP("class $name " . ($parent ? "extends $parent " : '') . "{\n");
- return $name;
+ $this->methods_stack[] = array($this->current_method, $this->current_mode);
+ $this->current_method = $name;
+
+ return $this->beginFunction($name, $param_list);
}
- function endClass()
+ function endMethod()
{
$this->writePHP(" }\n");
+ list($this->current_method, $this->current_mode) = array_pop($this->methods_stack);
}
/**
@@ -177,5 +189,32 @@
{
return '$' . $this->getTempVariable();
}
+
+ protected function _append($code)
+ {
+ if(!$this->current_method)
+ {
+ $this->code .= $code;
+ return;
+ }
+
+ if(!isset($this->methods[$this->current_method]))
+ $this->methods[$this->current_method] = '';
+
+ $this->methods[$this->current_method] .= $code;
+ }
+
+ protected function _renderMethods()
+ {
+ return implode("\n", $this->methods);
+ }
+
+ protected function _renderIncludeList()
+ {
+ $include_code = '';
+ foreach($this->include_list as $include_file)
+ $include_code .= "require_once('$include_file');\n";
+ return $include_code;
+ }
}
Modified: 3.x/trunk/limb/macro/src/lmbMacroCompiler.class.php
===================================================================
--- 3.x/trunk/limb/macro/src/lmbMacroCompiler.class.php 2007-09-04 13:46:13 UTC (rev 6259)
+++ 3.x/trunk/limb/macro/src/lmbMacroCompiler.class.php 2007-09-04 22:35:49 UTC (rev 6260)
@@ -63,16 +63,11 @@
return array($class, $render_func, $compiled_file_path);
}
- function _generateTemplateCode($prefix, $root_node)
+ function _generateTemplateCode($hash, $root_node)
{
- $code_writer = new lmbMacroCodeWriter();
- $code_writer->registerInclude('limb/macro/src/lmbMacroTemplateExecutor.class.php');
- $class = $code_writer->beginClass('TemplateExecutor' . $prefix, 'lmbMacroTemplateExecutor');
- $render_func = $code_writer->beginFunction('render');
+ $code_writer = new lmbMacroCodeWriter($class = 'TemplateExecutor' . $hash);
$root_node->generate($code_writer);
- $code_writer->endFunction();
- $code_writer->endClass();
- return array($class, $render_func, $code_writer->renderCode());
+ return array($class, $code_writer->getRenderMethod(), $code_writer->renderCode());
}
function parseTemplate($source_file_path, $root_node)
Modified: 3.x/trunk/limb/macro/src/lmbMacroTemplateExecutor.class.php
===================================================================
--- 3.x/trunk/limb/macro/src/lmbMacroTemplateExecutor.class.php 2007-09-04 13:46:13 UTC (rev 6259)
+++ 3.x/trunk/limb/macro/src/lmbMacroTemplateExecutor.class.php 2007-09-04 22:35:49 UTC (rev 6260)
@@ -15,7 +15,7 @@
*/
class lmbMacroTemplateExecutor
{
- function __construct($vars)
+ function __construct($vars = array())
{
foreach($vars as $name => $value)
$this->name = $value;
Modified: 3.x/trunk/limb/macro/tests/cases/lmbMacroCodeWriterTest.class.php
===================================================================
--- 3.x/trunk/limb/macro/tests/cases/lmbMacroCodeWriterTest.class.php 2007-09-04 13:46:13 UTC (rev 6259)
+++ 3.x/trunk/limb/macro/tests/cases/lmbMacroCodeWriterTest.class.php 2007-09-04 22:35:49 UTC (rev 6260)
@@ -15,30 +15,28 @@
function setUp()
{
- $this->writer = new lmbMacroCodeWriter();
+ $this->class = 'Foo' . mt_rand();
+ $this->writer = new lmbMacroCodeWriter($this->class);
}
- function testGetCode()
+ function testRenderEmpty()
{
- $this->assertEqual($this->writer->renderCode(),'');
+ $object = $this->_instantiate();
+ $this->assertIsA($object, 'lmbMacroTemplateExecutor');
+ $this->assertNull($object->render());
}
- function testGetSetCode()
- {
- $this->writer->setCode($code = 'code');
- $this->assertEqual($code, $this->writer->getCode());
- }
-
function testWritePHP()
{
- $this->writer->writePHP('echo ("Hello World!");');
- $this->assertEqual($this->writer->renderCode(),'<?php echo ("Hello World!"); ?>');
+ $this->writer->writePHP('return "Hello World!";');
+ $object = $this->_instantiate();
+ $this->assertEqual($object->render(), 'Hello World!');
}
function testWriteHTML()
{
$this->writer->writeHTML('<p>Hello World!</p>');
- $this->assertEqual($this->writer->renderCode(),'<p>Hello World!</p>');
+ $this->assertEqual($this->_render(),'<p>Hello World!</p>');
}
function testSwithBetweenPHPAndHTML()
@@ -46,47 +44,50 @@
$this->writer->writePHP('echo ("Hello World!");');
$this->writer->writeHTML('<p>Hello World!</p>');
$this->writer->writePHP('echo ("Hello World!");');
- $this->assertEqual($this->writer->renderCode(),
- '<?php echo ("Hello World!"); ?><p>Hello World!</p><?php echo ("Hello World!"); ?>');
+
+ $this->assertEqual($this->_render(), "Hello World!<p>Hello World!</p>Hello World!");
}
- function testRegisterInclude()
+ function testFunction()
{
- $this->writer->registerInclude('test.php');
- $this->assertEqual($this->writer->renderCode(),'<?php '."require_once('test.php');\n".'?>');
- }
+ $params = array('$a', '$b');
+ $func = $this->writer->beginFunction('tpl' . mt_rand(), $params);
+ $this->writer->writePHP('echo $a . $b;');
+ $this->writer->endFunction();
+ $this->writer->writePHP("$func('a', 'b');");
- function testReset()
- {
- $this->writer->writePHP('echo ("Hello World!");');
- $this->writer->registerInclude('test.php');
- $this->writer->reset();
- $this->assertEqual($this->writer->renderCode(), '');
+ $this->assertEqual($this->_render(), 'ab');
}
- function testBeginClass()
+ function testMethod()
{
- $this->writer->beginClass('Foo', 'Bar');
- $this->assertEqual($this->writer->renderCode(), "<?php class Foo extends Bar {\n ?>");
- }
+ $params = array('$a', '$b');
+ $func = $this->writer->beginMethod('tpl' . mt_rand(), $params);
+ $this->writer->writePHP('echo $a . $b;');
+ $this->writer->endMethod();
+ $this->writer->writePHP("\$this->$func('a', 'b');");
- function testEndClass()
- {
- $this->writer->endClass();
- $this->assertEqual($this->writer->renderCode(),'<?php '." }\n".' ?>');
+ $this->assertEqual($this->_render(), 'ab');
}
- function testBeginFunction()
+ function testNestedMethods()
{
- $params = array('$a', '$b', '$c');
- $this->writer->beginFunction('tpl1', $params);
- $this->assertEqual($this->writer->renderCode(),"<?php function tpl1(\$a,\$b,\$c) {\n ?>");
- }
+ $params = array('$a', '$b');
+ //inside fooxxx method
+ $foo = $this->writer->beginMethod('foo' . mt_rand(), $params);
- function testEndFunction()
- {
- $this->writer->endFunction();
- $this->assertEqual($this->writer->renderCode(),'<?php '." }\n".' ?>');
+ //inside barxxx method, note, we're inside fooxxx as well
+ $bar = $this->writer->beginMethod('bar' . mt_rand(), $params);
+ $this->writer->writePHP('return $b . $a;');
+ $this->writer->endMethod();
+
+ $this->writer->writePHP('return $a . $b . ');//contecanating with barxxx method
+ $this->writer->writePHP("\$this->$bar(\$a, \$b);");
+ $this->writer->endMethod();
+
+ $this->writer->writePHP("echo \$this->$foo('a', 'b');");
+
+ $this->assertEqual($this->_render(), 'abba');
}
function testGetTempVariable()
@@ -110,5 +111,30 @@
$this->assertWantedPattern('/[a-z][a-z0-9]*/i', $var);
}
}
+
+ function _render()
+ {
+ $object = $this->_instantiate();
+ ob_start();
+ $object->render();
+ $out = ob_get_contents();
+ ob_end_clean();
+ return $out;
+ }
+
+ function _instantiate()
+ {
+ $this->_writeAndInclude($this->writer->renderCode());
+ $class = $this->class;
+ $object = new $class();
+ return $object;
+ }
+
+ function _writeAndInclude($code)
+ {
+ file_put_contents($file = LIMB_VAR_DIR . '/' . mt_rand() . '.php', $code);
+ include($file);
+ unlink($file);
+ }
}
More information about the limb-svn
mailing list