[ Index ]

PHP Cross Reference of Limb3

title

Body

[close]

/core/src/ -> lmbDecoratorGenerator.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  //code is based on MockGenerator class from SimpleTest test suite

  10  lmb_require('limb/core/src/lmbReflection.class.php');
  11  
  12  /**

  13   * class lmbDecoratorGenerator.

  14   *

  15   * @package core

  16   * @version $Id$

  17   */
  18  class lmbDecoratorGenerator
  19  {
  20    protected $_class;
  21    protected $_decorator_class;
  22    protected $_decorator_base;
  23    protected $_reflection;
  24  
  25    function generate($class, $decorator_class = null, $decorator_base = 'lmbDecorator')
  26    {
  27      $this->_class = $class;
  28  
  29      if(is_null($decorator_class))
  30        $this->_decorator_class = $class . 'Decorator';
  31      else
  32        $this->_decorator_class = $decorator_class;
  33  
  34      $this->_decorator_base = $decorator_base;
  35  
  36      if(class_exists($this->_decorator_class))
  37        return false;
  38  
  39      $this->_reflection = new lmbReflection($this->_class);
  40  
  41      $methods = array();
  42  
  43      return eval($this->_createClassCode() . " return true;");
  44    }
  45  
  46    protected function _createClassCode()
  47    {
  48      $implements = '';
  49      $interfaces = $this->_reflection->getInterfaces();
  50      if(function_exists('spl_classes'))
  51        $interfaces = array_diff($interfaces, array('Traversable'));
  52  
  53      if(count($interfaces) > 0)
  54        $implements = 'implements ' . implode(', ', $interfaces);
  55  
  56      $code = "class " . $this->_decorator_class . " extends " . $this->_decorator_base . " $implements {\n";
  57      $code .= "    function __construct(\$original) {\n";
  58      $code .= "        parent :: __construct(\$original);\n";
  59      $code .= "    }\n";
  60      $code .= $this->_createHandlerCode();
  61      $code .= "}\n";
  62      return $code;
  63    }
  64  
  65    protected function _createHandlerCode()
  66    {
  67      $code = '';
  68      $methods = $this->_reflection->getMethods();
  69      $base_reflection = new lmbReflection($this->_decorator_base);
  70      foreach($methods as $method)
  71      {
  72        if($this->_isMagicMethod($method))
  73          continue;
  74  
  75        if(in_array($method, $base_reflection->getMethods()))
  76          continue;
  77  
  78        $code .= "    " . $this->_reflection->getSignature($method) . " {\n";
  79        $code .= "        \$args = func_get_args();\n";
  80        $code .= "        return \$this->___invoke(\"$method\", \$args);\n";
  81        $code .= "    }\n";
  82      }
  83      return $code;
  84    }
  85  
  86    protected function _isMagicMethod($method)
  87    {
  88      return in_array(strtolower($method), array('__construct', '__destruct', '__clone'));
  89    }
  90  }
  91  ?>


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