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