1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of CaptainHook |
5
|
|
|
* |
6
|
|
|
* (c) Sebastian Feldmann <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace CaptainHook\App\Console\Command; |
13
|
|
|
|
14
|
|
|
use CaptainHook\App\CH; |
15
|
|
|
use CaptainHook\App\Config; |
16
|
|
|
use CaptainHook\App\Console\Command; |
17
|
|
|
use CaptainHook\App\Console\IOUtil; |
18
|
|
|
use RuntimeException; |
19
|
|
|
use SebastianFeldmann\Camino\Check; |
20
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
21
|
|
|
use Symfony\Component\Console\Input\InputOption; |
22
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Class ConfigAware |
26
|
|
|
* |
27
|
|
|
* Base class for all commands that need to be aware of the CaptainHook configuration. |
28
|
|
|
* |
29
|
|
|
* @package CaptainHook\App |
30
|
|
|
*/ |
31
|
|
|
abstract class ConfigAware extends Command |
32
|
|
|
{ |
33
|
|
|
/** |
34
|
|
|
* Set up the configuration command option |
35
|
|
|
* |
36
|
|
|
* @return void |
37
|
|
|
*/ |
38
|
43 |
|
protected function configure(): void |
39
|
|
|
{ |
40
|
43 |
|
parent::configure(); |
41
|
43 |
|
$this->addOption( |
42
|
43 |
|
'configuration', |
43
|
43 |
|
'c', |
44
|
43 |
|
InputOption::VALUE_OPTIONAL, |
45
|
43 |
|
'Path to your captainhook.json configuration', |
46
|
43 |
|
'./' . CH::CONFIG |
47
|
43 |
|
); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Create a new Config object |
52
|
|
|
* |
53
|
|
|
* @param \Symfony\Component\Console\Input\InputInterface $input |
54
|
|
|
* @param bool $failIfNotFound |
55
|
|
|
* @param array<string> $settings |
56
|
|
|
* @return \CaptainHook\App\Config |
57
|
|
|
* @throws \Exception |
58
|
|
|
*/ |
59
|
40 |
|
protected function createConfig(InputInterface $input, bool $failIfNotFound = false, array $settings = []): Config |
60
|
|
|
{ |
61
|
40 |
|
$config = Config\Factory::create($this->getConfigPath($input), $this->fetchConfigSettings($input, $settings)); |
62
|
40 |
|
if ($failIfNotFound && !$config->isLoadedFromFile()) { |
63
|
6 |
|
throw new RuntimeException( |
64
|
6 |
|
'Please create a captainhook configuration first' . PHP_EOL . |
65
|
6 |
|
'Run \'captainhook configure\'' . PHP_EOL . |
66
|
6 |
|
'If you have a configuration located elsewhere use the --configuration option' |
67
|
6 |
|
); |
68
|
|
|
} |
69
|
34 |
|
return $config; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Return the given config path option |
74
|
|
|
* |
75
|
|
|
* @param \Symfony\Component\Console\Input\InputInterface $input |
76
|
|
|
* @return string |
77
|
|
|
*/ |
78
|
40 |
|
private function getConfigPath(InputInterface $input): string |
79
|
|
|
{ |
80
|
40 |
|
$path = IOUtil::argToString($input->getOption('configuration')); |
81
|
|
|
|
82
|
|
|
// if path not absolute |
83
|
40 |
|
if (!Check::isAbsolutePath($path)) { |
84
|
|
|
// try to guess the config location and |
85
|
|
|
// transform relative path to absolute path |
86
|
7 |
|
if (substr($path, 0, 2) === './') { |
87
|
1 |
|
return getcwd() . substr($path, 1); |
88
|
|
|
} |
89
|
6 |
|
return getcwd() . '/' . $path; |
90
|
|
|
} |
91
|
33 |
|
return $path; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Return list of available options to overwrite the configuration settings |
96
|
|
|
* |
97
|
|
|
* @param \Symfony\Component\Console\Input\InputInterface $input |
98
|
|
|
* @param array<string> $settingNames |
99
|
|
|
* @return array<string, string> |
100
|
|
|
*/ |
101
|
40 |
|
private function fetchConfigSettings(InputInterface $input, array $settingNames): array |
102
|
|
|
{ |
103
|
40 |
|
$settings = []; |
104
|
40 |
|
foreach ($settingNames as $setting) { |
105
|
34 |
|
$value = IOUtil::argToString($input->getOption($setting)); |
106
|
34 |
|
if (!empty($value)) { |
107
|
31 |
|
$settings[$setting] = $value; |
108
|
|
|
} |
109
|
|
|
} |
110
|
40 |
|
return $settings; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Check which verbosity to use, either the cli option or the config file setting |
115
|
|
|
* |
116
|
|
|
* @param \Symfony\Component\Console\Output\OutputInterface $out |
117
|
|
|
* @param \CaptainHook\App\Config $config |
118
|
|
|
* @return void |
119
|
|
|
*/ |
120
|
32 |
|
protected function determineVerbosity(OutputInterface $out, Config $config): void |
121
|
|
|
{ |
122
|
32 |
|
$confVerbosity = IOUtil::mapConfigVerbosity($config->getVerbosity()); |
123
|
32 |
|
$cliVerbosity = $out->getVerbosity(); |
124
|
32 |
|
$out->setVerbosity(max($confVerbosity, $cliVerbosity)); |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
|