Passed
Pull Request — master (#49)
by Marco
02:55
created

LocateDependenciesViaComposer   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 115
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 6
dl 0
loc 115
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
B sourceLocatorFromAutoloadFiles() 0 26 2
A runInDirectory() 0 9 1
B __invoke() 0 40 1
A sourceLocatorFromAutoloadStatic() 0 15 1
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
use function array_map;
19
use function array_values;
20
use function assert;
21
use function chdir;
22
use function getcwd;
23
use function realpath;
24
use function reset;
25
26
final class LocateDependenciesViaComposer implements LocateDependencies
27
{
28
    /** @var Locator */
29
    private $astLocator;
30
31
    /** @var callable */
32
    private $makeComposerInstaller;
33
34
    public function __construct(
35
        callable $makeComposerInstaller,
36
        Locator $astLocator
37
    ) {
38
        // This is needed because the CWD of composer cannot be changed at runtime, but only at startup
39
        $this->makeComposerInstaller = $makeComposerInstaller;
40
        $this->astLocator            = $astLocator;
41
    }
42
43
    public function __invoke(string $installationPath) : SourceLocator
44
    {
45
        Assert::that($installationPath)->directory();
46
        Assert::that($installationPath . '/composer.json')->file();
47
48
        $this->runInDirectory(function () use ($installationPath) : void {
49
            $installer = ($this->makeComposerInstaller)($installationPath);
50
51
            Assert::that($installer)->isInstanceOf(Installer::class);
52
            assert($installer instanceof Installer);
53
54
            // Some defaults needed for this specific implementation:
55
            $installer->setDevMode(false);
56
            $installer->setDumpAutoloader(true);
57
            $installer->setRunScripts(false);
58
            $installer->setOptimizeAutoloader(true);
59
            $installer->setClassMapAuthoritative(true);
60
            $installer->setIgnorePlatformRequirements(true);
61
62
            $installer->run();
63
        }, $installationPath);
64
65
        $autoloadStatic = $installationPath . '/vendor/composer/autoload_static.php';
66
67
        Assert::that($autoloadStatic)->file();
68
69
        $autoloadMappingClasses = (new ClassReflector(new SingleFileSourceLocator(
70
            $autoloadStatic,
71
            $this->astLocator
72
        )))->getAllClasses();
73
74
        Assert::that($autoloadMappingClasses)->count(1);
75
76
        /** @var ReflectionClass $generatedAutoloadClass */
77
        $generatedAutoloadClass = reset($autoloadMappingClasses);
78
79
        return new AggregateSourceLocator([
80
            $this->sourceLocatorFromAutoloadStatic($generatedAutoloadClass),
81
            $this->sourceLocatorFromAutoloadFiles($generatedAutoloadClass),
82
            new PhpInternalSourceLocator($this->astLocator),
83
        ]);
84
    }
85
86
    private function sourceLocatorFromAutoloadStatic(ReflectionClass $autoloadStatic) : SourceLocator
87
    {
88
        $classMapProperty = $autoloadStatic->getProperty('classMap');
89
90
        Assert::that($classMapProperty)->notNull();
91
92
        assert($classMapProperty instanceof ReflectionProperty);
93
        $classMap = $classMapProperty->getDefaultValue();
94
95
        Assert::that($classMap)
96
              ->isArray()
97
              ->all()
98
              ->file();
99
100
        return new StaticClassMapSourceLocator($classMap, $this->astLocator);
101
    }
102
103
    private function sourceLocatorFromAutoloadFiles(ReflectionClass $autoloadStatic) : SourceLocator
104
    {
105
        $filesMapProperty = $autoloadStatic->getProperty('files');
106
107
        if (! $filesMapProperty) {
108
            return new AggregateSourceLocator();
109
        }
110
111
        Assert::that($filesMapProperty)->notNull();
112
113
        assert($filesMapProperty instanceof ReflectionProperty);
114
        $filesMap = $filesMapProperty->getDefaultValue();
115
116
        Assert::that($filesMap)
117
              ->isArray()
118
              ->all()
119
              ->file();
120
121
        return new AggregateSourceLocator(array_values(array_map(
122
            function (string $path) : SourceLocator {
123
                return new SingleFileSourceLocator(
124
                    realpath($path),
125
                    $this->astLocator
126
                );
127
            },
128
            $filesMap
129
        )));
130
    }
131
132
    private function runInDirectory(callable $callable, string $directoryOfExecution) : void
133
    {
134
        $originalDirectory = getcwd();
135
136
        try {
137
            chdir($directoryOfExecution);
138
            $callable();
139
        } finally {
140
            chdir($originalDirectory);
141
        }
142
    }
143
}
144