• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

codeigniter4 / CodeIgniter4 / 12673986434

08 Jan 2025 03:42PM UTC coverage: 84.455% (+0.001%) from 84.454%
12673986434

Pull #9385

github

web-flow
Merge 06e47f0ee into e475fd8fa
Pull Request #9385: refactor: Fix phpstan expr.resultUnused

20699 of 24509 relevant lines covered (84.45%)

190.57 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

97.75
/system/Router/AutoRouter.php
1
<?php
2

3
declare(strict_types=1);
4

5
/**
6
 * This file is part of CodeIgniter 4 framework.
7
 *
8
 * (c) CodeIgniter Foundation <admin@codeigniter.com>
9
 *
10
 * For the full copyright and license information, please view
11
 * the LICENSE file that was distributed with this source code.
12
 */
13

14
namespace CodeIgniter\Router;
15

16
use Closure;
17
use CodeIgniter\Exceptions\PageNotFoundException;
18
use CodeIgniter\HTTP\ResponseInterface;
19

20
/**
21
 * Router for Auto-Routing
22
 */
23
final class AutoRouter implements AutoRouterInterface
24
{
25
    /**
26
     * Sub-directory that contains the requested controller class.
27
     * Primarily used by 'autoRoute'.
28
     */
29
    private ?string $directory = null;
30

31
    public function __construct(
32
        /**
33
         * List of CLI routes that do not contain '*' routes.
34
         *
35
         * @var array<string, (Closure(mixed...): (ResponseInterface|string|void))|string> [routeKey => handler]
36
         */
37
        private readonly array $cliRoutes,
38
        /**
39
         * Default namespace for controllers.
40
         */
41
        private readonly string $defaultNamespace,
42
        /**
43
         * The name of the controller class.
44
         */
45
        private string $controller,
46
        /**
47
         * The name of the method to use.
48
         */
49
        private string $method,
50
        /**
51
         * Whether dashes in URI's should be converted
52
         * to underscores when determining method names.
53
         */
54
        private bool $translateURIDashes
55
    ) {
56
    }
29✔
57

58
    /**
59
     * Attempts to match a URI path against Controllers and directories
60
     * found in APPPATH/Controllers, to find a matching route.
61
     *
62
     * @param string $httpVerb HTTP verb like `GET`,`POST`
63
     *
64
     * @return array [directory_name, controller_name, controller_method, params]
65
     */
66
    public function getRoute(string $uri, string $httpVerb): array
67
    {
68
        $segments = explode('/', $uri);
27✔
69

70
        // WARNING: Directories get shifted out of the segments array.
71
        $segments = $this->scanControllers($segments);
27✔
72

73
        // If we don't have any segments left - use the default controller;
74
        // If not empty, then the first segment should be the controller
75
        if ($segments !== []) {
27✔
76
            $this->controller = ucfirst(array_shift($segments));
24✔
77
        }
78

79
        $controllerName = $this->controllerName();
27✔
80

81
        if (! $this->isValidSegment($controllerName)) {
27✔
82
            throw new PageNotFoundException($this->controller . ' is not a valid controller name');
3✔
83
        }
84

85
        // Use the method name if it exists.
86
        // If it doesn't, no biggie - the default method name
87
        // has already been set.
88
        if ($segments !== []) {
24✔
89
            $this->method = array_shift($segments) ?: $this->method;
14✔
90
        }
91

92
        // Prevent access to initController method
93
        if (strtolower($this->method) === 'initcontroller') {
24✔
94
            throw PageNotFoundException::forPageNotFound();
1✔
95
        }
96

97
        /** @var array $params An array of params to the controller method. */
98
        $params = [];
23✔
99

100
        if ($segments !== []) {
23✔
101
            $params = $segments;
4✔
102
        }
103

104
        // Ensure routes registered via $routes->cli() are not accessible via web.
105
        if ($httpVerb !== 'CLI') {
23✔
106
            $controller = '\\' . $this->defaultNamespace;
23✔
107

108
            $controller .= $this->directory !== null ? str_replace('/', '\\', $this->directory) : '';
23✔
109
            $controller .= $controllerName;
23✔
110

111
            $controller = strtolower($controller);
23✔
112
            $methodName = strtolower($this->methodName());
23✔
113

114
            foreach ($this->cliRoutes as $handler) {
23✔
115
                if (is_string($handler)) {
5✔
116
                    $handler = strtolower($handler);
5✔
117

118
                    // Like $routes->cli('hello/(:segment)', 'Home::$1')
119
                    if (str_contains($handler, '::$')) {
5✔
120
                        throw new PageNotFoundException(
1✔
121
                            'Cannot access CLI Route: ' . $uri
1✔
122
                        );
1✔
123
                    }
124

125
                    if (str_starts_with($handler, $controller . '::' . $methodName)) {
4✔
126
                        throw new PageNotFoundException(
2✔
127
                            'Cannot access CLI Route: ' . $uri
2✔
128
                        );
2✔
129
                    }
130

131
                    if ($handler === $controller) {
2✔
132
                        throw new PageNotFoundException(
2✔
133
                            'Cannot access CLI Route: ' . $uri
2✔
134
                        );
2✔
135
                    }
136
                }
137
            }
138
        }
139

140
        // Load the file so that it's available for CodeIgniter.
141
        $file = APPPATH . 'Controllers/' . $this->directory . $controllerName . '.php';
18✔
142

143
        if (! is_file($file)) {
18✔
144
            throw PageNotFoundException::forControllerNotFound($this->controller, $this->method);
×
145
        }
146

147
        include_once $file;
18✔
148

149
        // Ensure the controller stores the fully-qualified class name
150
        // We have to check for a length over 1, since by default it will be '\'
151
        if (! str_contains($this->controller, '\\') && strlen($this->defaultNamespace) > 1) {
18✔
152
            $this->controller = '\\' . ltrim(
4✔
153
                str_replace(
4✔
154
                    '/',
4✔
155
                    '\\',
4✔
156
                    $this->defaultNamespace . $this->directory . $controllerName
4✔
157
                ),
4✔
158
                '\\'
4✔
159
            );
4✔
160
        }
161

162
        return [$this->directory, $this->controllerName(), $this->methodName(), $params];
18✔
163
    }
164

165
    /**
166
     * Tells the system whether we should translate URI dashes or not
167
     * in the URI from a dash to an underscore.
168
     *
169
     * @deprecated This method should be removed.
170
     */
171
    public function setTranslateURIDashes(bool $val = false): self
172
    {
173
        $this->translateURIDashes = $val;
11✔
174

175
        return $this;
11✔
176
    }
177

178
    /**
179
     * Scans the controller directory, attempting to locate a controller matching the supplied uri $segments
180
     *
181
     * @param array $segments URI segments
182
     *
183
     * @return array returns an array of remaining uri segments that don't map onto a directory
184
     */
185
    private function scanControllers(array $segments): array
186
    {
187
        $segments = array_filter($segments, static fn ($segment): bool => $segment !== '');
27✔
188
        // numerically reindex the array, removing gaps
189
        $segments = array_values($segments);
27✔
190

191
        // if a prior directory value has been set, just return segments and get out of here
192
        if (isset($this->directory)) {
27✔
193
            return $segments;
1✔
194
        }
195

196
        // Loop through our segments and return as soon as a controller
197
        // is found or when such a directory doesn't exist
198
        $c = count($segments);
26✔
199

200
        while ($c-- > 0) {
26✔
201
            $segmentConvert = ucfirst(
25✔
202
                $this->translateURIDashes ? str_replace('-', '_', $segments[0]) : $segments[0]
25✔
203
            );
25✔
204
            // as soon as we encounter any segment that is not PSR-4 compliant, stop searching
205
            if (! $this->isValidSegment($segmentConvert)) {
25✔
206
                return $segments;
3✔
207
            }
208

209
            $test = APPPATH . 'Controllers/' . $this->directory . $segmentConvert;
22✔
210

211
            // as long as each segment is *not* a controller file but does match a directory, add it to $this->directory
212
            if (! is_file($test . '.php') && is_dir($test)) {
22✔
213
                $this->setDirectory($segmentConvert, true, false);
6✔
214
                array_shift($segments);
6✔
215

216
                continue;
6✔
217
            }
218

219
            return $segments;
20✔
220
        }
221

222
        // This means that all segments were actually directories
223
        return $segments;
4✔
224
    }
225

226
    /**
227
     * Returns true if the supplied $segment string represents a valid PSR-4 compliant namespace/directory segment
228
     *
229
     * regex comes from https://www.php.net/manual/en/language.variables.basics.php
230
     */
231
    private function isValidSegment(string $segment): bool
232
    {
233
        return (bool) preg_match('/^[a-zA-Z_\x80-\xff][a-zA-Z0-9_\x80-\xff]*$/', $segment);
29✔
234
    }
235

236
    /**
237
     * Sets the sub-directory that the controller is in.
238
     *
239
     * @param bool $validate if true, checks to make sure $dir consists of only PSR4 compliant segments
240
     *
241
     * @deprecated This method should be removed.
242
     *
243
     * @return void
244
     */
245
    public function setDirectory(?string $dir = null, bool $append = false, bool $validate = true)
246
    {
247
        if ((string) $dir === '') {
10✔
248
            $this->directory = null;
1✔
249

250
            return;
1✔
251
        }
252

253
        if ($validate) {
9✔
254
            $segments = explode('/', trim($dir, '/'));
3✔
255

256
            foreach ($segments as $segment) {
3✔
257
                if (! $this->isValidSegment($segment)) {
3✔
258
                    return;
1✔
259
                }
260
            }
261
        }
262

263
        if (! $append || ((string) $this->directory === '')) {
8✔
264
            $this->directory = trim($dir, '/') . '/';
8✔
265
        } else {
266
            $this->directory .= trim($dir, '/') . '/';
×
267
        }
268
    }
269

270
    /**
271
     * Returns the name of the sub-directory the controller is in,
272
     * if any. Relative to APPPATH.'Controllers'.
273
     *
274
     * @deprecated This method should be removed.
275
     */
276
    public function directory(): string
277
    {
278
        return ((string) $this->directory !== '') ? $this->directory : '';
8✔
279
    }
280

281
    private function controllerName(): string
282
    {
283
        return $this->translateURIDashes
27✔
284
            ? str_replace('-', '_', $this->controller)
11✔
285
            : $this->controller;
27✔
286
    }
287

288
    private function methodName(): string
289
    {
290
        return $this->translateURIDashes
23✔
291
            ? str_replace('-', '_', $this->method)
7✔
292
            : $this->method;
23✔
293
    }
294
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc