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 AbstractLoader. |
19
|
|
|
* |
20
|
|
|
* @since 0.4.0 |
21
|
|
|
* |
22
|
|
|
* @package BrightNucleus\Config\Loader |
23
|
|
|
* @author Alain Schlesser <[email protected]> |
24
|
|
|
*/ |
25
|
|
|
abstract class AbstractLoader implements LoaderInterface |
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|null Data contained within the resource. Null if no data could be loaded/parsed. |
36
|
|
|
* @throws FailedToLoadConfigException If the configuration could not be loaded. |
37
|
|
|
*/ |
38
|
2 |
|
public function load($uri) |
39
|
|
|
{ |
40
|
|
|
try { |
41
|
2 |
|
$uri = $this->validateUri($uri); |
42
|
2 |
|
$data = $this->loadUri($uri); |
43
|
|
|
|
44
|
2 |
|
return $this->parseData($data); |
45
|
|
|
} catch (Exception $exception) { |
46
|
|
|
throw new FailedToLoadConfigException( |
47
|
|
|
sprintf( |
48
|
|
|
_('Could not load resource located at "%1$s". Reason: "%2$s".'), |
49
|
|
|
$uri, |
50
|
|
|
$exception->getMessage() |
51
|
|
|
), |
52
|
|
|
$exception->getCode(), |
53
|
|
|
$exception |
54
|
|
|
); |
55
|
|
|
} |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Validate and return the URI. |
60
|
|
|
* |
61
|
|
|
* @since 0.4.0 |
62
|
|
|
* |
63
|
|
|
* @param string $uri URI of the resource to load. |
64
|
|
|
* |
65
|
|
|
* @return string Validated URI. |
66
|
|
|
*/ |
67
|
|
|
protected function validateUri($uri) |
68
|
|
|
{ |
69
|
|
|
return $uri; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Parse the raw data and return it in parsed form. |
74
|
|
|
* |
75
|
|
|
* @since 0.4.0 |
76
|
|
|
* |
77
|
|
|
* @param array|null $data Raw data to be parsed. |
78
|
|
|
* |
79
|
|
|
* @return array|null Data in parsed form. Null if no parsable data found. |
80
|
|
|
*/ |
81
|
2 |
|
protected function parseData($data) |
82
|
|
|
{ |
83
|
2 |
|
return $data; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Load the contents of an resource identified by an URI. |
88
|
|
|
* |
89
|
|
|
* @since 0.4.0 |
90
|
|
|
* |
91
|
|
|
* @param string $uri URI of the resource. |
92
|
|
|
* |
93
|
|
|
* @return array|null Raw data loaded from the resource. Null if no data found. |
94
|
|
|
*/ |
95
|
|
|
abstract protected function loadUri($uri); |
96
|
|
|
} |
97
|
|
|
|