Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
17.65% |
18 / 102 |
|
16.67% |
1 / 6 |
CRAP | |
0.00% |
0 / 1 |
DumpAutoload | |
17.65% |
18 / 102 |
|
16.67% |
1 / 6 |
65.85 | |
0.00% |
0 / 1 |
__construct | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
1 | |||
generatedPrefixedAutoloader | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
2 | |||
generatedMainAutoloader | |
0.00% |
0 / 42 |
|
0.00% |
0 / 1 |
12 | |||
createInstalledVersionsFiles | |
92.86% |
13 / 14 |
|
0.00% |
0 / 1 |
2.00 | |||
prefixNewAutoloader | |
0.00% |
0 / 35 |
|
0.00% |
0 / 1 |
2 | |||
getSuffix | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
6 |
1 | <?php |
2 | |
3 | namespace BrianHenryIE\Strauss\Pipeline\Autoload; |
4 | |
5 | use BrianHenryIE\Strauss\Files\File; |
6 | use BrianHenryIE\Strauss\Config\AutoloadConfigInterface; |
7 | use BrianHenryIE\Strauss\Helpers\FileSystem; |
8 | use BrianHenryIE\Strauss\Pipeline\FileEnumerator; |
9 | use BrianHenryIE\Strauss\Pipeline\Prefixer; |
10 | use BrianHenryIE\Strauss\Types\DiscoveredSymbols; |
11 | use BrianHenryIE\Strauss\Types\NamespaceSymbol; |
12 | use Composer\Autoload\AutoloadGenerator; |
13 | use Composer\Config; |
14 | use Composer\Factory; |
15 | use Composer\IO\NullIO; |
16 | use Composer\Json\JsonFile; |
17 | use Composer\Repository\InstalledFilesystemRepository; |
18 | use Psr\Log\LoggerAwareTrait; |
19 | use Psr\Log\LoggerInterface; |
20 | use Psr\Log\NullLogger; |
21 | |
22 | class DumpAutoload |
23 | { |
24 | use LoggerAwareTrait; |
25 | |
26 | protected AutoloadConfigInterface $config; |
27 | |
28 | protected FileSystem $filesystem; |
29 | |
30 | protected Prefixer $projectReplace; |
31 | |
32 | protected FileEnumerator $fileEnumerator; |
33 | |
34 | public function __construct( |
35 | AutoloadConfigInterface $config, |
36 | Filesystem $filesystem, |
37 | LoggerInterface $logger, |
38 | Prefixer $projectReplace, |
39 | FileEnumerator $fileEnumerator |
40 | ) { |
41 | $this->config = $config; |
42 | $this->filesystem = $filesystem; |
43 | $this->setLogger($logger ?? new NullLogger()); |
44 | |
45 | $this->projectReplace = $projectReplace; |
46 | |
47 | $this->fileEnumerator = $fileEnumerator; |
48 | } |
49 | |
50 | /** |
51 | * Create `autoload.php` and the `vendor-prefixed/composer` directory. |
52 | */ |
53 | public function generatedPrefixedAutoloader(): void |
54 | { |
55 | $this->generatedMainAutoloader(); |
56 | |
57 | $this->createInstalledVersionsFiles(); |
58 | |
59 | $this->prefixNewAutoloader(); |
60 | } |
61 | |
62 | /** |
63 | * Uses `vendor/composer/installed.json` to output autoload files to `vendor-prefixed/composer`. |
64 | */ |
65 | protected function generatedMainAutoloader(): void |
66 | { |
67 | /** |
68 | * Unfortunately, `::dump()` creates the target directories if they don't exist, even though it otherwise respects `::setDryRun()`. |
69 | * |
70 | * {@see https://github.com/composer/composer/pull/12396} might fix this. |
71 | */ |
72 | if ($this->config->isDryRun()) { |
73 | return; |
74 | } |
75 | |
76 | $relativeTargetDir = $this->filesystem->getRelativePath( |
77 | $this->config->getProjectDirectory(), |
78 | $this->config->getTargetDirectory() |
79 | ); |
80 | |
81 | $defaultVendorDirBefore = Config::$defaultConfig['vendor-dir']; |
82 | Config::$defaultConfig['vendor-dir'] = $relativeTargetDir; |
83 | |
84 | $projectComposerJson = new JsonFile($this->config->getProjectDirectory() . 'composer.json'); |
85 | $projectComposerJsonArray = $projectComposerJson->read(); |
86 | if (isset($projectComposerJsonArray['config'], $projectComposerJsonArray['config']['vendor-dir'])) { |
87 | $projectComposerJsonArray['config']['vendor-dir'] = $relativeTargetDir; |
88 | } |
89 | |
90 | $composer = Factory::create(new NullIO(), $projectComposerJsonArray); |
91 | $installationManager = $composer->getInstallationManager(); |
92 | $package = $composer->getPackage(); |
93 | |
94 | /** |
95 | * Cannot use `$composer->getConfig()`, need to create a new one so the vendor-dir is correct. |
96 | */ |
97 | $config = new \Composer\Config(false, $this->config->getProjectDirectory()); |
98 | |
99 | $config->merge([ |
100 | 'config' => $projectComposerJsonArray['config'] ?? [] |
101 | ]); |
102 | |
103 | $generator = new ComposerAutoloadGenerator( |
104 | $this->config->getNamespacePrefix(), |
105 | $composer->getEventDispatcher() |
106 | ); |
107 | |
108 | $generator->setDryRun($this->config->isDryRun()); |
109 | $generator->setClassMapAuthoritative(true); |
110 | $generator->setRunScripts(false); |
111 | // $generator->setApcu($apcu, $apcuPrefix); |
112 | // $generator->setPlatformRequirementFilter($this->getPlatformRequirementFilter($input)); |
113 | $optimize = true; // $input->getOption('optimize') || $config->get('optimize-autoloader'); |
114 | $generator->setDevMode(false); |
115 | |
116 | $localRepo = new InstalledFilesystemRepository(new JsonFile($this->config->getTargetDirectory() . 'composer/installed.json')); |
117 | |
118 | $strictAmbiguous = false; // $input->getOption('strict-ambiguous') |
119 | |
120 | // This will output the autoload_static.php etc. files to `vendor-prefixed/composer`. |
121 | $generator->dump( |
122 | $config, |
123 | $localRepo, |
124 | $package, |
125 | $installationManager, |
126 | 'composer', |
127 | $optimize, |
128 | $this->getSuffix(), |
129 | $composer->getLocker(), |
130 | $strictAmbiguous |
131 | ); |
132 | |
133 | /** |
134 | * Tests fail if this is absent. |
135 | * |
136 | * Arguably this should be in ::setUp() and tearDown() in the test classes, but if other tools run after Strauss |
137 | * then they might expect it to be unmodified. |
138 | */ |
139 | Config::$defaultConfig['vendor-dir'] = $defaultVendorDirBefore; |
140 | } |
141 | |
142 | /** |
143 | * Create `InstalledVersions.php` and `installed.php`. |
144 | * |
145 | * This file is copied in all Composer installations. |
146 | * It is added always in `ComposerAutoloadGenerator::dump()`, called above. |
147 | * If the file does not exist, its entry in the classmap will not be prefixed and will cause autoloading issues for the real class. |
148 | * |
149 | * The accompanying `installed.php` is unique per install. Copy it and filter its packages to the packages that was copied. |
150 | */ |
151 | protected function createInstalledVersionsFiles(): void |
152 | { |
153 | if ($this->config->getVendorDirectory() === $this->config->getTargetDirectory()) { |
154 | return; |
155 | } |
156 | |
157 | $this->filesystem->copy($this->config->getVendorDirectory() . '/composer/InstalledVersions.php', $this->config->getTargetDirectory() . 'composer/InstalledVersions.php'); |
158 | |
159 | // This is just `<?php return array(...);` |
160 | $installedPhpString = $this->filesystem->read($this->config->getVendorDirectory() . '/composer/installed.php'); |
161 | $installed = eval(str_replace('<?php', '', $installedPhpString)); |
162 | |
163 | $targetPackages = $this->config->getPackagesToCopy(); |
164 | $targetPackagesNames = array_keys($targetPackages); |
165 | |
166 | $installed['versions'] = array_filter($installed['versions'], function ($packageName) use ($targetPackagesNames) { |
167 | return in_array($packageName, $targetPackagesNames); |
168 | }, ARRAY_FILTER_USE_KEY); |
169 | |
170 | $installedArrayString = var_export($installed, true); |
171 | |
172 | $newInstalledPhpString = "<?php return $installedArrayString;"; |
173 | |
174 | // Update `__DIR__` which was evaluated during the `include`/`eval`. |
175 | $newInstalledPhpString = preg_replace('/(\'install_path\' => )(.*)(\/\.\..*)/', "$1__DIR__ . '$3", $newInstalledPhpString); |
176 | |
177 | $this->filesystem->write($this->config->getTargetDirectory() . '/composer/installed.php', $newInstalledPhpString); |
178 | } |
179 | |
180 | protected function prefixNewAutoloader(): void |
181 | { |
182 | $this->logger->debug('Prefixing the new Composer autoloader.'); |
183 | |
184 | $projectFiles = $this->fileEnumerator->compileFileListForPaths([ |
185 | $this->config->getTargetDirectory() . 'composer', |
186 | ]); |
187 | |
188 | $phpFiles = array_filter( |
189 | $projectFiles->getFiles(), |
190 | fn($file) => $file->isPhpFile() |
191 | ); |
192 | |
193 | $phpFilesAbsolutePaths = array_map( |
194 | fn($file) => $file->getSourcePath(), |
195 | $phpFiles |
196 | ); |
197 | |
198 | $sourceFile = new File(__DIR__); |
199 | $composerAutoloadNamespaceSymbol = new NamespaceSymbol( |
200 | 'Composer\\Autoload', |
201 | $sourceFile |
202 | ); |
203 | $composerAutoloadNamespaceSymbol->setReplacement( |
204 | $this->config->getNamespacePrefix() . '\\Composer\\Autoload' |
205 | ); |
206 | $composerNamespaceSymbol = new NamespaceSymbol( |
207 | 'Composer', |
208 | $sourceFile |
209 | ); |
210 | $composerNamespaceSymbol->setReplacement( |
211 | $this->config->getNamespacePrefix() . '\\Composer' |
212 | ); |
213 | |
214 | $discoveredSymbols = new DiscoveredSymbols(); |
215 | $discoveredSymbols->add( |
216 | $composerNamespaceSymbol |
217 | ); |
218 | $discoveredSymbols->add( |
219 | $composerAutoloadNamespaceSymbol |
220 | ); |
221 | |
222 | $this->projectReplace->replaceInProjectFiles($discoveredSymbols, $phpFilesAbsolutePaths); |
223 | } |
224 | |
225 | /** |
226 | * If there is an existing autoloader, it will use the same suffix. If there is not, it pulls the suffix from |
227 | * {Composer::getLocker()} and clashes with the existing autoloader. |
228 | * |
229 | * @see AutoloadGenerator::dump() 412:431 |
230 | * @see https://github.com/composer/composer/blob/ae208dc1e182bd45d99fcecb956501da212454a1/src/Composer/Autoload/AutoloadGenerator.php#L429 |
231 | */ |
232 | protected function getSuffix(): ?string |
233 | { |
234 | return !$this->filesystem->fileExists($this->config->getTargetDirectory() . 'autoload.php') |
235 | ? bin2hex(random_bytes(16)) |
236 | : null; |
237 | } |
238 | } |