|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Noodlehaus\Parser; |
|
4
|
|
|
|
|
5
|
|
|
use Exception; |
|
6
|
|
|
use Noodlehaus\Exception\ParseException; |
|
7
|
|
|
use Noodlehaus\Exception\UnsupportedFormatException; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* PHP parser |
|
11
|
|
|
* |
|
12
|
|
|
* @package Config |
|
13
|
|
|
* @author Jesus A. Domingo <[email protected]> |
|
14
|
|
|
* @author Hassan Khan <[email protected]> |
|
15
|
|
|
* @author Filip Š <[email protected]> |
|
16
|
|
|
* @link https://github.com/noodlehaus/config |
|
17
|
|
|
* @license MIT |
|
18
|
|
|
*/ |
|
19
|
|
|
class Php implements ParserInterface |
|
20
|
|
|
{ |
|
21
|
|
|
/** |
|
22
|
|
|
* {@inheritDoc} |
|
23
|
|
|
* Loads a PHP file and gets its' contents as an array |
|
24
|
|
|
* |
|
25
|
|
|
* @throws ParseException If the PHP file throws an exception |
|
26
|
|
|
* @throws UnsupportedFormatException If the PHP file does not return an array |
|
27
|
|
|
*/ |
|
28
|
12 |
|
public function parseFile($filename) |
|
29
|
|
|
{ |
|
30
|
|
|
// Run the fileEval the string, if it throws an exception, rethrow it |
|
31
|
12 |
|
try { |
|
32
|
12 |
|
$data = require $filename; |
|
33
|
12 |
|
} catch (Exception $exception) { |
|
34
|
|
|
throw new ParseException( |
|
35
|
|
|
[ |
|
36
|
|
|
'message' => 'PHP file threw an exception', |
|
37
|
12 |
|
'exception' => $exception, |
|
38
|
6 |
|
] |
|
39
|
3 |
|
); |
|
40
|
|
|
} |
|
41
|
3 |
|
|
|
42
|
3 |
|
// Complete parsing |
|
43
|
|
|
return $this->parse($data, $filename); |
|
|
|
|
|
|
44
|
1 |
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* {@inheritDoc} |
|
48
|
9 |
|
*Loads a PHP string and gets its' contents as an array |
|
49
|
3 |
|
* |
|
50
|
1 |
|
* @throws ParseException If the PHP string throws an exception |
|
51
|
|
|
* @throws UnsupportedFormatException If the PHP string does not return an array |
|
52
|
|
|
*/ |
|
53
|
9 |
|
public function parseString($config) |
|
54
|
3 |
|
{ |
|
55
|
|
|
// Strip PHP start and end tags |
|
56
|
|
|
$config = str_replace('<?php', '', $config); |
|
57
|
6 |
|
$config = str_replace('<?', '', $config); |
|
58
|
|
|
$config = str_replace('?>', '', $config); |
|
59
|
|
|
|
|
60
|
|
|
// Eval the string, if it throws an exception, rethrow it |
|
61
|
|
|
try { |
|
62
|
|
|
$data = eval($config); |
|
63
|
3 |
|
} catch (Exception $exception) { |
|
|
|
|
|
|
64
|
|
|
throw new ParseException( |
|
65
|
3 |
|
[ |
|
66
|
|
|
'message' => 'PHP string threw an exception', |
|
67
|
|
|
'exception' => $exception, |
|
68
|
|
|
] |
|
69
|
|
|
); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
// Complete parsing |
|
73
|
|
|
return $this->parse($data); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* Completes parsing of PHP data |
|
78
|
|
|
* |
|
79
|
|
|
* @param array $data |
|
80
|
|
|
* @param strring $filename |
|
81
|
|
|
* |
|
82
|
|
|
* @throws ParseException If there is an error parsing the PHP data |
|
83
|
|
|
*/ |
|
84
|
|
|
protected function parse($data = null, $filename = null) |
|
|
|
|
|
|
85
|
|
|
{ |
|
86
|
|
|
// If we have a callable, run it and expect an array back |
|
87
|
|
|
if (is_callable($data)) { |
|
88
|
|
|
$data = call_user_func($data); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
// Check for array, if its anything else, throw an exception |
|
92
|
|
|
if (!is_array($data)) { |
|
93
|
|
|
throw new UnsupportedFormatException('PHP data does not return an array'); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
return $data; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* {@inheritDoc} |
|
101
|
|
|
*/ |
|
102
|
|
|
public static function getSupportedExtensions() |
|
103
|
|
|
{ |
|
104
|
|
|
return ['php']; |
|
105
|
|
|
} |
|
106
|
|
|
} |
|
107
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: