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