Няма описание

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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 Symfony\Component\Console\Output\OutputInterface;
  14. class Generator
  15. {
  16. /** @var \Illuminate\Config\Repository */
  17. protected $config;
  18. /** @var \Illuminate\View\Factory */
  19. protected $view;
  20. /** @var \Symfony\Component\Console\Output\OutputInterface */
  21. protected $output;
  22. protected $extra = array();
  23. protected $magic = array();
  24. protected $interfaces = array();
  25. protected $helpers;
  26. /**
  27. * @param \Illuminate\Config\Repository $config
  28. * @param \Illuminate\View\Factory $view
  29. * @param \Symfony\Component\Console\Output\OutputInterface $output
  30. * @param string $helpers
  31. */
  32. public function __construct(ConfigRepository $config,
  33. /* Illuminate\View\Factory */ $view,
  34. OutputInterface $output = null,
  35. $helpers = ''
  36. ) {
  37. $this->config = $config;
  38. $this->view = $view;
  39. // Find the drivers to add to the extra/interfaces
  40. $this->detectDrivers();
  41. $this->extra = array_merge($this->extra, $this->config->get('laravel-ide-helper::extra'));
  42. $this->magic = array_merge($this->magic, $this->config->get('laravel-ide-helper::magic'));
  43. $this->interfaces = array_merge($this->interfaces, $this->config->get('laravel-ide-helper::interfaces'));
  44. $this->helpers = $helpers;
  45. }
  46. /**
  47. * Generate the helper file contents;
  48. *
  49. * @return string;
  50. */
  51. public function generate()
  52. {
  53. $app = app();
  54. return $this->view->make('laravel-ide-helper::ide-helper')
  55. ->with('namespaces', $this->getNamespaces())
  56. ->with('helpers', $this->helpers)
  57. ->with('version', $app::VERSION)
  58. ->render();
  59. }
  60. protected function detectDrivers()
  61. {
  62. try{
  63. if (class_exists('Cache')) {
  64. $class = get_class(\Auth::driver());
  65. $this->extra['Auth'] = array($class);
  66. $this->interfaces['\Illuminate\Auth\UserProviderInterface'] = $class;
  67. }
  68. }catch (\Exception $e) {}
  69. try{
  70. if (class_exists('DB')) {
  71. $class = get_class(\DB::connection());
  72. $this->extra['DB'] = array($class);
  73. $this->interfaces['\Illuminate\Database\ConnectionInterface'] = $class;
  74. }
  75. }catch (\Exception $e) {}
  76. try{
  77. if (class_exists('Cache')) {
  78. $driver = get_class(\Cache::driver());
  79. $store = get_class(\Cache::getStore());
  80. $this->extra['Cache'] = array($driver, $store);
  81. $this->interfaces['\Illuminate\Cache\StoreInterface'] = $store;
  82. }
  83. }catch (\Exception $e) {}
  84. try{
  85. if (class_exists('Queue')) {
  86. $class = get_class(\Queue::connection());
  87. $this->extra['Queue'] = array($class);
  88. $this->interfaces['\Illuminate\Queue\QueueInterface'] = $class;
  89. }
  90. }catch (\Exception $e) {}
  91. try{
  92. if (class_exists('SSH')){
  93. $class = get_class(\SSH::connection());
  94. $this->extra['SSH'] = array($class);
  95. $this->interfaces['\Illuminate\Remote\ConnectionInterface'] = $class;
  96. }
  97. }catch (\Exception $e) {}
  98. // Make all interface classes absolute
  99. foreach ($this->interfaces as &$interface) {
  100. $interface = '\\' . ltrim($interface, '\\');
  101. }
  102. }
  103. /**
  104. * Find all namespaces/aliases that are valid for us to render
  105. *
  106. * @return array
  107. */
  108. protected function getNamespaces()
  109. {
  110. $namespaces = array();
  111. // Get all aliases
  112. foreach (AliasLoader::getInstance()->getAliases() as $name => $facade) {
  113. $magicMethods = array_key_exists($name, $this->magic) ? $this->magic[$name] : array();
  114. $alias = new Alias($name, $facade, $magicMethods, $this->interfaces);
  115. if ($alias->isValid()) {
  116. //Add extra methods, from other classes (magic static calls)
  117. if (array_key_exists($name, $this->extra)) {
  118. $alias->addClass($this->extra[$name]);
  119. }
  120. $namespace = $alias->getNamespace();
  121. if (!isset($namespaces[$namespace])) {
  122. $namespaces[$namespace] = array();
  123. }
  124. $namespaces[$namespace][] = $alias;
  125. }
  126. }
  127. return $namespaces;
  128. }
  129. /**
  130. * Get the driver/connection/store from the managers
  131. *
  132. * @param $alias
  133. * @return array|bool|string
  134. */
  135. public function getDriver($alias)
  136. {
  137. try {
  138. if ($alias == "Auth") {
  139. $driver = \Auth::driver();
  140. } elseif ($alias == "DB") {
  141. $driver = \DB::connection();
  142. } elseif ($alias == "Cache") {
  143. $driver = get_class(\Cache::driver());
  144. $store = get_class(\Cache::getStore());
  145. return array($driver, $store);
  146. } elseif ($alias == "Queue") {
  147. $driver = \Queue::connection();
  148. } else {
  149. return false;
  150. }
  151. return get_class($driver);
  152. } catch (\Exception $e) {
  153. $this->error("Could not determine driver/connection for $alias.");
  154. return false;
  155. }
  156. }
  157. /**
  158. * Write a string as error output.
  159. *
  160. * @param string $string
  161. * @return void
  162. */
  163. protected function error($string)
  164. {
  165. if($this->output){
  166. $this->output->writeln("<error>$string</error>");
  167. }else{
  168. echo $string . "\r\n";
  169. }
  170. }
  171. }