1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SumoCoders\FrameworkCoreBundle\Installer; |
4
|
|
|
|
5
|
|
|
use Composer\IO\IOInterface; |
6
|
|
|
use Symfony\Component\Yaml\Dumper; |
7
|
|
|
use Symfony\Component\Yaml\Exception\ParseException; |
8
|
|
|
use Symfony\Component\Yaml\Parser; |
9
|
|
|
|
10
|
|
|
class Installer |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* Get a decorated message |
14
|
|
|
* |
15
|
|
|
* @param string $message |
16
|
|
|
* @param mixed $type |
17
|
|
|
* @param bool $isDecorated |
18
|
|
|
* @return string |
19
|
|
|
*/ |
20
|
2 |
|
public function getDecoratedMessage($message, $type = null, $isDecorated = true) |
21
|
|
|
{ |
22
|
2 |
|
if ($isDecorated && $type !== null) { |
23
|
1 |
|
$message = '<' . $type . '>' . $message . '</' . $type . '>'; |
24
|
|
|
} |
25
|
|
|
|
26
|
2 |
|
return $message; |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @param IOInterface $io |
31
|
|
|
* @param string $question |
32
|
|
|
* @param mixed $default |
33
|
|
|
* @return string |
34
|
|
|
*/ |
35
|
|
|
public function ask(IOInterface $io, $question, $default = null) |
36
|
|
|
{ |
37
|
|
|
$question = $this->getDecoratedMessage($question, 'question', $io->isDecorated()) . ' '; |
38
|
|
|
if ($default !== null) { |
39
|
|
|
$question .= '(' . $this->getDecoratedMessage($default, 'comment', $io->isDecorated()) . ')'; |
40
|
|
|
} |
41
|
|
|
$question .= ': '; |
42
|
|
|
|
43
|
|
|
return $io->ask($question, $default); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @param IOInterface $io |
48
|
|
|
* @param string $question |
49
|
|
|
* @param bool $default |
50
|
|
|
* @return bool |
51
|
|
|
*/ |
52
|
|
|
public function askConfirmation(IOInterface $io, $question, $default = true) |
53
|
|
|
{ |
54
|
|
|
$question = $this->getDecoratedMessage($question, 'question', $io->isDecorated()) . ' '; |
55
|
|
|
$question .= '(Y/n): '; |
56
|
|
|
|
57
|
|
|
return $io->askConfirmation($question, $default); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Extract the client and project from a given path |
62
|
|
|
* As everyone should use the same setup we can get the client en project from the path. |
63
|
|
|
* |
64
|
|
|
* @param string $path |
65
|
|
|
* @return array |
66
|
|
|
*/ |
67
|
4 |
|
public function extractInformationFromPath($path) |
68
|
|
|
{ |
69
|
|
|
$defaultReturn = [ |
70
|
4 |
|
'is_local' => (substr($path, 0, 7) == '/Users/'), |
71
|
|
|
'client' => null, |
72
|
|
|
'project' => null, |
73
|
|
|
]; |
74
|
4 |
|
$chunks = explode('/', mb_strtolower(trim($path, '/'))); |
75
|
4 |
|
$sitesOffset = array_search('sites', $chunks); |
76
|
|
|
|
77
|
4 |
|
if ($sitesOffset) { |
78
|
2 |
View Code Duplication |
if (isset($chunks[$sitesOffset + 1])) { |
|
|
|
|
79
|
2 |
|
$defaultReturn['client'] = $chunks[$sitesOffset + 1]; |
80
|
|
|
} |
81
|
2 |
View Code Duplication |
if (isset($chunks[$sitesOffset + 2])) { |
|
|
|
|
82
|
2 |
|
$defaultReturn['project'] = $chunks[$sitesOffset + 2]; |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|
86
|
4 |
|
return $defaultReturn; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Update the Capfile with information provided in the config |
91
|
|
|
* |
92
|
|
|
* @param string $path |
93
|
|
|
* @param array $config |
94
|
|
|
*/ |
95
|
1 |
|
public function updateCapfile($path, array $config = null) |
96
|
|
|
{ |
97
|
1 |
|
$content = file_get_contents($path); |
98
|
|
|
|
99
|
1 |
|
if ($config !== null) { |
100
|
1 |
|
foreach ($config as $variableName => $value) { |
101
|
1 |
|
$content = $this->replaceSetRubyVar($variableName, $value, $content); |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
|
105
|
1 |
|
file_put_contents($path, $content); |
106
|
1 |
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Replace a variable set in Ruby with the actual value |
110
|
|
|
* |
111
|
|
|
* @param string $variableName |
112
|
|
|
* @param string $value |
113
|
|
|
* @param string $content |
114
|
|
|
* @return string |
115
|
|
|
*/ |
116
|
1 |
|
protected function replaceSetRubyVar($variableName, $value, $content) |
117
|
|
|
{ |
118
|
1 |
|
return preg_replace( |
119
|
1 |
|
'/(set.*:' . $variableName . ',.*)\'.*\'/iU', |
120
|
1 |
|
'$1\'' . $value . '\'', |
121
|
1 |
|
$content |
122
|
|
|
); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* Update a YAML-file with information provided in the config |
127
|
|
|
* |
128
|
|
|
* @param string $path |
129
|
|
|
* @param array $config |
130
|
|
|
*/ |
131
|
1 |
|
public function updateYmlFile($path, array $config = null) |
132
|
|
|
{ |
133
|
1 |
|
$yamlParser = new Parser(); |
134
|
1 |
|
$yamlDumper = new Dumper(); |
135
|
|
|
|
136
|
|
|
try { |
137
|
1 |
|
$content = $yamlParser->parse(file_get_contents($path)); |
138
|
|
|
|
139
|
|
|
$newContent = [ |
140
|
1 |
|
'parameters' => $this->arrayReplaceExisting($content['parameters'], $config) |
141
|
|
|
]; |
142
|
|
|
|
143
|
1 |
|
$yamlString = $yamlDumper->dump($newContent, 2); |
144
|
1 |
|
file_put_contents($path, $yamlString); |
145
|
|
|
} catch (ParseException $e) { |
146
|
|
|
// ignore errors |
147
|
|
|
} |
148
|
1 |
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* Replaces elements from passed arrays into the first array recursively but only when the key exists. |
152
|
|
|
* |
153
|
|
|
* @param array $content |
154
|
|
|
* @param array $config |
155
|
|
|
* @return array mixed |
156
|
|
|
*/ |
157
|
1 |
|
public function arrayReplaceExisting(array $content, array $config = null) |
158
|
|
|
{ |
159
|
1 |
|
if (null === $config) { |
160
|
|
|
return $content; |
161
|
|
|
} |
162
|
|
|
|
163
|
1 |
|
foreach ($config as $key => $value) { |
164
|
1 |
|
if (is_array($value)) { |
165
|
|
|
// if it has only numeric keys we can handle it as a value |
166
|
|
|
if (array_keys($value) == range(0, count($value) - 1)) { |
167
|
|
|
if (array_key_exists($key, $content)) { |
168
|
|
|
$content[$key] = $value; |
169
|
|
|
} |
170
|
|
|
} else { |
171
|
|
|
$content[$key] = $this->arrayReplaceExisting($content[$key], $value); |
172
|
|
|
} |
173
|
|
|
} else { |
174
|
1 |
|
if (array_key_exists($key, $content)) { |
175
|
1 |
|
$content[$key] = $value; |
176
|
|
|
} |
177
|
|
|
} |
178
|
|
|
} |
179
|
|
|
|
180
|
1 |
|
return $content; |
181
|
|
|
} |
182
|
|
|
} |
183
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.