Passed
Pull Request — main (#27)
by Dimitri
05:06
created

MiddlewareCollector::createRouter()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 1
dl 0
loc 9
ccs 3
cts 3
cp 1
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * This file is part of Blitz PHP framework.
5
 *
6
 * (c) 2022 Dimitri Sitchet Tomkeu <[email protected]>
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 BlitzPHP\Cli\Commands\Routes;
13
14
use BlitzPHP\Container\Services;
15
use BlitzPHP\Http\Request;
16
use BlitzPHP\Router\Router;
17
18
/**
19
 * Collecte les middlewares pour une route
20
 */
21
final class MiddlewareCollector
22
{
23
    /**
24
     * @param bool $resetRoutes Indique s'il faut réinitialiser les routes définies. S'il est défini sur true, les middlewares de routage sont introuvables.
25
     */
26
    public function __construct(private readonly bool $resetRoutes = false)
27
    {
28
    }
29
30
    /**
31
     * @param string $method Methode HTTP
32
     * @param string $uri    Chemin URI pour trouver des middlewares
33
     *
34
     * @return array|array{before: list<string>, after: list<string>} tableau d'alias de middleware ou de nom de classe
0 ignored issues
show
Documentation Bug introduced by
The doc comment array|array{before: list...>, after: list<string>} at position 6 could not be parsed: Expected '}' at position 6, but found 'list'.
Loading history...
35
     */
36
    public function get(string $method, string $uri): array
37
    {
38
        if ($method === 'cli') {
39 2
            return [];
40
        }
41
42 2
        $request = Services::request(false)->withMethod($method);
43
44 2
        $router = $this->createRouter($request);
45
46 2
        $finder = new MiddlewareFinder($router);
47
48 2
        return $finder->find($uri);
49
    }
50
51
    private function createRouter(Request $request): Router
52
    {
53 2
        $routes = Services::routes();
54
55
        if ($this->resetRoutes) {
56 2
            $routes->resetRoutes();
57
        }
58
59 2
        return new Router($routes, $request);
60
    }
61
}
62