|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace flight\commands; |
| 6 | + |
| 7 | +use Flight; |
| 8 | +use flight\net\Route; |
| 9 | + |
| 10 | +/** |
| 11 | + * @property-read ?bool $get |
| 12 | + * @property-read ?bool $post |
| 13 | + * @property-read ?bool $delete |
| 14 | + * @property-read ?bool $put |
| 15 | + * @property-read ?bool $patch |
| 16 | + */ |
| 17 | +class RouteCommand extends AbstractBaseCommand |
| 18 | +{ |
| 19 | + /** |
| 20 | + * Construct |
| 21 | + * |
| 22 | + * @param array<string,mixed> $config JSON config from .runway-config.json |
| 23 | + */ |
| 24 | + public function __construct(array $config) |
| 25 | + { |
| 26 | + parent::__construct('routes', 'Gets all routes for an application', $config); |
| 27 | + |
| 28 | + $this->option('--get', 'Only return GET requests'); |
| 29 | + $this->option('--post', 'Only return POST requests'); |
| 30 | + $this->option('--delete', 'Only return DELETE requests'); |
| 31 | + $this->option('--put', 'Only return PUT requests'); |
| 32 | + $this->option('--patch', 'Only return PATCH requests'); |
| 33 | + } |
| 34 | + |
| 35 | + /** |
| 36 | + * Executes the function |
| 37 | + * |
| 38 | + * @return void |
| 39 | + */ |
| 40 | + public function execute() |
| 41 | + { |
| 42 | + $io = $this->app()->io(); |
| 43 | + |
| 44 | + if(isset($this->config['index_root']) === false) { |
| 45 | + $io->error('index_root not set in .runway-config.json', true); |
| 46 | + return; |
| 47 | + } |
| 48 | + |
| 49 | + $io->bold('Routes', true); |
| 50 | + |
| 51 | + $cwd = getcwd(); |
| 52 | + |
| 53 | + $index_root = $cwd . '/' . $this->config['index_root']; |
| 54 | + |
| 55 | + // This makes it so the framework doesn't actually execute |
| 56 | + Flight::map('start', function () { |
| 57 | + return; |
| 58 | + }); |
| 59 | + include($index_root); |
| 60 | + $routes = Flight::router()->getRoutes(); |
| 61 | + $arrayOfRoutes = []; |
| 62 | + foreach ($routes as $route) { |
| 63 | + if ($this->shouldAddRoute($route) === true) { |
| 64 | + $middlewares = []; |
| 65 | + if (!empty($route->middleware)) { |
| 66 | + try { |
| 67 | + $middlewares = array_map(function ($middleware) { |
| 68 | + $middleware_class_name = explode("\\", get_class($middleware)); |
| 69 | + return preg_match("/^class@anonymous/", end($middleware_class_name)) ? 'Anonymous' : end($middleware_class_name); |
| 70 | + }, $route->middleware); |
| 71 | + } catch (\TypeError $e) { |
| 72 | + $middlewares[] = 'Bad Middleware'; |
| 73 | + } finally { |
| 74 | + if(is_string($route->middleware) === true) { |
| 75 | + $middlewares[] = $route->middleware; |
| 76 | + } |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + $arrayOfRoutes[] = [ |
| 81 | + 'Pattern' => $route->pattern, |
| 82 | + 'Methods' => implode(', ', $route->methods), |
| 83 | + 'Alias' => $route->alias ?? '', |
| 84 | + 'Streamed' => $route->is_streamed ? 'Yes' : 'No', |
| 85 | + 'Middleware' => !empty($middlewares) ? implode(",", $middlewares) : '-' |
| 86 | + ]; |
| 87 | + } |
| 88 | + } |
| 89 | + $io->table($arrayOfRoutes, [ |
| 90 | + 'head' => 'boldGreen' |
| 91 | + ]); |
| 92 | + } |
| 93 | + |
| 94 | + /** |
| 95 | + * Whether or not to add the route based on the request |
| 96 | + * |
| 97 | + * @param Route $route Flight Route object |
| 98 | + * |
| 99 | + * @return boolean |
| 100 | + */ |
| 101 | + public function shouldAddRoute(Route $route) |
| 102 | + { |
| 103 | + $boolval = false; |
| 104 | + |
| 105 | + $showAll = !$this->get && !$this->post && !$this->put && !$this->delete && !$this->patch; |
| 106 | + if ($showAll === true) { |
| 107 | + $boolval = true; |
| 108 | + } else { |
| 109 | + $methods = [ 'GET', 'POST', 'PUT', 'DELETE', 'PATCH' ]; |
| 110 | + foreach ($methods as $method) { |
| 111 | + $lowercaseMethod = strtolower($method); |
| 112 | + if ( |
| 113 | + $this->{$lowercaseMethod} === true && |
| 114 | + ( |
| 115 | + $route->methods[0] === '*' || |
| 116 | + in_array($method, $route->methods, true) === true |
| 117 | + ) |
| 118 | + ) { |
| 119 | + $boolval = true; |
| 120 | + break; |
| 121 | + } |
| 122 | + } |
| 123 | + } |
| 124 | + return $boolval; |
| 125 | + } |
| 126 | +} |
0 commit comments