[ Index ]

PHP Cross Reference of Limb3

title

Body

[close]

/core/src/ -> lmbReflection.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 SimpleReflection class from SimpleTest test suite
  10  
  11  /**

  12   * class lmbReflection.

  13   *

  14   * @package core

  15   * @version $Id$

  16   */
  17  class lmbReflection
  18  {
  19    protected $_interface;
  20  
  21    function __construct($interface)
  22    {
  23      $this->_interface = $interface;
  24    }
  25  
  26    function classExists()
  27    {
  28      if(!class_exists($this->_interface))
  29        return false;
  30  
  31      $reflection = new ReflectionClass($this->_interface);
  32      return ! $reflection->isInterface();
  33    }
  34  
  35    function classOrInterfaceExists()
  36    {
  37      return $this->_classOrInterfaceExistsWithAutoload($this->_interface, true);
  38    }
  39  
  40    protected function _classOrInterfaceExistsWithAutoload($interface, $autoload)
  41    {
  42      if(function_exists('interface_exists'))
  43      {
  44        if(interface_exists($this->_interface, $autoload))
  45          return true;
  46      }
  47      return class_exists($this->_interface, $autoload);
  48    }
  49  
  50    function getMethods()
  51    {
  52      return array_unique(get_class_methods($this->_interface));
  53    }
  54  
  55    function getInterfaces()
  56    {
  57      $reflection = new ReflectionClass($this->_interface);
  58      if($reflection->isInterface())
  59        return array($this->_interface);
  60  
  61      return $this->_onlyParents($reflection->getInterfaces());
  62    }
  63  
  64    function getInterfaceMethods()
  65    {
  66      $methods = array();
  67      foreach($this->getInterfaces() as $interface)
  68        $methods = array_merge($methods, get_class_methods($interface));
  69      return array_unique($methods);
  70    }
  71  
  72    protected function _isInterfaceMethod($method)
  73    {
  74      return in_array($method, $this->getInterfaceMethods());
  75    }
  76  
  77    function getParent()
  78    {
  79      $reflection = new ReflectionClass($this->_interface);
  80      $parent = $reflection->getParentClass();
  81      if($parent)
  82        return $parent->getName();
  83      return false;
  84    }
  85  
  86    function isAbstract()
  87    {
  88      $reflection = new ReflectionClass($this->_interface);
  89      return $reflection->isAbstract();
  90    }
  91  
  92    protected function _onlyParents($interfaces)
  93    {
  94      $parents = array();
  95      foreach($interfaces as $interface)
  96      {
  97        foreach($interfaces as $possible_parent)
  98        {
  99          if($interface->getName() == $possible_parent->getName())
 100            continue;
 101  
 102          if($interface->isSubClassOf($possible_parent))
 103            break;
 104        }
 105        $parents[] = $interface->getName();
 106      }
 107      return $parents;
 108    }
 109  
 110    function getSignature($name)
 111    {
 112      if($name == '__get')
 113        return 'function __get($key)';
 114  
 115      if($name == '__set')
 116        return 'function __set($key, $value)';
 117  
 118      if(!is_callable(array($this->_interface, $name)))
 119        return "function $name()";
 120  
 121      if($this->_isInterfaceMethod($name))
 122        return $this->_getFullSignature($name);
 123  
 124      return "function $name()";
 125    }
 126  
 127    protected function _getFullSignature($name)
 128    {
 129      $interface = new ReflectionClass($this->_interface);
 130      $method = $interface->getMethod($name);
 131      $reference = $method->returnsReference() ? '&' : '';
 132      return "function $reference$name(" .
 133            implode(', ', $this->_getParameterSignatures($method)) .
 134            ")";
 135    }
 136  
 137    protected function _getParameterSignatures($method)
 138    {
 139      $signatures = array();
 140      foreach($method->getParameters() as $parameter)
 141      {
 142        $type = $parameter->getClass();
 143        $signatures[] =
 144            (! is_null($type) ? $type->getName() . ' ' : '') .
 145            ($parameter->isPassedByReference() ? '&' : '') .
 146            '$' . $this->_suppressSpurious($parameter->getName()) .
 147            ($this->_isOptional($parameter) ? ' = null' : '');
 148      }
 149      return $signatures;
 150    }
 151  
 152    protected function _suppressSpurious($name)
 153    {
 154      return str_replace(array('[', ']', ' '), '', $name);
 155    }
 156  
 157    protected function _isOptional($parameter)
 158    {
 159      if(method_exists($parameter, 'isOptional'))
 160        return $parameter->isOptional();
 161  
 162      return false;
 163    }
 164  }
 165  ?>


Generated: Tue Oct 14 04:47:40 2008 Cross-referenced by PHPXref 0.7