Fixed CLI --env and --lang options having no effect if they aren't added before all the other options

This commit is contained in:
Matias Griese 2022-01-05 19:52:57 +02:00
parent c4eefc13a7
commit cc8ec10098
2 changed files with 31 additions and 10 deletions

View File

@ -1,3 +1,9 @@
# v1.7.27
## 01/04/2022
3. [](#bugfix)
* Fixed CLI `--env` and `--lang` options having no effect if they aren't added before all the other options
# v1.7.26.1
## 01/04/2022

View File

@ -10,11 +10,14 @@
namespace Grav\Console\Application;
use Grav\Common\Grav;
use Symfony\Component\Console\ConsoleEvents;
use Symfony\Component\Console\Event\ConsoleCommandEvent;
use Symfony\Component\Console\Formatter\OutputFormatterStyle;
use Symfony\Component\Console\Input\InputDefinition;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\EventDispatcher\EventDispatcher;
/**
* Class GpmApplication
@ -30,16 +33,28 @@ class Application extends \Symfony\Component\Console\Application
protected $initialized = false;
/**
* @param InputInterface $input
* @return string|null
* PluginApplication constructor.
* @param string $name
* @param string $version
*/
public function getCommandName(InputInterface $input): ?string
public function __construct(string $name = 'UNKNOWN', string $version = 'UNKNOWN')
{
parent::__construct($name, $version);
// Add listener to prepare environment.
$dispatcher = new EventDispatcher();
$dispatcher->addListener(ConsoleEvents::COMMAND, [$this, 'prepareEnvironment']);
$this->setDispatcher($dispatcher);
}
public function prepareEnvironment(ConsoleCommandEvent $event): void
{
$input = $event->getInput();
$this->environment = $input->getOption('env');
$this->language = $input->getOption('lang') ?? $this->language;
$this->init();
return parent::getCommandName($input);
$this->init();
}
/**
@ -58,7 +73,7 @@ class Application extends \Symfony\Component\Console\Application
}
/**
* Add global a --env option.
* Add global --env and --lang options.
*
* @return InputDefinition
*/
@ -67,16 +82,16 @@ class Application extends \Symfony\Component\Console\Application
$inputDefinition = parent::getDefaultInputDefinition();
$inputDefinition->addOption(
new InputOption(
'env',
null,
'--env',
'',
InputOption::VALUE_OPTIONAL,
'Use environment configuration (defaults to localhost)'
)
);
$inputDefinition->addOption(
new InputOption(
'lang',
null,
'--lang',
'',
InputOption::VALUE_OPTIONAL,
'Language to be used (defaults to en)'
)