Re-format source code of Alias class

This commit is contained in:
Thach Nguyen
2018-01-27 03:21:47 +07:00
parent 042d341b65
commit e6f7ca7433
+287 -284
View File
@@ -1,284 +1,287 @@
<?php <?php
/** /**
* Laravel IDE Helper Generator * Laravel IDE Helper Generator
* *
* @author Barry vd. Heuvel <[email protected]> * @author Barry vd. Heuvel <[email protected]>
* @copyright 2014 Barry vd. Heuvel / Fruitcake Studio (http://www.fruitcakestudio.nl) * @copyright 2014 Barry vd. Heuvel / Fruitcake Studio (http://www.fruitcakestudio.nl)
* @license http://www.opensource.org/licenses/mit-license.php MIT * @license http://www.opensource.org/licenses/mit-license.php MIT
* @link https://github.com/barryvdh/laravel-ide-helper * @link https://github.com/barryvdh/laravel-ide-helper
*/ */
namespace Barryvdh\LaravelIdeHelper; namespace Barryvdh\LaravelIdeHelper;
class Alias class Alias
{ {
protected $alias; protected $alias;
protected $facade; protected $facade;
protected $extends = null; protected $extends = null;
protected $classType = 'class'; protected $classType = 'class';
protected $short; protected $short;
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 $usedMethods = array(); protected $usedMethods = array();
protected $valid = false; protected $valid = false;
protected $magicMethods = array(); protected $magicMethods = array();
protected $interfaces = array(); protected $interfaces = array();
/** /**
* @param string $alias * @param string $alias
* @param string $facade * @param string $facade
* @param array $magicMethods * @param array $magicMethods
* @param array $interfaces * @param array $interfaces
*/ */
public function __construct($alias, $facade, $magicMethods = array(), $interfaces = array()) public function __construct($alias, $facade, $magicMethods = array(), $interfaces = array())
{ {
$this->alias = $alias; $this->alias = $alias;
$this->magicMethods = $magicMethods; $this->magicMethods = $magicMethods;
$this->interfaces = $interfaces; $this->interfaces = $interfaces;
// Make the class absolute // Make the class absolute
$facade = '\\' . ltrim($facade, '\\'); $facade = '\\' . ltrim($facade, '\\');
$this->facade = $facade; $this->facade = $facade;
$this->detectRoot(); $this->detectRoot();
if ((!$this->isTrait() && $this->root)) { if ((!$this->isTrait() && $this->root)) {
$this->valid = true; $this->valid = true;
} else { } else {
return; return;
} }
$this->addClass($this->root); $this->addClass($this->root);
$this->detectNamespace(); $this->detectNamespace();
$this->detectClassType(); $this->detectClassType();
if($facade === '\Illuminate\Database\Eloquent\Model'){ if ($facade === '\Illuminate\Database\Eloquent\Model') {
$this->usedMethods = array('decrement', 'increment'); $this->usedMethods = array('decrement', 'increment');
} }
} }
/** /**
* Add one or more classes to analyze * Add one or more classes to analyze
* *
* @param array|string $classes * @param array|string $classes
*/ */
public function addClass($classes) public function addClass($classes)
{ {
$classes = (array)$classes; $classes = (array)$classes;
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{ } else {
echo "Class not exists: $class\r\n"; $this->error('Class not exists: ' . $class);
} }
} }
} }
/** /**
* Check if this class is valid to process. * Check if this class is valid to process.
* @return bool * @return bool
*/ */
public function isValid() public function isValid()
{ {
return $this->valid; return $this->valid;
} }
/** /**
* Get the classtype, 'interface' or 'class' * Get the classType, 'interface' or 'class'
* *
* @return string * @return string
*/ */
public function getClasstype() public function getClassType()
{ {
return $this->classType; return $this->classType;
} }
/** /**
* Get the class which this alias extends * Get the class which this alias extends
* *
* @return null|string * @return null|string
*/ */
public function getExtends() public function getExtends()
{ {
return $this->extends; return $this->extends;
} }
/** /**
* Get the Alias by which this class is called * Get the Alias by which this class is called
* *
* @return string * @return string
*/ */
public function getAlias() public function getAlias()
{ {
return $this->alias; return $this->alias;
} }
/** /**
* Return the short name (without namespace) * Return the short name (without namespace)
*/ *
public function getShortName(){ * @return string
return $this->short; */
} public function getShortName()
/** {
* Get the namespace from the alias return $this->short;
* }
* @return string
*/ /**
public function getNamespace() * Get the namespace from the alias
{ *
return $this->namespace; * @return string
} */
public function getNamespace()
/** {
* Get the methods found by this Alias return $this->namespace;
* }
* @return array
*/ /**
public function getMethods() * Get the methods found by this Alias
{ *
$this->addMagicMethods(); * @return array|Method[]
$this->detectMethods(); */
return $this->methods; public function getMethods()
} {
$this->addMagicMethods();
/** $this->detectMethods();
* Detect the namespace return $this->methods;
*/ }
protected function detectNamespace()
{ /**
if (strpos($this->alias, '\\')) { * Detect the namespace
$nsParts = explode('\\', $this->alias); */
$this->short = array_pop($nsParts); protected function detectNamespace()
$this->namespace = implode('\\', $nsParts); {
}else{ if (strpos($this->alias, '\\')) {
$this->short = $this->alias; $nsParts = explode('\\', $this->alias);
} $this->short = array_pop($nsParts);
} $this->namespace = implode('\\', $nsParts);
} else {
/** $this->short = $this->alias;
* Detect the class type }
*/ }
protected function detectClassType()
{ /**
//Some classes extend the facade * Detect the class type
if (interface_exists($this->facade)) { */
$this->classType = 'interface'; protected function detectClassType()
$this->extends = $this->facade; {
} else { //Some classes extend the facade
$this->classType = 'class'; if (interface_exists($this->facade)) {
if (class_exists($this->facade)) { $this->classType = 'interface';
$this->extends = $this->facade; $this->extends = $this->facade;
} } else {
} $this->classType = 'class';
} if (class_exists($this->facade)) {
$this->extends = $this->facade;
/** }
* Get the real root of a facade }
* }
* @return bool|string
*/ /**
protected function detectRoot() * Get the real root of a facade
{ *
$facade = $this->facade; * @return bool|string
*/
try { protected function detectRoot()
//If possible, get the facade root {
if (method_exists($facade, 'getFacadeRoot')) { $facade = $this->facade;
$root = get_class($facade::getFacadeRoot());
} else { try {
$root = $facade; //If possible, get the facade root
} if (method_exists($facade, 'getFacadeRoot')) {
$root = get_class($facade::getFacadeRoot());
//If it doesn't exist, skip it } else {
if (!class_exists($root) && !interface_exists($root)) { $root = $facade;
return; }
}
//If it doesn't exist, skip it
$this->root = $root; if (!class_exists($root) && !interface_exists($root)) {
return;
//When the database connection is not set, some classes will be skipped }
} catch (\PDOException $e) {
$this->error( $this->root = $root;
"PDOException: " . $e->getMessage() . "\nPlease configure your database connection correctly, or use the sqlite memory driver (-M). Skipping $facade."
); //When the database connection is not set, some classes will be skipped
} catch (\Exception $e) { } catch (\PDOException $e) {
$this->error("Exception: " . $e->getMessage() . "\nSkipping $facade."); $this->error(
} 'PDOException: ' . $e->getMessage() . "\nPlease configure your database connection correctly, or use the sqlite memory driver (-M). Skipping $facade."
} );
} catch (\Exception $e) {
/** $this->error('Exception: ' . $e->getMessage() . "\nSkipping $facade.");
* Detect if this class is a trait or not. }
* }
* @return bool
*/ /**
protected function isTrait() * Detect if this class is a trait or not.
{ *
// Check if the facade is not a Trait * @return bool
if (function_exists('trait_exists') && trait_exists($this->facade)) { */
return true; protected function isTrait()
} {
return false; // Check if the facade is not a Trait
} if (function_exists('trait_exists') && trait_exists($this->facade)) {
return true;
/** }
* Add magic methods, as defined in the configuration files return false;
*/ }
protected function addMagicMethods()
{ /**
foreach($this->magicMethods as $magic => $real){ * Add magic methods, as defined in the configuration files
list($className, $name) = explode('::', $real); */
if(!class_exists($className) && !interface_exists($className)){ protected function addMagicMethods()
continue; {
} foreach ($this->magicMethods as $magic => $real) {
$method = new \ReflectionMethod($className, $name); list($className, $name) = explode('::', $real);
$class = new \ReflectionClass($className); if (!class_exists($className) && !interface_exists($className)) {
continue;
if(!in_array($method->name, $this->usedMethods)){ }
if($class !== $this->root){ $method = new \ReflectionMethod($className, $name);
$this->methods[] = new Method($method, $this->alias, $class, $magic, $this->interfaces); $class = new \ReflectionClass($className);
}
$this->usedMethods[] = $magic; 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. }
* }
* @return string
*/ /**
protected function detectMethods() * Get the methods for one or multiple classes.
{ *
* @return string
foreach ($this->classes as $class) { */
$reflection = new \ReflectionClass($class); protected function detectMethods()
{
$methods = $reflection->getMethods(\ReflectionMethod::IS_PUBLIC); foreach ($this->classes as $class) {
if ($methods) { $reflection = new \ReflectionClass($class);
foreach ($methods as $method) {
if (!in_array($method->name, $this->usedMethods)) { $methods = $reflection->getMethods(\ReflectionMethod::IS_PUBLIC);
// Only add the methods to the output when the root is not the same as the class. if ($methods) {
// And don't add the __*() methods foreach ($methods as $method) {
if ($this->extends !== $class && substr($method->name, 0, 2) !== '__') { if (!in_array($method->name, $this->usedMethods)) {
$this->methods[] = new Method($method, $this->alias, $reflection, $method->name, $this->interfaces); // Only add the methods to the output when the root is not the same as the class.
} // And don't add the __*() methods
$this->usedMethods[] = $method->name; if ($this->extends !== $class && substr($method->name, 0, 2) !== '__') {
} $this->methods[] = new Method($method, $this->alias, $reflection, $method->name, $this->interfaces);
} }
} $this->usedMethods[] = $method->name;
} }
} }
}
/** }
* Output an error. }
*
* @param string $string /**
* @return void * Output an error.
*/ *
protected function error($string) * @param string $string
{ * @return void
echo $string . "\r\n"; */
} protected function error($string)
} {
echo $string . "\r\n";
}
}