1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* \AppserverIo\Psr\Naming\ResourceIdentifier |
5
|
|
|
* |
6
|
|
|
* NOTICE OF LICENSE |
7
|
|
|
* |
8
|
|
|
* This source file is subject to the Open Software License (OSL 3.0) |
9
|
|
|
* that is available through the world-wide-web at this URL: |
10
|
|
|
* http://opensource.org/licenses/osl-3.0.php |
11
|
|
|
* |
12
|
|
|
* PHP version 5 |
13
|
|
|
* |
14
|
|
|
* @author Tim Wagner <[email protected]> |
15
|
|
|
* @copyright 2015 TechDivision GmbH <[email protected]> |
16
|
|
|
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) |
17
|
|
|
* @link https://github.com/appserver-io/appserver |
18
|
|
|
* @link http://www.appserver.io |
19
|
|
|
*/ |
20
|
|
|
|
21
|
|
|
namespace AppserverIo\Psr\Naming; |
22
|
|
|
|
23
|
|
|
use AppserverIo\Properties\PropertiesInterface; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* This is a resource identifier implementation to use a URL as a unique |
27
|
|
|
* identifier for lookup a enterprise bean from the container. |
28
|
|
|
* |
29
|
|
|
* Usually a normal URL will be enough, but as the container always uses |
30
|
|
|
* an application name we need some functionality to explode that from |
31
|
|
|
* the path. |
32
|
|
|
* |
33
|
|
|
* @author Tim Wagner <[email protected]> |
34
|
|
|
* @copyright 2015 TechDivision GmbH <[email protected]> |
35
|
|
|
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) |
36
|
|
|
* @link https://github.com/appserver-io/appserver |
37
|
|
|
* @link http://www.appserver.io |
38
|
|
|
*/ |
39
|
|
|
class ResourceIdentifier |
40
|
|
|
{ |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* The URL data of the resource identifier. |
44
|
|
|
* |
45
|
|
|
* @var array |
46
|
|
|
*/ |
47
|
|
|
protected $data = array(); |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Initializes the resource identifier with the data of the passed array. |
51
|
|
|
* |
52
|
|
|
* @param array $urlElements The data to initialize the identifier with |
53
|
|
|
*/ |
54
|
13 |
|
public function __construct(array $urlElements = array()) |
55
|
|
|
{ |
56
|
13 |
|
$this->data = $urlElements; |
57
|
13 |
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Returns the value for the passed key, if available. |
61
|
|
|
* |
62
|
|
|
* @param string $key The key of the value to return |
63
|
|
|
* |
64
|
|
|
* @return mixed|null The requested value |
65
|
|
|
*/ |
66
|
12 |
|
protected function getValue($key) |
67
|
|
|
{ |
68
|
12 |
|
if (isset($this->data[$key])) { |
69
|
12 |
|
return $this->data[$key]; |
70
|
|
|
} |
71
|
6 |
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Sets the value with the passed key, existing values |
75
|
|
|
* are overwritten. |
76
|
|
|
* |
77
|
|
|
* @param string $key The key of the value |
78
|
|
|
* @param string $value The value to set |
79
|
|
|
* |
80
|
|
|
* @return void |
81
|
|
|
*/ |
82
|
1 |
|
protected function setValue($key, $value) |
83
|
|
|
{ |
84
|
1 |
|
$this->data[$key] = $value; |
85
|
1 |
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Sets the URL scheme. |
89
|
|
|
* |
90
|
|
|
* @param string $scheme The URL scheme |
91
|
|
|
* |
92
|
|
|
* @return void |
93
|
|
|
*/ |
94
|
|
|
public function setScheme($scheme) |
95
|
|
|
{ |
96
|
|
|
$this->setValue('scheme', $scheme); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Returns the URL scheme. |
101
|
|
|
* |
102
|
|
|
* @return string|null The URL scheme |
103
|
|
|
*/ |
104
|
1 |
|
public function getScheme() |
105
|
|
|
{ |
106
|
1 |
|
return $this->getValue('scheme'); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Sets the URL user. |
111
|
|
|
* |
112
|
|
|
* @param string $user The URL user |
113
|
|
|
* |
114
|
|
|
* @return void |
115
|
|
|
*/ |
116
|
|
|
public function setUser($user) |
117
|
|
|
{ |
118
|
|
|
$this->setValue('user', $user); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Returns the URL user. |
123
|
|
|
* |
124
|
|
|
* @return string|null The URL user |
125
|
|
|
*/ |
126
|
1 |
|
public function getUser() |
127
|
|
|
{ |
128
|
1 |
|
return $this->getValue('user'); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* Sets the URL password. |
133
|
|
|
* |
134
|
|
|
* @param string $pass The URL password |
135
|
|
|
* |
136
|
|
|
* @return void |
137
|
|
|
*/ |
138
|
|
|
public function setPass($pass) |
139
|
|
|
{ |
140
|
|
|
$this->setValue('pass', $pass); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* Returns the URL password. |
145
|
|
|
* |
146
|
|
|
* @return string|null The URL password |
147
|
|
|
*/ |
148
|
1 |
|
public function getPass() |
149
|
|
|
{ |
150
|
1 |
|
return $this->getValue('pass'); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* Sets the URL host. |
155
|
|
|
* |
156
|
|
|
* @param string $host The URL host |
157
|
|
|
* |
158
|
|
|
* @return void |
159
|
|
|
*/ |
160
|
|
|
public function setHost($host) |
161
|
|
|
{ |
162
|
|
|
$this->setValue('host', $host); |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* Returns the URL host. |
167
|
|
|
* |
168
|
|
|
* @return string|null The URL host |
169
|
|
|
*/ |
170
|
1 |
|
public function getHost() |
171
|
|
|
{ |
172
|
1 |
|
return $this->getValue('host'); |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* Sets the URL port. |
177
|
|
|
* |
178
|
|
|
* @param integer $port The URL port |
179
|
|
|
* |
180
|
|
|
* @return void |
181
|
|
|
*/ |
182
|
|
|
public function setPort($port) |
183
|
|
|
{ |
184
|
|
|
$this->setValue('port', $port); |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
/** |
188
|
|
|
* Returns the URL port. |
189
|
|
|
* |
190
|
|
|
* @return integer|null The URL port |
191
|
|
|
*/ |
192
|
1 |
|
public function getPort() |
193
|
|
|
{ |
194
|
1 |
|
return $this->getValue('port'); |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
/** |
198
|
|
|
* Sets the URL path. |
199
|
|
|
* |
200
|
|
|
* @param string $path The URL path |
201
|
|
|
* |
202
|
|
|
* @return void |
203
|
|
|
*/ |
204
|
|
|
public function setPath($path) |
205
|
|
|
{ |
206
|
|
|
$this->setValue('path', $path); |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
/** |
210
|
|
|
* Returns the URL path. |
211
|
|
|
* |
212
|
|
|
* @return string|null The URL path |
213
|
|
|
*/ |
214
|
1 |
|
public function getPath() |
215
|
|
|
{ |
216
|
1 |
|
return $this->getValue('path'); |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
/** |
220
|
|
|
* Sets the URL query. |
221
|
|
|
* |
222
|
|
|
* @param string $query The URL query |
223
|
|
|
* |
224
|
|
|
* @return void |
225
|
|
|
*/ |
226
|
|
|
public function setQuery($query) |
227
|
|
|
{ |
228
|
|
|
$this->setValue('query', $query); |
229
|
|
|
} |
230
|
|
|
|
231
|
|
|
/** |
232
|
|
|
* Returns the URL query. |
233
|
|
|
* |
234
|
|
|
* @return string|null The URL query |
235
|
|
|
*/ |
236
|
1 |
|
public function getQuery() |
237
|
|
|
{ |
238
|
1 |
|
return $this->getValue('query'); |
239
|
|
|
} |
240
|
|
|
|
241
|
|
|
/** |
242
|
|
|
* Extracts and returns the path information from the path. |
243
|
|
|
* |
244
|
|
|
* @return string|null The path information |
245
|
|
|
*/ |
246
|
1 |
|
public function getPathInfo() |
247
|
|
|
{ |
248
|
1 |
|
if ($path = $this->getPath()) { |
249
|
1 |
|
return str_replace($this->getFilename(), '', $path); |
250
|
|
|
} |
251
|
|
|
} |
252
|
|
|
|
253
|
|
|
/** |
254
|
|
|
* Extracts and returns the filename from the path. |
255
|
|
|
* |
256
|
|
|
* @return string|null The filename |
257
|
|
|
*/ |
258
|
1 |
|
public function getFilename() |
259
|
|
|
{ |
260
|
|
|
|
261
|
|
|
// firs check if the resource identifier has a path |
262
|
1 |
|
if ($path = $this->getPath()) { |
263
|
|
|
// we're searching for the . => signals a file |
264
|
1 |
|
$foundDot = strpos($path, '.'); |
265
|
|
|
|
266
|
|
|
// if we can't find one, we don't have a filename |
267
|
1 |
|
if ($foundDot === false) { |
268
|
|
|
return $path; |
269
|
|
|
} |
270
|
|
|
|
271
|
|
|
// after that look for the first slash => that'll be the end of the file extension |
272
|
1 |
|
$foundPathInfo = strpos($path, '/', $foundDot - 1); |
273
|
|
|
|
274
|
|
|
// if we can't find one, we don't have a path information to separate |
275
|
1 |
|
if ($foundPathInfo === false) { |
276
|
|
|
return $path; |
277
|
|
|
} |
278
|
|
|
|
279
|
|
|
// return the filename by cutting it out of the complete path |
280
|
1 |
|
return substr($path, 0, $foundPathInfo); |
281
|
|
|
} |
282
|
|
|
} |
283
|
|
|
|
284
|
|
|
/** |
285
|
|
|
* Returns the context name from the URL. |
286
|
|
|
* |
287
|
|
|
* @return string|null The context name |
288
|
|
|
*/ |
289
|
1 |
|
public function getContextName() |
290
|
|
|
{ |
291
|
1 |
|
if ($filename = $this->getFilename()) { |
292
|
1 |
|
$filenameParts = explode('/', trim($filename, '/')); |
293
|
1 |
|
return reset($filenameParts); |
294
|
|
|
} |
295
|
|
|
} |
296
|
|
|
|
297
|
|
|
/** |
298
|
|
|
* Adds a query parameter to the resource identifier, usually this will be a session ID. |
299
|
|
|
* |
300
|
|
|
* @param string $keyValuePair The query parameter we want to add |
301
|
|
|
* |
302
|
|
|
* @return void |
303
|
|
|
*/ |
304
|
|
|
public function addQueryParam($keyValuePair) |
305
|
|
|
{ |
306
|
|
|
if ($query = $this->getValue('query')) { |
307
|
|
|
$this->setQuery($query . '&' . $keyValuePair); |
308
|
|
|
} else { |
309
|
|
|
$this->setQuery($keyValuePair); |
310
|
|
|
} |
311
|
|
|
} |
312
|
|
|
|
313
|
|
|
/** |
314
|
|
|
* Creates a new resource identifier instance with the data extracted from the passed URL. |
315
|
|
|
* |
316
|
|
|
* @param string $url The URL to load the data from |
317
|
|
|
* |
318
|
|
|
* @return void |
319
|
|
|
*/ |
320
|
1 |
|
public function populateFromUrl($url) |
321
|
|
|
{ |
322
|
1 |
|
foreach (parse_url($url) as $key => $value) { |
|
|
|
|
323
|
1 |
|
$this->setValue($key, $value); |
324
|
1 |
|
} |
325
|
1 |
|
} |
326
|
|
|
|
327
|
|
|
/** |
328
|
|
|
* create a new resource identifier with the URL parts from the passed properties. |
329
|
|
|
* |
330
|
|
|
* @param \AppserverIo\Properties\PropertiesInterface $properties The configuration properties |
331
|
|
|
* |
332
|
|
|
* @return \AppserverIo\Psr\Naming\ResourceIdentifier The initialized instance |
333
|
|
|
*/ |
334
|
|
|
public static function createFromProperties(PropertiesInterface $properties) |
335
|
|
|
{ |
336
|
|
|
return new ResourceIdentifier($properties->toIndexedArray()); |
337
|
|
|
} |
338
|
|
|
} |
339
|
|
|
|
There are different options of fixing this problem.
If you want to be on the safe side, you can add an additional type-check:
If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:
Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.