|
1
|
|
|
<?php |
|
2
|
|
|
namespace Ciandt\Behat\PlaceholdersExtension\Config; |
|
3
|
|
|
|
|
4
|
|
|
use Symfony\Component\Yaml\Yaml; |
|
5
|
|
|
use Ciandt\Behat\PlaceholdersExtension\Utils\PlaceholderUtils; |
|
6
|
|
|
use Ciandt\Behat\PlaceholdersExtension\PlaceholderContainer\PlaceholderContainer; |
|
7
|
|
|
use Ciandt\Behat\PlaceholdersExtension\Exception\MissingSectionException; |
|
8
|
|
|
use Ciandt\Behat\PlaceholdersExtension\Exception\UndefinedPlaceholderException; |
|
9
|
|
|
use Exception; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* Description of ConfigsRepository |
|
13
|
|
|
* |
|
14
|
|
|
* @author bwowk |
|
15
|
|
|
*/ |
|
16
|
|
|
class PlaceholdersRepository |
|
17
|
|
|
{ |
|
18
|
|
|
const PLACEHOLDER_REGEX = '/\${(?P<placeholder>[a-zA-Z0-9_-]+)}/'; |
|
19
|
|
|
|
|
20
|
|
|
private $configs; |
|
21
|
|
|
|
|
22
|
|
|
private $beforeScenarioSubscriber; |
|
23
|
|
|
|
|
24
|
|
|
private $placeholders = array(); |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @var string |
|
28
|
|
|
*/ |
|
29
|
|
|
private $environment; |
|
30
|
|
|
|
|
31
|
|
|
public function __construct($configs_mapping, $beforeScenarioSubscriber) |
|
32
|
|
|
{ |
|
33
|
|
|
$this->configs = $this->loadConfigFiles($configs_mapping); |
|
34
|
|
|
$this->beforeScenarioSubscriber = $beforeScenarioSubscriber; |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* |
|
39
|
|
|
* @return string[] |
|
40
|
|
|
* @todo read configs and also bring alternative @config:section tags |
|
41
|
|
|
*/ |
|
42
|
|
|
public function getTags() |
|
43
|
|
|
{ |
|
44
|
|
|
return array_keys($this->configs); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* reads the YAML placeholder definitions and returns an associative array |
|
49
|
|
|
* |
|
50
|
|
|
* @param type $config_files |
|
|
|
|
|
|
51
|
|
|
* @todo use %paths.base% value |
|
52
|
|
|
* @return array |
|
53
|
|
|
*/ |
|
54
|
|
|
private function loadConfigFiles($configs_mapping) |
|
55
|
|
|
{ |
|
56
|
|
|
$placeholder_maps = array(); |
|
57
|
|
|
foreach ($configs_mapping as $tag => $file_path) { |
|
58
|
|
|
$placeholder_maps[$tag]['config'] = Yaml::parse(file_get_contents($file_path)); |
|
59
|
|
|
$placeholder_maps[$tag]['path'] = $file_path; |
|
60
|
|
|
} |
|
61
|
|
|
return $placeholder_maps; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
View Code Duplication |
private function getConfig($key) |
|
|
|
|
|
|
65
|
|
|
{ |
|
66
|
|
|
if (key_exists($key, $this->configs)) { |
|
67
|
|
|
return $this->configs[$key]['config']; |
|
68
|
|
|
} |
|
69
|
|
|
return null; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
View Code Duplication |
private function getFilePath($key) |
|
|
|
|
|
|
73
|
|
|
{ |
|
74
|
|
|
if (key_exists($key, $this->configs)) { |
|
75
|
|
|
return $this->configs[$key]['path']; |
|
76
|
|
|
} |
|
77
|
|
|
return null; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
public function getEnvironment() |
|
81
|
|
|
{ |
|
82
|
|
|
return $this->environment; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
public function setEnvironment($environment) |
|
86
|
|
|
{ |
|
87
|
|
|
$this->environment = $environment; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
private function getScenarioTags(){ |
|
91
|
|
|
return $this->beforeScenarioSubscriber->getScenarioTags(); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
public function getReplacement($placeholder, $replaced = array()) { |
|
95
|
|
|
|
|
96
|
|
|
// if the current placeholder was already replaced before, this is a cyclic dependecy |
|
97
|
|
|
if (in_array($placeholder, $replaced)){ |
|
98
|
|
|
$tree = implode('>', $replaced); |
|
99
|
|
|
throw new Exception("Cyclic placeholder dependecy detected. Trying to replace $placeholder again when already replaced: $tree"); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
// if there's a runtime placeholder defined for that key, return it right away |
|
103
|
|
|
if (array_key_exists($placeholder, $this->placeholders)) { |
|
104
|
|
|
return $this->placeholders[$placeholder]; |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
$tags = $this->getScenarioTags(); |
|
108
|
|
|
|
|
109
|
|
|
//@todo abort if there's no config tag |
|
110
|
|
|
$configTag = PlaceholderUtils::getConfigTag($tags); |
|
111
|
|
|
$configKey = PlaceholderUtils::getConfigKey($configTag); |
|
112
|
|
|
$section = PlaceholderUtils::getSectionKey($configTag); |
|
113
|
|
|
$placeholders = $this->getSectionPlaceholders($configKey, $section); |
|
114
|
|
|
|
|
115
|
|
|
$variantTags = PlaceholderUtils::filterVariantTags($tags, false); |
|
116
|
|
|
$variant = end($variantTags); |
|
117
|
|
|
$environment = $this->getEnvironment(); |
|
118
|
|
|
$configPath = $this->getFilePath($configKey); |
|
119
|
|
|
$keys = array('$' . $variant, '$' . $environment, $placeholder); |
|
120
|
|
|
$treePosition = "$configPath>$section>placeholders"; |
|
121
|
|
|
|
|
122
|
|
|
$replacement = $this->recursivePlaceholderSearch($keys, $placeholders, $treePosition); |
|
123
|
|
|
|
|
124
|
|
|
if (is_null($replacement)){ |
|
125
|
|
|
throw new UndefinedPlaceholderException("No $placeholder placeholder was defined on runtime or on $treePosition>$placeholder for variant $variant and environment $environment"); |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
//if the replaced string doesn't have placeholders itself |
|
129
|
|
|
if (preg_match_all(self::PLACEHOLDER_REGEX, $replacement, $matches, PREG_SET_ORDER) == 0){ |
|
130
|
|
|
return $replacement; |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
//if it does have, replace them before returning |
|
134
|
|
|
foreach ($matches as $match) { |
|
|
|
|
|
|
135
|
|
|
$replaced[] = $placeholder; |
|
136
|
|
|
$replacement = str_replace('${' . $match['placeholder'] . '}', $this->getReplacement($match['placeholder'], $replaced), $replacement); |
|
137
|
|
|
} |
|
138
|
|
|
return $replacement; |
|
139
|
|
|
|
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
private function recursivePlaceholderSearch($keys, $values, $treePosition) |
|
143
|
|
|
{ |
|
144
|
|
|
if (empty($keys) || is_scalar($values)) { |
|
145
|
|
|
return $values; |
|
146
|
|
|
} |
|
147
|
|
|
$key = array_pop($keys); |
|
148
|
|
|
if (key_exists($key, $values)) { |
|
149
|
|
|
$specificValue = $this->recursivePlaceholderSearch($keys, $values[$key], "$treePosition>$key"); |
|
150
|
|
|
if ($specificValue) { |
|
151
|
|
|
return $specificValue; |
|
152
|
|
|
} |
|
153
|
|
|
} if (key_exists('$default', $values)) { |
|
154
|
|
|
$defaultValue = $this->recursivePlaceholderSearch($keys, $values['$default'], $treePosition . '>$default'); |
|
155
|
|
|
if ($defaultValue) { |
|
156
|
|
|
return $defaultValue; |
|
157
|
|
|
} |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
return null; |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
private function getSectionPlaceholders($configKey, $section){ |
|
164
|
|
|
$config = $this->getConfig($configKey); |
|
165
|
|
|
if (!isset($config) || !key_exists($section, $config)){ |
|
166
|
|
|
throw new MissingSectionException( |
|
167
|
|
|
$configKey, |
|
168
|
|
|
$this->getFilePath($configKey), |
|
169
|
|
|
$section); |
|
170
|
|
|
} |
|
171
|
|
|
return $config[$section]['placeholders']; |
|
172
|
|
|
} |
|
173
|
|
|
|
|
174
|
|
|
public function setPlaceholder($key, $value, $environment = '$default', $variant = '$default') { |
|
175
|
|
|
if (!isset($this->placeholders[$key])) { |
|
176
|
|
|
$this->placeholders[$key] = array(); |
|
177
|
|
|
} |
|
178
|
|
|
if (!isset($this->placeholders[$key][$environment])) { |
|
179
|
|
|
$this->placeholders[$key][$environment] = array(); |
|
180
|
|
|
} |
|
181
|
|
|
$this->placeholders[$key][$environment][$variant] = $value; |
|
182
|
|
|
} |
|
183
|
|
|
|
|
184
|
|
|
} |
|
185
|
|
|
|
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italyis not defined by the methodfinale(...).The most likely cause is that the parameter was removed, but the annotation was not.