/
RouteCompiler.php
235 lines (203 loc) · 8.04 KB
/
RouteCompiler.php
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Routing;
/**
* RouteCompiler compiles Route instances to CompiledRoute instances.
*
* @author Fabien Potencier <fabien@symfony.com>
*/
class RouteCompiler implements RouteCompilerInterface
{
protected $options;
protected $route;
protected $variables;
protected $firstOptional;
protected $segments;
protected $tokens;
protected $staticPrefix;
protected $regex;
/**
* Compiles the current route instance.
*
* @param Route $route A Route instance
*
* @return CompiledRoute A CompiledRoute instance
*/
public function compile(Route $route)
{
$this->route = $route;
$this->firstOptional = 0;
$this->segments = array();
$this->variables = array();
$this->tokens = array();
$this->staticPrefix = '';
$this->regex = '';
$this->options = $this->getOptions();
$this->preCompile();
$this->tokenize();
foreach ($this->tokens as $token) {
call_user_func_array(array($this, 'compileFor'.ucfirst(array_shift($token))), $token);
}
$this->postCompile();
$separator = '';
if (count($this->tokens)) {
$lastToken = $this->tokens[count($this->tokens) - 1];
$separator = 'separator' == $lastToken[0] ? $lastToken[2] : '';
}
$this->regex = "#^".implode("", $this->segments)."".preg_quote($separator, '#')."$#x";
// optimize tokens for generation
$tokens = array();
foreach ($this->tokens as $i => $token) {
if ($i + 1 === count($this->tokens) && 'separator' === $token[0]) {
// trailing /
$tokens[] = array('text', $token[2], '', null);
} elseif ('separator' !== $token[0]) {
$tokens[] = $token;
}
}
$tokens = array_reverse($tokens);
return new CompiledRoute($this->route, $this->staticPrefix, $this->regex, $tokens, $this->variables);
}
/**
* Pre-compiles a route.
*/
protected function preCompile()
{
}
/**
* Post-compiles a route.
*/
protected function postCompile()
{
// all segments after the last static segment are optional
// be careful, the n-1 is optional only if n is empty
for ($i = $this->firstOptional, $max = count($this->segments); $i < $max; $i++) {
$this->segments[$i] = (0 == $i ? '/?' : '').str_repeat(' ', $i - $this->firstOptional).'(?:'.$this->segments[$i];
$this->segments[] = str_repeat(' ', $max - $i - 1).')?';
}
$this->staticPrefix = '';
foreach ($this->tokens as $token) {
switch ($token[0]) {
case 'separator':
break;
case 'text':
// text is static
$this->staticPrefix .= $token[1].$token[2];
break;
default:
// everything else indicates variable parts. break switch and for loop
break 2;
}
}
}
/**
* Tokenizes the route.
*
* @throws \InvalidArgumentException When route can't be parsed
*/
private function tokenize()
{
$this->tokens = array();
$buffer = $this->route->getPattern();
$afterASeparator = false;
$currentSeparator = '';
// a route is an array of (separator + variable) or (separator + text) segments
while (strlen($buffer)) {
if (false !== $this->tokenizeBufferBefore($buffer, $tokens, $afterASeparator, $currentSeparator)) {
// a custom token
$this->customToken = true;
} else if ($afterASeparator && preg_match('#^\{([\w\d_]+)\}#', $buffer, $match)) {
// a variable
$this->tokens[] = array('variable', $currentSeparator, $match[0], $match[1]);
$currentSeparator = '';
$buffer = substr($buffer, strlen($match[0]));
$afterASeparator = false;
} else if ($afterASeparator && preg_match('#^('.$this->options['text_regex'].')(?:'.$this->options['segment_separators_regex'].'|$)#', $buffer, $match)) {
// a text
$this->tokens[] = array('text', $currentSeparator, $match[1], null);
$currentSeparator = '';
$buffer = substr($buffer, strlen($match[1]));
$afterASeparator = false;
} else if (!$afterASeparator && preg_match('#^'.$this->options['segment_separators_regex'].'#', $buffer, $match)) {
// a separator
$this->tokens[] = array('separator', $currentSeparator, $match[0], null);
$currentSeparator = $match[0];
$buffer = substr($buffer, strlen($match[0]));
$afterASeparator = true;
} else if (false !== $this->tokenizeBufferAfter($buffer, $tokens, $afterASeparator, $currentSeparator)) {
// a custom token
$this->customToken = true;
} else {
// parsing problem
throw new \InvalidArgumentException(sprintf('Unable to parse "%s" route near "%s".', $this->route->getPattern(), $buffer));
}
}
}
/**
* Tokenizes the buffer before default logic is applied.
*
* This method must return false if the buffer has not been parsed.
*
* @param string $buffer The current route buffer
* @param array $tokens An array of current tokens
* @param Boolean $afterASeparator Whether the buffer is just after a separator
* @param string $currentSeparator The last matched separator
*
* @return Boolean true if a token has been generated, false otherwise
*/
protected function tokenizeBufferBefore(&$buffer, &$tokens, &$afterASeparator, &$currentSeparator)
{
return false;
}
/**
* Tokenizes the buffer after default logic is applied.
*
* This method must return false if the buffer has not been parsed.
*
* @param string $buffer The current route buffer
* @param array $tokens An array of current tokens
* @param Boolean $afterASeparator Whether the buffer is just after a separator
* @param string $currentSeparator The last matched separator
*
* @return Boolean true if a token has been generated, false otherwise
*/
protected function tokenizeBufferAfter(&$buffer, &$tokens, &$afterASeparator, &$currentSeparator)
{
return false;
}
protected function compileForText($separator, $text)
{
$this->firstOptional = count($this->segments) + 1;
$this->segments[] = preg_quote($separator, '#').preg_quote($text, '#');
}
protected function compileForVariable($separator, $name, $variable)
{
if (null === $requirement = $this->route->getRequirement($variable)) {
$requirement = $this->options['variable_content_regex'];
}
$this->segments[] = preg_quote($separator, '#').'(?P<'.$variable.'>'.$requirement.')';
$this->variables[$variable] = $name;
if (null === $this->route->getDefault($variable)) {
$this->firstOptional = count($this->segments);
}
}
protected function compileForSeparator($separator, $regexSeparator)
{
}
private function getOptions()
{
$options = $this->route->getOptions();
// compute some regexes
$quoter = function ($a) { return preg_quote($a, '#'); };
$options['segment_separators_regex'] = '(?:'.implode('|', array_map($quoter, $options['segment_separators'])).')';
$options['variable_content_regex'] = '[^'.implode('', array_map($quoter, $options['segment_separators'])).']+?';
return $options;
}
}