1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the LightSAML-Core package. |
5
|
|
|
* |
6
|
|
|
* (c) Milos Tomic <[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 LightSaml\Context; |
13
|
|
|
|
14
|
|
|
abstract class AbstractContext implements ContextInterface |
15
|
|
|
{ |
16
|
|
|
/** @var ContextInterface|null */ |
17
|
|
|
private $parent; |
18
|
|
|
|
19
|
|
|
/** @var ContextInterface[] */ |
20
|
|
|
private $subContexts = array(); |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @return ContextInterface|null |
24
|
|
|
*/ |
25
|
71 |
|
public function getParent() |
26
|
|
|
{ |
27
|
71 |
|
return $this->parent; |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @return ContextInterface |
32
|
|
|
*/ |
33
|
63 |
|
public function getTopParent() |
34
|
|
|
{ |
35
|
63 |
|
if ($this->getParent()) { |
36
|
3 |
|
return $this->getParent()->getTopParent(); |
37
|
|
|
} |
38
|
|
|
|
39
|
63 |
|
return $this; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @param ContextInterface|null $parent |
44
|
|
|
* |
45
|
|
|
* @return ContextInterface |
46
|
|
|
*/ |
47
|
130 |
|
public function setParent(ContextInterface $parent = null) |
48
|
|
|
{ |
49
|
130 |
|
$this->parent = $parent; |
50
|
|
|
|
51
|
130 |
|
return $this; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @param string $name |
56
|
|
|
* @param null|string $class |
57
|
|
|
* |
58
|
|
|
* @return ContextInterface|null |
59
|
|
|
*/ |
60
|
134 |
|
public function getSubContext($name, $class = null) |
61
|
|
|
{ |
62
|
134 |
|
if (isset($this->subContexts[$name])) { |
63
|
103 |
|
return $this->subContexts[$name]; |
64
|
|
|
} |
65
|
|
|
|
66
|
131 |
|
if ($class) { |
|
|
|
|
67
|
129 |
|
$result = $this->createSubContext($class); |
68
|
129 |
|
$this->addSubContext($name, $result); |
69
|
|
|
|
70
|
129 |
|
return $result; |
71
|
|
|
} |
72
|
|
|
|
73
|
4 |
|
return null; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @param string $class |
78
|
|
|
* @param bool $autoCreate |
79
|
|
|
* |
80
|
|
|
* @return ContextInterface|null |
81
|
|
|
*/ |
82
|
|
|
public function getSubContextByClass($class, $autoCreate) |
83
|
|
|
{ |
84
|
|
|
return $this->getSubContext($class, $autoCreate ? $class : null); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @param string $name |
89
|
|
|
* @param object|ContextInterface $subContext |
90
|
|
|
* |
91
|
|
|
* @return AbstractContext |
92
|
|
|
*/ |
93
|
139 |
|
public function addSubContext($name, $subContext) |
94
|
|
|
{ |
95
|
139 |
|
if (false === is_object($subContext)) { |
96
|
1 |
|
throw new \InvalidArgumentException('Expected object or ContextInterface'); |
97
|
|
|
} |
98
|
|
|
|
99
|
138 |
|
$existing = isset($this->subContexts[$name]) ? $this->subContexts[$name] : null; |
100
|
138 |
|
if ($existing === $subContext) { |
101
|
1 |
|
return $this; |
102
|
|
|
} |
103
|
|
|
|
104
|
138 |
|
$this->subContexts[$name] = $subContext; |
105
|
138 |
|
if ($subContext instanceof ContextInterface) { |
106
|
130 |
|
$subContext->setParent($this); |
107
|
|
|
} |
108
|
|
|
|
109
|
138 |
|
if ($existing instanceof ContextInterface) { |
110
|
1 |
|
$existing->setParent(null); |
111
|
|
|
} |
112
|
|
|
|
113
|
138 |
|
return $this; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* @param string $name |
118
|
|
|
* |
119
|
|
|
* @return ContextInterface |
120
|
|
|
*/ |
121
|
2 |
|
public function removeSubContext($name) |
122
|
|
|
{ |
123
|
2 |
|
$subContext = $this->getSubContext($name, false); |
|
|
|
|
124
|
|
|
|
125
|
2 |
|
if ($subContext) { |
126
|
2 |
|
$subContext->setParent(null); |
127
|
2 |
|
unset($this->subContexts[$name]); |
128
|
|
|
} |
129
|
|
|
|
130
|
2 |
|
return $this; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* @param string $name |
135
|
|
|
* |
136
|
|
|
* @return bool |
137
|
|
|
*/ |
138
|
2 |
|
public function containsSubContext($name) |
139
|
|
|
{ |
140
|
2 |
|
return isset($this->subContexts[$name]); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* @return ContextInterface |
145
|
|
|
*/ |
146
|
1 |
|
public function clearSubContexts() |
147
|
|
|
{ |
148
|
1 |
|
foreach ($this->subContexts as $subContext) { |
149
|
1 |
|
$subContext->setParent(null); |
150
|
|
|
} |
151
|
1 |
|
$this->subContexts = array(); |
152
|
|
|
|
153
|
1 |
|
return $this; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* @return \ArrayIterator |
158
|
|
|
*/ |
159
|
6 |
|
public function getIterator() |
160
|
|
|
{ |
161
|
6 |
|
return new \ArrayIterator($this->subContexts); |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* @param string $ownName |
166
|
|
|
* |
167
|
|
|
* @return array |
168
|
|
|
*/ |
169
|
2 |
|
public function debugPrintTree($ownName = 'root') |
170
|
|
|
{ |
171
|
|
|
$result = array( |
172
|
2 |
|
$ownName => static::class, |
173
|
|
|
); |
174
|
|
|
|
175
|
2 |
|
if ($this->subContexts) { |
|
|
|
|
176
|
2 |
|
$arr = array(); |
177
|
2 |
|
foreach ($this->subContexts as $name => $subContext) { |
178
|
2 |
View Code Duplication |
if ($subContext instanceof ContextInterface) { |
|
|
|
|
179
|
2 |
|
$arr = array_merge($arr, $subContext->debugPrintTree($name)); |
180
|
|
|
} else { |
181
|
2 |
|
$arr = array_merge($arr, array($name => get_class($subContext))); |
182
|
|
|
} |
183
|
|
|
} |
184
|
2 |
|
$result[$ownName.'__children'] = $arr; |
185
|
|
|
} |
186
|
|
|
|
187
|
2 |
|
return $result; |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
/** |
191
|
|
|
* @return string |
192
|
|
|
*/ |
193
|
1 |
|
public function __toString() |
194
|
|
|
{ |
195
|
1 |
|
return json_encode($this->debugPrintTree(), JSON_PRETTY_PRINT); |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
/** |
199
|
|
|
* @param string $path |
200
|
|
|
* |
201
|
|
|
* @return ContextInterface |
202
|
|
|
*/ |
203
|
3 |
|
public function getPath($path) |
204
|
|
|
{ |
205
|
3 |
|
if (is_string($path)) { |
206
|
3 |
|
$path = explode('/', $path); |
207
|
3 |
|
} elseif (false === is_array($path)) { |
208
|
|
|
throw new \InvalidArgumentException('Expected string or array'); |
209
|
|
|
} |
210
|
|
|
|
211
|
3 |
|
$name = array_shift($path); |
212
|
3 |
|
$subContext = $this->getSubContext($name); |
213
|
3 |
|
if (null == $subContext) { |
214
|
2 |
|
return null; |
215
|
|
|
} |
216
|
|
|
|
217
|
3 |
|
if (empty($path)) { |
218
|
1 |
|
return $subContext; |
219
|
|
|
} else { |
220
|
3 |
|
return $subContext->getPath($path); |
|
|
|
|
221
|
|
|
} |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
/** |
225
|
|
|
* @param string $class |
226
|
|
|
* |
227
|
|
|
* @return ContextInterface |
228
|
|
|
*/ |
229
|
129 |
|
protected function createSubContext($class) |
230
|
|
|
{ |
231
|
129 |
|
$result = new $class(); |
232
|
|
|
|
233
|
129 |
|
return $result; |
234
|
|
|
} |
235
|
|
|
} |
236
|
|
|
|
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
string
values, the empty string''
is a special case, in particular the following results might be unexpected: