Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
33.33% |
1 / 3 |
|
50.00% |
1 / 2 |
CRAP | |
0.00% |
0 / 1 |
| ComposerAutoloadGenerator | |
33.33% |
1 / 3 |
|
50.00% |
1 / 2 |
3.19 | |
0.00% |
0 / 1 |
| __construct | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
| getFileIdentifier | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * Extend Composer's `AutoloadGenerator` to override the `getFileIdentifier()` method's hash to provide true uniqueness. |
| 4 | * |
| 5 | * `files` autoloaders' entries in `composer/autoload_static.php` and `composer/autoload_files.php` are given |
| 6 | * a `fileIdentifier` that is a hash of the package name and path. It is used in |
| 7 | * `$GLOBALS['__composer_autoload_files'][$fileIdentifier]` to ensure that the file is only `require`d once. |
| 8 | * |
| 9 | * It does not use the contents of the file in the hash. When two projects include the same package, that package's |
| 10 | * files' identifiers will be the same in both. |
| 11 | * |
| 12 | * This subclass overrides the `getFileIdentifier()` method to include a unique string for the project, presumably |
| 13 | * the `namespace_prefix`. |
| 14 | * |
| 15 | * {@see DumpAutoload::generatedPrefixedAutoloader()} calls {@see AutoloadGenerator::dump()} which eventually calls |
| 16 | * {@see AutoloadGenerator::getFileIdentifier()} which is used in {@see AutoloadGenerator::getIncludeFilesFile()} to |
| 17 | * generate `autoload_files.php` which is loaded in {@see AutoloadGenerator::getStaticFilesFile()} to create |
| 18 | * `autoload_static.php`. |
| 19 | */ |
| 20 | |
| 21 | namespace BrianHenryIE\Strauss\Pipeline\Autoload; |
| 22 | |
| 23 | use Composer\Autoload\AutoloadGenerator; |
| 24 | use Composer\EventDispatcher\EventDispatcher; |
| 25 | use Composer\IO\IOInterface; |
| 26 | use Composer\Package\PackageInterface; |
| 27 | |
| 28 | class ComposerAutoloadGenerator extends AutoloadGenerator |
| 29 | { |
| 30 | /** |
| 31 | * A string to include in the fileIdentifier hash to ensure it is unique across projects. |
| 32 | */ |
| 33 | protected string $projectUniqueString; |
| 34 | |
| 35 | /** |
| 36 | * Constructor |
| 37 | * |
| 38 | * @param string $projectUniqueString A string to include in the hash to ensure uniqueness across projects, probably `namespace_prefix`. |
| 39 | * @param EventDispatcher $eventDispatcher Used to dispatch `optimize` script when {@see AutoloadGenerator::$runScripts} is true, which defaults to `false`. |
| 40 | * @param IOInterface|null $io Used to write errors and warnings. Default `null`. |
| 41 | */ |
| 42 | public function __construct( |
| 43 | string $projectUniqueString, |
| 44 | EventDispatcher $eventDispatcher, |
| 45 | ?IOInterface $io = null |
| 46 | ) { |
| 47 | parent::__construct($eventDispatcher, $io); |
| 48 | |
| 49 | $this->projectUniqueString = $projectUniqueString; |
| 50 | } |
| 51 | |
| 52 | /** |
| 53 | * Get a unique id for the `files` autoload entry. |
| 54 | * |
| 55 | * `$path` here is `PackageInterface->getTargetDir()`.`PackageInterface::getAutoload()['files'][]` |
| 56 | * |
| 57 | * @override |
| 58 | * @see AutoloadGenerator::getFileIdentifier() |
| 59 | * |
| 60 | * @param PackageInterface $package The package to get the file identifier for. |
| 61 | * @param string $path Relative path from `vendor`. |
| 62 | * |
| 63 | * @return string |
| 64 | */ |
| 65 | protected function getFileIdentifier(PackageInterface $package, string $path) |
| 66 | { |
| 67 | return hash('md5', $package->getName() . ':' . $path . ':' . $this->projectUniqueString); |
| 68 | } |
| 69 | } |