|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace CodeZero\ComposerPreloadFiles; |
| 4 | + |
| 5 | +use Composer\Autoload\AutoloadGenerator as ComposerAutoloadGenerator; |
| 6 | +use Composer\Composer; |
| 7 | +use Composer\IO\IOInterface; |
| 8 | +use Composer\Package\CompletePackage; |
| 9 | +use Composer\Pcre\Preg; |
| 10 | +use Composer\Util\Filesystem; |
| 11 | +use Composer\Util\Platform; |
| 12 | + |
| 13 | +class AutoloadGenerator extends ComposerAutoloadGenerator |
| 14 | +{ |
| 15 | + /** |
| 16 | + * Add preload files to the autoload files. |
| 17 | + * |
| 18 | + * @param \Composer\Composer $composer |
| 19 | + * @param \Composer\IO\IOInterface $io |
| 20 | + * @param \Composer\Util\Filesystem $filesystem |
| 21 | + * |
| 22 | + * @return void |
| 23 | + */ |
| 24 | + public function addPreloadFilesToAutoloadFiles(Composer $composer, IOInterface $io, Filesystem $filesystem) |
| 25 | + { |
| 26 | + $preloadFiles = $this->parsePreloadFiles($composer, $filesystem); |
| 27 | + |
| 28 | + if (count($preloadFiles) === 0) { |
| 29 | + return; |
| 30 | + } |
| 31 | + |
| 32 | + $io->writeError('<info>Adding preload files to the autoload files.</info>'); |
| 33 | + |
| 34 | + // Some pathfinding... |
| 35 | + // Do not remove double realpath() calls. |
| 36 | + // Fixes failing Windows realpath() implementation. |
| 37 | + // See https://bugs.php.net/bug.php?id=72738 |
| 38 | + $basePath = $filesystem->normalizePath(realpath(realpath(Platform::getCwd()))); |
| 39 | + $vendorDir = $composer->getConfig()->get('vendor-dir'); |
| 40 | + $vendorPath = $filesystem->normalizePath(realpath(realpath($vendorDir))); |
| 41 | + $targetDir = $vendorPath.'/composer'; |
| 42 | + |
| 43 | + $this->prependPreloadFilesToAutoloadFilesFile($filesystem, $preloadFiles, $targetDir, $basePath, $vendorPath); |
| 44 | + $this->regenerateAutoloadStaticFile($filesystem, $targetDir, $basePath, $vendorPath); |
| 45 | + } |
| 46 | + |
| 47 | + /** |
| 48 | + * Prepend preload files to the 'autoload_files.php' file. |
| 49 | + * |
| 50 | + * @param \Composer\Util\Filesystem $filesystem |
| 51 | + * @param array $preloadFiles |
| 52 | + * @param string $targetDir |
| 53 | + * @param string $basePath |
| 54 | + * @param string $vendorPath |
| 55 | + * |
| 56 | + * @return void |
| 57 | + */ |
| 58 | + protected function prependPreloadFilesToAutoloadFilesFile(Filesystem $filesystem, $preloadFiles, $targetDir, $basePath, $vendorPath) |
| 59 | + { |
| 60 | + $vendorPathCode = $filesystem->findShortestPathCode(realpath($targetDir), $vendorPath, true); |
| 61 | + $appBaseDirCode = $filesystem->findShortestPathCode($vendorPath, $basePath, true); |
| 62 | + $appBaseDirCode = str_replace('__DIR__', '$vendorDir', $appBaseDirCode); |
| 63 | + $autoloadFilesFilePath = $targetDir.'/autoload_files.php'; |
| 64 | + |
| 65 | + // Merge preload files with original files. |
| 66 | + $originalFiles = $this->getOriginalAutoloadFiles($autoloadFilesFilePath); |
| 67 | + $allFiles = array_merge($preloadFiles, $originalFiles); |
| 68 | + |
| 69 | + // Write new 'autoload_files.php'. |
| 70 | + $filesystem->filePutContentsIfModified( |
| 71 | + $autoloadFilesFilePath, |
| 72 | + $this->getIncludeFilesFile($allFiles, $filesystem, $basePath, $vendorPath, $vendorPathCode, $appBaseDirCode) |
| 73 | + ); |
| 74 | + } |
| 75 | + |
| 76 | + /** |
| 77 | + * Regenerate the 'autoload_static.php' file. |
| 78 | + * |
| 79 | + * @param \Composer\Util\Filesystem $filesystem |
| 80 | + * @param string $targetDir |
| 81 | + * @param string $basePath |
| 82 | + * @param string $vendorPath |
| 83 | + * |
| 84 | + * @return void |
| 85 | + */ |
| 86 | + protected function regenerateAutoloadStaticFile(Filesystem $filesystem, $targetDir, $basePath, $vendorPath) |
| 87 | + { |
| 88 | + // Get the class name suffix from 'autoload.php'. |
| 89 | + // https://github.com/composer/composer/blob/main/src/Composer/Autoload/AutoloadGenerator.php#L390-L392 |
| 90 | + $autoloadContent = file_get_contents($vendorPath.'/autoload.php'); |
| 91 | + $suffix = null; |
| 92 | + if (Preg::isMatch('{ComposerAutoloaderInit([^:\s]+)::}', $autoloadContent, $match)) { |
| 93 | + $suffix = $match[1]; |
| 94 | + } |
| 95 | + |
| 96 | + // Write new 'autoload_static.php'. |
| 97 | + $filesystem->filePutContentsIfModified( |
| 98 | + $targetDir.'/autoload_static.php', |
| 99 | + $this->getStaticFile($suffix, $targetDir, $vendorPath, $basePath) |
| 100 | + ); |
| 101 | + } |
| 102 | + |
| 103 | + /** |
| 104 | + * Get the original files to autoload. |
| 105 | + * |
| 106 | + * @param string $autoloadFilesFilePath |
| 107 | + * |
| 108 | + * @return array |
| 109 | + */ |
| 110 | + protected function getOriginalAutoloadFiles($autoloadFilesFilePath) |
| 111 | + { |
| 112 | + if (file_exists($autoloadFilesFilePath)) { |
| 113 | + return include $autoloadFilesFilePath; |
| 114 | + } |
| 115 | + |
| 116 | + return []; |
| 117 | + } |
| 118 | + |
| 119 | + /** |
| 120 | + * Parse preload files from the root package and all vendor packages. |
| 121 | + * |
| 122 | + * @param \Composer\Composer $composer |
| 123 | + * @param \Composer\Util\Filesystem $filesystem |
| 124 | + * |
| 125 | + * @return array |
| 126 | + */ |
| 127 | + protected function parsePreloadFiles(Composer $composer, Filesystem $filesystem) |
| 128 | + { |
| 129 | + $installationManager = $composer->getInstallationManager(); |
| 130 | + $preloadFilesKey = 'preload-files'; |
| 131 | + $preloadFiles = []; |
| 132 | + |
| 133 | + // Do not remove double realpath() calls. |
| 134 | + // Fixes failing Windows realpath() implementation. |
| 135 | + // See https://bugs.php.net/bug.php?id=72738 |
| 136 | + $basePath = $filesystem->normalizePath(realpath(realpath(Platform::getCwd()))); |
| 137 | + |
| 138 | + $rootPackage = $composer->getPackage(); |
| 139 | + $rootPackageConfig = $rootPackage->getExtra(); |
| 140 | + $rootPackagePreloadFiles = $rootPackageConfig[$preloadFilesKey] ?? []; |
| 141 | + |
| 142 | + foreach ($rootPackagePreloadFiles as $file) { |
| 143 | + $identifier = $this->getFileIdentifier($rootPackage, $file); |
| 144 | + $preloadFiles[$identifier] = $filesystem->normalizePath($basePath . '/' . $file); |
| 145 | + } |
| 146 | + |
| 147 | + $otherPackages = $composer->getRepositoryManager()->getLocalRepository()->getCanonicalPackages(); |
| 148 | + |
| 149 | + foreach ($otherPackages as $package) { |
| 150 | + if ( ! ($package instanceof CompletePackage)) { |
| 151 | + continue; |
| 152 | + } |
| 153 | + |
| 154 | + $packageBaseDir = $filesystem->normalizePath($installationManager->getInstallPath($package)); |
| 155 | + $packageConfig = $package->getExtra(); |
| 156 | + $packagePreloadFiles = $packageConfig[$preloadFilesKey] ?? []; |
| 157 | + |
| 158 | + foreach ($packagePreloadFiles as $file) { |
| 159 | + $identifier = $this->getFileIdentifier($package, $file); |
| 160 | + $preloadFiles[$identifier] = $filesystem->normalizePath($packageBaseDir . '/' . $file); |
| 161 | + } |
| 162 | + } |
| 163 | + |
| 164 | + return $preloadFiles; |
| 165 | + } |
| 166 | +} |
0 commit comments