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