1 | <?php |
||
18 | class Php implements FileParserInterface |
||
19 | { |
||
20 | /** |
||
21 | * {@inheritDoc} |
||
22 | * Loads a PHP file and gets its' contents as an array |
||
23 | * |
||
24 | * @throws ParseException If the PHP file throws an exception |
||
25 | * @throws UnsupportedFormatException If the PHP file does not return an array |
||
26 | */ |
||
27 | 12 | public function parse($path) |
|
28 | { |
||
29 | // Require the file, if it throws an exception, rethrow it |
||
30 | try { |
||
31 | 12 | $temp = require $path; |
|
32 | 12 | } catch (Exception $exception) { |
|
33 | 3 | throw new ParseException( |
|
34 | array( |
||
35 | 3 | 'message' => 'PHP file threw an exception', |
|
36 | 3 | 'exception' => $exception, |
|
37 | ) |
||
38 | 3 | ); |
|
39 | } |
||
40 | |||
41 | // If we have a callable, run it and expect an array back |
||
42 | 9 | if (is_callable($temp)) { |
|
43 | 3 | $temp = call_user_func($temp); |
|
44 | 3 | } |
|
45 | |||
46 | // Check for array, if its anything else, throw an exception |
||
47 | 9 | if (!is_array($temp)) { |
|
48 | 3 | throw new UnsupportedFormatException('PHP file does not return an array'); |
|
49 | } |
||
50 | |||
51 | 6 | return $temp; |
|
52 | } |
||
53 | |||
54 | /** |
||
55 | * {@inheritDoc} |
||
56 | */ |
||
57 | 3 | public function getSupportedExtensions() |
|
61 | } |
||
62 |