-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathSettingsHandler.php
110 lines (98 loc) · 3.42 KB
/
SettingsHandler.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
<?php
namespace DigipolisGent\Robo\Helpers\EventHandler\DefaultHandler;
use Ckr\Util\ArrayMerger;
use DigipolisGent\CommandBuilder\CommandBuilder;
use DigipolisGent\Robo\Helpers\EventHandler\AbstractTaskEventHandler;
use DigipolisGent\Robo\Task\Deploy\Ssh\Auth\KeyFile;
use DigipolisGent\Robo\Task\General\Common\DigipolisPropertiesAware;
use Robo\Contract\ConfigAwareInterface;
abstract class SettingsHandler extends AbstractTaskEventHandler implements ConfigAwareInterface
{
use DigipolisPropertiesAware;
use \DigipolisGent\Robo\Task\General\Tasks;
use \DigipolisGent\Robo\Task\Deploy\Tasks;
use \Consolidation\Config\ConfigAwareTrait;
public static $defaultEnvironmentOverrideSettings = [
'environment_env_var' => 'HOSTNAME',
'environment_matcher' => '\\DigipolisGent\\Robo\\Helpers\\Util\\EnvironmentMatcher::regexMatch',
];
/**
* Process environment-specific overrides.
*
* @param array $settings
* @return array
*/
protected function processEnvironmentOverrides($settings)
{
$settings += static::$defaultEnvironmentOverrideSettings;
if (!isset($settings['environment_overrides']) || !$settings['environment_overrides']) {
return $settings;
}
$server = $this->getFirstServer($settings);
if (!$server) {
return $settings;
}
// Parse the env var on the server.
$auth = new KeyFile($settings['user'], $settings['private-key']);
$fullOutput = '';
$this->taskSsh($server, $auth)
->exec(
(string) CommandBuilder::create('echo')
->addRawArgument('$' . $settings['environment_env_var']),
function ($output) use (&$fullOutput) {
$fullOutput .= $output;
}
)
->run();
$envVarValue = substr($fullOutput, 0, (strpos($fullOutput, "\n") ?: strlen($fullOutput)));
foreach ($settings['environment_overrides'] as $environmentMatch => $overrides) {
if (call_user_func($settings['environment_matcher'], $environmentMatch, $envVarValue)) {
$settings = ArrayMerger::doMerge($settings, $overrides);
}
}
return $settings;
}
/**
* Get the first server entry from the remote settings.
*
* @param array $settings
*
* @return string|bool
* First server if found, false otherwise.
*
* @see self::processEnvironmentOverrides
*/
protected function getFirstServer($settings)
{
foreach ($settings as $key => $value) {
if (preg_match('/^server/', $key) === 1) {
return $value;
}
}
return false;
}
/**
* Helper functions to replace tokens in an array.
*
* @param string|array $input
* The array or string containing the tokens to replace.
* @param array $replacements
* The token replacements.
*
* @return string|array
* The input with the tokens replaced with their values.
*/
protected function tokenReplace($input, $replacements)
{
if (is_string($input)) {
return strtr($input, $replacements);
}
if (is_scalar($input) || empty($input)) {
return $input;
}
foreach ($input as &$i) {
$i = $this->tokenReplace($i, $replacements);
}
return $input;
}
}