1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
|
4
|
|
|
namespace Shopware\Psh\Config; |
5
|
|
|
|
6
|
|
|
use Symfony\Component\Yaml\Parser; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Load the config data from a yaml file |
10
|
|
|
*/ |
11
|
|
|
class YamlConfigFileLoader extends ConfigFileLoader |
12
|
|
|
{ |
13
|
|
|
const KEY_HEADER = 'header'; |
14
|
|
|
|
15
|
|
|
const KEY_DYNAMIC_VARIABLES = 'dynamic'; |
16
|
|
|
|
17
|
|
|
const KEY_CONST_VARIABLES = 'const'; |
18
|
|
|
|
19
|
|
|
const KEY_DOTENV_PATHS = 'dotenv'; |
20
|
|
|
|
21
|
|
|
const KEY_COMMAND_PATHS = 'paths'; |
22
|
|
|
|
23
|
|
|
const KEY_ENVIRONMENTS = 'environments'; |
24
|
|
|
|
25
|
|
|
const KEY_TEMPLATES = 'templates'; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var Parser |
29
|
|
|
*/ |
30
|
|
|
private $yamlReader; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var ConfigBuilder |
34
|
|
|
*/ |
35
|
|
|
private $configBuilder; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var string |
39
|
|
|
*/ |
40
|
|
|
private $applicationRootDirectory; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @param Parser $yamlReader |
44
|
|
|
* @param ConfigBuilder $configBuilder |
45
|
|
|
* @param string $applicationRootDirectory |
46
|
|
|
*/ |
47
|
|
|
public function __construct(Parser $yamlReader, ConfigBuilder $configBuilder, string $applicationRootDirectory) |
48
|
|
|
{ |
49
|
|
|
$this->yamlReader = $yamlReader; |
50
|
|
|
$this->configBuilder = $configBuilder; |
51
|
|
|
$this->applicationRootDirectory = $applicationRootDirectory; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @inheritdoc |
56
|
|
|
*/ |
57
|
|
|
public function isSupported(string $file): bool |
58
|
|
|
{ |
59
|
|
|
return in_array(pathinfo($file, PATHINFO_BASENAME), ['.psh.yaml', '.psh.yml', '.psh.yml.dist', '.psh.yml.override', '.psh.yaml.dist', '.psh.yaml.override'], true); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @inheritdoc |
64
|
|
|
*/ |
65
|
|
View Code Duplication |
public function load(string $file, array $params): Config |
|
|
|
|
66
|
|
|
{ |
67
|
|
|
$contents = $this->loadFileContents($file); |
68
|
|
|
$rawConfigData = $this->parseFileContents($contents); |
69
|
|
|
|
70
|
|
|
$this->configBuilder->start(); |
71
|
|
|
|
72
|
|
|
$this->configBuilder |
73
|
|
|
->setHeader( |
74
|
|
|
$this->extractData(self::KEY_HEADER, $rawConfigData, '') |
75
|
|
|
); |
76
|
|
|
|
77
|
|
|
$this->setConfigData($file, $rawConfigData); |
78
|
|
|
|
79
|
|
|
$environments = $this->extractData(self::KEY_ENVIRONMENTS, $rawConfigData, []); |
80
|
|
|
|
81
|
|
|
foreach ($environments as $name => $data) { |
82
|
|
|
$this->configBuilder->start($name); |
83
|
|
|
$this->setConfigData($file, $data); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
return $this->configBuilder |
87
|
|
|
->create($params); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @param string $file |
92
|
|
|
* @param array $rawConfigData |
93
|
|
|
*/ |
94
|
|
|
private function setConfigData(string $file, array $rawConfigData) |
95
|
|
|
{ |
96
|
|
|
$this->configBuilder->setCommandPaths( |
97
|
|
|
$this->extractPaths($file, $rawConfigData, self::KEY_COMMAND_PATHS) |
98
|
|
|
); |
99
|
|
|
|
100
|
|
|
$this->configBuilder->setDynamicVariables( |
|
|
|
|
101
|
|
|
$this->extractData(self::KEY_DYNAMIC_VARIABLES, $rawConfigData, []) |
102
|
|
|
); |
103
|
|
|
|
104
|
|
|
$this->configBuilder->setConstants( |
|
|
|
|
105
|
|
|
$this->extractData(self::KEY_CONST_VARIABLES, $rawConfigData, []) |
106
|
|
|
); |
107
|
|
|
|
108
|
|
|
$this->configBuilder->setTemplates( |
109
|
|
|
$this->extractTemplates($file, $rawConfigData) |
110
|
|
|
); |
111
|
|
|
|
112
|
|
|
$this->configBuilder->setDotenvPaths( |
113
|
|
|
$this->extractPaths($file, $rawConfigData, self::KEY_DOTENV_PATHS) |
114
|
|
|
); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* @param string $key |
119
|
|
|
* @param array $rawConfig |
120
|
|
|
* @param mixed $default |
121
|
|
|
* @return mixed|null |
122
|
|
|
*/ |
123
|
|
|
private function extractData(string $key, array $rawConfig, $default = false) |
124
|
|
|
{ |
125
|
|
|
if (!array_key_exists($key, $rawConfig)) { |
126
|
|
|
return $default; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
return $rawConfig[$key]; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
|
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* @param string $contents |
136
|
|
|
* @return array |
137
|
|
|
*/ |
138
|
|
|
private function parseFileContents(string $contents): array |
139
|
|
|
{ |
140
|
|
|
return $this->yamlReader->parse($contents); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* @param string $file |
145
|
|
|
* @param array $rawConfigData |
146
|
|
|
* @param string $key |
147
|
|
|
* |
148
|
|
|
* @return array |
149
|
|
|
*/ |
150
|
|
|
private function extractPaths(string $file, array $rawConfigData, string $key): array |
151
|
|
|
{ |
152
|
|
|
$paths = $this->extractData($key, $rawConfigData, []); |
153
|
|
|
|
154
|
|
|
return array_map(function ($path) use ($file) { |
155
|
|
|
return $this->fixPath($this->applicationRootDirectory, $path, $file); |
156
|
|
|
}, $paths); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* @param string $file |
161
|
|
|
* @param array $rawConfigData |
162
|
|
|
* @return array |
163
|
|
|
*/ |
164
|
|
|
private function extractTemplates(string $file, array $rawConfigData): array |
165
|
|
|
{ |
166
|
|
|
$templates = $this->extractData(self::KEY_TEMPLATES, $rawConfigData, []); |
167
|
|
|
|
168
|
|
|
return array_map(function ($template) use ($file) { |
169
|
|
|
$template['source'] = $this->fixPath($this->applicationRootDirectory, $template['source'], $file); |
170
|
|
|
$template['destination'] = $this->makeAbsolutePath($file, $template['destination']); |
171
|
|
|
|
172
|
|
|
return $template; |
173
|
|
|
}, $templates); |
174
|
|
|
} |
175
|
|
|
} |
176
|
|
|
|
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.