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
|
|
|
* Yaml |
26
|
|
|
* Konfig's YAML parser class. |
27
|
|
|
* |
28
|
|
|
* @category FileParser |
29
|
|
|
* @package Konfig |
30
|
|
|
* @author Xeriab Nabil (aka KodeBurner) <[email protected]> |
31
|
|
|
* @license https://raw.github.com/xeriab/konfig/master/LICENSE MIT |
32
|
|
|
* @link https://xeriab.github.io/projects/konfig |
33
|
|
|
* |
34
|
|
|
* @implements Exen\Konfig\FileParser\AbstractFileParser |
35
|
|
|
*/ |
36
|
|
|
class Yaml extends AbstractFileParser |
37
|
|
|
{ |
38
|
|
|
/** |
39
|
|
|
* Loads a YAML/YML file as an array. |
40
|
|
|
* |
41
|
|
|
* @param string $path File path |
42
|
|
|
* |
43
|
|
|
* @throws ParseException If there is an error parsing YAML/YML file |
44
|
|
|
* |
45
|
|
|
* @return array The parsed data |
46
|
|
|
* |
47
|
|
|
* @since 0.1.0 |
48
|
|
|
*/ |
49
|
6 |
|
public function parse($path) |
50
|
|
|
{ |
51
|
6 |
|
$data = null; |
52
|
|
|
|
53
|
|
|
try { |
54
|
6 |
|
$data = $this->loadFile($path); |
55
|
5 |
|
} catch (Exception $ex) { |
56
|
3 |
|
throw new ParseException( |
57
|
|
|
[ |
58
|
3 |
|
'message' => 'Error parsing YAML file', |
59
|
3 |
|
'file' => realpath($path), |
60
|
3 |
|
'line' => $ex->getParsedLine(), |
|
|
|
|
61
|
3 |
|
'exception' => $ex, |
62
|
|
|
] |
63
|
2 |
|
); |
64
|
|
|
} |
65
|
|
|
|
66
|
3 |
|
return $data; |
|
|
|
|
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* {@inheritdoc} |
71
|
|
|
* |
72
|
|
|
* @return array Supported extensions |
73
|
|
|
* |
74
|
|
|
* @since 0.1.0 |
75
|
|
|
*/ |
76
|
3 |
|
public function getSupportedFileExtensions() |
77
|
|
|
{ |
78
|
3 |
|
return ['yaml', 'yml']; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Loads in the given file and parses it. |
83
|
|
|
* |
84
|
|
|
* @param string $file File to load |
85
|
|
|
* |
86
|
|
|
* @return string|array|stdClass The parsed file data |
87
|
|
|
* |
88
|
|
|
* @since 0.2.4 |
89
|
|
|
* @codeCoverageIgnore |
90
|
|
|
*/ |
91
|
|
|
protected function loadFile($file = null) |
92
|
|
|
{ |
93
|
|
|
$this->file = $file; |
94
|
|
|
$contents = $this->parseVars(Utils::getContent($file)); |
95
|
|
|
|
96
|
|
|
return YamlParser::parse($contents); |
|
|
|
|
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Returns the formatted configuration file contents. |
101
|
|
|
* |
102
|
|
|
* @param array $contents configuration array |
103
|
|
|
* |
104
|
|
|
* @return string formatted configuration file contents |
105
|
|
|
* |
106
|
|
|
* @since 0.2.4 |
107
|
|
|
* @codeCoverageIgnore |
108
|
|
|
*/ |
109
|
|
|
protected function exportFormat($contents = null) |
110
|
|
|
{ |
111
|
|
|
$this->prepVars($contents); |
|
|
|
|
112
|
|
|
|
113
|
|
|
return YamlParser::dump($contents); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* __toString. |
118
|
|
|
* |
119
|
|
|
* @return string |
120
|
|
|
* @since 0.1.2 |
121
|
|
|
* @codeCoverageIgnore |
122
|
|
|
*/ |
123
|
|
|
public function __toString() |
124
|
|
|
{ |
125
|
|
|
return 'Exen\Konfig\FileParser\Yaml' . PHP_EOL; |
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
// END OF ./src/FileParser/Yaml.php FILE |
130
|
|
|
|
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: