Passed
Push — main ( dd4eea...91afa7 )
by Dimitri
03:22
created

ControllerFinder   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 21
c 0
b 0
f 0
dl 0
loc 50
rs 10
wmc 7

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A find() 0 34 6
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\Autoloader\Locator;
15
use BlitzPHP\Loader\Services;
16
17
/**
18
 * Recherche tous les contrôleurs dans un namespace pour la liste des routes automatiques.
19
 */
20
final class ControllerFinder
21
{
22
    private Locator $locator;
23
24
    /**
25
     * @param string $namespace Namespace dans lequel on recherche
26
     */
27
    public function __construct(private string $namespace)
28
    {
29
        $this->locator   = Services::locator();
30
    }
31
32
    /**
33
     * @return string[]
34
     * @phpstan-return class-string[]
35
     */
36
    public function find(): array
37
    {
38
        $nsArray = explode('\\', trim($this->namespace, '\\'));
39
        $count   = count($nsArray);
40
        $ns      = '';
41
        $files   = [];
42
43
        for ($i = 0; $i < $count; $i++) {
44
            $ns .= '\\' . array_shift($nsArray);
45
            $path = implode('\\', $nsArray);
46
47
            $files = $this->locator->listNamespaceFiles($ns, $path);
48
49
            if ($files !== []) {
50
                break;
51
            }
52
        }
53
54
        $classes = [];
55
56
        foreach ($files as $file) {
57
            if (\is_file($file)) {
58
                $classnameOrEmpty = $this->locator->getClassname($file);
59
60
                if ($classnameOrEmpty !== '') {
61
                    /** @phpstan-var class-string $classname */
62
                    $classname = $classnameOrEmpty;
63
64
                    $classes[] = $classname;
65
                }
66
            }
67
        }
68
69
        return $classes;
70
    }
71
}
72