|
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 Alain Schlesser, Bright Nucleus |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace BrightNucleus\Config; |
|
13
|
|
|
|
|
14
|
|
|
use Exception; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* Create new object instances that implement ConfigInterface. |
|
18
|
|
|
* |
|
19
|
|
|
* @since 0.3.0 |
|
20
|
|
|
* |
|
21
|
|
|
* @package BrightNucleus\Config |
|
22
|
|
|
* @author Alain Schlesser <[email protected]> |
|
23
|
|
|
*/ |
|
24
|
|
|
class ConfigFactory |
|
25
|
|
|
{ |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* Create a new ConfigInterface object from a file. |
|
29
|
|
|
* |
|
30
|
|
|
* If a comma-separated list of files is provided, they are checked in sequence until the first one could be loaded |
|
31
|
|
|
* successfully. |
|
32
|
|
|
* |
|
33
|
|
|
* @since 0.3.0 |
|
34
|
|
|
* |
|
35
|
|
|
* @param string|array $_ List of files. |
|
36
|
|
|
* |
|
37
|
|
|
* @return ConfigInterface Instance of a ConfigInterface implementation. |
|
|
|
|
|
|
38
|
|
|
*/ |
|
39
|
1 |
|
public static function createFromFile($_) |
|
40
|
|
|
{ |
|
41
|
1 |
|
$files = array_reverse(func_get_args()); |
|
42
|
|
|
|
|
43
|
1 |
|
if (is_array($files[0])) { |
|
44
|
|
|
$files = $files[0]; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
1 |
|
while (count($files) > 0) { |
|
48
|
|
|
try { |
|
49
|
1 |
|
$file = array_pop($files); |
|
50
|
|
|
|
|
51
|
1 |
|
if (! file_exists($file)) { |
|
52
|
1 |
|
continue; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
1 |
|
return new Config(include $file); |
|
56
|
|
|
} catch (Exception $exception) { |
|
|
|
|
|
|
57
|
|
|
|
|
58
|
|
|
} |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
return new Config([]); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* Create a new ConfigInterface object from an array. |
|
66
|
|
|
* |
|
67
|
|
|
* @since 0.3.0 |
|
68
|
|
|
* |
|
69
|
|
|
* @param array $array Array with configuration values. |
|
70
|
|
|
* |
|
71
|
|
|
* @return ConfigInterface Instance of a ConfigInterface implementation. |
|
|
|
|
|
|
72
|
|
|
*/ |
|
73
|
1 |
|
public static function createFromArray(array $array) |
|
74
|
|
|
{ |
|
75
|
1 |
|
return new Config($array); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* Create a new ConfigInterface object. |
|
80
|
|
|
* |
|
81
|
|
|
* Tries to deduce the correct creation method by inspecting the provided arguments. |
|
82
|
|
|
* |
|
83
|
|
|
* @since 0.3.0 |
|
84
|
|
|
* |
|
85
|
|
|
* @param mixed $_ Array with configuration values. |
|
86
|
|
|
* |
|
87
|
|
|
* @return ConfigInterface Instance of a ConfigInterface implementation. |
|
|
|
|
|
|
88
|
|
|
*/ |
|
89
|
3 |
|
public static function create($_) |
|
|
|
|
|
|
90
|
|
|
{ |
|
91
|
3 |
|
if (func_num_args() < 1) { |
|
92
|
|
|
return new Config([]); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
3 |
|
$arguments = func_get_args(); |
|
96
|
|
|
|
|
97
|
3 |
|
if (is_array($arguments[0]) && func_num_args() === 1) { |
|
98
|
1 |
|
return self::createFromArray($arguments[0]); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
2 |
|
return self::createFromFile($arguments); |
|
102
|
|
|
} |
|
103
|
|
|
} |
|
104
|
|
|
|
This check looks for the generic type
arrayas a return type and suggests a more specific type. This type is inferred from the actual code.