1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of the Gerrie package. |
4
|
|
|
* |
5
|
|
|
* (c) Andreas Grunwald <[email protected]> |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace Gerrie\Component\Configuration; |
12
|
|
|
|
13
|
|
|
use Gerrie\Component\Console\InputExtendedInterface; |
14
|
|
|
use Symfony\Component\Yaml\Yaml; |
15
|
|
|
|
16
|
|
|
class ConfigurationFactory |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* Creates a configuration based on a YAML configuration file. |
20
|
|
|
* |
21
|
|
|
* @param string $configFile |
22
|
|
|
* @return Configuration |
23
|
|
|
*/ |
24
|
3 |
|
public static function getConfigurationByConfigFile($configFile) |
25
|
|
|
{ |
26
|
3 |
|
if (file_exists($configFile) === false) { |
27
|
1 |
|
$message = 'Configuration file "%s" not found or accessible.'; |
28
|
1 |
|
$message = sprintf($message, $configFile); |
29
|
1 |
|
throw new \RuntimeException($message, 1415381521); |
30
|
|
|
} |
31
|
|
|
|
32
|
2 |
|
$config = Yaml::parse($configFile); |
33
|
2 |
|
$configuration = new Configuration($config); |
|
|
|
|
34
|
|
|
|
35
|
2 |
|
return $configuration; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Creates a configuration based on a YAML configuration file, command line options and command line arguments. |
40
|
|
|
* The command line options will be merged into the configuration. |
41
|
|
|
* The command line arguments will be merged into the configuration as well. |
42
|
|
|
* |
43
|
|
|
* The command line options got a higher priority as the configuration file. |
44
|
|
|
* The command line arguments will be added to the configuration file and not overwritten. |
45
|
|
|
* |
46
|
|
|
* @param string $configFile |
47
|
|
|
* @param InputExtendedInterface $input |
48
|
|
|
* @return Configuration |
49
|
|
|
*/ |
50
|
4 |
|
public static function getConfigurationByConfigFileAndCommandOptionsAndArguments($configFile, InputExtendedInterface $input) |
51
|
|
|
{ |
52
|
4 |
|
if ($input->isOptionSet('config-file') === true) { |
53
|
1 |
|
$configuration = self::getConfigurationByConfigFile($configFile); |
54
|
1 |
|
} else { |
55
|
3 |
|
$configuration = new Configuration(); |
56
|
|
|
} |
57
|
|
|
|
58
|
4 |
|
$configuration = self::mergeCommandOptionsIntoConfiguration($configuration, $input); |
59
|
4 |
|
$configuration = self::mergeCommandArgumentsIntoConfiguration($configuration, $input); |
60
|
|
|
|
61
|
4 |
|
return $configuration; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Merges the command line options into a existing configuration. |
66
|
|
|
* |
67
|
|
|
* E.g. |
68
|
|
|
* * Database credentials |
69
|
|
|
* * SSH settings |
70
|
|
|
* |
71
|
|
|
* @param Configuration $config |
72
|
|
|
* @param InputExtendedInterface $input |
73
|
|
|
* @return Configuration |
74
|
|
|
*/ |
75
|
4 |
|
protected static function mergeCommandOptionsIntoConfiguration(Configuration $config, InputExtendedInterface $input) |
76
|
|
|
{ |
77
|
|
|
$configurationMapping = [ |
78
|
|
|
// Database credentials |
79
|
4 |
|
'database-host' => 'Database.Host', |
80
|
4 |
|
'database-user' => 'Database.Username', |
81
|
4 |
|
'database-pass' => 'Database.Password', |
82
|
4 |
|
'database-port' => 'Database.Port', |
83
|
4 |
|
'database-name' => 'Database.Name', |
84
|
|
|
// SSH settings |
85
|
4 |
|
'ssh-key' => 'SSH.KeyFile', |
86
|
4 |
|
]; |
87
|
|
|
|
88
|
4 |
|
foreach ($configurationMapping as $optionName => $configName) { |
89
|
4 |
|
if ($input->isOptionSet($optionName) === true) { |
90
|
4 |
|
$config->setConfigurationValue($configName, $input->getOption($optionName)); |
91
|
|
|
|
92
|
|
|
// If the value is not set / available, set this to nothing |
93
|
4 |
|
} elseif (!$config->getConfigurationValue($configName)) { |
94
|
4 |
|
$config->setConfigurationValue($configName, null); |
95
|
4 |
|
} |
96
|
4 |
|
} |
97
|
|
|
|
98
|
4 |
|
return $config; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Merges the command line arguments into a existing configuration. |
103
|
|
|
* |
104
|
|
|
* E.g. |
105
|
|
|
* * Instances |
106
|
|
|
* |
107
|
|
|
* @param Configuration $config |
108
|
|
|
* @param InputExtendedInterface $input |
109
|
|
|
* @return Configuration |
110
|
|
|
*/ |
111
|
4 |
|
protected static function mergeCommandArgumentsIntoConfiguration(Configuration $config, InputExtendedInterface $input) |
112
|
|
|
{ |
113
|
4 |
|
if ($input->hasArgument('instances') === false) { |
114
|
2 |
|
return $config; |
115
|
|
|
} |
116
|
|
|
|
117
|
2 |
|
$argumentInstances = $input->getArgument('instances'); |
118
|
|
|
|
119
|
2 |
|
if (count($argumentInstances) === 0) { |
120
|
1 |
|
return $config; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
// Gerrie is a reserved keyword for project names |
124
|
1 |
|
$config->setConfigurationValue('Gerrit.Gerrie', $argumentInstances); |
125
|
|
|
|
126
|
1 |
|
return $config; |
127
|
|
|
} |
128
|
|
|
} |
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.