Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clarify that autoloader-suffix should be a non-empty-string #10725

Merged
merged 1 commit into from Apr 13, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions doc/06-config.md
Expand Up @@ -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

Expand Down
1 change: 1 addition & 0 deletions phpstan/config.neon
Expand Up @@ -16,6 +16,7 @@ parameters:
- '../src/Composer/Console/HtmlOutputFormatter.php'

reportUnmatchedIgnoredErrors: false
treatPhpDocTypesAsCertain: false

ignoreErrors:
# unused parameters
Expand Down
19 changes: 13 additions & 6 deletions src/Composer/Autoload/AutoloadGenerator.php
Expand Up @@ -159,12 +159,12 @@ public function setPlatformRequirementFilter(PlatformRequirementFilterInterface
/**
* @param string $targetDir
* @param bool $scanPsrPackages
* @param string $suffix
* @param 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
Expand Down Expand Up @@ -374,16 +374,23 @@ public static function autoload(\$class)
}
$classmapFile .= ");\n";

if (!$suffix) {
if (!$config->get('autoloader-suffix') && Filesystem::isReadable($vendorPath.'/autoload.php')) {
if ('' === $suffix) {
$suffix = null;
}
if (null === $suffix) {
$suffix = $config->get('autoloader-suffix');

// carry over existing autoload.php's suffix if possible and none is configured
if (null === $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));
// generate one if we still haven't got a suffix
if (null === $suffix) {
$suffix = md5(uniqid('', true));
}
}

Expand Down
7 changes: 7 additions & 0 deletions src/Composer/Config.php
Expand Up @@ -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;
Expand Down