From 2934a0aad3e495d01219ea9153c494a9ebdbf7e2 Mon Sep 17 00:00:00 2001 From: Jordi Boggiano Date: Wed, 13 Apr 2022 12:37:09 +0200 Subject: [PATCH] Clarify that autoloader-suffix should be a non-empty-string, fixes #10720 --- doc/06-config.md | 4 ++-- src/Composer/Autoload/AutoloadGenerator.php | 12 ++++++------ src/Composer/Config.php | 7 +++++++ 3 files changed, 15 insertions(+), 8 deletions(-) diff --git a/doc/06-config.md b/doc/06-config.md index 636cf955508c..270f1bebddca 100644 --- a/doc/06-config.md +++ b/doc/06-config.md @@ -315,8 +315,8 @@ with other autoloaders. ## autoloader-suffix -Defaults to `null`. String to be used as a suffix for the generated Composer -autoloader. When null a random one will be generated. +Defaults to `null`. Non-empty string to be used as a suffix for the generated +Composer autoloader. When null a random one will be generated. ## optimize-autoloader diff --git a/src/Composer/Autoload/AutoloadGenerator.php b/src/Composer/Autoload/AutoloadGenerator.php index 265b890d6ba4..db6a7f7637ee 100644 --- a/src/Composer/Autoload/AutoloadGenerator.php +++ b/src/Composer/Autoload/AutoloadGenerator.php @@ -159,12 +159,12 @@ public function setPlatformRequirementFilter(PlatformRequirementFilterInterface /** * @param string $targetDir * @param bool $scanPsrPackages - * @param string $suffix + * @param non-empty-string|null $suffix * @return int * @throws \Seld\JsonLint\ParsingException * @throws \RuntimeException */ - public function dump(Config $config, InstalledRepositoryInterface $localRepo, RootPackageInterface $rootPackage, InstallationManager $installationManager, $targetDir, $scanPsrPackages = false, $suffix = '') + public function dump(Config $config, InstalledRepositoryInterface $localRepo, RootPackageInterface $rootPackage, InstallationManager $installationManager, $targetDir, $scanPsrPackages = false, $suffix = null) { if ($this->classMapAuthoritative) { // Force scanPsrPackages when classmap is authoritative @@ -374,16 +374,16 @@ public static function autoload(\$class) } $classmapFile .= ");\n"; - if (!$suffix) { - if (!$config->get('autoloader-suffix') && Filesystem::isReadable($vendorPath.'/autoload.php')) { + if (null === $suffix || '' === $suffix) { + if (null === $config->get('autoloader-suffix') && Filesystem::isReadable($vendorPath.'/autoload.php')) { $content = file_get_contents($vendorPath.'/autoload.php'); if (Preg::isMatch('{ComposerAutoloaderInit([^:\s]+)::}', $content, $match)) { $suffix = $match[1]; } } - if (!$suffix) { - $suffix = $config->get('autoloader-suffix') ?: md5(uniqid('', true)); + if (null === $suffix) { + $suffix = $config->get('autoloader-suffix') ?? md5(uniqid('', true)); } } diff --git a/src/Composer/Config.php b/src/Composer/Config.php index 0d927f1903d7..e81c11738572 100644 --- a/src/Composer/Config.php +++ b/src/Composer/Config.php @@ -427,6 +427,13 @@ public function get($key, $flags = 0) return $protos; + case 'autoloader-suffix': + if ($this->config[$key] === '') { // we need to guarantee null or non-empty-string + return null; + } + + return $this->process($this->config[$key], $flags); + default: if (!isset($this->config[$key])) { return null;