-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRequest.php
More file actions
76 lines (69 loc) · 1.96 KB
/
Copy pathRequest.php
File metadata and controls
76 lines (69 loc) · 1.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
<?php
/**
* @author Vladimir Shapovalov <vovasn@gmail.com>
*/
class Request
{
/**
* @var array
*/
private $params;
/**
* @var array
*/
private $parsed;
/**
* Returns the command line arguments.
* @return array
*/
public function getParams()
{
if ($this->params === null) {
if (isset($_SERVER['argv'])) {
$this->params = $_SERVER['argv'];
array_shift($this->params);
} else {
$this->params = [];
}
}
return $this->params;
}
/**
* Sets the command line arguments.
* @param array $params the command line arguments
*/
public function setParams(array $params)
{
$this->params = $params;
}
/**
* Resolves the current request into a route and the associated parameters.
* @return array the first element is the route, and the second is the associated parameters.
* @throws Exception when parameter is wrong and can not be resolved
*/
public function getParsed()
{
if ($this->parsed !== null) {
return $this->parsed;
}
$rawParams = $this->getParams();
$params = [];
$prevOption = null;
foreach ($rawParams as $param) {
if (preg_match('/^--([\w-]+)(?:=(.*))?$/', $param, $matches)) {
$name = $matches[1];
$params[$name] = isset($matches[2]) ? $matches[2] : true;
$prevOption = &$params[$name];
} elseif (preg_match('/^-([\w-]+)(?:=(.*))?$/', $param, $matches)) {
$name = $matches[1];
$params[$name] = isset($matches[2]) ? $matches[2] : true;
} elseif ($prevOption === true) {
$prevOption = $param;
} else {
$params[] = $param;
}
}
$this->parsed = $params;
return $params;
}
}