1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Konfig. |
5
|
|
|
* |
6
|
|
|
* Yet another simple configuration loader library. |
7
|
|
|
* |
8
|
|
|
* PHP version 5 |
9
|
|
|
* |
10
|
|
|
* @category Library |
11
|
|
|
* @package Konfig |
12
|
|
|
* @author Xeriab Nabil (aka KodeBurner) <[email protected]> |
13
|
|
|
* @license https://raw.github.com/xeriab/konfig/master/LICENSE MIT |
14
|
|
|
* @link https://xeriab.github.io/projects/konfig |
15
|
|
|
*/ |
16
|
|
|
|
17
|
|
|
namespace Exen\Konfig\FileParser; |
18
|
|
|
|
19
|
|
|
use Exception; |
20
|
|
|
use Exen\Konfig\Exception\ParseException; |
21
|
|
|
use Exen\Konfig\Utils; |
22
|
|
|
use Symfony\Component\Yaml\Yaml as YamlParser; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Konfig's YAML parser class. |
26
|
|
|
* |
27
|
|
|
* @category FileParser |
28
|
|
|
* @package Konfig |
29
|
|
|
* @author Xeriab Nabil (aka KodeBurner) <[email protected]> |
30
|
|
|
* @license https://raw.github.com/xeriab/konfig/master/LICENSE MIT |
31
|
|
|
* @link https://xeriab.github.io/projects/konfig |
32
|
|
|
* |
33
|
|
|
* @implements Exen\Konfig\FileParser\AbstractFileParser |
34
|
|
|
*/ |
35
|
|
|
class Yaml extends AbstractFileParser |
36
|
|
|
{ |
37
|
|
|
/** |
38
|
|
|
* Loads a YAML/YML file as an array. |
39
|
|
|
* |
40
|
|
|
* @param string $path File path |
41
|
|
|
* |
42
|
|
|
* @throws ParseException If there is an error parsing YAML/YML file |
43
|
|
|
* |
44
|
|
|
* @return array|object|stdClass The parsed data |
45
|
|
|
* |
46
|
|
|
* @since 0.1.0 |
47
|
|
|
*/ |
48
|
6 |
|
public function parse($path) |
49
|
|
|
{ |
50
|
6 |
|
$data = null; |
51
|
|
|
|
52
|
|
|
try { |
53
|
6 |
|
$data = $this->loadFile($path); |
54
|
4 |
|
} catch (Exception $ex) { |
55
|
3 |
|
throw new ParseException( |
56
|
|
|
[ |
57
|
3 |
|
'message' => 'Error parsing YAML file', |
58
|
3 |
|
'file' => realpath($path), |
59
|
3 |
|
'line' => $ex->getParsedLine(), |
|
|
|
|
60
|
3 |
|
'exception' => $ex, |
61
|
|
|
] |
62
|
1 |
|
); |
63
|
|
|
} |
64
|
|
|
|
65
|
3 |
|
return $data; |
|
|
|
|
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* {@inheritdoc} |
70
|
|
|
* |
71
|
|
|
* @return array Supported extensions |
72
|
|
|
* |
73
|
|
|
* @since 0.1.0 |
74
|
|
|
*/ |
75
|
3 |
|
public function getSupportedFileExtensions() |
76
|
|
|
{ |
77
|
3 |
|
return ['yaml', 'yml']; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Loads in the given file and parses it. |
82
|
|
|
* |
83
|
|
|
* @param string $file File to load |
84
|
|
|
* |
85
|
|
|
* @return string|array|stdClass The parsed file data |
86
|
|
|
* |
87
|
|
|
* @since 0.2.4 |
88
|
|
|
* @codeCoverageIgnore |
89
|
|
|
*/ |
90
|
|
|
protected function loadFile($file = null) |
91
|
|
|
{ |
92
|
|
|
$this->file = $file; |
93
|
|
|
$contents = $this->parseVars(Utils::getContent($file)); |
94
|
|
|
|
95
|
|
|
if (extension_loaded('yaml')) { |
96
|
|
|
return yaml_parse($contents); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
return YamlParser::parse($contents); |
|
|
|
|
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Returns the formatted configuration file contents. |
104
|
|
|
* |
105
|
|
|
* @param array $contents configuration array |
106
|
|
|
* |
107
|
|
|
* @return string formatted configuration file contents |
108
|
|
|
* |
109
|
|
|
* @since 0.2.4 |
110
|
|
|
* @codeCoverageIgnore |
111
|
|
|
*/ |
112
|
|
|
protected function exportFormat($contents = null) |
113
|
|
|
{ |
114
|
|
|
$this->prepVars($contents); |
|
|
|
|
115
|
|
|
|
116
|
|
|
if (extension_loaded('yaml')) { |
117
|
|
|
return yaml_emit($contents); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
return YamlParser::dump($contents); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* __toString. |
125
|
|
|
* |
126
|
|
|
* @return string |
127
|
|
|
* @since 0.1.2 |
128
|
|
|
* @codeCoverageIgnore |
129
|
|
|
*/ |
130
|
|
|
public function __toString() |
131
|
|
|
{ |
132
|
|
|
return 'Exen\Konfig\FileParser\Yaml' . PHP_EOL; |
133
|
|
|
} |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
// END OF ./src/FileParser/Yaml.php FILE |
137
|
|
|
|
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the parent class: