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