|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace ComposerRequireChecker\FileLocator; |
|
4
|
|
|
|
|
5
|
|
|
use Generator; |
|
6
|
|
|
|
|
7
|
|
|
final class LocateComposerPackageSourceFiles |
|
8
|
|
|
{ |
|
9
|
8 |
|
public function __invoke(string $composerJsonPath) : Generator |
|
10
|
|
|
{ |
|
11
|
8 |
|
$packageDir = dirname($composerJsonPath); |
|
12
|
8 |
|
$composerData = json_decode(file_get_contents($composerJsonPath), true); |
|
13
|
|
|
|
|
14
|
8 |
|
yield from $this->locateFilesInClassmapDefinitions( |
|
15
|
8 |
|
$this->getFilePaths($composerData['autoload']['classmap'] ?? [], $packageDir) |
|
16
|
|
|
); |
|
17
|
8 |
|
yield from $this->locateFilesInFilesInFilesDefinitions( |
|
18
|
8 |
|
$this->getFilePaths($composerData['autoload']['files'] ?? [], $packageDir) |
|
19
|
|
|
); |
|
20
|
8 |
|
yield from $this->locateFilesInPsr0Definitions( |
|
21
|
8 |
|
$this->getFilePaths($composerData['autoload']['psr-0'] ?? [], $packageDir) |
|
22
|
|
|
); |
|
23
|
8 |
|
yield from $this->locateFilesInPsr4Definitions( |
|
24
|
8 |
|
$this->getFilePaths($composerData['autoload']['psr-4'] ?? [], $packageDir) |
|
25
|
|
|
); |
|
26
|
8 |
|
} |
|
27
|
|
|
|
|
28
|
8 |
|
private function getFilePaths(array $sourceDirs, string $packageDir) : array |
|
29
|
|
|
{ |
|
30
|
8 |
|
$flattened = array_reduce( |
|
31
|
8 |
|
$sourceDirs, |
|
32
|
|
|
function (array $sourceDirs, $sourceDir) { |
|
33
|
8 |
|
return array_merge($sourceDirs, (array)$sourceDir); |
|
34
|
8 |
|
}, |
|
35
|
8 |
|
[] |
|
36
|
|
|
); |
|
37
|
8 |
|
return array_values(array_map( |
|
38
|
8 |
|
function (string $sourceDir) use ($packageDir) { |
|
39
|
8 |
|
return $this->normalizePath($packageDir . '/' . ltrim($sourceDir, '/')); |
|
40
|
8 |
|
}, |
|
41
|
8 |
|
$flattened |
|
42
|
|
|
)); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
8 |
|
private function normalizePath(string $path) : string |
|
46
|
|
|
{ |
|
47
|
8 |
|
return str_replace('\\', '/', $path); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
8 |
|
private function locateFilesInPsr0Definitions(array $locations) : Generator |
|
51
|
|
|
{ |
|
52
|
8 |
|
yield from $this->locateFilesInFilesInFilesDefinitions($locations); |
|
53
|
8 |
|
} |
|
54
|
|
|
|
|
55
|
8 |
|
private function locateFilesInPsr4Definitions(array $locations) : Generator |
|
56
|
|
|
{ |
|
57
|
8 |
|
yield from $this->locateFilesInFilesInFilesDefinitions($locations); |
|
58
|
8 |
|
} |
|
59
|
|
|
|
|
60
|
8 |
|
private function locateFilesInClassmapDefinitions(array $locations) : Generator |
|
61
|
|
|
{ |
|
62
|
8 |
|
yield from $this->locateFilesInFilesInFilesDefinitions($locations); |
|
63
|
8 |
|
} |
|
64
|
|
|
|
|
65
|
8 |
|
private function locateFilesInFilesInFilesDefinitions(array $locations) : Generator |
|
66
|
|
|
{ |
|
67
|
8 |
|
foreach ($locations as $location) { |
|
68
|
8 |
|
if (is_file($location)) { |
|
69
|
2 |
|
yield $location; |
|
70
|
6 |
|
} elseif (is_dir($location)) { |
|
71
|
8 |
|
yield from $this->extractFilesFromDirectory($location); |
|
72
|
|
|
} |
|
73
|
|
|
} |
|
74
|
8 |
|
} |
|
75
|
|
|
|
|
76
|
6 |
|
private function extractFilesFromDirectory(string $directory) : Generator |
|
77
|
|
|
{ |
|
78
|
6 |
|
yield from (new LocateAllFilesByExtension())->__invoke(new \ArrayIterator([$directory]), '.php'); |
|
79
|
6 |
|
} |
|
80
|
|
|
} |
|
81
|
|
|
|