class BaseCommand
Same name in this branch
- 11.1.x core/lib/Drupal/Core/Ajax/BaseCommand.php \Drupal\Core\Ajax\BaseCommand
Base class for Composer commands
@author Ryan Weaver <ryan@knplabs.com> @author Konstantin Kudryashov <ever.zet@gmail.com>
Hierarchy
- class \Symfony\Component\Console\Command\Command
- class \Composer\Command\BaseCommand extends \Symfony\Component\Console\Command\Command
Expanded class hierarchy of BaseCommand
1 file declares its use of BaseCommand
- ComposerScaffoldCommand.php in composer/
Plugin/ Scaffold/ ComposerScaffoldCommand.php
File
-
vendor/
composer/ composer/ src/ Composer/ Command/ BaseCommand.php, line 45
Namespace
Composer\CommandView source
abstract class BaseCommand extends Command {
/**
* @var Composer|null
*/
private $composer;
/**
* @var IOInterface
*/
private $io;
/**
* Gets the application instance for this command.
*/
public function getApplication() : Application {
$application = parent::getApplication();
if (!$application instanceof Application) {
throw new \RuntimeException('Composer commands can only work with an ' . Application::class . ' instance set');
}
return $application;
}
/**
* @param bool $required Should be set to false, or use `requireComposer` instead
* @param bool|null $disablePlugins If null, reads --no-plugins as default
* @param bool|null $disableScripts If null, reads --no-scripts as default
* @throws \RuntimeException
* @return Composer|null
* @deprecated since Composer 2.3.0 use requireComposer or tryComposer depending on whether you have $required set to true or false
*/
public function getComposer(bool $required = true, ?bool $disablePlugins = null, ?bool $disableScripts = null) {
if ($required) {
return $this->requireComposer($disablePlugins, $disableScripts);
}
return $this->tryComposer($disablePlugins, $disableScripts);
}
/**
* Retrieves the default Composer\Composer instance or throws
*
* Use this instead of getComposer if you absolutely need an instance
*
* @param bool|null $disablePlugins If null, reads --no-plugins as default
* @param bool|null $disableScripts If null, reads --no-scripts as default
* @throws \RuntimeException
*/
public function requireComposer(?bool $disablePlugins = null, ?bool $disableScripts = null) : Composer {
if (null === $this->composer) {
$application = parent::getApplication();
if ($application instanceof Application) {
$this->composer = $application->getComposer(true, $disablePlugins, $disableScripts);
assert($this->composer instanceof Composer);
}
else {
throw new \RuntimeException('Could not create a Composer\\Composer instance, you must inject ' . 'one if this command is not used with a Composer\\Console\\Application instance');
}
}
return $this->composer;
}
/**
* Retrieves the default Composer\Composer instance or null
*
* Use this instead of getComposer(false)
*
* @param bool|null $disablePlugins If null, reads --no-plugins as default
* @param bool|null $disableScripts If null, reads --no-scripts as default
*/
public function tryComposer(?bool $disablePlugins = null, ?bool $disableScripts = null) : ?Composer {
if (null === $this->composer) {
$application = parent::getApplication();
if ($application instanceof Application) {
$this->composer = $application->getComposer(false, $disablePlugins, $disableScripts);
}
}
return $this->composer;
}
/**
* @return void
*/
public function setComposer(Composer $composer) {
$this->composer = $composer;
}
/**
* Removes the cached composer instance
*
* @return void
*/
public function resetComposer() {
$this->composer = null;
$this->getApplication()
->resetComposer();
}
/**
* Whether or not this command is meant to call another command.
*
* This is mainly needed to avoid duplicated warnings messages.
*
* @return bool
*/
public function isProxyCommand() {
return false;
}
/**
* @return IOInterface
*/
public function getIO() {
if (null === $this->io) {
$application = parent::getApplication();
if ($application instanceof Application) {
$this->io = $application->getIO();
}
else {
$this->io = new NullIO();
}
}
return $this->io;
}
/**
* @return void
*/
public function setIO(IOInterface $io) {
$this->io = $io;
}
/**
* @inheritdoc
*
* Backport suggested values definition from symfony/console 6.1+
*
* TODO drop when PHP 8.1 / symfony 6.1+ can be required
*/
public function complete(CompletionInput $input, CompletionSuggestions $suggestions) : void {
$definition = $this->getDefinition();
$name = (string) $input->getCompletionName();
if (CompletionInput::TYPE_OPTION_VALUE === $input->getCompletionType() && $definition->hasOption($name) && ($option = $definition->getOption($name)) instanceof InputOption) {
$option->complete($input, $suggestions);
}
elseif (CompletionInput::TYPE_ARGUMENT_VALUE === $input->getCompletionType() && $definition->hasArgument($name) && ($argument = $definition->getArgument($name)) instanceof InputArgument) {
$argument->complete($input, $suggestions);
}
else {
parent::complete($input, $suggestions);
}
}
/**
* @inheritDoc
*
* @return void
*/
protected function initialize(InputInterface $input, OutputInterface $output) {
// initialize a plugin-enabled Composer instance, either local or global
$disablePlugins = $input->hasParameterOption('--no-plugins');
$disableScripts = $input->hasParameterOption('--no-scripts');
$application = parent::getApplication();
if ($application instanceof Application && $application->getDisablePluginsByDefault()) {
$disablePlugins = true;
}
if ($application instanceof Application && $application->getDisableScriptsByDefault()) {
$disableScripts = true;
}
if ($this instanceof SelfUpdateCommand) {
$disablePlugins = true;
$disableScripts = true;
}
$composer = $this->tryComposer($disablePlugins, $disableScripts);
$io = $this->getIO();
if (null === $composer) {
$composer = Factory::createGlobal($this->getIO(), $disablePlugins, $disableScripts);
}
if ($composer) {
$preCommandRunEvent = new PreCommandRunEvent(PluginEvents::PRE_COMMAND_RUN, $input, $this->getName());
$composer->getEventDispatcher()
->dispatch($preCommandRunEvent->getName(), $preCommandRunEvent);
}
if (true === $input->hasParameterOption([
'--no-ansi',
]) && $input->hasOption('no-progress')) {
$input->setOption('no-progress', true);
}
$envOptions = [
'COMPOSER_NO_AUDIT' => [
'no-audit',
],
'COMPOSER_NO_DEV' => [
'no-dev',
'update-no-dev',
],
'COMPOSER_PREFER_STABLE' => [
'prefer-stable',
],
'COMPOSER_PREFER_LOWEST' => [
'prefer-lowest',
],
'COMPOSER_MINIMAL_CHANGES' => [
'minimal-changes',
],
];
foreach ($envOptions as $envName => $optionNames) {
foreach ($optionNames as $optionName) {
if (true === $input->hasOption($optionName)) {
if (false === $input->getOption($optionName) && (bool) Platform::getEnv($envName)) {
$input->setOption($optionName, true);
}
}
}
}
if (true === $input->hasOption('ignore-platform-reqs')) {
if (!$input->getOption('ignore-platform-reqs') && (bool) Platform::getEnv('COMPOSER_IGNORE_PLATFORM_REQS')) {
$input->setOption('ignore-platform-reqs', true);
$io->writeError('<warning>COMPOSER_IGNORE_PLATFORM_REQS is set. You may experience unexpected errors.</warning>');
}
}
if (true === $input->hasOption('ignore-platform-req') && (!$input->hasOption('ignore-platform-reqs') || !$input->getOption('ignore-platform-reqs'))) {
$ignorePlatformReqEnv = Platform::getEnv('COMPOSER_IGNORE_PLATFORM_REQ');
if (0 === count($input->getOption('ignore-platform-req')) && is_string($ignorePlatformReqEnv) && '' !== $ignorePlatformReqEnv) {
$input->setOption('ignore-platform-req', explode(',', $ignorePlatformReqEnv));
$io->writeError('<warning>COMPOSER_IGNORE_PLATFORM_REQ is set to ignore ' . $ignorePlatformReqEnv . '. You may experience unexpected errors.</warning>');
}
}
parent::initialize($input, $output);
}
/**
* Calls {@see Factory::create()} with the given arguments, taking into account flags and default states for disabling scripts and plugins
*
* @param mixed $config either a configuration array or a filename to read from, if null it will read from
* the default filename
* @return Composer
*/
protected function createComposerInstance(InputInterface $input, IOInterface $io, $config = null, ?bool $disablePlugins = null, ?bool $disableScripts = null) : Composer {
$disablePlugins = $disablePlugins === true || $input->hasParameterOption('--no-plugins');
$disableScripts = $disableScripts === true || $input->hasParameterOption('--no-scripts');
$application = parent::getApplication();
if ($application instanceof Application && $application->getDisablePluginsByDefault()) {
$disablePlugins = true;
}
if ($application instanceof Application && $application->getDisableScriptsByDefault()) {
$disableScripts = true;
}
return Factory::create($io, $config, $disablePlugins, $disableScripts);
}
/**
* Returns preferSource and preferDist values based on the configuration.
*
* @return bool[] An array composed of the preferSource and preferDist values
*/
protected function getPreferredInstallOptions(Config $config, InputInterface $input, bool $keepVcsRequiresPreferSource = false) {
$preferSource = false;
$preferDist = false;
switch ($config->get('preferred-install')) {
case 'source':
$preferSource = true;
break;
case 'dist':
$preferDist = true;
break;
case 'auto':
default:
// noop
break;
}
if (!$input->hasOption('prefer-dist') || !$input->hasOption('prefer-source')) {
return [
$preferSource,
$preferDist,
];
}
if ($input->hasOption('prefer-install') && is_string($input->getOption('prefer-install'))) {
if ($input->getOption('prefer-source')) {
throw new \InvalidArgumentException('--prefer-source can not be used together with --prefer-install');
}
if ($input->getOption('prefer-dist')) {
throw new \InvalidArgumentException('--prefer-dist can not be used together with --prefer-install');
}
switch ($input->getOption('prefer-install')) {
case 'dist':
$input->setOption('prefer-dist', true);
break;
case 'source':
$input->setOption('prefer-source', true);
break;
case 'auto':
$preferDist = false;
$preferSource = false;
break;
default:
throw new \UnexpectedValueException('--prefer-install accepts one of "dist", "source" or "auto", got ' . $input->getOption('prefer-install'));
}
}
if ($input->getOption('prefer-source') || $input->getOption('prefer-dist') || $keepVcsRequiresPreferSource && $input->hasOption('keep-vcs') && $input->getOption('keep-vcs')) {
$preferSource = $input->getOption('prefer-source') || $keepVcsRequiresPreferSource && $input->hasOption('keep-vcs') && $input->getOption('keep-vcs');
$preferDist = $input->getOption('prefer-dist');
}
return [
$preferSource,
$preferDist,
];
}
protected function getPlatformRequirementFilter(InputInterface $input) : PlatformRequirementFilterInterface {
if (!$input->hasOption('ignore-platform-reqs') || !$input->hasOption('ignore-platform-req')) {
throw new \LogicException('Calling getPlatformRequirementFilter from a command which does not define the --ignore-platform-req[s] flags is not permitted.');
}
if (true === $input->getOption('ignore-platform-reqs')) {
return PlatformRequirementFilterFactory::ignoreAll();
}
$ignores = $input->getOption('ignore-platform-req');
if (count($ignores) > 0) {
return PlatformRequirementFilterFactory::fromBoolOrList($ignores);
}
return PlatformRequirementFilterFactory::ignoreNothing();
}
/**
* @param array<string> $requirements
*
* @return array<string, string>
*/
protected function formatRequirements(array $requirements) {
$requires = [];
$requirements = $this->normalizeRequirements($requirements);
foreach ($requirements as $requirement) {
if (!isset($requirement['version'])) {
throw new \UnexpectedValueException('Option ' . $requirement['name'] . ' is missing a version constraint, use e.g. ' . $requirement['name'] . ':^1.0');
}
$requires[$requirement['name']] = $requirement['version'];
}
return $requires;
}
/**
* @param array<string> $requirements
*
* @return list<array{name: string, version?: string}>
*/
protected function normalizeRequirements(array $requirements) {
$parser = new VersionParser();
return $parser->parseNameVersionPairs($requirements);
}
/**
* @param array<TableSeparator|mixed[]> $table
*
* @return void
*/
protected function renderTable(array $table, OutputInterface $output) {
$renderer = new Table($output);
$renderer->setStyle('compact');
$renderer->setRows($table)
->render();
}
/**
* @return int
*/
protected function getTerminalWidth() {
$terminal = new Terminal();
$width = $terminal->getWidth();
if (Platform::isWindows()) {
$width--;
}
else {
$width = max(80, $width);
}
return $width;
}
/**
* @internal
* @param 'format'|'audit-format' $optName
* @return Auditor::FORMAT_*
*/
protected function getAuditFormat(InputInterface $input, string $optName = 'audit-format') : string {
if (!$input->hasOption($optName)) {
throw new \LogicException('This should not be called on a Command which has no ' . $optName . ' option defined.');
}
$val = $input->getOption($optName);
if (!in_array($val, Auditor::FORMATS, true)) {
throw new \InvalidArgumentException('--' . $optName . ' must be one of ' . implode(', ', Auditor::FORMATS) . '.');
}
return $val;
}
}
Members
Title Sort descending | Deprecated | Modifiers | Object type | Summary | Overriden Title | Overrides |
---|---|---|---|---|---|---|
BaseCommand::$composer | private | property | ||||
BaseCommand::$io | private | property | ||||
BaseCommand::complete | public | function | @inheritdoc | Overrides Command::complete | 1 | |
BaseCommand::createComposerInstance | protected | function | Calls { | |||
BaseCommand::formatRequirements | protected | function | ||||
BaseCommand::getApplication | public | function | Gets the application instance for this command. | Overrides Command::getApplication | ||
BaseCommand::getAuditFormat | protected | function | @internal | |||
BaseCommand::getComposer | Deprecated | public | function | |||
BaseCommand::getIO | public | function | ||||
BaseCommand::getPlatformRequirementFilter | protected | function | ||||
BaseCommand::getPreferredInstallOptions | protected | function | Returns preferSource and preferDist values based on the configuration. | |||
BaseCommand::getTerminalWidth | protected | function | ||||
BaseCommand::initialize | protected | function | @inheritDoc | Overrides Command::initialize | 1 | |
BaseCommand::isProxyCommand | public | function | Whether or not this command is meant to call another command. | 2 | ||
BaseCommand::normalizeRequirements | protected | function | ||||
BaseCommand::renderTable | protected | function | ||||
BaseCommand::requireComposer | public | function | Retrieves the default Composer\Composer instance or throws | |||
BaseCommand::resetComposer | public | function | Removes the cached composer instance | |||
BaseCommand::setComposer | public | function | ||||
BaseCommand::setIO | public | function | ||||
BaseCommand::tryComposer | public | function | Retrieves the default Composer\Composer instance or null | |||
Command::$aliases | private | property | 1 | |||
Command::$application | private | property | ||||
Command::$code | private | property | ||||
Command::$definition | private | property | ||||
Command::$description | private | property | 1 | |||
Command::$fullDefinition | private | property | ||||
Command::$help | private | property | ||||
Command::$helperSet | private | property | ||||
Command::$hidden | private | property | ||||
Command::$ignoreValidationErrors | private | property | 2 | |||
Command::$name | private | property | ||||
Command::$processTitle | private | property | ||||
Command::$synopsis | private | property | ||||
Command::$usages | private | property | ||||
Command::addArgument | public | function | Adds an argument. | 2 | ||
Command::addOption | public | function | Adds an option. | 2 | ||
Command::addUsage | public | function | Add a command usage example, it'll be prefixed with the command name. | 2 | ||
Command::configure | protected | function | Configures the current command. | 50 | ||
Command::execute | protected | function | Executes the current command. | 51 | ||
Command::FAILURE | public | constant | ||||
Command::getAliases | public | function | Returns the aliases for the command. | |||
Command::getDefaultDescription | public static | function | ||||
Command::getDefaultName | public static | function | ||||
Command::getDefinition | public | function | Gets the InputDefinition attached to this Command. | 2 | ||
Command::getDescription | public | function | Returns the description for the command. | |||
Command::getHelp | public | function | Returns the help for the command. | 2 | ||
Command::getHelper | public | function | Gets a helper instance by name. | 2 | ||
Command::getHelperSet | public | function | Gets the helper set. | 1 | ||
Command::getName | public | function | Returns the command name. | |||
Command::getNativeDefinition | public | function | Gets the InputDefinition to be used to create representations of this Command. | 2 | ||
Command::getProcessedHelp | public | function | Returns the processed help for the command replacing the %command.name% and %command.full_name% patterns with the real values dynamically. |
2 | ||
Command::getSynopsis | public | function | Returns the synopsis for the command. | 2 | ||
Command::getUsages | public | function | Returns alternative usages of the command. | 2 | ||
Command::ignoreValidationErrors | public | function | Ignores validation errors. | 2 | ||
Command::interact | protected | function | Interacts with the user. | 5 | ||
Command::INVALID | public | constant | ||||
Command::isEnabled | public | function | Checks whether the command is enabled or not in the current environment. | 2 | ||
Command::isHidden | public | function | ||||
Command::mergeApplicationDefinition | public | function | Merges the application definition with the command definition. | 2 | ||
Command::run | public | function | Runs the command. | 4 | ||
Command::setAliases | public | function | Sets the aliases for the command. | |||
Command::setApplication | public | function | 2 | |||
Command::setCode | public | function | Sets the code to execute when running this command. | 2 | ||
Command::setDefinition | public | function | Sets an array of argument and option instances. | 2 | ||
Command::setDescription | public | function | Sets the description for the command. | |||
Command::setHelp | public | function | Sets the help for the command. | 2 | ||
Command::setHelperSet | public | function | 2 | |||
Command::setHidden | public | function | ||||
Command::setName | public | function | Sets the name of the command. | |||
Command::setProcessTitle | public | function | Sets the process title of the command. | 2 | ||
Command::SUCCESS | public | constant | ||||
Command::validateName | private | function | Validates a command name. | |||
Command::__construct | public | function | 15 |