[ Index ]

PHP Cross Reference of Limb3

title

Body

[close]

/js/src/ -> lmbJsPreprocessor.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  lmb_require('limb/fs/src/lmbFs.class.php');
  10  lmb_require('limb/js/src/lmbJsDirectiveHandlers.class.php');
  11  
  12  @define('LIMB_JS_INCLUDE_PATH', 'www/js;limb/*/shared/js');
  13  
  14  /**

  15   * class lmbJsPreprocessor.

  16   *

  17   * @package js

  18   * @version $Id$

  19   */
  20  class lmbJsPreprocessor
  21  {
  22    protected $processed = array();
  23    protected $directives = array();
  24    protected $error = false;
  25  
  26    function __construct()
  27    {
  28      $this->toolkit = lmbToolkit :: instance();
  29      $this->addDirective('include', array(&$this, '_processInclude'));
  30    }
  31  
  32    function addDirective($directive, $handler)
  33    {
  34      $this->handlers[$directive] = $handler;
  35    }
  36  
  37    function processFiles($files)
  38    {
  39      $contents = '';
  40  
  41      foreach($files as $file)
  42      {
  43        if(!$processed_file_contents = $this->processFile($file))
  44          continue;
  45  
  46        $contents .= $processed_file_contents . "\n";
  47      }
  48  
  49      return $contents;
  50    }
  51  
  52    function processFile($file)
  53    {
  54      $file = lmbFs :: normalizePath($file);
  55  
  56       if($this->_isProcessed($file))
  57        return '';
  58  
  59      $contents = file_get_contents($file);
  60      $this->_markAsProcessed($file);
  61  
  62      $result = $this->_processDirectives($contents);
  63  
  64      return $result;
  65    }
  66  
  67    protected function _processDirectives(&$contents)
  68    {
  69      $processed_contents = preg_replace_callback('~^//#([a-z_\\-0-9]+)\s+(.*)$~m',
  70                                                  array(&$this, '_processDirective'),
  71                                                  $contents);
  72  
  73      // repeatedly throw saved exception to prevent preg_replace_callback warning
  74      if($this->error)
  75        throw $this->error;
  76  
  77      return $processed_contents;
  78    }
  79  
  80    protected function _processDirective($matches)
  81    {
  82      $params = $this->_parseDirectiveParams($matches[2]);
  83  
  84      if(!isset($this->handlers[$matches[1]]))
  85        return '';
  86  
  87      return call_user_func_array($this->handlers[$matches[1]], $params);
  88    }
  89  
  90    protected function _parseDirectiveParams($params_string)
  91    {
  92      if(!$params_string)
  93        return array();
  94  
  95      $params = explode(' ', $params_string);
  96      foreach($params as $key => $param)
  97      {
  98        if(!$param)
  99          unset($params[$key]);
 100      }
 101  
 102      return $params;
 103    }
 104  
 105    protected function _markAsProcessed($file)
 106    {
 107      $this->processed[$file] = 1;
 108    }
 109  
 110    protected function _isProcessed($file)
 111    {
 112      return isset($this->processed[$file]);
 113    }
 114  
 115    protected function _locateFiles($name)
 116    {
 117      $locator = $this->toolkit->getFileLocator(LIMB_JS_INCLUDE_PATH, 'js');
 118      return $locator->locateAll($name);
 119    }
 120  
 121    protected function _processInclude($filename)
 122    {
 123      try
 124      {
 125        $files = $this->_locateFiles(trim($filename, " \" '\r "));
 126      }
 127      catch(lmbException $e)
 128      {
 129        // temporarily stop and save exception to prevent preg_replace_callback warning
 130        if(!$this->error)
 131          $this->error = $e;
 132  
 133        return '';
 134      }
 135  
 136      return trim($this->processFiles($files));
 137    }
 138  }
 139  
 140  ?>


Generated: Fri Aug 29 04:49:26 2008 Cross-referenced by PHPXref 0.7