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
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Description of ConfigsRepository |
12
|
|
|
* |
13
|
|
|
* @author bwowk |
14
|
|
|
*/ |
15
|
|
|
class PlaceholdersRepository |
16
|
|
|
{ |
17
|
|
|
|
18
|
|
|
private $configs; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var string |
22
|
|
|
*/ |
23
|
|
|
private $environment; |
24
|
|
|
|
25
|
|
|
public function __construct($configs_mapping) |
26
|
|
|
{ |
27
|
|
|
$this->configs = $this->loadConfigFiles($configs_mapping); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* |
32
|
|
|
* @return string[] |
33
|
|
|
* @todo read configs and also bring alternative @config:section tags |
34
|
|
|
*/ |
35
|
|
|
public function getTags() |
36
|
|
|
{ |
37
|
|
|
return array_keys($this->configs); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* |
42
|
|
|
* @param type $config_files |
|
|
|
|
43
|
|
|
* @todo user %paths.base% value |
44
|
|
|
*/ |
45
|
|
|
private function loadConfigFiles($configs_mapping) |
46
|
|
|
{ |
47
|
|
|
$placeholder_maps = array(); |
48
|
|
|
foreach ($configs_mapping as $tag => $file_path) { |
49
|
|
|
$placeholder_maps[$tag]['config'] = Yaml::parse(file_get_contents($file_path)); |
50
|
|
|
$placeholder_maps[$tag]['path'] = $file_path; |
51
|
|
|
} |
52
|
|
|
return $placeholder_maps; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
View Code Duplication |
public function getConfig($key) |
|
|
|
|
56
|
|
|
{ |
57
|
|
|
if (key_exists($key, $this->configs)) { |
58
|
|
|
return $this->configs[$key]['config']; |
59
|
|
|
} |
60
|
|
|
return null; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
View Code Duplication |
public function getFilePath($key) |
|
|
|
|
64
|
|
|
{ |
65
|
|
|
if (key_exists($key, $this->configs)) { |
66
|
|
|
return $this->configs[$key]['path']; |
67
|
|
|
} |
68
|
|
|
return null; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
public function getEnvironment() |
72
|
|
|
{ |
73
|
|
|
return $this->environment; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
public function setEnvironment($environment) |
77
|
|
|
{ |
78
|
|
|
$this->environment = $environment; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
public function getReplacement($placeholder, PlaceholderContainer $container) { |
82
|
|
|
$placeholders = $this->getSectionPlaceholders($container); |
83
|
|
|
|
84
|
|
|
$variant = $container->getVariant(); |
85
|
|
|
$environment = $this->getEnvironment(); |
86
|
|
|
$configPath = $this->getFilePath($container->getConfigKey()); |
87
|
|
|
$section = $container->getSectionKey(); |
88
|
|
|
$keys = array('$' . $variant, '$' . $environment, $placeholder); |
89
|
|
|
$treePosition = "$configPath>$section>placeholders"; |
90
|
|
|
|
91
|
|
|
return $this->recursivePlaceholderSearch($keys, $placeholders, $treePosition); |
92
|
|
|
|
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
private function recursivePlaceholderSearch($keys, $values, $treePosition) |
96
|
|
|
{ |
97
|
|
|
if (empty($keys) || is_string($values)) { |
98
|
|
|
return $values; |
99
|
|
|
} |
100
|
|
|
$key = array_pop($keys); |
101
|
|
|
if (key_exists($key, $values)) { |
102
|
|
|
return $this->recursivePlaceholderSearch($keys, $values[$key], "$treePosition>$key"); |
103
|
|
|
} elseif (key_exists('$default', $values)) { |
104
|
|
|
return $this->recursivePlaceholderSearch($keys, $values['$default'], $treePosition . '>$default'); |
105
|
|
|
} else { |
106
|
|
|
throw new UndefinedPlaceholderException("No placeholder is defined on $treePosition>$key"); |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
private function getSectionPlaceholders(PlaceholderContainer $container){ |
111
|
|
|
$configKey = $container->getConfigKey(); |
112
|
|
|
$config = $this->getConfig($configKey); |
113
|
|
|
$sectionKey = $container->getSectionKey(); |
114
|
|
|
if (!key_exists($sectionKey, $config)){ |
115
|
|
|
throw new MissingSectionException( |
116
|
|
|
$configKey, |
117
|
|
|
$this->getFilePath($configKey), |
118
|
|
|
$sectionKey); |
119
|
|
|
} |
120
|
|
|
return $config[$sectionKey]['placeholders']; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
} |
124
|
|
|
|
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
$italy
is not defined by the methodfinale(...)
.The most likely cause is that the parameter was removed, but the annotation was not.