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 PHPLoader. |
19
|
|
|
* |
20
|
|
|
* @since 0.4.0 |
21
|
|
|
* |
22
|
|
|
* @package BrightNucleus\Config\Loader |
23
|
|
|
* @author Alain Schlesser <[email protected]> |
24
|
|
|
*/ |
25
|
|
|
class PHPLoader extends AbstractLoader |
26
|
|
|
{ |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Check whether the loader is able to load a given URI. |
30
|
|
|
* |
31
|
|
|
* @since 0.4.0 |
32
|
|
|
* |
33
|
|
|
* @param string $uri URI to check. |
34
|
|
|
* |
35
|
|
|
* @return bool Whether the loader can load the given URI. |
36
|
|
|
*/ |
37
|
2 |
|
public static function canLoad($uri) |
38
|
|
|
{ |
39
|
2 |
|
$path = pathinfo($uri); |
40
|
|
|
|
41
|
2 |
|
return 'php' === 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
|
2 |
|
protected function loadUri($uri) |
55
|
|
|
{ |
56
|
|
|
try { |
57
|
|
|
// Try to load the file through PHP's include(). |
58
|
|
|
// Make sure we don't accidentally create output. |
59
|
2 |
|
ob_start(); |
60
|
2 |
|
$data = include($uri); |
61
|
2 |
|
ob_end_clean(); |
62
|
|
|
|
63
|
2 |
|
return $data; |
64
|
|
|
} catch (Exception $exception) { |
65
|
|
|
throw new FailedToLoadConfigException( |
66
|
|
|
sprintf( |
67
|
|
|
_('Could not include PHP config file "%1$s". Reason: "%2$s".'), |
68
|
|
|
$uri, |
69
|
|
|
$exception->getMessage() |
70
|
|
|
), |
71
|
|
|
$exception->getCode(), |
72
|
|
|
$exception |
73
|
|
|
); |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Validate and return the URI. |
79
|
|
|
* |
80
|
|
|
* @since 0.4.0 |
81
|
|
|
* |
82
|
|
|
* @param string $uri URI of the resource to load. |
83
|
|
|
* |
84
|
|
|
* @return string Validated URI. |
85
|
|
|
* @throws FailedToLoadConfigException If the URI does not exist or is not readable. |
86
|
|
|
*/ |
87
|
2 |
View Code Duplication |
protected function validateUri($uri) |
|
|
|
|
88
|
|
|
{ |
89
|
2 |
|
if (! is_readable($uri)) { |
90
|
|
|
throw new FailedToLoadConfigException( |
91
|
|
|
sprintf( |
92
|
|
|
_('The requested PHP config file "%1$s" does not exist or is not readable.'), |
93
|
|
|
$uri |
94
|
|
|
) |
95
|
|
|
); |
96
|
|
|
} |
97
|
|
|
|
98
|
2 |
|
return $uri; |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.