setName('config-set')
->setDefinition([
new InputArgument('key', InputArgument::OPTIONAL, 'Runtime-configurable option to update.'),
new CodeArgument('value', CodeArgument::OPTIONAL, 'New runtime value for the selected option.'),
])
->setDescription('Update one runtime-configurable PsySH setting for the current session.');
}
public function asText(): string
{
return \implode("\n", [
'Usage:',
' config set \\ \\',
'',
'Help:',
' Set a runtime-configurable PsySH setting for the current session.',
'',
'Examples:',
' >>> config set verbosity debug',
' >>> config set pager off',
' >>> config set \\ --help',
'',
'Supported Options:',
$this->renderSettableKeys(),
]);
}
public function asTextForInput(InputInterface $input): string
{
$key = $input->getArgument('key');
if ($key === null) {
return $this->asText();
}
$option = $this->getOption((string) $key);
if ($option === null) {
return $this->asText();
}
return \implode("\n", [
'Usage:',
\sprintf(' config set %s \\', $option['name']),
'',
'Help:',
\sprintf(' Set %s for the current session.', $this->formatOptionName($option['name'])),
'',
'Accepted Values:',
' '.$this->formatAcceptedValues($option),
'',
'Current Value:',
' '.$this->formatValue($option['getter']()),
]);
}
protected function execute(InputInterface $input, OutputInterface $output): int
{
$key = $input->getArgument('key');
if ($key === null) {
throw new \InvalidArgumentException('Please specify a runtime-configurable option to update.');
}
$option = $this->getOption($key);
if ($option === null) {
$output->writeln(\sprintf('%s', $this->unsupportedMessage((string) $key)));
return 1;
}
$rawValue = $input->getArgument('value');
if ($rawValue === null) {
throw new \InvalidArgumentException(\sprintf('Please specify a value for `%s`. Accepted values: %s', $option['name'], $this->formatAcceptedValues($option)));
}
try {
$value = $option['parser']((string) $rawValue);
$changed = $option['setter']($value);
} catch (\InvalidArgumentException $e) {
$output->writeln(\sprintf('%s', $e->getMessage()));
return 1;
}
if ($option['refresh'] && $changed !== false) {
$this->getShell()->applyRuntimeConfigChange($option['name']);
}
$output->writeln(\sprintf(
'%s = %s',
$option['name'],
$this->formatValue($option['getter']())
));
return 0;
}
private function renderSettableKeys(): string
{
$lines = [];
foreach ($this->getOptions() as $option) {
$lines[] = \sprintf(' %s (%s)', $this->formatOptionName($option['name']), $this->formatAcceptedValues($option));
}
return \implode("\n", $lines);
}
}