1 | <?php declare(strict_types=1); |
||
16 | class ScriptFinder |
||
17 | { |
||
18 | private const VALID_EXTENSIONS = [ |
||
19 | 'sh', |
||
20 | 'psh', |
||
21 | ]; |
||
22 | |||
23 | /** |
||
24 | * @var ScriptsPath[] |
||
25 | */ |
||
26 | private $scriptsPaths; |
||
27 | |||
28 | /** |
||
29 | * @var DescriptionReader |
||
30 | */ |
||
31 | private $scriptDescriptionReader; |
||
32 | |||
33 | /** |
||
34 | * @param ScriptsPath[] $scriptsPaths |
||
35 | */ |
||
36 | public function __construct(array $scriptsPaths, DescriptionReader $scriptDescriptionReader) |
||
37 | { |
||
38 | $this->scriptsPaths = $scriptsPaths; |
||
39 | $this->scriptDescriptionReader = $scriptDescriptionReader; |
||
40 | } |
||
41 | |||
42 | /** |
||
43 | * @throws ScriptPathNotValid |
||
44 | * @return Script[] |
||
45 | */ |
||
46 | public function getAllScripts(): array |
||
47 | { |
||
48 | $scripts = []; |
||
49 | |||
50 | foreach ($this->scriptsPaths as $path) { |
||
51 | $this->testPathValidity($path); |
||
52 | |||
53 | foreach (scandir($path->getPath(), SCANDIR_SORT_ASCENDING) as $fileName) { |
||
54 | if (!$this->isValidScript($fileName)) { |
||
55 | continue; |
||
56 | } |
||
57 | |||
58 | $description = $this->scriptDescriptionReader->read($path->getPath() . '/' . $fileName); |
||
59 | $newScript = new Script($path->getPath(), $fileName, $path->isHidden(), $path->getWorkingDirectory(), $path->getNamespace(), $description); |
||
60 | |||
61 | $scripts[$newScript->getName()] = $newScript; |
||
62 | } |
||
63 | } |
||
64 | |||
65 | return $scripts; |
||
66 | } |
||
67 | |||
68 | public function getAllVisibleScripts(): array |
||
69 | { |
||
70 | return array_filter($this->getAllScripts(), static function (Script $script): bool { |
||
71 | return !$script->isHidden(); |
||
72 | }); |
||
73 | } |
||
74 | |||
75 | public function findScriptsByPartialName(string $query): array |
||
83 | |||
84 | /** |
||
85 | * @param string[] $scriptNames |
||
86 | * @return Script[] |
||
87 | */ |
||
88 | public function findScriptsInOrder(array $scriptNames): array |
||
97 | |||
98 | /** |
||
99 | * @throws ScriptNotFound |
||
100 | */ |
||
101 | public function findScriptByName(string $scriptName): Script |
||
111 | |||
112 | /** |
||
113 | * @param $fileName |
||
114 | */ |
||
115 | private function isValidScript(string $fileName): bool |
||
121 | |||
122 | private function testPathValidity(ScriptsPath $path): void |
||
128 | } |
||
129 |