PageRenderTime 36ms CodeModel.GetById 26ms RepoModel.GetById 1ms app.codeStats 0ms

/vendor/barryvdh/laravel-ide-helper/src/Generator.php

https://gitlab.com/edot92/jpagithub
PHP | 283 lines | 199 code | 35 blank | 49 comment | 29 complexity | b38173197a004c1b75de06f3230d254e MD5 | raw file
  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. use Illuminate\Foundation\AliasLoader;
  12. use Illuminate\Config\Repository as ConfigRepository;
  13. use ReflectionClass;
  14. use Symfony\Component\Console\Output\OutputInterface;
  15. class Generator
  16. {
  17. /** @var \Illuminate\Config\Repository */
  18. protected $config;
  19. /** @var \Illuminate\View\Factory */
  20. protected $view;
  21. /** @var \Symfony\Component\Console\Output\OutputInterface */
  22. protected $output;
  23. protected $extra = array();
  24. protected $magic = array();
  25. protected $interfaces = array();
  26. protected $helpers;
  27. /**
  28. * @param \Illuminate\Config\Repository $config
  29. * @param \Illuminate\View\Factory $view
  30. * @param \Symfony\Component\Console\Output\OutputInterface $output
  31. * @param string $helpers
  32. */
  33. public function __construct(/*ConfigRepository */ $config,
  34. /* Illuminate\View\Factory */ $view,
  35. OutputInterface $output = null,
  36. $helpers = ''
  37. ) {
  38. $this->config = $config;
  39. $this->view = $view;
  40. // Find the drivers to add to the extra/interfaces
  41. $this->detectDrivers();
  42. $this->extra = array_merge($this->extra, $this->config->get('ide-helper.extra'));
  43. $this->magic = array_merge($this->magic, $this->config->get('ide-helper.magic'));
  44. $this->interfaces = array_merge($this->interfaces, $this->config->get('ide-helper.interfaces'));
  45. // Make all interface classes absolute
  46. foreach ($this->interfaces as &$interface) {
  47. $interface = '\\' . ltrim($interface, '\\');
  48. }
  49. $this->helpers = $helpers;
  50. }
  51. /**
  52. * Generate the helper file contents;
  53. *
  54. * @param string $format The format to generate the helper in (php/json)
  55. * @return string;
  56. */
  57. public function generate($format = 'php')
  58. {
  59. // Check if the generator for this format exists
  60. $method = 'generate'.ucfirst($format).'Helper';
  61. if (method_exists($this, $method)) {
  62. return $this->$method();
  63. }
  64. return $this->generatePhpHelper();
  65. }
  66. public function generatePhpHelper()
  67. {
  68. $app = app();
  69. return $this->view->make('ide-helper::helper')
  70. ->with('namespaces', $this->getNamespaces())
  71. ->with('helpers', $this->helpers)
  72. ->with('version', $app->version())
  73. ->render();
  74. }
  75. public function generateJsonHelper()
  76. {
  77. $classes = array();
  78. foreach ($this->getNamespaces() as $aliases) {
  79. foreach($aliases as $alias) {
  80. $functions = array();
  81. foreach ($alias->getMethods() as $method) {
  82. $functions[$method->getName()] = '('. $method->getParamsWithDefault().')';
  83. }
  84. $classes[$alias->getAlias()] = array(
  85. 'functions' => $functions,
  86. );
  87. }
  88. }
  89. $flags = JSON_FORCE_OBJECT;
  90. if (defined('JSON_PRETTY_PRINT')) {
  91. $flags |= JSON_PRETTY_PRINT;
  92. }
  93. return json_encode(array(
  94. 'php' => array(
  95. 'classes' => $classes,
  96. ),
  97. ), $flags);
  98. }
  99. protected function detectDrivers()
  100. {
  101. $this->interfaces['\Illuminate\Contracts\Auth\Authenticatable'] = config('auth.providers.users.model', config('auth.model', 'App\User'));
  102. try{
  103. if (class_exists('Auth') && is_a('Auth', '\Illuminate\Support\Facades\Auth', true)) {
  104. if (class_exists('\Illuminate\Foundation\Application')) {
  105. $authMethod = version_compare(\Illuminate\Foundation\Application::VERSION, '5.2', '>=') ? 'guard' : 'driver';
  106. } else {
  107. $refClass = new ReflectionClass('\Laravel\Lumen\Application');
  108. $versionStr = $refClass->newInstanceWithoutConstructor()->version();
  109. $authMethod = strpos($versionStr, 'Lumen (5.0') === 0 ? 'driver' : (strpos($versionStr, 'Lumen (5.1') === 0 ? 'driver' : 'guard');
  110. }
  111. $class = get_class(\Auth::$authMethod());
  112. $this->extra['Auth'] = array($class);
  113. $this->interfaces['\Illuminate\Auth\UserProviderInterface'] = $class;
  114. }
  115. }catch (\Exception $e) {}
  116. try{
  117. if (class_exists('DB') && is_a('DB', '\Illuminate\Support\Facades\DB', true)) {
  118. $class = get_class(\DB::connection());
  119. $this->extra['DB'] = array($class);
  120. $this->interfaces['\Illuminate\Database\ConnectionInterface'] = $class;
  121. }
  122. }catch (\Exception $e) {}
  123. try{
  124. if (class_exists('Cache') && is_a('Cache', '\Illuminate\Support\Facades\Cache', true)) {
  125. $driver = get_class(\Cache::driver());
  126. $store = get_class(\Cache::getStore());
  127. $this->extra['Cache'] = array($driver, $store);
  128. $this->interfaces['\Illuminate\Cache\StoreInterface'] = $store;
  129. }
  130. }catch (\Exception $e) {}
  131. try{
  132. if (class_exists('Queue') && is_a('Queue', '\Illuminate\Support\Facades\Queue', true)) {
  133. $class = get_class(\Queue::connection());
  134. $this->extra['Queue'] = array($class);
  135. $this->interfaces['\Illuminate\Queue\QueueInterface'] = $class;
  136. }
  137. }catch (\Exception $e) {}
  138. try{
  139. if (class_exists('SSH') && is_a('SSH', '\Illuminate\Support\Facades\SSH', true)){
  140. $class = get_class(\SSH::connection());
  141. $this->extra['SSH'] = array($class);
  142. $this->interfaces['\Illuminate\Remote\ConnectionInterface'] = $class;
  143. }
  144. }catch (\Exception $e) {}
  145. }
  146. /**
  147. * Find all namespaces/aliases that are valid for us to render
  148. *
  149. * @return array
  150. */
  151. protected function getNamespaces()
  152. {
  153. $namespaces = array();
  154. // Get all aliases
  155. foreach ($this->getAliases() as $name => $facade) {
  156. // Skip the Redis facade, if not available (otherwise Fatal PHP Error)
  157. if ($facade == 'Illuminate\Support\Facades\Redis' && !class_exists('Predis\Client')) {
  158. continue;
  159. }
  160. $magicMethods = array_key_exists($name, $this->magic) ? $this->magic[$name] : array();
  161. $alias = new Alias($name, $facade, $magicMethods, $this->interfaces);
  162. if ($alias->isValid()) {
  163. //Add extra methods, from other classes (magic static calls)
  164. if (array_key_exists($name, $this->extra)) {
  165. $alias->addClass($this->extra[$name]);
  166. }
  167. $namespace = $alias->getNamespace();
  168. if (!isset($namespaces[$namespace])) {
  169. $namespaces[$namespace] = array();
  170. }
  171. $namespaces[$namespace][] = $alias;
  172. }
  173. }
  174. return $namespaces;
  175. }
  176. protected function getAliases()
  177. {
  178. // For Laravel, use the AliasLoader
  179. if (class_exists('Illuminate\Foundation\AliasLoader')) {
  180. return AliasLoader::getInstance()->getAliases();
  181. }
  182. $facades = [
  183. 'App' => 'Illuminate\Support\Facades\App',
  184. 'Auth' => 'Illuminate\Support\Facades\Auth',
  185. 'Bus' => 'Illuminate\Support\Facades\Bus',
  186. 'DB' => 'Illuminate\Support\Facades\DB',
  187. 'Cache' => 'Illuminate\Support\Facades\Cache',
  188. 'Cookie' => 'Illuminate\Support\Facades\Cookie',
  189. 'Crypt' => 'Illuminate\Support\Facades\Crypt',
  190. 'Event' => 'Illuminate\Support\Facades\Event',
  191. 'Hash' => 'Illuminate\Support\Facades\Hash',
  192. 'Log' => 'Illuminate\Support\Facades\Log',
  193. 'Mail' => 'Illuminate\Support\Facades\Mail',
  194. 'Queue' => 'Illuminate\Support\Facades\Queue',
  195. 'Request' => 'Illuminate\Support\Facades\Request',
  196. 'Schema' => 'Illuminate\Support\Facades\Schema',
  197. 'Session' => 'Illuminate\Support\Facades\Session',
  198. 'Storage' => 'Illuminate\Support\Facades\Storage',
  199. //'Validator' => 'Illuminate\Support\Facades\Validator',
  200. ];
  201. // Only return the ones that actually exist
  202. return array_filter($facades, function($alias){
  203. return class_exists($alias);
  204. });
  205. }
  206. /**
  207. * Get the driver/connection/store from the managers
  208. *
  209. * @param $alias
  210. * @return array|bool|string
  211. */
  212. public function getDriver($alias)
  213. {
  214. try {
  215. if ($alias == "Auth") {
  216. $driver = \Auth::driver();
  217. } elseif ($alias == "DB") {
  218. $driver = \DB::connection();
  219. } elseif ($alias == "Cache") {
  220. $driver = get_class(\Cache::driver());
  221. $store = get_class(\Cache::getStore());
  222. return array($driver, $store);
  223. } elseif ($alias == "Queue") {
  224. $driver = \Queue::connection();
  225. } else {
  226. return false;
  227. }
  228. return get_class($driver);
  229. } catch (\Exception $e) {
  230. $this->error("Could not determine driver/connection for $alias.");
  231. return false;
  232. }
  233. }
  234. /**
  235. * Write a string as error output.
  236. *
  237. * @param string $string
  238. * @return void
  239. */
  240. protected function error($string)
  241. {
  242. if($this->output){
  243. $this->output->writeln("<error>$string</error>");
  244. }else{
  245. echo $string . "\r\n";
  246. }
  247. }
  248. }