1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Karma; |
4
|
|
|
|
5
|
|
|
use Gaufrette\Filesystem; |
6
|
|
|
use Symfony\Component\Yaml\Yaml; |
7
|
|
|
use Symfony\Component\Yaml\Exception\ParseException; |
8
|
|
|
use Karma\Formatters\Raw; |
9
|
|
|
use Karma\Formatters\Rules; |
10
|
|
|
|
11
|
|
|
class ProfileReader |
12
|
|
|
{ |
13
|
|
|
const |
14
|
|
|
DEFAULT_FORMATTER_INDEX = 'default'; |
15
|
|
|
|
16
|
|
|
private |
17
|
|
|
$templatesSuffix, |
|
|
|
|
18
|
|
|
$masterFilename, |
19
|
|
|
$configurationDirectory, |
20
|
|
|
$formatters, |
21
|
|
|
$defaultFormatterName; |
22
|
|
|
|
23
|
|
|
public function __construct(Filesystem $fs) |
24
|
|
|
{ |
25
|
|
|
$this->templatesSuffix = null; |
26
|
|
|
$this->masterFilename = null; |
27
|
|
|
$this->configurationDirectory = null; |
28
|
|
|
|
29
|
|
|
$this->defaultFormatterName = self::DEFAULT_FORMATTER_INDEX; |
30
|
|
|
$this->formatters = array( |
31
|
|
|
self::DEFAULT_FORMATTER_INDEX => new Raw(), |
32
|
|
|
); |
33
|
|
|
|
34
|
|
|
$this->read($fs); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
private function read(Filesystem $fs) |
38
|
|
|
{ |
39
|
|
|
$profileFilename = Application::PROFILE_FILENAME; |
40
|
|
|
|
41
|
|
|
if($fs->has($profileFilename)) |
42
|
|
|
{ |
43
|
|
|
$this->processProfileContent($fs->read($profileFilename)); |
44
|
|
|
} |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
private function processProfileContent($content) |
48
|
|
|
{ |
49
|
|
|
try |
50
|
|
|
{ |
51
|
|
|
$values = Yaml::parse($content); |
52
|
|
|
} |
53
|
|
|
catch(ParseException $e) |
54
|
|
|
{ |
55
|
|
|
throw new \RuntimeException(sprintf( |
56
|
|
|
'Error while parsing profile : %s', |
57
|
|
|
$e->getMessage() |
58
|
|
|
)); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
if(isset($values['suffix'])) |
62
|
|
|
{ |
63
|
|
|
$this->templatesSuffix = $values['suffix']; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
if(isset($values['master'])) |
67
|
|
|
{ |
68
|
|
|
$this->masterFilename = $values['master']; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
if(isset($values['confDir'])) |
72
|
|
|
{ |
73
|
|
|
$this->configurationDirectory = $values['confDir']; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
if(isset($values['formatters'])) |
77
|
|
|
{ |
78
|
|
|
$this->parseFormatters($values['formatters']); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
if(isset($values['defaultFormatter']) && is_string($values['defaultFormatter'])) |
82
|
|
|
{ |
83
|
|
|
$this->defaultFormatterName = $values['defaultFormatter']; |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
private function parseFormatters($content) |
88
|
|
|
{ |
89
|
|
|
if(! is_array($content)) |
90
|
|
|
{ |
91
|
|
|
throw new \InvalidArgumentException('Syntax error in profile [formatters]'); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
foreach($content as $name => $rules) |
95
|
|
|
{ |
96
|
|
|
if(! is_array($rules)) |
97
|
|
|
{ |
98
|
|
|
throw new \InvalidArgumentException('Syntax error in profile [formatters]'); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
$this->formatters[$name] = new Rules($rules); |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
public function hasTemplatesSuffix() |
106
|
|
|
{ |
107
|
|
|
return $this->templatesSuffix !== null; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
public function getTemplatesSuffix() |
111
|
|
|
{ |
112
|
|
|
return $this->templatesSuffix; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
public function hasMasterFilename() |
116
|
|
|
{ |
117
|
|
|
return $this->masterFilename !== null; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
public function getMasterFilename() |
121
|
|
|
{ |
122
|
|
|
return $this->masterFilename; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
public function hasConfigurationDirectory() |
126
|
|
|
{ |
127
|
|
|
return $this->configurationDirectory !== null; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
public function getConfigurationDirectory() |
131
|
|
|
{ |
132
|
|
|
return $this->configurationDirectory; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
public function hasFormatter($index) |
136
|
|
|
{ |
137
|
|
|
return isset($this->formatters[$index]); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
public function getFormatter($index = null) |
141
|
|
|
{ |
142
|
|
|
$formatter = $this->formatters[$this->getDefaultFormatterName()]; |
143
|
|
|
|
144
|
|
|
if($this->hasFormatter($index)) |
145
|
|
|
{ |
146
|
|
|
$formatter = $this->formatters[$index]; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
return $formatter; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
private function getDefaultFormatterName() |
153
|
|
|
{ |
154
|
|
|
$name = self::DEFAULT_FORMATTER_INDEX; |
155
|
|
|
|
156
|
|
|
if($this->hasFormatter($this->defaultFormatterName)) |
157
|
|
|
{ |
158
|
|
|
$name = $this->defaultFormatterName; |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
return $name; |
162
|
|
|
} |
163
|
|
|
} |
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.