1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* This file is part of Symplify |
7
|
|
|
* Copyright (c) 2015 Tomas Votruba (http://tomasvotruba.cz). |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace Symplify\ControllerAutowire\HttpKernel\Controller; |
11
|
|
|
|
12
|
|
|
use Nette\Caching\Storages\DevNullStorage; |
13
|
|
|
use Nette\Loaders\RobotLoader; |
14
|
|
|
use Symplify\ControllerAutowire\Contract\HttpKernel\ControllerFinderInterface; |
15
|
|
|
|
16
|
|
|
final class ControllerFinder implements ControllerFinderInterface |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* @var string |
20
|
|
|
*/ |
21
|
|
|
private $namePart; |
22
|
|
|
|
23
|
5 |
|
public function __construct(string $namePart = 'Controller') |
24
|
|
|
{ |
25
|
5 |
|
$this->namePart = $namePart; |
26
|
5 |
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* {@inheritdoc} |
30
|
|
|
*/ |
31
|
5 |
|
public function findControllersInDirs(array $dirs) : array |
32
|
|
|
{ |
33
|
5 |
|
$robot = new RobotLoader(); |
34
|
5 |
|
$robot->setCacheStorage(new DevNullStorage()); |
35
|
5 |
|
foreach ($dirs as $dir) { |
36
|
5 |
|
$robot->addDirectory($dir); |
37
|
|
|
} |
38
|
5 |
|
$robot->ignoreDirs .= ', Tests'; |
39
|
5 |
|
$robot->acceptFiles = '*' . $this->namePart . '.php'; |
40
|
5 |
|
$robot->rebuild(); |
41
|
|
|
|
42
|
5 |
|
$controllerClasses = array_keys($robot->getIndexedClasses()); |
43
|
5 |
|
sort($controllerClasses); |
44
|
|
|
|
45
|
5 |
|
$controllerClasses = $this->prepareServiceKeys($controllerClasses); |
46
|
|
|
|
47
|
|
|
return $controllerClasses; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
private function prepareServiceKeys(array $controllerClasses) : array |
51
|
|
|
{ |
52
|
|
|
$controllerClassesWithKeys = []; |
53
|
|
|
foreach ($controllerClasses as $key => $controllerClass) { |
54
|
|
|
$key = strtr(strtolower($controllerClass), ['\\' => '.']); |
55
|
|
|
$controllerClassesWithKeys[$key] = $controllerClass; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
return $controllerClassesWithKeys; |
59
|
|
|
} |
60
|
|
|
} |
61
|
|
|
|