1
|
|
|
<?php |
2
|
|
|
namespace DevOp\Core\Router; |
3
|
|
|
|
4
|
|
|
class Route |
5
|
|
|
{ |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* @var string |
9
|
|
|
*/ |
10
|
|
|
private $pattern; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* @var string |
14
|
|
|
*/ |
15
|
|
|
private $regEx; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* |
19
|
|
|
* @var array |
20
|
|
|
*/ |
21
|
|
|
private $values = []; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var array |
25
|
|
|
*/ |
26
|
|
|
private $parameters = []; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var mixed |
30
|
|
|
*/ |
31
|
|
|
private $callback; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @param string $name |
35
|
|
|
* @param string $pattern |
36
|
|
|
* @param mixed $callback |
37
|
|
|
*/ |
38
|
18 |
|
public function __construct($pattern, $callback) |
39
|
|
|
{ |
40
|
18 |
|
$this->pattern = $pattern; |
41
|
18 |
|
$this->callback = $callback; |
42
|
|
|
|
43
|
18 |
|
$this->compile($pattern); |
44
|
18 |
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @return array |
48
|
|
|
*/ |
49
|
|
|
public function getValues() |
50
|
|
|
{ |
51
|
|
|
return $this->values; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @param array $values |
56
|
|
|
* @return $this |
57
|
|
|
*/ |
58
|
2 |
|
public function setValues(array $values) |
59
|
|
|
{ |
60
|
2 |
|
$this->values = $values; |
61
|
|
|
|
62
|
2 |
|
return $this; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @return string |
67
|
|
|
*/ |
68
|
2 |
|
public function getRegEx() |
69
|
|
|
{ |
70
|
2 |
|
return $this->regEx; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @return string |
75
|
|
|
*/ |
76
|
2 |
|
public function getPattern() |
77
|
|
|
{ |
78
|
2 |
|
return $this->pattern; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @return array |
83
|
|
|
*/ |
84
|
2 |
|
public function getParameters() |
85
|
|
|
{ |
86
|
2 |
|
return $this->parameters; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @return mixed |
91
|
|
|
*/ |
92
|
4 |
|
public function getCallback() |
93
|
|
|
{ |
94
|
4 |
|
return $this->callback; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @param string $pattern |
99
|
|
|
* @return string |
100
|
|
|
*/ |
101
|
18 |
|
public function compile($pattern) |
102
|
|
|
{ |
103
|
18 |
|
$matches = []; |
104
|
18 |
|
preg_match_all('#\{(.*?)\}#s', $pattern, $matches, PREG_SET_ORDER); |
105
|
|
|
|
106
|
18 |
|
if (empty($matches)) { |
107
|
18 |
|
$this->regEx = "{$pattern}"; |
108
|
18 |
|
return; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
$components = array_map(function($values) { |
112
|
|
|
return ['name' => $values[0], 'value' => $values[1]]; |
113
|
|
|
}, $matches); |
114
|
|
|
|
115
|
|
|
$replaces = $this->build($components); |
116
|
|
|
|
117
|
|
|
$keys = array_keys($replaces); |
118
|
|
|
$values = array_values($replaces); |
119
|
|
|
|
120
|
|
|
$this->regEx = sprintf("%s", str_replace($keys, $values, $pattern)); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* @param array $components |
125
|
|
|
* @return array |
126
|
|
|
*/ |
127
|
|
|
private function build($components) |
128
|
|
|
{ |
129
|
|
|
|
130
|
|
|
$replaces = []; |
131
|
|
|
|
132
|
|
|
foreach ($components AS $route) { |
133
|
|
|
$placeholder = ltrim($route['value'], '/'); |
134
|
|
|
$optional = substr($route['value'], 0, 1) === '/'; |
135
|
|
|
if (strchr($placeholder, ':')) { |
136
|
|
|
list($parameter, $value) = explode(':', $placeholder); |
137
|
|
|
$regEx = "(?P<$parameter>{$value})"; |
138
|
|
|
$this->parameters[] = $parameter; |
139
|
|
|
} else { |
140
|
|
|
$regEx = "(?P<$placeholder>[^/]++)"; |
141
|
|
|
$this->parameters[] = $placeholder; |
142
|
|
|
} |
143
|
|
|
$replaces[$route['name']] = !$optional ? $regEx : "(?:/{$regEx})"; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
return $replaces; |
147
|
|
|
} |
148
|
|
|
} |
149
|
|
|
|