Various fixes

* Fixed the init command not being registered
* pdoshell: Improved configuration handling
This commit is contained in:
2022-02-08 01:45:55 +01:00
parent 7c4ca953f6
commit 7e04b6c001
3 changed files with 210 additions and 4 deletions

View File

@ -29,23 +29,46 @@ class PdoShell {
private ?array $lastQuery = null;
private ShellState $state;
#[EnumSetting('output', [ 'table', 'vertical', 'dump' ])]
#[EnumSetting('table.style', [ 'box', 'compact', 'borderless' ])]
private array $defaultOptions = [
'output' => 'table',
'table.maxwidth' => 40,
'table.style' => 'box',
'capture' => false,
'syntax' => 'none',
];
private array $validOptions = [
'output' => [ 'table', 'vertical', 'dump' ],
'table.maxwidth' => 'int',
'table.style' => [ 'box', 'compact', 'borderless' ],
'capture' => 'bool',
'syntax' => [ 'none', 'mysql', 'sqlite' ]
];
public function __construct(OutputInterface $output)
{
$this->output = $output;
$this->state = new ShellState();
$this->options = $this->defaultOptions;
}
public function getState(): ShellState
{
return $this->state;
}
private function promptForCommand()
{
$prompt = sprintf("PDO:[%s%s]> ", $this->resource, $this->db?"":"?");
$info = $this->resource . ($this->db?"":"?");
if ($this->getOption('capture')) {
$info .= '][#' . $this->vars['capture_index'];
}
$prompt = sprintf("PDO:[%s]> ", $info);
$input = readline($prompt);
return $input;
@ -62,6 +85,11 @@ class PdoShell {
];
}
private function getOption(string $name): mixed
{
return $this->options[$name] ?? null;
}
private function expand(string $string): string
{
return $string;
@ -125,6 +153,10 @@ class PdoShell {
case '.help':
$this->doHelpCommand($args);
break;
case '.capture':
$this->doSetCommand(['capture', 1]);
$this->doVarCommand(['capture_index', 0]);
break;
case '.quit':
case '.exit':
@ -211,7 +243,14 @@ class PdoShell {
if (empty($varname)) {
foreach ($this->options as $var=>$value) {
$this->output->writeln("<info>{$var}</>: <comment>".var_export($value,true)."</>");
if (!array_key_exists($var, $this->validOptions)) {
$info = "";
} elseif (is_array($this->validOptions[$var])) {
$info = "[<fg=cyan>".join("</>,<fg=cyan>", $this->validOptions[$var])."</>]";
} else {
$info = "<<fg=magenta>".$this->validOptions[$var]."</>>";
}
$this->output->writeln("<info>{$var}</>: <comment>".var_export($value,true)."</> ".$info);
}
return;
}
@ -221,6 +260,33 @@ class PdoShell {
$this->output->writeln("<error>No such option {$varname}</>");
return;
}
if (array_key_exists($varname, $this->validOptions)) {
$v = $this->validOptions[$varname];
if (is_array($v)) {
if (!in_array($value, $v)) {
$this->output->writeln("<error>Bad value for {$varname}</> Valid values: ".join(", ",$v)."</>");
return;
}
} elseif (is_string($v)) {
switch ($v) {
case 'int':
if (!ctype_digit($value)) {
$this->output->writeln("<error>Bad value for {$varname}</> Expected integer</>");
return;
}
break;
case 'bool':
$true = in_array($value, [ 1, "true", "yes", "on" ]);
$false = in_array($value, [ 0, "false", "no", "off" ]);
if (!$true && !$false) {
$this->output->writeln("<error>Bad value for {$varname}</> Expected boolean</>");
return;
}
$value = $true;
break;
}
}
}
$this->options[$varname] = $value;
} else {
$this->output->writeln(var_export($this->options[$varname]??null,true));
@ -368,4 +434,4 @@ class EnumSetting
{
}
}
}