From 8dcc74a95f0eade39832f16e8d93ce8026b10160 Mon Sep 17 00:00:00 2001 From: "Barry vd. Heuvel" Date: Wed, 6 Aug 2014 13:04:52 +0200 Subject: [PATCH] Add magic methods --- src/Alias.php | 39 +++++++++++++++++++++++++++++++-------- src/Generator.php | 9 ++------- src/Method.php | 4 ++-- 3 files changed, 35 insertions(+), 17 deletions(-) diff --git a/src/Alias.php b/src/Alias.php index f5aa274..051fff2 100644 --- a/src/Alias.php +++ b/src/Alias.php @@ -16,18 +16,20 @@ class Alias protected $alias; protected $facade; protected $extends = null; - protected $classTYpe = 'class'; + protected $classType = 'class'; protected $namespace = '__root'; protected $root = null; protected $classes = array(); protected $methods = array(); - protected $used_methods = array(); + protected $usedMethods = array(); protected $valid = false; + protected $magicMethods = array(); protected $interfaces = array(); - public function __construct($alias, $facade, $interfaces = array()) + public function __construct($alias, $facade, $magicMethods = array(), $interfaces = array()) { $this->alias = $alias; + $this->magicMethods = $magicMethods; $this->interfaces = $interfaces; // Make the class absolute @@ -61,6 +63,8 @@ class Alias foreach ($classes as $class) { if (class_exists($class) || interface_exists($class)) { $this->classes[] = $class; + }else{ + echo "Class not exists: $class\r\n"; } } @@ -122,6 +126,7 @@ class Alias */ public function getMethods() { + $this->addMagicMethods(); $this->detectMethods(); return $this->methods; @@ -207,6 +212,24 @@ class Alias 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. * @@ -221,13 +244,13 @@ class Alias $methods = $reflection->getMethods(\ReflectionMethod::IS_PUBLIC); if ($methods) { foreach ($methods as $method) { - if (!in_array($method->name, $this->used_methods)) { - // Only add the methods to the output when the root is not the same as the facade. + if (!in_array($method->name, $this->usedMethods)) { + // Only add the methods to the output when the root is not the same as the class. // And don't add the __*() methods - if ($this->facade !== $this->root && substr($method->name, 0, 2) !== '__') { - $this->methods[] = new Method($method, $this->alias, $reflection, $this->interfaces); + if ($this->extends !== $class && $method->name !== '__clone') { + $this->methods[] = new Method($method, $this->alias, $reflection, $method->name, $this->interfaces); } - $this->used_methods[] = $method->name; + $this->usedMethods[] = $method->name; } } } diff --git a/src/Generator.php b/src/Generator.php index 04d967e..7931cf7 100644 --- a/src/Generator.php +++ b/src/Generator.php @@ -40,7 +40,6 @@ class Generator $this->magic = $this->config->get('laravel-ide-helper::magic'); $this->interfaces = $this->config->get('laravel-ide-helper::interfaces'); $this->helpers = $helpers; - } /** @@ -68,7 +67,8 @@ class Generator // Get all aliases 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()) { @@ -82,11 +82,6 @@ class Generator $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(); if (!isset($namespaces[$namespace])) { $namespaces[$namespace] = array(); diff --git a/src/Method.php b/src/Method.php index e93e7b0..f751b5b 100644 --- a/src/Method.php +++ b/src/Method.php @@ -26,10 +26,10 @@ class Method protected $params_with_default = 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->name = $method->name; + $this->name = $methodName ?: $method->name; $this->namespace = $method->getDeclaringClass()->getNamespaceName(); //Create a DocBlock and serializer instance