[limb-svn] r6244 - in 3.x/trunk/limb/macro: src tests/cases
svn at limb-project.com
svn at limb-project.com
Thu Aug 30 00:59:00 MSD 2007
Author: pachanga
Date: 2007-08-30 00:59:00 +0400 (Thu, 30 Aug 2007)
New Revision: 6244
URL: http://fisheye.limb-project.com/changelog/limb/?cs=6244
Added:
3.x/trunk/limb/macro/tests/cases/lmbMacroTagAcceptanceTest.class.php
Modified:
3.x/trunk/limb/macro/src/lmbMacroCompiler.class.php
3.x/trunk/limb/macro/src/lmbMacroParser.class.php
3.x/trunk/limb/macro/src/lmbMacroTagDictionary.class.php
3.x/trunk/limb/macro/src/lmbMacroTemplate.class.php
3.x/trunk/limb/macro/src/lmbMacroTemplateLocator.class.php
Log:
-- adding first acceptance tests(work is still in progress
Modified: 3.x/trunk/limb/macro/src/lmbMacroCompiler.class.php
===================================================================
--- 3.x/trunk/limb/macro/src/lmbMacroCompiler.class.php 2007-08-29 11:53:10 UTC (rev 6243)
+++ 3.x/trunk/limb/macro/src/lmbMacroCompiler.class.php 2007-08-29 20:59:00 UTC (rev 6244)
@@ -7,6 +7,12 @@
* @license LGPL http://www.gnu.org/copyleft/lesser.html
*/
+lmb_require('limb/macro/src/lmbMacroTagDictionary.class.php');
+lmb_require('limb/macro/src/lmbMacroTemplateLocator.class.php');
+lmb_require('limb/macro/src/lmbMacroTreeBuilder.class.php');
+lmb_require('limb/macro/src/lmbMacroNode.class.php');
+lmb_require('limb/macro/src/lmbMacroParser.class.php');
+
/**
* class lmbMacroCompiler.
*
@@ -21,11 +27,6 @@
protected $tree_builder;
/**
- * @var lmbMacroConfig
- */
- protected $config;
-
- /**
* @var lmbMacroTemplateLocator
*/
protected $template_locator;
@@ -41,12 +42,16 @@
protected $tag_dictionary;
- function __construct($config, $tag_dictionary, $template_locator)
+ function __construct($tag_dictionary = null, $template_locator = null)
{
- $this->config = $config;
$this->template_locator = $template_locator;
+ if(!$this->template_locator)
+ $this->template_locator = new lmbMacroTemplateLocator();
$this->tag_dictionary = $tag_dictionary;
+ if(!$this->tag_dictionary)
+ $this->tag_dictionary = lmbMacroTagDictionary :: instance();
+
$this->tree_builder = new lmbMacroTreeBuilder($this);
}
@@ -55,56 +60,33 @@
if(!$source_file_path = $this->template_locator->locateSourceTemplate($file_name))
throw new lmbMacroException('Template source file not found', array('file_name' => $file_name));
- $root_node = new lmbMacroRootNode(new lmbMacroSourceLocation($source_file_path, ''));
-
+ $root_node = new lmbMacroNode(new lmbMacroSourceLocation($source_file_path, ''));
$this->parseTemplate($file_name, $root_node);
-
$root_node->prepare();
$compiled_file_path = $this->template_locator->locateCompiledTemplate($file_name);
- $generated_code = $this->_generateTemplateCode(md5($compiled_file_path), $root_node);
+ list($render_func, $generated_code) = $this->_generateTemplateCode(md5($compiled_file_path), $root_node);
self :: writeFile($compiled_file_path, $generated_code);
+ return array($render_func, $compiled_file_path);
}
function _generateTemplateCode($prefix, $root_node)
{
$code_writer = new lmbMacroCodeWriter();
$code_writer->setFunctionPrefix($prefix);
-
- $constructor_func = $code_writer->beginFunction('($root, $components)');
- $root_node->generateConstructor($code_writer);
- $code_writer->endFunction();
-
- $render_func = $code_writer->beginFunction('($root, $components)');
- $code_writer->writePHP('$template = $root;' . "\n");
+ $render_func = $code_writer->beginFunction('($root)');
$root_node->generate($code_writer);
$code_writer->endFunction();
-
- $code_writer->writePHP('$GLOBALS[\'TemplateRender\'][$compiled_template_path] = \'' . $render_func . '\';');
- $code_writer->writePHP('$GLOBALS[\'TemplateConstruct\'][$compiled_template_path] = \'' . $constructor_func . '\';');
-
- return $code_writer->renderCode();
+ return array($render_func, $code_writer->renderCode());
}
function parseTemplate($source_file_path, $root_node)
{
- $parser = new lmbMacroParser($this->tree_builder,
- $this->config,
- $this->template_locator,
- $this->tag_dictionary);
-
+ $parser = new lmbMacroParser($this->tree_builder, $this->template_locator, $this->tag_dictionary);
$parser->parse($source_file_path, $root_node);
}
/**
- * @return lmbMacroConfig
- */
- function getConfig()
- {
- return $this->config;
- }
-
- /**
* @return lmbMacroTemplateLocator
*/
function getTemplateLocator()
Modified: 3.x/trunk/limb/macro/src/lmbMacroParser.class.php
===================================================================
--- 3.x/trunk/limb/macro/src/lmbMacroParser.class.php 2007-08-29 11:53:10 UTC (rev 6243)
+++ 3.x/trunk/limb/macro/src/lmbMacroParser.class.php 2007-08-29 20:59:00 UTC (rev 6244)
@@ -7,6 +7,10 @@
* @license LGPL http://www.gnu.org/copyleft/lesser.html
*/
+lmb_require('limb/macro/src/lmbMacroTokenizerListener.interface.php');
+lmb_require('limb/macro/src/lmbMacroLiteralParsingState.class.php');
+lmb_require('limb/macro/src/lmbMacroTagParsingState.class.php');
+
/**
* class lmbMacroParser.
*
@@ -20,13 +24,8 @@
protected $literal_parsing_state;
/**
- * @var lmbMacroConfig
+ * @var lmbMacroTreebuilder
*/
- protected $config;
-
- /**
- * @var lmbMacrotree_builder
- */
protected $tree_builder;
/**
@@ -34,11 +33,9 @@
*/
protected $template_locator;
- function __construct($tree_builder, $config, $template_locator, $tag_dictionary)
+ function __construct($tree_builder, $template_locator, $tag_dictionary)
{
$this->tree_builder = $tree_builder;
-
- $this->config = $config;
$this->template_locator = $template_locator;
$this->component_parsing_state = $this->_createComponentParsingState($tag_dictionary);
@@ -69,7 +66,7 @@
$source_file_path = $this->template_locator->locateSourceTemplate($file_name);
if(empty($source_file_path))
- throw new lmbMacroException('Template source file not found', array('file_name' => $file_name));
+ throw new lmbMacroException('Template source file not found', array('file_name' => $file_name));
$tags_before_parse = $this->tree_builder->getExpectedTagCount();
Modified: 3.x/trunk/limb/macro/src/lmbMacroTagDictionary.class.php
===================================================================
--- 3.x/trunk/limb/macro/src/lmbMacroTagDictionary.class.php 2007-08-29 11:53:10 UTC (rev 6243)
+++ 3.x/trunk/limb/macro/src/lmbMacroTagDictionary.class.php 2007-08-29 20:59:00 UTC (rev 6244)
@@ -18,7 +18,17 @@
class lmbMacroTagDictionary
{
protected $info = array();
+ static protected $instance;
+ static function instance()
+ {
+ if(self :: $instance)
+ return self :: $instance;
+
+ self :: $instance = new lmbMacroTagDictionary();
+ return self :: $instance;
+ }
+
function register($taginfo, $file)
{
$tag_to_lower = strtolower($taginfo->getTag());
Modified: 3.x/trunk/limb/macro/src/lmbMacroTemplate.class.php
===================================================================
--- 3.x/trunk/limb/macro/src/lmbMacroTemplate.class.php 2007-08-29 11:53:10 UTC (rev 6243)
+++ 3.x/trunk/limb/macro/src/lmbMacroTemplate.class.php 2007-08-29 20:59:00 UTC (rev 6244)
@@ -7,6 +7,9 @@
* @license LGPL http://www.gnu.org/copyleft/lesser.html
*/
+lmb_require('limb/macro/src/lmbMacroTemplateLocator.class.php');
+lmb_require('limb/macro/src/lmbMacroCompiler.class.php');
+
/**
* class lmbMacroTemplate.
*
@@ -18,12 +21,14 @@
protected $file;
protected $cache_dir;
protected $vars = array();
- protected $includes = array();
- function __construct($file, $cache_dir)
+ function __construct($file, $cache_dir, $locator = null)
{
$this->file = $file;
$this->cache_dir = $cache_dir;
+ $this->locator = $locator;
+ if(!$this->locator)
+ $this->locator = new lmbMacroTemplateLocator();
}
function set($name, $value)
@@ -34,99 +39,20 @@
function render()
{
ob_start();
- $file = $this->_compile($class);
- include($file);
- $body = new $class($this->vars);
- $body->paint();
+
+ $compiler = new lmbMacroCompiler();
+ list($func, $compiled_file) = $compiler->compile($this->file);
+
+ include_once($compiled_file);
+ $func($this);
+
$out = ob_get_contents();
ob_end_clean();
return $out;
}
- protected function _compile(&$class_name)
+ function createCompiler()
{
- $contents = file_get_contents($this->file);
- $prefix = 'p' . md5($this->file);
- $class_name = "{$prefix}Body";
-
- $this->_processVars($contents);
- $body = $this->_generateBody($class_name, $contents);
-
- $compiled_file = $this->cache_dir . '/' . $prefix . '.php';
- file_put_contents($compiled_file, $body, LOCK_EX);
- return $compiled_file;
}
-
- protected function _generateBody($class_name, $contents)
- {
- $include_methods = '';
- foreach($this->includes as $name => $body)
- $include_methods .= "$body\n";
-
- $code = <<<EOD
-<?php
-class {$class_name}
-{
- protected \$vars = array();
-
- function __construct(\$vars)
- {
- \$this->vars = \$vars;
- }
-
- function __get(\$name)
- {
- if(isset(\$this->vars[\$name]))
- return \$this->vars[\$name];
- }
-
- $include_methods
-
- function paint(){ ?>$contents<?php }
}
-?>
-EOD;
- return $code;
- }
- protected function _processVars(&$contents)
- {
- $contents = str_replace('<?=', '<?php echo ', $contents);
- $contents = preg_replace('~<\?(?!php|=)~', '<?php ', $contents);
- $contents = str_replace('@$', '$this->', $contents);
- $contents = preg_replace_callback('~\{(\$[^\W]+)\}~', array($this, '_varSugarCallback'), $contents);
- $contents = preg_replace_callback('~\{([^\W]+\([^\}]+)\}~', array($this, '_functionSugarCallback'), $contents);
- }
-
- protected function _varSugarCallback($matches)
- {
- return '<?php echo ' . $matches[1] . ' ?>';
- }
-
- protected function _functionSugarCallback($matches)
- {
- return '<?php echo ' . $matches[1] . ' ?>';
- }
-
- protected function _includeCallback($matches)
- {
- if(!preg_match('~file=(?:"|\')([^"\']+)(?:"|\')~', $matches[0], $m))
- throw new lmbException('Invalid <%INCLUDE..>: ' . $matches[0]);
-
- $file = lmbFs :: normalizePath($m[1]);
-
- $args = '';
- if(preg_match('~args=(?:"|\')\(([^\)]+)\)(?:"|\')~', $matches[0], $m))
- $args = $m[1];
-
- $contents = file_get_contents($file);
- $this->_processIncludes($contents);
- $method_name = 'paintInclude' . sizeof($this->includes);
- $method_body = "function $method_name(){ \$args = func_get_args();extract(\$args);?>$contents<?php }";
-
- $this->includes[$method_name] = $method_body;
-
- return "<?php \$this->$method_name(array($args)); ?>";
- }
-}
-
Modified: 3.x/trunk/limb/macro/src/lmbMacroTemplateLocator.class.php
===================================================================
--- 3.x/trunk/limb/macro/src/lmbMacroTemplateLocator.class.php 2007-08-29 11:53:10 UTC (rev 6243)
+++ 3.x/trunk/limb/macro/src/lmbMacroTemplateLocator.class.php 2007-08-29 20:59:00 UTC (rev 6244)
@@ -15,26 +15,26 @@
*/
class lmbMacroTemplateLocator
{
- protected $config;
- protected $templates_dir;
+ protected $cache_dir;
- public function __construct($config)
+ function __construct()
{
- $this->config = $config;
- $this->templates_dir = 'templates/';//fix it
+ $this->cache_dir = LIMB_VAR_DIR . '/compiled';
}
- public function locateCompiledTemplate($file_name)
+ function locateCompiledTemplate($file_name)
{
- return $this->config->getCacheDir() . '/' . md5($file_name) . '.php';
+ return $this->cache_dir . '/' . md5($file_name) . '.php';
}
- public function locateSourceTemplate($file_name)
+ function locateSourceTemplate($file_name)
{
- return $this->templates_dir . '/' . $file_name;
+ //fix this later
+ if(lmbFs :: isPathAbsolute($file_name))
+ return $file_name;
}
- public function readTemplateFile($file_name)
+ function readTemplateFile($file_name)
{
return file_get_contents($file_name, 1);
}
Added: 3.x/trunk/limb/macro/tests/cases/lmbMacroTagAcceptanceTest.class.php
===================================================================
--- 3.x/trunk/limb/macro/tests/cases/lmbMacroTagAcceptanceTest.class.php (rev 0)
+++ 3.x/trunk/limb/macro/tests/cases/lmbMacroTagAcceptanceTest.class.php 2007-08-29 20:59:00 UTC (rev 6244)
@@ -0,0 +1,62 @@
+<?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
+ */
+
+lmb_require('limb/macro/src/lmbMacroTemplate.class.php');
+lmb_require('limb/macro/src/lmbMacroTagDictionary.class.php');
+lmb_require('limb/macro/src/lmbMacroTag.class.php');
+lmb_require('limb/macro/src/lmbMacroTagInfo.class.php');
+lmb_require('limb/fs/src/lmbFs.class.php');
+
+class MacroTagFooTest extends lmbMacroTag
+{
+ function generateContents($code)
+ {
+ $code->writeHtml('foo!');
+ }
+}
+
+class MacroTagBarTest extends lmbMacroTag
+{
+ function generateContents($code)
+ {
+ $code->writeHtml('bar');
+ }
+}
+
+$foo_info = new lmbMacroTagInfo('foo', 'MacroTagFooTest');
+$bar_info = new lmbMacroTagInfo('bar', 'MacroTagBarTest');
+
+lmbMacroTagDictionary :: instance()->register($foo_info, __FILE__);
+lmbMacroTagDictionary :: instance()->register($bar_info, __FILE__);
+
+class lmbMacroTagAcceptanceTest extends UnitTestCase
+{
+ function setUp()
+ {
+ lmbFs :: rm(LIMB_VAR_DIR . '/tpl');
+ lmbFs :: mkdir(LIMB_VAR_DIR . '/tpl');
+ }
+
+ function testTemplateRendering()
+ {
+ $code = '<h1><%foo/%><%bar/%></h1>';
+ $tpl = $this->_createTemplate($code);
+ $out = $tpl->render();
+ $this->assertEqual($out, '<h1>foo!bar</h1>');
+ }
+
+ protected function _createTemplate($code)
+ {
+ $file = LIMB_VAR_DIR . '/tpl/' . mt_rand() . '.html';
+ $cache_dir = LIMB_VAR_DIR . '/tpl/compiled';
+ $tpl = new lmbMacroTemplate($file, $cache_dir);
+ return $tpl;
+ }
+}
+
More information about the limb-svn
mailing list