Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
18.26% |
21 / 115 |
|
28.57% |
2 / 7 |
CRAP | |
0.00% |
0 / 1 |
| DumpAutoload | |
18.26% |
21 / 115 |
|
28.57% |
2 / 7 |
174.83 | |
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 / 50 |
|
0.00% |
0 / 1 |
56 | |||
| isOptimizeAutoloaderEnabled | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 | |||
| createInstalledVersionsFiles | |
92.86% |
13 / 14 |
|
0.00% |
0 / 1 |
2.00 | |||
| prefixNewAutoloader | |
0.00% |
0 / 37 |
|
0.00% |
0 / 1 |
6 | |||
| getSuffix | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
6 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace BrianHenryIE\Strauss\Pipeline\Autoload; |
| 4 | |
| 5 | use BrianHenryIE\Strauss\Composer\ComposerPackage; |
| 6 | use BrianHenryIE\Strauss\Files\File; |
| 7 | use BrianHenryIE\Strauss\Config\AutoloadConfigInterface; |
| 8 | use BrianHenryIE\Strauss\Config\OptimizeAutoloaderConfigInterface; |
| 9 | use BrianHenryIE\Strauss\Helpers\FileSystem; |
| 10 | use BrianHenryIE\Strauss\Pipeline\FileEnumerator; |
| 11 | use BrianHenryIE\Strauss\Pipeline\Prefixer; |
| 12 | use BrianHenryIE\Strauss\Types\DiscoveredSymbols; |
| 13 | use BrianHenryIE\Strauss\Types\NamespaceSymbol; |
| 14 | use Composer\Autoload\AutoloadGenerator; |
| 15 | use Composer\Config; |
| 16 | use Composer\Factory; |
| 17 | use Composer\IO\NullIO; |
| 18 | use Composer\Json\JsonFile; |
| 19 | use Composer\Repository\InstalledFilesystemRepository; |
| 20 | use League\Flysystem\FilesystemException; |
| 21 | use Psr\Log\LoggerAwareTrait; |
| 22 | use Psr\Log\LoggerInterface; |
| 23 | use Seld\JsonLint\ParsingException; |
| 24 | |
| 25 | /** |
| 26 | * @phpstan-import-type ComposerJsonArray from ComposerPackage |
| 27 | */ |
| 28 | class DumpAutoload |
| 29 | { |
| 30 | use LoggerAwareTrait; |
| 31 | |
| 32 | protected AutoloadConfigInterface $config; |
| 33 | |
| 34 | protected FileSystem $filesystem; |
| 35 | |
| 36 | protected Prefixer $projectReplace; |
| 37 | |
| 38 | protected FileEnumerator $fileEnumerator; |
| 39 | |
| 40 | public function __construct( |
| 41 | AutoloadConfigInterface $config, |
| 42 | Filesystem $filesystem, |
| 43 | LoggerInterface $logger, |
| 44 | Prefixer $projectReplace, |
| 45 | FileEnumerator $fileEnumerator |
| 46 | ) { |
| 47 | $this->config = $config; |
| 48 | $this->filesystem = $filesystem; |
| 49 | $this->setLogger($logger); |
| 50 | $this->projectReplace = $projectReplace; |
| 51 | $this->fileEnumerator = $fileEnumerator; |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * Create `autoload.php` and the `vendor-prefixed/composer` directory. |
| 56 | * @throws ParsingException |
| 57 | * @throws FilesystemException |
| 58 | */ |
| 59 | public function generatedPrefixedAutoloader(): void |
| 60 | { |
| 61 | $this->generatedMainAutoloader(); |
| 62 | |
| 63 | $this->createInstalledVersionsFiles(); |
| 64 | |
| 65 | $this->prefixNewAutoloader(); |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * Uses `vendor/composer/installed.json` to output autoload files to `vendor-prefixed/composer`. |
| 70 | * |
| 71 | * @throws ParsingException |
| 72 | * @throws FilesystemException |
| 73 | */ |
| 74 | protected function generatedMainAutoloader(): void |
| 75 | { |
| 76 | /** |
| 77 | * Unfortunately, `::dump()` creates the target directories if they don't exist, even though it otherwise respects `::setDryRun()`. |
| 78 | * |
| 79 | * {@see https://github.com/composer/composer/pull/12396} might fix this. |
| 80 | */ |
| 81 | if ($this->config->isDryRun()) { |
| 82 | return; |
| 83 | } |
| 84 | |
| 85 | $relativeTargetDir = $this->filesystem->getRelativePath( |
| 86 | $this->config->getProjectDirectory(), |
| 87 | $this->config->getTargetDirectory() |
| 88 | ); |
| 89 | |
| 90 | $defaultVendorDirBefore = Config::$defaultConfig['vendor-dir']; |
| 91 | Config::$defaultConfig['vendor-dir'] = $relativeTargetDir; |
| 92 | |
| 93 | $projectComposerJson = new JsonFile($this->config->getProjectDirectory() . Factory::getComposerFile()); |
| 94 | |
| 95 | /** @var ComposerJsonArray $projectComposerJsonArray */ |
| 96 | $projectComposerJsonArray = $projectComposerJson->read(); |
| 97 | if (isset($projectComposerJsonArray['config'], $projectComposerJsonArray['config']['vendor-dir'])) { |
| 98 | $projectComposerJsonArray['config']['vendor-dir'] = $relativeTargetDir; |
| 99 | } |
| 100 | |
| 101 | /** |
| 102 | * Loop over all packages that should be included and ensure the root package requires them. Composer only |
| 103 | * includes packages in the autoloader that are required by a parent package (including root). Without this, |
| 104 | * packages that are selectively prefixed are not included in the autoloader. |
| 105 | * |
| 106 | * @see AutoloadGenerator::filterPackageMap() |
| 107 | */ |
| 108 | foreach ($this->config->getPackagesToPrefix() as $name => $package) { |
| 109 | $projectComposerJsonArray['require'][$name] = '*'; |
| 110 | } |
| 111 | |
| 112 | // Include the project root autoload in the vendor-prefixed autoloader? |
| 113 | if (isset($projectComposerJsonArray['autoload']) && !$this->config->isIncludeRootAutoload()) { |
| 114 | $projectComposerJsonArray['autoload'] = []; |
| 115 | } |
| 116 | |
| 117 | $composer = Factory::create(new NullIO(), $projectComposerJsonArray); |
| 118 | $installationManager = $composer->getInstallationManager(); |
| 119 | $package = $composer->getPackage(); |
| 120 | |
| 121 | /** |
| 122 | * Cannot use `$composer->getConfig()`, need to create a new one so the `vendor-dir` is correct. |
| 123 | */ |
| 124 | $config = new Config(false, $this->config->getProjectDirectory()); |
| 125 | |
| 126 | /** @var array{config?: array<string, mixed>} $projectComposerConfigMergeArray */ |
| 127 | $projectComposerConfigMergeArray = ['config' => $projectComposerJsonArray['config'] ?? []]; |
| 128 | |
| 129 | $config->merge($projectComposerConfigMergeArray); |
| 130 | |
| 131 | $generator = new ComposerAutoloadGenerator( |
| 132 | $this->config->getNamespacePrefix(), |
| 133 | $composer->getEventDispatcher() |
| 134 | ); |
| 135 | $isOptimize = $this->isOptimizeAutoloaderEnabled(); |
| 136 | $generator->setDryRun($this->config->isDryRun()); |
| 137 | $generator->setClassMapAuthoritative($isOptimize); |
| 138 | $generator->setRunScripts(false); |
| 139 | // $generator->setApcu($apcu, $apcuPrefix); |
| 140 | // $generator->setPlatformRequirementFilter($this->getPlatformRequirementFilter($input)); |
| 141 | |
| 142 | $installedJsonFile = new JsonFile($this->config->getTargetDirectory() . 'composer/installed.json'); |
| 143 | /** @var array{dev?:bool} $installedJson */ |
| 144 | $installedJson = $installedJsonFile->read(); |
| 145 | $localRepo = new InstalledFilesystemRepository($installedJsonFile); |
| 146 | |
| 147 | /** |
| 148 | * If the target directory is different to the vendor directory, then we do not want to include dev |
| 149 | * dependencies, but if it is vendor, then unless composer install was run with --no-dev, we do want them. |
| 150 | */ |
| 151 | if ($this->config->getVendorDirectory() !== $this->config->getTargetDirectory()) { |
| 152 | $isDevMode = false; |
| 153 | } else { |
| 154 | $isDevMode = (bool) ($installedJson['dev'] ?? false); |
| 155 | } |
| 156 | $generator->setDevMode($isDevMode); |
| 157 | |
| 158 | $strictAmbiguous = false; // $input->getOption('strict-ambiguous') |
| 159 | |
| 160 | // This will output the autoload_static.php etc. files to `vendor-prefixed/composer`. |
| 161 | $generator->dump( |
| 162 | $config, |
| 163 | $localRepo, |
| 164 | $package, |
| 165 | $installationManager, |
| 166 | 'composer', |
| 167 | $isOptimize, |
| 168 | $this->getSuffix(), |
| 169 | $composer->getLocker(), |
| 170 | $strictAmbiguous |
| 171 | ); |
| 172 | |
| 173 | /** |
| 174 | * Tests fail if this is absent. |
| 175 | * |
| 176 | * Arguably this should be in ::setUp() and tearDown() in the test classes, but if other tools run after Strauss |
| 177 | * then they might expect it to be unmodified. |
| 178 | */ |
| 179 | Config::$defaultConfig['vendor-dir'] = $defaultVendorDirBefore; |
| 180 | } |
| 181 | |
| 182 | /** |
| 183 | * Keep backward compatibility with configs implementing only AutoloadConfigInterface. |
| 184 | */ |
| 185 | protected function isOptimizeAutoloaderEnabled(): bool |
| 186 | { |
| 187 | return $this->config instanceof OptimizeAutoloaderConfigInterface |
| 188 | ? $this->config->isOptimizeAutoloader() |
| 189 | : true; |
| 190 | } |
| 191 | |
| 192 | /** |
| 193 | * Create `InstalledVersions.php` and `installed.php`. |
| 194 | * |
| 195 | * This file is copied in all Composer installations. |
| 196 | * It is added always in `ComposerAutoloadGenerator::dump()`, called above. |
| 197 | * If the file does not exist, its entry in the classmap will not be prefixed and will cause autoloading issues for the real class. |
| 198 | * |
| 199 | * The accompanying `installed.php` is unique per install. Copy it and filter its packages to the packages that was copied. |
| 200 | * @throws FilesystemException |
| 201 | */ |
| 202 | protected function createInstalledVersionsFiles(): void |
| 203 | { |
| 204 | if ($this->config->getVendorDirectory() === $this->config->getTargetDirectory()) { |
| 205 | return; |
| 206 | } |
| 207 | |
| 208 | $this->filesystem->copy($this->config->getVendorDirectory() . '/composer/InstalledVersions.php', $this->config->getTargetDirectory() . 'composer/InstalledVersions.php'); |
| 209 | |
| 210 | // This is just `<?php return array(...);` |
| 211 | $installedPhpString = $this->filesystem->read($this->config->getVendorDirectory() . '/composer/installed.php'); |
| 212 | $installed = eval(str_replace('<?php', '', $installedPhpString)); |
| 213 | |
| 214 | $targetPackages = $this->config->getPackagesToCopy(); |
| 215 | $targetPackagesNames = array_keys($targetPackages); |
| 216 | |
| 217 | $installed['versions'] = array_filter($installed['versions'], function ($packageName) use ($targetPackagesNames) { |
| 218 | return in_array($packageName, $targetPackagesNames); |
| 219 | }, ARRAY_FILTER_USE_KEY); |
| 220 | |
| 221 | $installedArrayString = var_export($installed, true); |
| 222 | |
| 223 | $newInstalledPhpString = "<?php return $installedArrayString;"; |
| 224 | |
| 225 | // Update `__DIR__` which was evaluated during the `include`/`eval`. |
| 226 | $newInstalledPhpString = preg_replace('/(\'install_path\' => )(.*)(\/\.\..*)/', "$1__DIR__ . '$3", $newInstalledPhpString); |
| 227 | |
| 228 | $this->filesystem->write($this->config->getTargetDirectory() . '/composer/installed.php', $newInstalledPhpString); |
| 229 | } |
| 230 | |
| 231 | /** |
| 232 | * @throws FilesystemException |
| 233 | */ |
| 234 | protected function prefixNewAutoloader(): void |
| 235 | { |
| 236 | if ($this->config->getVendorDirectory() === $this->config->getTargetDirectory()) { |
| 237 | return; |
| 238 | } |
| 239 | |
| 240 | $this->logger->debug('Prefixing the new Composer autoloader.'); |
| 241 | |
| 242 | $projectFiles = $this->fileEnumerator->compileFileListForPaths([ |
| 243 | $this->config->getTargetDirectory() . 'composer', |
| 244 | ]); |
| 245 | |
| 246 | $phpFiles = array_filter( |
| 247 | $projectFiles->getFiles(), |
| 248 | fn($file) => $file->isPhpFile() |
| 249 | ); |
| 250 | |
| 251 | $phpFilesAbsolutePaths = array_map( |
| 252 | fn($file) => $file->getSourcePath(), |
| 253 | $phpFiles |
| 254 | ); |
| 255 | |
| 256 | $sourceFile = new File(__DIR__, __DIR__); |
| 257 | $composerAutoloadNamespaceSymbol = new NamespaceSymbol( |
| 258 | 'Composer\\Autoload', |
| 259 | $sourceFile |
| 260 | ); |
| 261 | $composerAutoloadNamespaceSymbol->setReplacement( |
| 262 | $this->config->getNamespacePrefix() . '\\Composer\\Autoload' |
| 263 | ); |
| 264 | $composerNamespaceSymbol = new NamespaceSymbol( |
| 265 | 'Composer', |
| 266 | $sourceFile |
| 267 | ); |
| 268 | $composerNamespaceSymbol->setReplacement( |
| 269 | $this->config->getNamespacePrefix() . '\\Composer' |
| 270 | ); |
| 271 | |
| 272 | $discoveredSymbols = new DiscoveredSymbols(); |
| 273 | $discoveredSymbols->add( |
| 274 | $composerNamespaceSymbol |
| 275 | ); |
| 276 | $discoveredSymbols->add( |
| 277 | $composerAutoloadNamespaceSymbol |
| 278 | ); |
| 279 | |
| 280 | $this->projectReplace->replaceInProjectFiles($discoveredSymbols, $phpFilesAbsolutePaths); |
| 281 | } |
| 282 | |
| 283 | /** |
| 284 | * If there is an existing autoloader, it will use the same suffix. If there is not, it pulls the suffix from |
| 285 | * {Composer::getLocker()} and clashes with the existing autoloader. |
| 286 | * |
| 287 | * @see https://github.com/composer/composer/blob/ae208dc1e182bd45d99fcecb956501da212454a1/src/Composer/Autoload/AutoloadGenerator.php#L429 |
| 288 | * @see AutoloadGenerator::dump() 412:431 |
| 289 | * @throws \Random\RandomException in PHP 8.2+ |
| 290 | * @throws FilesystemException |
| 291 | */ |
| 292 | protected function getSuffix(): ?string |
| 293 | { |
| 294 | return !$this->filesystem->fileExists($this->config->getTargetDirectory() . 'autoload.php') |
| 295 | ? bin2hex(random_bytes(16)) |
| 296 | : null; |
| 297 | } |
| 298 | } |