1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace Shopware\Psh\Listing; |
4
|
|
|
|
5
|
|
|
use Shopware\Psh\Config\ScriptsPath; |
6
|
|
|
use function array_filter; |
7
|
|
|
use function in_array; |
8
|
|
|
use function levenshtein; |
9
|
|
|
use function mb_strpos; |
10
|
|
|
use function pathinfo; |
11
|
|
|
use function scandir; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Load all scripts from all the supplied paths and create an array of scripts |
15
|
|
|
*/ |
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 |
76
|
|
|
{ |
77
|
|
|
$scripts = $this->getAllVisibleScripts(); |
78
|
|
|
|
79
|
|
|
return array_filter($scripts, static function ($key) use ($query) { |
80
|
|
|
return mb_strpos($key, $query) !== false || levenshtein($key, $query) < 3; |
81
|
|
|
}, ARRAY_FILTER_USE_KEY); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @param string[] $scriptNames |
86
|
|
|
* @return Script[] |
87
|
|
|
*/ |
88
|
|
|
public function findScriptsInOrder(array $scriptNames): array |
89
|
|
|
{ |
90
|
|
|
$scripts = []; |
91
|
|
|
foreach ($scriptNames as $scriptName) { |
92
|
|
|
$scripts[] = $this->findScriptByName($scriptName); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
return $scripts; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @throws ScriptNotFound |
100
|
|
|
*/ |
101
|
|
|
public function findScriptByName(string $scriptName): Script |
102
|
|
|
{ |
103
|
|
|
foreach ($this->getAllScripts() as $script) { |
104
|
|
|
if ($script->getName() === $scriptName) { |
105
|
|
|
return $script; |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
throw (new ScriptNotFound('Unable to find script named "' . $scriptName . '"'))->setScriptName($scriptName); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* @param $fileName |
114
|
|
|
*/ |
115
|
|
|
private function isValidScript(string $fileName): bool |
116
|
|
|
{ |
117
|
|
|
$extension = pathinfo($fileName, PATHINFO_EXTENSION); |
118
|
|
|
|
119
|
|
|
return in_array($extension, self::VALID_EXTENSIONS, true); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
private function testPathValidity(ScriptsPath $path): void |
123
|
|
|
{ |
124
|
|
|
if (!$path->isValid()) { |
125
|
|
|
throw new ScriptPathNotValid("The given script path: '{$path->getPath()}' is not a valid directory"); |
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
|