diff --git a/src/Composer/Autoload/AutoloadGenerator.php b/src/Composer/Autoload/AutoloadGenerator.php index cbf3b0aba..161e943ae 100644 --- a/src/Composer/Autoload/AutoloadGenerator.php +++ b/src/Composer/Autoload/AutoloadGenerator.php @@ -53,10 +53,15 @@ class AutoloadGenerator private $classMapAuthoritative = false; /** - * @var bool|string + * @var bool */ private $apcu = false; + /** + * @var string|null + */ + private $apcuPrefix; + /** * @var bool */ @@ -92,11 +97,13 @@ class AutoloadGenerator /** * Whether or not generated autoloader considers APCu caching. * - * @param bool|string $apcu + * @param bool $apcu + * @param string|null $apcuPrefix */ - public function setApcu($apcu) + public function setApcu($apcu, $apcuPrefix = null) { - $this->apcu = is_string($apcu) ? $apcu : (bool) $apcu; + $this->apcu = (bool) $apcu; + $this->apcuPrefix = $apcuPrefix !== null ? (string) $apcuPrefix : $apcuPrefix; } /** @@ -858,7 +865,7 @@ CLASSMAPAUTHORITATIVE; } if ($this->apcu) { - $apcuPrefix = var_export(is_string($this->apcu) ? $this->apcu : substr(base64_encode(md5(uniqid('', true), true)), 0, -3), true); + $apcuPrefix = var_export(($this->apcuPrefix ?: substr(base64_encode(md5(uniqid('', true), true)), 0, -3)), true); $file .= <<setApcuPrefix($apcuPrefix); diff --git a/src/Composer/Command/DumpAutoloadCommand.php b/src/Composer/Command/DumpAutoloadCommand.php index ee6baabdb..500569a25 100644 --- a/src/Composer/Command/DumpAutoloadCommand.php +++ b/src/Composer/Command/DumpAutoloadCommand.php @@ -14,6 +14,7 @@ namespace Composer\Command; use Composer\Plugin\CommandEvent; use Composer\Plugin\PluginEvents; +use Symfony\Component\Console\Input\InputDefinition; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Output\OutputInterface; @@ -33,7 +34,8 @@ class DumpAutoloadCommand extends BaseCommand new InputOption('no-scripts', null, InputOption::VALUE_NONE, 'Skips the execution of all scripts defined in composer.json file.'), new InputOption('optimize', 'o', InputOption::VALUE_NONE, 'Optimizes PSR0 and PSR4 packages to be loaded with classmaps too, good for production.'), new InputOption('classmap-authoritative', 'a', InputOption::VALUE_NONE, 'Autoload classes from the classmap only. Implicitly enables `--optimize`.'), - new InputOption('apcu', null, InputOption::VALUE_OPTIONAL, 'Use APCu to cache found/not-found classes.'), + new InputOption('apcu', null, InputOption::VALUE_NONE, 'Use APCu to cache found/not-found classes.'), + new InputOption('apcu-prefix', null, InputOption::VALUE_REQUIRED, 'Use a custom prefix for the APCu autoloader cache'), new InputOption('no-dev', null, InputOption::VALUE_NONE, 'Disables autoload-dev rules.'), new InputOption('ignore-platform-req', null, InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY, 'Ignore a specific platform requirement (php & ext- packages).'), new InputOption('ignore-platform-reqs', null, InputOption::VALUE_NONE, 'Ignore all platform requirements (php & ext- packages).'), @@ -62,7 +64,7 @@ EOT $optimize = $input->getOption('optimize') || $config->get('optimize-autoloader'); $authoritative = $input->getOption('classmap-authoritative') || $config->get('classmap-authoritative'); - $apcu = strpos($input, '--apcu') !== false || $config->get('apcu-autoloader'); + $apcu = $input->getOption('apcu') || $config->get('apcu-autoloader'); if ($authoritative) { $this->getIO()->write('Generating optimized autoload files (authoritative)'); @@ -77,7 +79,7 @@ EOT $generator = $composer->getAutoloadGenerator(); $generator->setDevMode(!$input->getOption('no-dev')); $generator->setClassMapAuthoritative($authoritative); - $generator->setApcu($apcu ? $input->getOption('apcu') ?: true : false); + $generator->setApcu($apcu, $input->getOption('apcu-prefix')); $generator->setRunScripts(!$input->getOption('no-scripts')); $generator->setIgnorePlatformRequirements($ignorePlatformReqs); $numberOfClasses = $generator->dump($config, $localRepo, $package, $installationManager, 'composer', $optimize); diff --git a/src/Composer/Command/InstallCommand.php b/src/Composer/Command/InstallCommand.php index 07f388867..cbcb0eae7 100644 --- a/src/Composer/Command/InstallCommand.php +++ b/src/Composer/Command/InstallCommand.php @@ -48,7 +48,8 @@ class InstallCommand extends BaseCommand new InputOption('verbose', 'v|vv|vvv', InputOption::VALUE_NONE, 'Shows more details including new commits pulled in when updating packages.'), new InputOption('optimize-autoloader', 'o', InputOption::VALUE_NONE, 'Optimize autoloader during autoloader dump'), new InputOption('classmap-authoritative', 'a', InputOption::VALUE_NONE, 'Autoload classes from the classmap only. Implicitly enables `--optimize-autoloader`.'), - new InputOption('apcu-autoloader', null, InputOption::VALUE_OPTIONAL, 'Use APCu to cache found/not-found classes (passing an argument sets the APCu prefix)'), + new InputOption('apcu-autoloader', null, InputOption::VALUE_NONE, 'Use APCu to cache found/not-found classes.'), + new InputOption('apcu-autoloader-prefix', null, InputOption::VALUE_REQUIRED, 'Use a custom prefix for the APCu autoloader cache'), new InputOption('ignore-platform-req', null, InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY, 'Ignore a specific platform requirement (php & ext- packages).'), new InputOption('ignore-platform-reqs', null, InputOption::VALUE_NONE, 'Ignore all platform requirements (php & ext- packages).'), new InputArgument('packages', InputArgument::IS_ARRAY | InputArgument::OPTIONAL, 'Should not be provided, use composer require instead to add a given package to composer.json.'), @@ -102,7 +103,7 @@ EOT $optimize = $input->getOption('optimize-autoloader') || $config->get('optimize-autoloader'); $authoritative = $input->getOption('classmap-authoritative') || $config->get('classmap-authoritative'); - $apcu = strpos($input, '--apcu-autoloader') !== false || $config->get('apcu-autoloader'); + $apcu = $input->getOption('apcu-autoloader') || $config->get('apcu-autoloader'); $ignorePlatformReqs = $input->getOption('ignore-platform-reqs') ?: ($input->getOption('ignore-platform-req') ?: false); @@ -118,7 +119,7 @@ EOT ->setRunScripts(!$input->getOption('no-scripts')) ->setOptimizeAutoloader($optimize) ->setClassMapAuthoritative($authoritative) - ->setApcuAutoloader($apcu ? $input->getOption('apcu-autoloader') ?: true : false) + ->setApcuAutoloader($apcu, $input->getOption('apcu-autoloader-prefix')) ->setIgnorePlatformRequirements($ignorePlatformReqs) ; diff --git a/src/Composer/Command/RemoveCommand.php b/src/Composer/Command/RemoveCommand.php index 99be3810b..9e2a46b5a 100644 --- a/src/Composer/Command/RemoveCommand.php +++ b/src/Composer/Command/RemoveCommand.php @@ -54,7 +54,8 @@ class RemoveCommand extends BaseCommand new InputOption('ignore-platform-reqs', null, InputOption::VALUE_NONE, 'Ignore all platform requirements (php & ext- packages).'), new InputOption('optimize-autoloader', 'o', InputOption::VALUE_NONE, 'Optimize autoloader during autoloader dump'), new InputOption('classmap-authoritative', 'a', InputOption::VALUE_NONE, 'Autoload classes from the classmap only. Implicitly enables `--optimize-autoloader`.'), - new InputOption('apcu-autoloader', null, InputOption::VALUE_OPTIONAL, 'Use APCu to cache found/not-found classes (passing a string sets the APCu prefix)'), + new InputOption('apcu-autoloader', null, InputOption::VALUE_NONE, 'Use APCu to cache found/not-found classes.'), + new InputOption('apcu-autoloader-prefix', null, InputOption::VALUE_REQUIRED, 'Use a custom prefix for the APCu autoloader cache'), )) ->setHelp( <<getOption('update-no-dev'); $optimize = $input->getOption('optimize-autoloader') || $composer->getConfig()->get('optimize-autoloader'); $authoritative = $input->getOption('classmap-authoritative') || $composer->getConfig()->get('classmap-authoritative'); - $apcu = strpos($input, '--apcu-autoloader') !== false || $composer->getConfig()->get('apcu-autoloader'); + $apcu = $input->getOption('apcu-autoloader') ?: $composer->getConfig()->get('apcu-autoloader'); $updateAllowTransitiveDependencies = Request::UPDATE_LISTED_WITH_TRANSITIVE_DEPS_NO_ROOT_REQUIRE; $flags = ''; @@ -248,7 +249,7 @@ EOT ->setDevMode($updateDevMode) ->setOptimizeAutoloader($optimize) ->setClassMapAuthoritative($authoritative) - ->setApcuAutoloader($apcu ? $input->getOption('apcu-autoloader') ?: true : false) + ->setApcuAutoloader($apcu, $input->getOption('apcu-autoloader-prefix')) ->setUpdate(true) ->setInstall(!$input->getOption('no-install')) ->setUpdateAllowList($packages) diff --git a/src/Composer/Command/RequireCommand.php b/src/Composer/Command/RequireCommand.php index b1ad95b67..f276b781c 100644 --- a/src/Composer/Command/RequireCommand.php +++ b/src/Composer/Command/RequireCommand.php @@ -76,7 +76,8 @@ class RequireCommand extends InitCommand new InputOption('sort-packages', null, InputOption::VALUE_NONE, 'Sorts packages when adding/updating a new dependency'), new InputOption('optimize-autoloader', 'o', InputOption::VALUE_NONE, 'Optimize autoloader during autoloader dump'), new InputOption('classmap-authoritative', 'a', InputOption::VALUE_NONE, 'Autoload classes from the classmap only. Implicitly enables `--optimize-autoloader`.'), - new InputOption('apcu-autoloader', null, InputOption::VALUE_OPTIONAL, 'Use APCu to cache found/not-found classes (passing a string sets the APCu prefix)'), + new InputOption('apcu-autoloader', null, InputOption::VALUE_NONE, 'Use APCu to cache found/not-found classes.'), + new InputOption('apcu-autoloader-prefix', null, InputOption::VALUE_REQUIRED, 'Use a custom prefix for the APCu autoloader cache'), )) ->setHelp( <<getOption('update-no-dev'); $optimize = $input->getOption('optimize-autoloader') || $composer->getConfig()->get('optimize-autoloader'); $authoritative = $input->getOption('classmap-authoritative') || $composer->getConfig()->get('classmap-authoritative'); - $apcu = strpos($input, '--apcu-autoloader') !== false || $composer->getConfig()->get('apcu-autoloader'); + $apcu = $input->getOption('apcu-autoloader') || $composer->getConfig()->get('apcu-autoloader'); $updateAllowTransitiveDependencies = Request::UPDATE_ONLY_LISTED; $flags = ''; @@ -301,7 +302,7 @@ EOT ->setRunScripts(!$input->getOption('no-scripts')) ->setOptimizeAutoloader($optimize) ->setClassMapAuthoritative($authoritative) - ->setApcuAutoloader($apcu ? $input->getOption('apcu-autoloader') ?: true : false) + ->setApcuAutoloader($apcu, $input->getOption('apcu-autoloader-prefix')) ->setUpdate(true) ->setInstall(!$input->getOption('no-install')) ->setUpdateAllowTransitiveDependencies($updateAllowTransitiveDependencies) diff --git a/src/Composer/Command/UpdateCommand.php b/src/Composer/Command/UpdateCommand.php index bff37129f..cd404e95b 100644 --- a/src/Composer/Command/UpdateCommand.php +++ b/src/Composer/Command/UpdateCommand.php @@ -59,7 +59,8 @@ class UpdateCommand extends BaseCommand new InputOption('verbose', 'v|vv|vvv', InputOption::VALUE_NONE, 'Shows more details including new commits pulled in when updating packages.'), new InputOption('optimize-autoloader', 'o', InputOption::VALUE_NONE, 'Optimize autoloader during autoloader dump.'), new InputOption('classmap-authoritative', 'a', InputOption::VALUE_NONE, 'Autoload classes from the classmap only. Implicitly enables `--optimize-autoloader`.'), - new InputOption('apcu-autoloader', null, InputOption::VALUE_OPTIONAL, 'Use APCu to cache found/not-found classes (passing a string sets the APCu prefix)'), + new InputOption('apcu-autoloader', null, InputOption::VALUE_NONE, 'Use APCu to cache found/not-found classes.'), + new InputOption('apcu-autoloader-prefix', null, InputOption::VALUE_REQUIRED, 'Use a custom prefix for the APCu autoloader cache'), new InputOption('ignore-platform-req', null, InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY, 'Ignore a specific platform requirement (php & ext- packages).'), new InputOption('ignore-platform-reqs', null, InputOption::VALUE_NONE, 'Ignore all platform requirements (php & ext- packages).'), new InputOption('prefer-stable', null, InputOption::VALUE_NONE, 'Prefer stable versions of dependencies.'), @@ -189,7 +190,7 @@ EOT $optimize = $input->getOption('optimize-autoloader') || $config->get('optimize-autoloader'); $authoritative = $input->getOption('classmap-authoritative') || $config->get('classmap-authoritative'); - $apcu = strpos($input, '--apcu-autoloader') !== false || $config->get('apcu-autoloader'); + $apcu = $input->getOption('apcu-autoloader') || $config->get('apcu-autoloader'); $updateAllowTransitiveDependencies = Request::UPDATE_ONLY_LISTED; if ($input->getOption('with-all-dependencies')) { @@ -210,7 +211,7 @@ EOT ->setRunScripts(!$input->getOption('no-scripts')) ->setOptimizeAutoloader($optimize) ->setClassMapAuthoritative($authoritative) - ->setApcuAutoloader($apcu ? $input->getOption('apcu-autoloader') ?: true : false) + ->setApcuAutoloader($apcu, $input->getOption('apcu-autoloader-prefix')) ->setUpdate(true) ->setInstall(!$input->getOption('no-install')) ->setUpdateMirrors($updateMirrors) diff --git a/src/Composer/Installer.php b/src/Composer/Installer.php index 17d319e00..50ada1981 100644 --- a/src/Composer/Installer.php +++ b/src/Composer/Installer.php @@ -126,6 +126,7 @@ class Installer protected $optimizeAutoloader = false; protected $classMapAuthoritative = false; protected $apcuAutoloader = false; + protected $apcuAutoloaderPrefix; protected $devMode = false; protected $dryRun = false; protected $verbose = false; @@ -307,7 +308,7 @@ class Installer $this->autoloadGenerator->setDevMode($this->devMode); $this->autoloadGenerator->setClassMapAuthoritative($this->classMapAuthoritative); - $this->autoloadGenerator->setApcu($this->apcuAutoloader); + $this->autoloadGenerator->setApcu($this->apcuAutoloader, $this->apcuAutoloaderPrefix); $this->autoloadGenerator->setRunScripts($this->runScripts); $this->autoloadGenerator->setIgnorePlatformRequirements($this->ignorePlatformReqs); $this->autoloadGenerator->dump($this->config, $localRepo, $this->package, $this->installationManager, 'composer', $this->optimizeAutoloader); @@ -1023,12 +1024,14 @@ class Installer /** * Whether or not generated autoloader considers APCu caching. * - * @param bool|string $apcuAutoloader + * @param bool $apcuAutoloader + * @param string|null $apcuAutoloaderPrefix * @return Installer */ - public function setApcuAutoloader($apcuAutoloader = false) + public function setApcuAutoloader($apcuAutoloader = false, $apcuAutoloaderPrefix = null) { - $this->apcuAutoloader = is_string($apcuAutoloader) ? $apcuAutoloader : (bool) $apcuAutoloader; + $this->apcuAutoloader = $apcuAutoloader; + $this->apcuAutoloaderPrefix = $apcuAutoloaderPrefix; return $this; } diff --git a/tests/Composer/Test/Autoload/AutoloadGeneratorTest.php b/tests/Composer/Test/Autoload/AutoloadGeneratorTest.php index 0cdc58dda..ece9bb2a0 100644 --- a/tests/Composer/Test/Autoload/AutoloadGeneratorTest.php +++ b/tests/Composer/Test/Autoload/AutoloadGeneratorTest.php @@ -853,7 +853,7 @@ EOF; file_put_contents($this->vendorDir.'/c/c/foo/ClassMapBaz.php', 'generator->setClassMapAuthoritative(true); - $this->generator->setApcu('custom\'Prefix'); + $this->generator->setApcu(true, 'custom\'Prefix'); $this->generator->dump($this->config, $this->repository, $package, $this->im, 'composer', false, '_7'); $this->assertFileExists($this->vendorDir.'/composer/autoload_classmap.php', "ClassMap file needs to be generated.");