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