[ Index ]

PHP Cross Reference of Limb3

title

Body

[close]

/macro/src/ -> lmbMacroCompiler.class.php (source)

   1  <?php
   2  /*
   3   * Limb PHP Framework
   4   *
   5   * @link http://limb-project.com 
   6   * @copyright  Copyright &copy; 2004-2007 BIT(http://bit-creative.com)
   7   * @license    LGPL http://www.gnu.org/copyleft/lesser.html 
   8   */
   9  
  10  /**

  11   * class lmbMacroCompiler.

  12   *

  13   * @package macro

  14   * @version $Id$

  15   */
  16  class lmbMacroCompiler
  17  {
  18    /**

  19    * @var lmbMacroTreeBuilder

  20    */
  21    protected $tree_builder;
  22  
  23    /**

  24    * @var lmbMacroConfig

  25    */
  26    protected $config;
  27  
  28    /**

  29    * @var lmbMacroTemplateLocator

  30    */
  31    protected $template_locator;
  32  
  33    /**

  34    * @var lmbMacroSourceParser

  35    */
  36    protected $parser;
  37  
  38    /**

  39    * @var lmbMacroTagDictionary

  40    */
  41    protected $tag_dictionary;
  42  
  43  
  44    function __construct($config, $tag_dictionary, $template_locator)
  45    {
  46      $this->config = $config;
  47      $this->template_locator = $template_locator;
  48  
  49      $this->tag_dictionary = $tag_dictionary;
  50      $this->tree_builder = new lmbMacroTreeBuilder($this);
  51    }
  52  
  53    function compile($file_name)
  54    {
  55      if(!$source_file_path = $this->template_locator->locateSourceTemplate($file_name))    
  56       throw new lmbMacroException('Template source file not found', array('file_name' => $file_name));
  57  
  58      $root_node = new lmbMacroRootNode(new lmbMacroSourceLocation($source_file_path, ''));
  59  
  60      $this->parseTemplate($file_name, $root_node);
  61  
  62      $root_node->prepare();
  63  
  64      $compiled_file_path = $this->template_locator->locateCompiledTemplate($file_name);
  65      $generated_code = $this->_generateTemplateCode(md5($compiled_file_path), $root_node);
  66      self :: writeFile($compiled_file_path, $generated_code);
  67    }
  68  
  69    function _generateTemplateCode($prefix, $root_node)
  70    {
  71      $code_writer = new lmbMacroCodeWriter();
  72      $code_writer->setFunctionPrefix($prefix);
  73  
  74      $constructor_func = $code_writer->beginFunction('($root, $components)');
  75      $root_node->generateConstructor($code_writer);
  76      $code_writer->endFunction();
  77  
  78      $render_func = $code_writer->beginFunction('($root, $components)');
  79      $code_writer->writePHP('$template = $root;' . "\n");
  80      $root_node->generate($code_writer);
  81      $code_writer->endFunction();
  82  
  83      $code_writer->writePHP('$GLOBALS[\'TemplateRender\'][$compiled_template_path] = \'' . $render_func . '\';');
  84      $code_writer->writePHP('$GLOBALS[\'TemplateConstruct\'][$compiled_template_path] = \'' . $constructor_func . '\';');
  85  
  86      return $code_writer->renderCode();
  87    }
  88  
  89    function parseTemplate($source_file_path, $root_node)
  90    {
  91      $parser = new lmbMacroParser($this->tree_builder,                                           
  92                                   $this->config,
  93                                   $this->template_locator,
  94                                   $this->tag_dictionary);
  95  
  96      $parser->parse($source_file_path, $root_node);
  97    }
  98  
  99    /**

 100    * @return lmbMacroConfig

 101    **/
 102    function getConfig()
 103    {
 104      return $this->config;
 105    }
 106  
 107    /**

 108    * @return lmbMacroTemplateLocator

 109    **/
 110    function getTemplateLocator()
 111    {
 112      return $this->template_locator;
 113    }
 114  
 115    /**

 116    * @return lmbMacroTreeBuilder

 117    **/
 118    function getTreeBuilder()
 119    {
 120      return $this->tree_builder;
 121    }
 122  
 123    function getTagDictionary()
 124    {
 125      return $this->tag_dictionary;
 126    }
 127  
 128    static function writeFile($file, $data)
 129    {
 130      $dirname = dirname($file);    
 131      lmbFs :: mkdir($dirname);
 132  
 133      file_put_contents($file, $data);
 134    }
 135  }
 136  ?>


Generated: Tue Oct 7 05:02:03 2008 Cross-referenced by PHPXref 0.7