PageRenderTime 23ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/vendor/laravel/framework/src/Illuminate/Foundation/Console/RouteListCommand.php

https://gitlab.com/mdabutaleb/bitm-laravel-1
PHP | 246 lines | 200 code | 10 blank | 36 comment | 2 complexity | ddfb6a9c454c7b1b61770f7165dc7878 MD5 | raw file
  1. <?php
  2. namespace Illuminate\Foundation\Console;
  3. use Illuminate\Support\Arr;
  4. use Illuminate\Support\Str;
  5. use Illuminate\Routing\Route;
  6. use Illuminate\Routing\Router;
  7. use Illuminate\Console\Command;
  8. use Symfony\Component\Console\Input\InputOption;
  9. class RouteListCommand extends Command
  10. {
  11. /**
  12. * The console command name.
  13. *
  14. * @var string
  15. */
  16. protected $name = 'route:list';
  17. /**
  18. * The console command description.
  19. *
  20. * @var string
  21. */
  22. protected $description = 'List all registered routes';
  23. /**
  24. * The router instance.
  25. *
  26. * @var \Illuminate\Routing\Router
  27. */
  28. protected $router;
  29. /**
  30. * An array of all the registered routes.
  31. *
  32. * @var \Illuminate\Routing\RouteCollection
  33. */
  34. protected $routes;
  35. /**
  36. * The table headers for the command.
  37. *
  38. * @var array
  39. */
  40. protected $headers = ['Domain', 'Method', 'URI', 'Name', 'Action', 'Middleware'];
  41. /**
  42. * Create a new route command instance.
  43. *
  44. * @param \Illuminate\Routing\Router $router
  45. * @return void
  46. */
  47. public function __construct(Router $router)
  48. {
  49. parent::__construct();
  50. $this->router = $router;
  51. $this->routes = $router->getRoutes();
  52. }
  53. /**
  54. * Execute the console command.
  55. *
  56. * @return void
  57. */
  58. public function fire()
  59. {
  60. if (count($this->routes) == 0) {
  61. return $this->error("Your application doesn't have any routes.");
  62. }
  63. $this->displayRoutes($this->getRoutes());
  64. }
  65. /**
  66. * Compile the routes into a displayable format.
  67. *
  68. * @return array
  69. */
  70. protected function getRoutes()
  71. {
  72. $results = [];
  73. foreach ($this->routes as $route) {
  74. $results[] = $this->getRouteInformation($route);
  75. }
  76. if ($sort = $this->option('sort')) {
  77. $results = Arr::sort($results, function ($value) use ($sort) {
  78. return $value[$sort];
  79. });
  80. }
  81. if ($this->option('reverse')) {
  82. $results = array_reverse($results);
  83. }
  84. return array_filter($results);
  85. }
  86. /**
  87. * Get the route information for a given route.
  88. *
  89. * @param \Illuminate\Routing\Route $route
  90. * @return array
  91. */
  92. protected function getRouteInformation(Route $route)
  93. {
  94. return $this->filterRoute([
  95. 'host' => $route->domain(),
  96. 'method' => implode('|', $route->methods()),
  97. 'uri' => $route->uri(),
  98. 'name' => $route->getName(),
  99. 'action' => $route->getActionName(),
  100. 'middleware' => $this->getMiddleware($route),
  101. ]);
  102. }
  103. /**
  104. * Display the route information on the console.
  105. *
  106. * @param array $routes
  107. * @return void
  108. */
  109. protected function displayRoutes(array $routes)
  110. {
  111. $this->table($this->headers, $routes);
  112. }
  113. /**
  114. * Get before filters.
  115. *
  116. * @param \Illuminate\Routing\Route $route
  117. * @return string
  118. */
  119. protected function getMiddleware($route)
  120. {
  121. $middlewares = array_values($route->middleware());
  122. $actionName = $route->getActionName();
  123. if (! empty($actionName) && $actionName !== 'Closure') {
  124. $middlewares = array_merge($middlewares, $this->getControllerMiddleware($actionName));
  125. }
  126. return implode(',', $middlewares);
  127. }
  128. /**
  129. * Get the middleware for the given Controller@action name.
  130. *
  131. * @param string $actionName
  132. * @return array
  133. */
  134. protected function getControllerMiddleware($actionName)
  135. {
  136. $segments = explode('@', $actionName);
  137. return $this->getControllerMiddlewareFromInstance(
  138. $this->laravel->make($segments[0]),
  139. isset($segments[1]) ? $segments[1] : null
  140. );
  141. }
  142. /**
  143. * Get the middlewares for the given controller instance and method.
  144. *
  145. * @param \Illuminate\Routing\Controller $controller
  146. * @param string|null $method
  147. * @return array
  148. */
  149. protected function getControllerMiddlewareFromInstance($controller, $method)
  150. {
  151. if (! method_exists($controller, 'getMiddleware')) {
  152. return [];
  153. }
  154. $middleware = $this->router->getMiddleware();
  155. $results = [];
  156. foreach ($controller->getMiddleware() as $data) {
  157. if (! is_string($data['middleware'])) {
  158. continue;
  159. }
  160. if (! $method || ! $this->methodExcludedByOptions($method, $data['options'])) {
  161. $results[] = Arr::get($middleware, $data['middleware'], $data['middleware']);
  162. }
  163. }
  164. return $results;
  165. }
  166. /**
  167. * Determine if the given options exclude a particular method.
  168. *
  169. * @param string $method
  170. * @param array $options
  171. * @return bool
  172. */
  173. protected function methodExcludedByOptions($method, array $options)
  174. {
  175. return (! empty($options['only']) && ! in_array($method, (array) $options['only'])) ||
  176. (! empty($options['except']) && in_array($method, (array) $options['except']));
  177. }
  178. /**
  179. * Filter the route by URI and / or name.
  180. *
  181. * @param array $route
  182. * @return array|null
  183. */
  184. protected function filterRoute(array $route)
  185. {
  186. if (($this->option('name') && ! Str::contains($route['name'], $this->option('name'))) ||
  187. $this->option('path') && ! Str::contains($route['uri'], $this->option('path')) ||
  188. $this->option('method') && ! Str::contains($route['method'], $this->option('method'))) {
  189. return;
  190. }
  191. return $route;
  192. }
  193. /**
  194. * Get the console command options.
  195. *
  196. * @return array
  197. */
  198. protected function getOptions()
  199. {
  200. return [
  201. ['method', null, InputOption::VALUE_OPTIONAL, 'Filter the routes by method.'],
  202. ['name', null, InputOption::VALUE_OPTIONAL, 'Filter the routes by name.'],
  203. ['path', null, InputOption::VALUE_OPTIONAL, 'Filter the routes by path.'],
  204. ['reverse', 'r', InputOption::VALUE_NONE, 'Reverse the ordering of the routes.'],
  205. ['sort', null, InputOption::VALUE_OPTIONAL, 'The column (host, method, uri, name, action, middleware) to sort by.', 'uri'],
  206. ];
  207. }
  208. }