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

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

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