|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * This file is part of CodeIgniter 4 framework. |
| 5 | + * |
| 6 | + * (c) CodeIgniter Foundation <admin@codeigniter.com> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view |
| 9 | + * the LICENSE file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +namespace CodeIgniter\Commands\Utilities\Routes\AutoRouterImproved; |
| 13 | + |
| 14 | +use CodeIgniter\Commands\Utilities\Routes\ControllerFinder; |
| 15 | +use CodeIgniter\Commands\Utilities\Routes\FilterCollector; |
| 16 | + |
| 17 | +/** |
| 18 | + * Collects data for Auto Routing Improved. |
| 19 | + */ |
| 20 | +final class AutoRouteCollector |
| 21 | +{ |
| 22 | + /** |
| 23 | + * @var string namespace to search |
| 24 | + */ |
| 25 | + private string $namespace; |
| 26 | + |
| 27 | + private string $defaultController; |
| 28 | + private string $defaultMethod; |
| 29 | + private array $httpMethods; |
| 30 | + |
| 31 | + /** |
| 32 | + * List of controllers in Defined Routes that should not be accessed via Auto-Routing. |
| 33 | + * |
| 34 | + * @var class-string[] |
| 35 | + */ |
| 36 | + private array $protectedControllers; |
| 37 | + |
| 38 | + /** |
| 39 | + * @param string $namespace namespace to search |
| 40 | + */ |
| 41 | + public function __construct( |
| 42 | + string $namespace, |
| 43 | + string $defaultController, |
| 44 | + string $defaultMethod, |
| 45 | + array $httpMethods, |
| 46 | + array $protectedControllers |
| 47 | + ) { |
| 48 | + $this->namespace = $namespace; |
| 49 | + $this->defaultController = $defaultController; |
| 50 | + $this->defaultMethod = $defaultMethod; |
| 51 | + $this->httpMethods = $httpMethods; |
| 52 | + $this->protectedControllers = $protectedControllers; |
| 53 | + } |
| 54 | + |
| 55 | + /** |
| 56 | + * @return array<int, array<int, string>> |
| 57 | + * @phpstan-return list<list<string>> |
| 58 | + */ |
| 59 | + public function get(): array |
| 60 | + { |
| 61 | + $finder = new ControllerFinder($this->namespace); |
| 62 | + $reader = new ControllerMethodReader($this->namespace, $this->httpMethods); |
| 63 | + |
| 64 | + $tbody = []; |
| 65 | + |
| 66 | + foreach ($finder->find() as $class) { |
| 67 | + // Exclude controllers in Defined Routes. |
| 68 | + if (in_array($class, $this->protectedControllers, true)) { |
| 69 | + continue; |
| 70 | + } |
| 71 | + |
| 72 | + $routes = $reader->read( |
| 73 | + $class, |
| 74 | + $this->defaultController, |
| 75 | + $this->defaultMethod |
| 76 | + ); |
| 77 | + |
| 78 | + if ($routes === []) { |
| 79 | + continue; |
| 80 | + } |
| 81 | + |
| 82 | + $routes = $this->addFilters($routes); |
| 83 | + |
| 84 | + foreach ($routes as $item) { |
| 85 | + $tbody[] = [ |
| 86 | + strtoupper($item['method']) . '(auto)', |
| 87 | + $item['route'] . $item['route_params'], |
| 88 | + $item['handler'], |
| 89 | + $item['before'], |
| 90 | + $item['after'], |
| 91 | + ]; |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + return $tbody; |
| 96 | + } |
| 97 | + |
| 98 | + private function addFilters($routes) |
| 99 | + { |
| 100 | + $filterCollector = new FilterCollector(true); |
| 101 | + |
| 102 | + foreach ($routes as &$route) { |
| 103 | + // Search filters for the URI with all params |
| 104 | + $sampleUri = $this->generateSampleUri($route); |
| 105 | + $filtersLongest = $filterCollector->get($route['method'], $route['route'] . $sampleUri); |
| 106 | + |
| 107 | + // Search filters for the URI without optional params |
| 108 | + $sampleUri = $this->generateSampleUri($route, false); |
| 109 | + $filtersShortest = $filterCollector->get($route['method'], $route['route'] . $sampleUri); |
| 110 | + |
| 111 | + // Get common array elements |
| 112 | + $filters['before'] = array_intersect($filtersLongest['before'], $filtersShortest['before']); |
| 113 | + $filters['after'] = array_intersect($filtersLongest['after'], $filtersShortest['after']); |
| 114 | + |
| 115 | + $route['before'] = implode(' ', array_map('class_basename', $filters['before'])); |
| 116 | + $route['after'] = implode(' ', array_map('class_basename', $filters['after'])); |
| 117 | + } |
| 118 | + |
| 119 | + return $routes; |
| 120 | + } |
| 121 | + |
| 122 | + private function generateSampleUri(array $route, bool $longest = true): string |
| 123 | + { |
| 124 | + $sampleUri = ''; |
| 125 | + |
| 126 | + if (isset($route['params'])) { |
| 127 | + $i = 1; |
| 128 | + |
| 129 | + foreach ($route['params'] as $required) { |
| 130 | + if ($longest && ! $required) { |
| 131 | + $sampleUri .= '/' . $i++; |
| 132 | + } |
| 133 | + } |
| 134 | + } |
| 135 | + |
| 136 | + return $sampleUri; |
| 137 | + } |
| 138 | +} |
0 commit comments