1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Bright Nucleus Config Component. |
4
|
|
|
* |
5
|
|
|
* @package BrightNucleus\Config |
6
|
|
|
* @author Alain Schlesser <[email protected]> |
7
|
|
|
* @license MIT |
8
|
|
|
* @link http://www.brightnucleus.com/ |
9
|
|
|
* @copyright 2016-2017 Alain Schlesser, Bright Nucleus |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace BrightNucleus\Config\Loader; |
13
|
|
|
|
14
|
|
|
use BrightNucleus\Config\Exception\FailedToLoadConfigException; |
15
|
|
|
use Exception; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Class JSONLoader. |
19
|
|
|
* |
20
|
|
|
* @since 0.1.0 |
21
|
|
|
* |
22
|
|
|
* @package BrightNucleus\Config\Loader |
23
|
|
|
* @author Pascal Knecht <[email protected]> |
24
|
|
|
*/ |
25
|
|
|
class JSONLoader extends AbstractLoader |
26
|
|
|
{ |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Load the configuration from an URI. |
30
|
|
|
* |
31
|
|
|
* @since 0.4.0 |
32
|
|
|
* |
33
|
|
|
* @param string $uri URI of the resource to load. |
34
|
|
|
* |
35
|
|
|
* @return array Data contained within the resource. |
|
|
|
|
36
|
|
|
*/ |
37
|
|
|
public static function canLoad($uri) |
38
|
|
|
{ |
39
|
|
|
$path = pathinfo($uri); |
40
|
|
|
|
41
|
|
|
return 'json' === mb_strtolower($path['extension']); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Load the contents of an resource identified by an URI. |
46
|
|
|
* |
47
|
|
|
* @since 0.4.0 |
48
|
|
|
* |
49
|
|
|
* @param string $uri URI of the resource. |
50
|
|
|
* |
51
|
|
|
* @return array|null Raw data loaded from the resource. Null if no data found. |
52
|
|
|
* @throws FailedToLoadConfigException If the resource could not be loaded. |
53
|
|
|
*/ |
54
|
|
|
protected function loadUri($uri) |
55
|
|
|
{ |
56
|
|
|
try { |
57
|
|
|
ob_start(); |
58
|
|
|
$data = json_decode(file_get_contents($uri), true); |
59
|
|
|
ob_end_clean(); |
60
|
|
|
|
61
|
|
|
return (array)$data; |
62
|
|
|
} catch (Exception $exception) { |
63
|
|
|
throw new FailedToLoadConfigException( |
64
|
|
|
sprintf( |
65
|
|
|
_('Could not include JSON config file "%1$s". Reason: "%2$s".'), |
66
|
|
|
$uri, |
67
|
|
|
$exception->getMessage() |
68
|
|
|
), |
69
|
|
|
$exception->getCode(), |
70
|
|
|
$exception |
71
|
|
|
); |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Validate and return the URI. |
77
|
|
|
* |
78
|
|
|
* @since 0.4.0 |
79
|
|
|
* |
80
|
|
|
* @param string $uri URI of the resource to load. |
81
|
|
|
* |
82
|
|
|
* @return string Validated URI. |
83
|
|
|
* @throws FailedToLoadConfigException If the URI does not exist or is not readable. |
84
|
|
|
*/ |
85
|
|
View Code Duplication |
protected function validateUri($uri) |
|
|
|
|
86
|
|
|
{ |
87
|
|
|
if (! is_readable($uri)) { |
88
|
|
|
throw new FailedToLoadConfigException( |
89
|
|
|
sprintf( |
90
|
|
|
_('The requested JSON config file "%1$s" does not exist or is not readable.'), |
91
|
|
|
$uri |
92
|
|
|
) |
93
|
|
|
); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
return $uri; |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|
This check compares the return type specified in the
@return
annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.