1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Roave\ApiCompare\LocateDependencies; |
6
|
|
|
|
7
|
|
|
use Assert\Assert; |
8
|
|
|
use Composer\Installer; |
9
|
|
|
use Roave\ApiCompare\SourceLocator\StaticClassMapSourceLocator; |
10
|
|
|
use Roave\BetterReflection\Reflection\ReflectionClass; |
11
|
|
|
use Roave\BetterReflection\Reflector\ClassReflector; |
12
|
|
|
use Roave\BetterReflection\SourceLocator\Ast\Locator; |
13
|
|
|
use Roave\BetterReflection\SourceLocator\Type\AggregateSourceLocator; |
14
|
|
|
use Roave\BetterReflection\SourceLocator\Type\PhpInternalSourceLocator; |
15
|
|
|
use Roave\BetterReflection\SourceLocator\Type\SingleFileSourceLocator; |
16
|
|
|
use Roave\BetterReflection\SourceLocator\Type\SourceLocator; |
17
|
|
|
|
18
|
|
|
final class LocateDependenciesViaComposer implements LocateDependencies |
19
|
|
|
{ |
20
|
|
|
/** @var Installer */ |
21
|
|
|
private $installer; |
22
|
|
|
|
23
|
|
|
/** @var Locator */ |
24
|
|
|
private $astLocator; |
25
|
|
|
|
26
|
|
|
public function __construct(Installer $installer, Locator $astLocator) |
27
|
|
|
{ |
28
|
|
|
$this->installer = $installer; |
29
|
|
|
$this->astLocator = $astLocator; |
30
|
|
|
|
31
|
|
|
// Some defaults needed for this specific implementation: |
32
|
|
|
$this->installer->setDevMode(false); |
33
|
|
|
$this->installer->setDumpAutoloader(true); |
34
|
|
|
$this->installer->setRunScripts(false); |
35
|
|
|
$this->installer->setOptimizeAutoloader(true); |
36
|
|
|
$this->installer->setClassMapAuthoritative(true); |
37
|
|
|
$this->installer->setIgnorePlatformRequirements(true); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
public function __invoke(string $installationPath) : SourceLocator |
41
|
|
|
{ |
42
|
|
|
Assert::that($installationPath)->directory(); |
43
|
|
|
Assert::that($installationPath . '/composer.json')->file(); |
44
|
|
|
|
45
|
|
|
$this->runInDirectory(function () { |
46
|
|
|
$this->installer->run(); |
47
|
|
|
}, $installationPath); |
48
|
|
|
|
49
|
|
|
$autoloadStatic = $installationPath . '/vendor/composer/autoload_static.php'; |
50
|
|
|
|
51
|
|
|
Assert::that($autoloadStatic)->file(); |
52
|
|
|
|
53
|
|
|
$autoloadMappingClasses = (new ClassReflector(new SingleFileSourceLocator( |
54
|
|
|
$autoloadStatic, |
55
|
|
|
$this->astLocator |
56
|
|
|
)))->getAllClasses(); |
57
|
|
|
|
58
|
|
|
Assert::that($autoloadMappingClasses)->count(1); |
59
|
|
|
|
60
|
|
|
/** @var ReflectionClass $generatedAutoloadClass */ |
61
|
|
|
$generatedAutoloadClass = reset($autoloadMappingClasses); |
62
|
|
|
|
63
|
|
|
return new AggregateSourceLocator([ |
64
|
|
|
$this->sourceLocatorFromAutoloadStatic($generatedAutoloadClass), |
65
|
|
|
$this->sourceLocatorFromAutoloadFiles($generatedAutoloadClass), |
66
|
|
|
new PhpInternalSourceLocator($this->astLocator), |
67
|
|
|
]); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
private function sourceLocatorFromAutoloadStatic(ReflectionClass $autoloadStatic) : SourceLocator |
71
|
|
|
{ |
72
|
|
|
$classMapProperty = $autoloadStatic->getProperty('classMap'); |
73
|
|
|
|
74
|
|
|
Assert::that($classMapProperty)->notNull(); |
75
|
|
|
|
76
|
|
|
$classMap = $classMapProperty->getDefaultValue(); |
77
|
|
|
|
78
|
|
|
Assert::that($classMap) |
79
|
|
|
->isArray() |
80
|
|
|
->all() |
81
|
|
|
->file(); |
82
|
|
|
|
83
|
|
|
return new StaticClassMapSourceLocator($classMap, $this->astLocator); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
private function sourceLocatorFromAutoloadFiles(ReflectionClass $autoloadStatic) : SourceLocator |
87
|
|
|
{ |
88
|
|
|
$filesMapProperty = $autoloadStatic->getProperty('files'); |
89
|
|
|
|
90
|
|
|
Assert::that($filesMapProperty)->notNull(); |
91
|
|
|
|
92
|
|
|
$filesMap = $filesMapProperty->getDefaultValue(); |
93
|
|
|
|
94
|
|
|
Assert::that($filesMap) |
95
|
|
|
->isArray() |
96
|
|
|
->all() |
97
|
|
|
->file(); |
98
|
|
|
|
99
|
|
|
return new AggregateSourceLocator(array_values(array_map( |
100
|
|
|
function (string $path) : SourceLocator { |
101
|
|
|
return new SingleFileSourceLocator( |
102
|
|
|
realpath($path), |
103
|
|
|
$this->astLocator |
104
|
|
|
); |
105
|
|
|
}, |
106
|
|
|
$filesMap |
107
|
|
|
))); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
private function runInDirectory(callable $callable, string $directoryOfExecution) : void |
111
|
|
|
{ |
112
|
|
|
$originalDirectory = getcwd(); |
113
|
|
|
|
114
|
|
|
try { |
115
|
|
|
chdir($directoryOfExecution); |
116
|
|
|
$callable(); |
117
|
|
|
} finally { |
118
|
|
|
chdir($originalDirectory); |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
|