1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Northwoods\Config; |
4
|
|
|
|
5
|
|
|
use Northwoods\Config\Loader\LoaderFactory; |
6
|
|
|
|
7
|
|
|
class ConfigFactory |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* @return ConfigInterface |
11
|
|
|
*/ |
12
|
3 |
|
public static function make(array $options, LoaderFactory $loader = null) |
13
|
|
|
{ |
14
|
3 |
|
$factory = new static($loader); |
15
|
3 |
|
$type = array_path($options, 'type', 'php'); |
16
|
|
|
|
17
|
3 |
|
if (!empty($options['environment'])) { |
18
|
2 |
|
return $factory->forEnvironment($options['directory'], $options['environment'], $type); |
19
|
|
|
} |
20
|
|
|
|
21
|
1 |
|
return $factory->forDirectory($options['directory'], $type); |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var LoaderFactory |
26
|
|
|
*/ |
27
|
|
|
private $loader; |
28
|
|
|
|
29
|
3 |
|
public function __construct(LoaderFactory $loader = null) |
30
|
|
|
{ |
31
|
3 |
|
$this->loader = $loader ?: new LoaderFactory(); |
32
|
3 |
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @param string $directory |
36
|
|
|
* @return ConfigDirectory |
37
|
|
|
*/ |
38
|
3 |
|
public function forDirectory($directory, $type = 'php') |
39
|
|
|
{ |
40
|
3 |
|
return new ConfigDirectory($directory, $this->loader->forType($type)); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @param string $directory |
45
|
|
|
* @param string $environment |
46
|
|
|
* @return ConfigCollection |
47
|
|
|
*/ |
48
|
2 |
|
public function forEnvironment($directory, $environment, $type = 'php') |
49
|
|
|
{ |
50
|
2 |
|
return new ConfigCollection( |
51
|
2 |
|
$this->forDirectory("$directory/$environment", $type), |
52
|
2 |
|
$this->forDirectory($directory, $type) |
53
|
2 |
|
); |
54
|
|
|
} |
55
|
|
|
} |
56
|
|
|
|