|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the overtrue/socialite. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) overtrue <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* This source file is subject to the MIT license that is bundled |
|
9
|
|
|
* with this source code in the file LICENSE. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace Overtrue\Socialite; |
|
13
|
|
|
|
|
14
|
|
|
use ArrayAccess; |
|
15
|
|
|
use InvalidArgumentException; |
|
|
|
|
|
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Class Config. |
|
19
|
|
|
*/ |
|
20
|
|
|
class Config implements ArrayAccess |
|
21
|
|
|
{ |
|
22
|
|
|
/** |
|
23
|
|
|
* @var array |
|
24
|
|
|
*/ |
|
25
|
|
|
protected $config; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* Config constructor. |
|
29
|
|
|
* |
|
30
|
|
|
* @param array $config |
|
31
|
|
|
*/ |
|
32
|
|
|
public function __construct(array $config) |
|
33
|
|
|
{ |
|
34
|
|
|
$this->config = $config; |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* Get an item from an array using "dot" notation. |
|
39
|
|
|
* |
|
40
|
|
|
* @param string $key |
|
41
|
|
|
* @param mixed $default |
|
42
|
|
|
* |
|
43
|
|
|
* @return mixed |
|
44
|
|
|
*/ |
|
45
|
|
View Code Duplication |
public function get($key, $default = null) |
|
|
|
|
|
|
46
|
|
|
{ |
|
47
|
|
|
$config = $this->config; |
|
48
|
|
|
|
|
49
|
|
|
if (is_null($key)) { |
|
50
|
|
|
return $config; |
|
51
|
|
|
} |
|
52
|
|
|
if (isset($config[$key])) { |
|
53
|
|
|
return $config[$key]; |
|
54
|
|
|
} |
|
55
|
|
|
foreach (explode('.', $key) as $segment) { |
|
56
|
|
|
if (!is_array($config) || !array_key_exists($segment, $config)) { |
|
57
|
|
|
return $default; |
|
58
|
|
|
} |
|
59
|
|
|
$config = $config[$segment]; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
return $config; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* Set an array item to a given value using "dot" notation. |
|
67
|
|
|
* |
|
68
|
|
|
* @param string $key |
|
69
|
|
|
* @param mixed $value |
|
70
|
|
|
* |
|
71
|
|
|
* @return array |
|
72
|
|
|
*/ |
|
73
|
|
|
public function set($key, $value) |
|
74
|
|
|
{ |
|
75
|
|
|
if (is_null($key)) { |
|
76
|
|
|
throw new InvalidArgumentException('Invalid config key.'); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
$keys = explode('.', $key); |
|
80
|
|
|
|
|
81
|
|
|
while (count($keys) > 1) { |
|
82
|
|
|
$key = array_shift($keys); |
|
83
|
|
|
if (!isset($this->config[$key]) || !is_array($this->config[$key])) { |
|
84
|
|
|
$this->config[$key] = []; |
|
85
|
|
|
} |
|
86
|
|
|
$this->config = &$this->config[$key]; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
$this->config[array_shift($keys)] = $value; |
|
90
|
|
|
|
|
91
|
|
|
return $this->config; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* Determine if the given configuration value exists. |
|
96
|
|
|
* |
|
97
|
|
|
* @param string $key |
|
98
|
|
|
* |
|
99
|
|
|
* @return bool |
|
100
|
|
|
*/ |
|
101
|
|
|
public function has($key) |
|
102
|
|
|
{ |
|
103
|
|
|
return (bool) $this->get($key); |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* Whether a offset exists. |
|
108
|
|
|
* |
|
109
|
|
|
* @link http://php.net/manual/en/arrayaccess.offsetexists.php |
|
110
|
|
|
* |
|
111
|
|
|
* @param mixed $offset <p> |
|
112
|
|
|
* An offset to check for. |
|
113
|
|
|
* </p> |
|
114
|
|
|
* |
|
115
|
|
|
* @return bool true on success or false on failure. |
|
116
|
|
|
* </p> |
|
117
|
|
|
* <p> |
|
118
|
|
|
* The return value will be casted to boolean if non-boolean was returned |
|
119
|
|
|
* |
|
120
|
|
|
* @since 5.0.0 |
|
121
|
|
|
*/ |
|
122
|
|
|
public function offsetExists($offset) |
|
123
|
|
|
{ |
|
124
|
|
|
return array_key_exists($offset, $this->config); |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
/** |
|
128
|
|
|
* Offset to retrieve. |
|
129
|
|
|
* |
|
130
|
|
|
* @link http://php.net/manual/en/arrayaccess.offsetget.php |
|
131
|
|
|
* |
|
132
|
|
|
* @param mixed $offset <p> |
|
133
|
|
|
* The offset to retrieve. |
|
134
|
|
|
* </p> |
|
135
|
|
|
* |
|
136
|
|
|
* @return mixed Can return all value types |
|
137
|
|
|
* |
|
138
|
|
|
* @since 5.0.0 |
|
139
|
|
|
*/ |
|
140
|
|
|
public function offsetGet($offset) |
|
141
|
|
|
{ |
|
142
|
|
|
return $this->get($offset); |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
/** |
|
146
|
|
|
* Offset to set. |
|
147
|
|
|
* |
|
148
|
|
|
* @link http://php.net/manual/en/arrayaccess.offsetset.php |
|
149
|
|
|
* |
|
150
|
|
|
* @param mixed $offset <p> |
|
151
|
|
|
* The offset to assign the value to. |
|
152
|
|
|
* </p> |
|
153
|
|
|
* @param mixed $value <p> |
|
154
|
|
|
* The value to set. |
|
155
|
|
|
* </p> |
|
156
|
|
|
* |
|
157
|
|
|
* @since 5.0.0 |
|
158
|
|
|
*/ |
|
159
|
|
|
public function offsetSet($offset, $value) |
|
160
|
|
|
{ |
|
161
|
|
|
$this->set($offset, $value); |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
|
|
/** |
|
165
|
|
|
* Offset to unset. |
|
166
|
|
|
* |
|
167
|
|
|
* @link http://php.net/manual/en/arrayaccess.offsetunset.php |
|
168
|
|
|
* |
|
169
|
|
|
* @param mixed $offset <p> |
|
170
|
|
|
* The offset to unset. |
|
171
|
|
|
* </p> |
|
172
|
|
|
* |
|
173
|
|
|
* @since 5.0.0 |
|
174
|
|
|
*/ |
|
175
|
|
|
public function offsetUnset($offset) |
|
176
|
|
|
{ |
|
177
|
|
|
$this->set($offset, null); |
|
178
|
|
|
} |
|
179
|
|
|
} |
|
180
|
|
|
|
Let’s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let’s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.phpHowever, as
OtherDir/Foo.phpdoes not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: