暫無描述

Alias.php 7.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. <?php
  2. /**
  3. * Laravel IDE Helper Generator
  4. *
  5. * @author Barry vd. Heuvel <barryvdh@gmail.com>
  6. * @copyright 2014 Barry vd. Heuvel / Fruitcake Studio (http://www.fruitcakestudio.nl)
  7. * @license http://www.opensource.org/licenses/mit-license.php MIT
  8. * @link https://github.com/barryvdh/laravel-ide-helper
  9. */
  10. namespace Barryvdh\LaravelIdeHelper;
  11. class Alias
  12. {
  13. protected $alias;
  14. protected $facade;
  15. protected $extends = null;
  16. protected $classType = 'class';
  17. protected $namespace = '__root';
  18. protected $root = null;
  19. protected $classes = array();
  20. protected $methods = array();
  21. protected $usedMethods = array();
  22. protected $valid = false;
  23. protected $magicMethods = array();
  24. protected $interfaces = array();
  25. /**
  26. * @param string $alias
  27. * @param string $facade
  28. * @param array $magicMethods
  29. * @param array $interfaces
  30. */
  31. public function __construct($alias, $facade, $magicMethods = array(), $interfaces = array())
  32. {
  33. $this->alias = $alias;
  34. $this->magicMethods = $magicMethods;
  35. $this->interfaces = $interfaces;
  36. // Make the class absolute
  37. $facade = '\\' . ltrim($facade, '\\');
  38. $this->facade = $facade;
  39. $this->detectRoot();
  40. if ((!$this->isTrait() && $this->root)) {
  41. $this->valid = true;
  42. } else {
  43. return;
  44. }
  45. $this->addClass($this->root);
  46. $this->detectNamespace();
  47. $this->detectClassType();
  48. }
  49. /**
  50. * Add one or more classes to analyze
  51. *
  52. * @param array|string $classes
  53. */
  54. public function addClass($classes)
  55. {
  56. $classes = (array)$classes;
  57. foreach ($classes as $class) {
  58. if (class_exists($class) || interface_exists($class)) {
  59. $this->classes[] = $class;
  60. }else{
  61. echo "Class not exists: $class\r\n";
  62. }
  63. }
  64. }
  65. /**
  66. * Check if this class is valid to process.
  67. * @return bool
  68. */
  69. public function isValid()
  70. {
  71. return $this->valid;
  72. }
  73. /**
  74. * Get the classtype, 'interface' or 'class'
  75. *
  76. * @return string
  77. */
  78. public function getClasstype()
  79. {
  80. return $this->classType;
  81. }
  82. /**
  83. * Get the class which this alias extends
  84. *
  85. * @return null|string
  86. */
  87. public function getExtends()
  88. {
  89. return $this->extends;
  90. }
  91. /**
  92. * Get the Alias by which this class is called
  93. *
  94. * @return string
  95. */
  96. public function getAlias()
  97. {
  98. return $this->alias;
  99. }
  100. /**
  101. * Get the namespace from the alias
  102. *
  103. * @return string
  104. */
  105. public function getNamespace()
  106. {
  107. return $this->namespace;
  108. }
  109. /**
  110. * Get the methods found by this Alias
  111. *
  112. * @return array
  113. */
  114. public function getMethods()
  115. {
  116. $this->addMagicMethods();
  117. $this->detectMethods();
  118. return $this->methods;
  119. }
  120. /**
  121. * Detect the namespace
  122. */
  123. protected function detectNamespace()
  124. {
  125. if (strpos($this->alias, '\\')) {
  126. $nsParts = explode('\\', $this->alias);
  127. $this->short = array_pop($nsParts);
  128. $this->namespace = implode('\\', $nsParts);
  129. }
  130. }
  131. /**
  132. * Detect the class type
  133. */
  134. protected function detectClassType()
  135. {
  136. //Some classes extend the facade
  137. if (interface_exists($this->facade)) {
  138. $this->classType = 'interface';
  139. $this->extends = $this->facade;
  140. } else {
  141. $this->classType = 'class';
  142. if (class_exists($this->facade)) {
  143. $this->extends = $this->facade;
  144. }
  145. }
  146. }
  147. /**
  148. * Get the real root of a facade
  149. *
  150. * @return bool|string
  151. */
  152. protected function detectRoot()
  153. {
  154. $facade = $this->facade;
  155. try {
  156. //If possible, get the facade root
  157. if (method_exists($facade, 'getFacadeRoot')) {
  158. $root = get_class($facade::getFacadeRoot());
  159. } else {
  160. $root = $facade;
  161. }
  162. //If it doesn't exist, skip it
  163. if (!class_exists($root) && !interface_exists($root)) {
  164. $this->error("Class $root is not found.");
  165. return;
  166. }
  167. $this->root = $root;
  168. //When the database connection is not set, some classes will be skipped
  169. } catch (\PDOException $e) {
  170. $this->error(
  171. "PDOException: " . $e->getMessage() . "\nPlease configure your database connection correctly, or use the sqlite memory driver (-M). Skipping $facade."
  172. );
  173. } catch (\Exception $e) {
  174. $this->error("Exception: " . $e->getMessage() . "\nSkipping $facade.");
  175. }
  176. }
  177. /**
  178. * Detect if this class is a trait or not.
  179. *
  180. * @return bool
  181. */
  182. protected function isTrait()
  183. {
  184. // Check if the facade is not a Trait
  185. if (function_exists('trait_exists') && trait_exists($this->facade)) {
  186. return true;
  187. }
  188. return false;
  189. }
  190. /**
  191. * Add magic methods, as defined in the configuration files
  192. */
  193. protected function addMagicMethods()
  194. {
  195. foreach($this->magicMethods as $magic => $real){
  196. list($className, $name) = explode('::', $real);
  197. if(!class_exists($className) && !interface_exists($className)){
  198. continue;
  199. }
  200. $method = new \ReflectionMethod($className, $name);
  201. $class = new \ReflectionClass($className);
  202. if(!in_array($method->name, $this->usedMethods)){
  203. if($class !== $this->root){
  204. $this->methods[] = new Method($method, $this->alias, $class, $magic, $this->interfaces);
  205. }
  206. $this->usedMethods[] = $magic;
  207. }
  208. }
  209. }
  210. /**
  211. * Get the methods for one or multiple classes.
  212. *
  213. * @return string
  214. */
  215. protected function detectMethods()
  216. {
  217. foreach ($this->classes as $class) {
  218. $reflection = new \ReflectionClass($class);
  219. $methods = $reflection->getMethods(\ReflectionMethod::IS_PUBLIC);
  220. if ($methods) {
  221. foreach ($methods as $method) {
  222. if (!in_array($method->name, $this->usedMethods)) {
  223. // Only add the methods to the output when the root is not the same as the class.
  224. // And don't add the __*() methods
  225. if ($this->extends !== $class && $method->name !== '__clone') {
  226. $this->methods[] = new Method($method, $this->alias, $reflection, $method->name, $this->interfaces);
  227. }
  228. $this->usedMethods[] = $method->name;
  229. }
  230. }
  231. }
  232. }
  233. }
  234. /**
  235. * Output an error.
  236. *
  237. * @param string $string
  238. * @return void
  239. */
  240. protected function error($string)
  241. {
  242. echo $string . "\r\n";
  243. }
  244. }