|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
/* |
|
6
|
|
|
* This file is part of the box project. |
|
7
|
|
|
* |
|
8
|
|
|
* (c) Kevin Herrera <[email protected]> |
|
9
|
|
|
* Théo Fidry <[email protected]> |
|
10
|
|
|
* |
|
11
|
|
|
* This source file is subject to the MIT license that is bundled |
|
12
|
|
|
* with this source code in the file LICENSE. |
|
13
|
|
|
*/ |
|
14
|
|
|
|
|
15
|
|
|
namespace KevinGH\Box\Console\Command; |
|
16
|
|
|
|
|
17
|
|
|
use KevinGH\Box\Configuration\Configuration; |
|
18
|
|
|
use KevinGH\Box\Console\ConfigurationLoader; |
|
19
|
|
|
use KevinGH\Box\Console\IO\IO; |
|
20
|
|
|
use KevinGH\Box\Json\JsonValidationException; |
|
21
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
|
22
|
|
|
use Symfony\Component\Console\Input\InputOption; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* Allows a configuration file path to be specified for a command. |
|
26
|
|
|
* |
|
27
|
|
|
* @private |
|
28
|
|
|
*/ |
|
29
|
|
|
abstract class ConfigurableBaseCommand extends BaseCommand |
|
30
|
|
|
{ |
|
31
|
|
|
private const CONFIG_PARAM = 'config'; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* {@inheritdoc} |
|
35
|
|
|
*/ |
|
36
|
|
|
protected function configure(): void |
|
37
|
|
|
{ |
|
38
|
|
|
$this->addOption( |
|
39
|
|
|
self::CONFIG_PARAM, |
|
40
|
|
|
'c', |
|
41
|
|
|
InputOption::VALUE_REQUIRED, |
|
42
|
|
|
'The alternative configuration file path.' |
|
43
|
|
|
); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* Returns the configuration settings. |
|
48
|
|
|
* |
|
49
|
|
|
* @param bool $allowNoFile Load the config nonetheless if not file is found when true |
|
50
|
|
|
* |
|
51
|
|
|
* @throws JsonValidationException |
|
52
|
|
|
*/ |
|
53
|
|
|
final protected function getConfig(IO $io, bool $allowNoFile = false): Configuration |
|
54
|
|
|
{ |
|
55
|
|
|
return ConfigurationLoader::getConfig( |
|
56
|
|
|
$io->getInput()->getOption(self::CONFIG_PARAM), |
|
57
|
|
|
$this->getConfigurationHelper(), |
|
58
|
|
|
$io, |
|
59
|
|
|
$allowNoFile |
|
60
|
|
|
); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
final protected function getConfigPath(InputInterface $input): string |
|
64
|
|
|
{ |
|
65
|
|
|
return $input->getOption(self::CONFIG_PARAM) ?? $this->getConfigurationHelper()->findDefaultPath(); |
|
66
|
|
|
} |
|
67
|
|
|
} |
|
68
|
|
|
|