1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* This file is part of Yolk - Gamer Network's PHP Framework. |
4
|
|
|
* |
5
|
|
|
* Copyright (c) 2015 Gamer Network Ltd. |
6
|
|
|
* |
7
|
|
|
* Distributed under the MIT License, a copy of which is available in the |
8
|
|
|
* LICENSE file that was bundled with this package, or online at: |
9
|
|
|
* https://github.com/gamernetwork/yolk-support |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace yolk\support; |
13
|
|
|
|
14
|
|
|
use yolk\contracts\support\Config; |
15
|
|
|
use yolk\contracts\support\Arrayable; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Simple PHP array-based, read-only configuration class. |
19
|
|
|
* |
20
|
|
|
* Config files are simply PHP files that define arrays of key/value pairs. |
21
|
|
|
* $config = array( |
22
|
|
|
* 'logs' => array( |
23
|
|
|
* 'debug' => '/var/log/my_app/debug.log', |
24
|
|
|
* 'error' => '/var/log/my_app/error.log', |
25
|
|
|
* ), |
26
|
|
|
* 'debug' => true |
27
|
|
|
* }; |
28
|
|
|
* |
29
|
|
|
* Supports accessing nested keys using dot-notation: |
30
|
|
|
* * 'logs' will return all defined logs |
31
|
|
|
* * 'logs.debug' will return the debug log definition |
32
|
|
|
* |
33
|
|
|
*/ |
34
|
|
|
class BaseConfig extends collections\BaseDictionary implements Config { |
35
|
|
|
|
36
|
|
|
public function load( $file, $key = '' ) { |
37
|
|
|
|
38
|
|
|
$config = []; |
39
|
|
|
|
40
|
|
|
require $file; |
41
|
|
|
|
42
|
|
|
if( !isset($config) || !is_array($config) ) |
43
|
|
|
throw new \LogicException('Invalid Configuration'); |
44
|
|
|
|
45
|
|
|
if( empty($key) ) |
46
|
|
|
$this->merge($config); |
47
|
|
|
else |
48
|
|
|
$this->set($key, $config); |
49
|
|
|
|
50
|
|
|
return $this; |
51
|
|
|
|
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
public function has( $key ) { |
55
|
|
|
return $this->get($key) !== null; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
public function get( $key, $default = null ) { |
59
|
|
|
|
60
|
|
|
$parts = explode('.', $key); |
61
|
|
|
$context = &$this->items; |
62
|
|
|
|
63
|
|
|
foreach( $parts as $part ) { |
64
|
|
|
if( !isset($context[$part]) ) { |
65
|
|
|
return $default; |
66
|
|
|
} |
67
|
|
|
$context = &$context[$part]; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
return $context; |
71
|
|
|
|
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
public function set( $key, $value ) { |
75
|
|
|
|
76
|
|
|
$parts = explode('.', $key); |
77
|
|
|
$count = count($parts) - 1; |
78
|
|
|
$context = &$this->items; |
79
|
|
|
|
80
|
|
|
for( $i = 0; $i <= $count; $i++ ) { |
81
|
|
|
$part = $parts[$i]; |
82
|
|
|
if( !isset($context[$part]) && ($i < $count) ) { |
83
|
|
|
$context[$part] = []; |
84
|
|
|
} |
85
|
|
|
elseif( $i == $count ) { |
86
|
|
|
$context[$part] = $value; |
87
|
|
|
if( $parts[0] == 'php' ) { |
88
|
|
|
ini_set($part, $value); |
89
|
|
|
} |
90
|
|
|
return $this; |
91
|
|
|
} |
92
|
|
|
$context = &$context[$part]; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
return $this; |
96
|
|
|
|
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
public function merge( array $config ) { |
100
|
|
|
|
101
|
|
|
foreach( $config as $k => $v ) { |
102
|
|
|
$this->set($k, $v); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
return $this; |
106
|
|
|
|
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
// EOF |