|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Karma\Configuration\Parser; |
|
4
|
|
|
|
|
5
|
|
|
class VariableParser extends AbstractSectionParser |
|
6
|
|
|
{ |
|
7
|
|
|
use \Karma\Configuration\FilterInputVariable; |
|
8
|
|
|
|
|
9
|
|
|
const |
|
10
|
|
|
ASSIGNMENT = '=', |
|
11
|
|
|
ENV_SEPARATOR = ',', |
|
12
|
|
|
DEFAULT_ENV = 'default'; |
|
13
|
|
|
|
|
14
|
|
|
private |
|
15
|
|
|
$currentVariable, |
|
|
|
|
|
|
16
|
|
|
$variables, |
|
17
|
|
|
$valueFound; |
|
18
|
|
|
|
|
19
|
|
|
public function __construct() |
|
20
|
|
|
{ |
|
21
|
|
|
$this->currentVariable = null; |
|
22
|
|
|
$this->variables = array(); |
|
23
|
|
|
$this->valueFound = false; |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
|
|
protected function parseLine($line) |
|
27
|
|
|
{ |
|
28
|
|
|
if($this->isACommentLine($line)) |
|
29
|
|
|
{ |
|
30
|
|
|
return true; |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
$variableName = $this->extractVariableName($line); |
|
34
|
|
|
|
|
35
|
|
|
if($variableName !== null) |
|
36
|
|
|
{ |
|
37
|
|
|
return $this->changeCurrentVariable($variableName); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
$this->parseEnvironmentValue($line); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
private function extractVariableName($line) |
|
44
|
|
|
{ |
|
45
|
|
|
$variableName = null; |
|
46
|
|
|
|
|
47
|
|
|
if(preg_match('~(?P<variableName>[^:]+):$~', $line, $matches)) |
|
48
|
|
|
{ |
|
49
|
|
|
$variableName = trim($matches['variableName']); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
return $variableName; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
private function checkCurrentVariableState() |
|
56
|
|
|
{ |
|
57
|
|
|
if($this->currentVariable !== null && $this->valueFound === false) |
|
58
|
|
|
{ |
|
59
|
|
|
$this->triggerError(sprintf( |
|
60
|
|
|
'Variable %s has no value (declared in file %s line %d)', |
|
61
|
|
|
$this->currentVariable, |
|
62
|
|
|
$this->variables[$this->currentVariable]['file'], |
|
63
|
|
|
$this->variables[$this->currentVariable]['line'] |
|
64
|
|
|
)); |
|
65
|
|
|
} |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
private function changeCurrentVariable($variableName) |
|
69
|
|
|
{ |
|
70
|
|
|
$this->checkCurrentVariableState(); |
|
71
|
|
|
$this->currentVariable = $variableName; |
|
72
|
|
|
|
|
73
|
|
|
if(isset($this->variables[$this->currentVariable])) |
|
74
|
|
|
{ |
|
75
|
|
|
$this->triggerError(sprintf( |
|
76
|
|
|
'Variable %s is already declared (in %s line %d)', |
|
77
|
|
|
$this->currentVariable, |
|
78
|
|
|
$this->variables[$this->currentVariable]['file'], |
|
79
|
|
|
$this->variables[$this->currentVariable]['line'] |
|
80
|
|
|
)); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
$this->variables[$this->currentVariable] = array( |
|
84
|
|
|
'env' => array(), |
|
85
|
|
|
'file' => $this->currentFilePath, |
|
86
|
|
|
'line' => $this->currentLineNumber, |
|
87
|
|
|
); |
|
88
|
|
|
|
|
89
|
|
|
$this->valueFound = false; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
private function parseEnvironmentValue($line) |
|
93
|
|
|
{ |
|
94
|
|
|
if($this->currentVariable === null) |
|
95
|
|
|
{ |
|
96
|
|
|
$this->triggerError('Missing variable name'); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
if(substr_count($line, self::ASSIGNMENT) < 1) |
|
100
|
|
|
{ |
|
101
|
|
|
$this->triggerError("line must contains = ($line)"); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
list($envList, $value) = explode(self::ASSIGNMENT, $line, 2); |
|
105
|
|
|
$environments = array_map('trim', explode(self::ENV_SEPARATOR, $envList)); |
|
106
|
|
|
|
|
107
|
|
|
$value = $this->parseList($value); |
|
108
|
|
|
$value = $this->filterValue($value); |
|
109
|
|
|
|
|
110
|
|
|
foreach($environments as $environment) |
|
111
|
|
|
{ |
|
112
|
|
|
if(array_key_exists($environment, $this->variables[$this->currentVariable]['env'])) |
|
113
|
|
|
{ |
|
114
|
|
|
$this->triggerError("Duplicated value for environment $environment and variable $this->currentVariable"); |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
$this->variables[$this->currentVariable]['env'][$environment] = $value; |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
$this->valueFound = true; |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
public function getVariables() |
|
124
|
|
|
{ |
|
125
|
|
|
return $this->variables; |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
public function setCurrentFile($filePath) |
|
129
|
|
|
{ |
|
130
|
|
|
parent::setCurrentFile($filePath); |
|
131
|
|
|
|
|
132
|
|
|
$this->currentVariable = null; |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
public function endOfFileCheck() |
|
136
|
|
|
{ |
|
137
|
|
|
$this->checkCurrentVariableState(); |
|
138
|
|
|
} |
|
139
|
|
|
} |
Only declaring a single property per statement allows you to later on add doc comments more easily.
It is also recommended by PSR2, so it is a common style that many people expect.