Add magic methods

This commit is contained in:
Barry vd. Heuvel
2014-08-06 13:04:52 +02:00
parent 27ffcb883c
commit 8dcc74a95f
3 changed files with 35 additions and 17 deletions
+31 -8
View File
@@ -16,18 +16,20 @@ class Alias
protected $alias; protected $alias;
protected $facade; protected $facade;
protected $extends = null; protected $extends = null;
protected $classTYpe = 'class'; protected $classType = 'class';
protected $namespace = '__root'; protected $namespace = '__root';
protected $root = null; protected $root = null;
protected $classes = array(); protected $classes = array();
protected $methods = array(); protected $methods = array();
protected $used_methods = array(); protected $usedMethods = array();
protected $valid = false; protected $valid = false;
protected $magicMethods = array();
protected $interfaces = array(); protected $interfaces = array();
public function __construct($alias, $facade, $interfaces = array()) public function __construct($alias, $facade, $magicMethods = array(), $interfaces = array())
{ {
$this->alias = $alias; $this->alias = $alias;
$this->magicMethods = $magicMethods;
$this->interfaces = $interfaces; $this->interfaces = $interfaces;
// Make the class absolute // Make the class absolute
@@ -61,6 +63,8 @@ class Alias
foreach ($classes as $class) { foreach ($classes as $class) {
if (class_exists($class) || interface_exists($class)) { if (class_exists($class) || interface_exists($class)) {
$this->classes[] = $class; $this->classes[] = $class;
}else{
echo "Class not exists: $class\r\n";
} }
} }
@@ -122,6 +126,7 @@ class Alias
*/ */
public function getMethods() public function getMethods()
{ {
$this->addMagicMethods();
$this->detectMethods(); $this->detectMethods();
return $this->methods; return $this->methods;
@@ -207,6 +212,24 @@ class Alias
return false; return false;
} }
protected function addMagicMethods(){
foreach($this->magicMethods as $magic => $real){
list($className, $name) = explode('::', $real);
if(!class_exists($className) && !interface_exists($className)){
continue;
}
$method = new \ReflectionMethod($className, $name);
$class = new \ReflectionClass($className);
if(!in_array($method->name, $this->usedMethods)){
if($class !== $this->root){
$this->methods[] = new Method($method, $this->alias, $class, $magic, $this->interfaces);
}
$this->usedMethods[] = $magic;
}
}
}
/** /**
* Get the methods for one or multiple classes. * Get the methods for one or multiple classes.
* *
@@ -221,13 +244,13 @@ class Alias
$methods = $reflection->getMethods(\ReflectionMethod::IS_PUBLIC); $methods = $reflection->getMethods(\ReflectionMethod::IS_PUBLIC);
if ($methods) { if ($methods) {
foreach ($methods as $method) { foreach ($methods as $method) {
if (!in_array($method->name, $this->used_methods)) { if (!in_array($method->name, $this->usedMethods)) {
// Only add the methods to the output when the root is not the same as the facade. // Only add the methods to the output when the root is not the same as the class.
// And don't add the __*() methods // And don't add the __*() methods
if ($this->facade !== $this->root && substr($method->name, 0, 2) !== '__') { if ($this->extends !== $class && $method->name !== '__clone') {
$this->methods[] = new Method($method, $this->alias, $reflection, $this->interfaces); $this->methods[] = new Method($method, $this->alias, $reflection, $method->name, $this->interfaces);
} }
$this->used_methods[] = $method->name; $this->usedMethods[] = $method->name;
} }
} }
} }
+2 -7
View File
@@ -40,7 +40,6 @@ class Generator
$this->magic = $this->config->get('laravel-ide-helper::magic'); $this->magic = $this->config->get('laravel-ide-helper::magic');
$this->interfaces = $this->config->get('laravel-ide-helper::interfaces'); $this->interfaces = $this->config->get('laravel-ide-helper::interfaces');
$this->helpers = $helpers; $this->helpers = $helpers;
} }
/** /**
@@ -68,7 +67,8 @@ class Generator
// Get all aliases // Get all aliases
foreach (AliasLoader::getInstance()->getAliases() as $name => $facade) { foreach (AliasLoader::getInstance()->getAliases() as $name => $facade) {
$alias = new Alias($name, $facade, $this->interfaces); $magicMethods = array_key_exists($name, $this->magic) ? $this->magic[$name] : array();
$alias = new Alias($name, $facade, $magicMethods, $this->interfaces);
if ($alias->isValid()) { if ($alias->isValid()) {
@@ -82,11 +82,6 @@ class Generator
$alias->addClass($this->extra[$name]); $alias->addClass($this->extra[$name]);
} }
//Add extra methods, from other classes (magic static calls)
if (array_key_exists($name, $this->magic)) {
$alias->addClass($this->magic[$name]);
}
$namespace = $alias->getNamespace(); $namespace = $alias->getNamespace();
if (!isset($namespaces[$namespace])) { if (!isset($namespaces[$namespace])) {
$namespaces[$namespace] = array(); $namespaces[$namespace] = array();
+2 -2
View File
@@ -26,10 +26,10 @@ class Method
protected $params_with_default = array(); protected $params_with_default = array();
protected $interfaces = array(); protected $interfaces = array();
public function __construct($method, $alias, $class, $interfaces = array()) public function __construct($method, $alias, $class, $methodName = null, $interfaces = array())
{ {
$this->interfaces = $interfaces; $this->interfaces = $interfaces;
$this->name = $method->name; $this->name = $methodName ?: $method->name;
$this->namespace = $method->getDeclaringClass()->getNamespaceName(); $this->namespace = $method->getDeclaringClass()->getNamespaceName();
//Create a DocBlock and serializer instance //Create a DocBlock and serializer instance