|
1
|
|
|
<?php |
|
2
|
|
|
namespace FMUP\Config; |
|
3
|
|
|
|
|
4
|
|
|
use FMUP\Config; |
|
5
|
|
|
|
|
6
|
|
|
class Ini implements ConfigInterface |
|
7
|
|
|
{ |
|
8
|
|
|
private $filePath; |
|
9
|
|
|
private $section; |
|
10
|
|
|
private $config = array(); |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* @param string $filePath |
|
14
|
|
|
* @param string|null $section |
|
15
|
|
|
*/ |
|
16
|
7 |
|
public function __construct($filePath, $section = null) |
|
17
|
|
|
{ |
|
18
|
7 |
|
$this->filePath = (string)$filePath; |
|
19
|
7 |
|
$this->section = is_null($section) ? null : (string) $section; |
|
20
|
7 |
|
} |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @return string |
|
24
|
|
|
* @throws Exception if file do not exists |
|
25
|
|
|
*/ |
|
26
|
7 |
|
protected function getFilePath() |
|
27
|
|
|
{ |
|
28
|
7 |
|
if (!file_exists($this->filePath)) { |
|
29
|
2 |
|
throw new Exception('File does not exist'); |
|
30
|
|
|
} |
|
31
|
5 |
|
return $this->filePath; |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @return null|string |
|
36
|
|
|
*/ |
|
37
|
5 |
|
protected function getSection() |
|
38
|
|
|
{ |
|
39
|
5 |
|
return $this->section; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @return array |
|
44
|
|
|
* @throws Exception if file do not exists |
|
45
|
|
|
*/ |
|
46
|
4 |
|
protected function getConfig() |
|
47
|
|
|
{ |
|
48
|
4 |
|
if (!$this->config) { |
|
|
|
|
|
|
49
|
4 |
|
$this->config = parse_ini_file($this->getFilePath(), $this->getSection()); |
|
50
|
|
|
} |
|
51
|
3 |
|
return $this->config; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* @param string|null $key |
|
56
|
|
|
* @return mixed|null |
|
57
|
|
|
*/ |
|
58
|
3 |
|
public function get($key = null) |
|
59
|
|
|
{ |
|
60
|
3 |
|
if ($this->has($key) || is_null($key)) { |
|
61
|
2 |
|
$config = $this->getConfig(); |
|
62
|
2 |
|
return is_null($key) ? $config : $config[$key]; |
|
63
|
|
|
} |
|
64
|
1 |
|
return null; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* @param string $paramName |
|
69
|
|
|
* @param string|null $value |
|
70
|
|
|
* @return $this |
|
71
|
|
|
*/ |
|
72
|
1 |
|
public function set($paramName, $value = null) |
|
73
|
|
|
{ |
|
74
|
1 |
|
$this->config = $this->getConfig(); |
|
75
|
1 |
|
$this->config[$paramName] = $value; |
|
76
|
1 |
|
return $this; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* @param string $paramName |
|
81
|
|
|
* @return bool |
|
82
|
|
|
*/ |
|
83
|
4 |
|
public function has($paramName) |
|
84
|
|
|
{ |
|
85
|
4 |
|
$config = $this->getConfig(); |
|
86
|
3 |
|
return isset($config[$paramName]); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* @param array $paramArray |
|
91
|
|
|
* @param bool|false $before |
|
92
|
|
|
* @return $this |
|
93
|
|
|
*/ |
|
94
|
1 |
|
public function mergeConfig(array $paramArray = array(), $before = false) |
|
95
|
|
|
{ |
|
96
|
1 |
|
if ($before) { |
|
97
|
1 |
|
$this->config = $this->getConfig() + $paramArray; |
|
98
|
|
|
} else { |
|
99
|
1 |
|
$this->config = $paramArray + $this->getConfig(); |
|
100
|
|
|
} |
|
101
|
1 |
|
return $this; |
|
102
|
|
|
} |
|
103
|
|
|
} |
|
104
|
|
|
|
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)or! empty(...)instead.