1
0
Fork 0

APCu prefix as a separate option

pull/9212/head
Lars Strojny 2020-09-16 10:49:00 +02:00
parent de0085767c
commit 0b6abf3b96
No known key found for this signature in database
GPG Key ID: 887416A2AD3B0CA9
8 changed files with 41 additions and 25 deletions

View File

@ -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 .= <<<APCU
\$loader->setApcuPrefix($apcuPrefix);

View File

@ -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('<info>Generating optimized autoload files (authoritative)</info>');
@ -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);

View File

@ -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)
;

View File

@ -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(
<<<EOT
@ -227,7 +228,7 @@ EOT
$updateDevMode = !$input->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)

View File

@ -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(
<<<EOT
@ -269,7 +270,7 @@ EOT
$updateDevMode = !$input->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)

View File

@ -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)

View File

@ -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;
}

View File

@ -853,7 +853,7 @@ EOF;
file_put_contents($this->vendorDir.'/c/c/foo/ClassMapBaz.php', '<?php class ClassMapBaz {}');
$this->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.");