@@ -26,6 +26,8 @@ class RouteCompiler implements RouteCompilerInterface
26
26
* @param Route $route A Route instance
27
27
*
28
28
* @return CompiledRoute A CompiledRoute instance
29
+ *
30
+ * @throws \LogicException If a variable is referenced more than once
29
31
*/
30
32
public function compile (Route $ route )
31
33
{
@@ -34,22 +36,22 @@ public function compile(Route $route)
34
36
$ tokens = array ();
35
37
$ variables = array ();
36
38
$ pos = 0 ;
37
- preg_match_all ('#.\{([\w\d_] +)\}# ' , $ pattern , $ matches , PREG_OFFSET_CAPTURE | PREG_SET_ORDER );
39
+ preg_match_all ('#.\{(\w +)\}# ' , $ pattern , $ matches , PREG_OFFSET_CAPTURE | PREG_SET_ORDER );
38
40
foreach ($ matches as $ match ) {
39
41
if ($ text = substr ($ pattern , $ pos , $ match [0 ][1 ] - $ pos )) {
40
42
$ tokens [] = array ('text ' , $ text );
41
43
}
42
- $ seps = array ( $ pattern [ $ pos ]);
44
+
43
45
$ pos = $ match [0 ][1 ] + strlen ($ match [0 ][0 ]);
44
46
$ var = $ match [1 ][0 ];
45
47
46
48
if ($ req = $ route ->getRequirement ($ var )) {
47
49
$ regexp = $ req ;
48
50
} else {
49
- if ( $ pos !== $ len ) {
50
- $ seps [] = $ pattern [ $ pos ];
51
- }
52
- $ regexp = sprintf ('[^%s]+? ' , preg_quote (implode ( '' , array_unique ( $ seps )) , self ::REGEX_DELIMITER ));
51
+ // Use the character following the variable as the separator when available
52
+ // Use the character preceding the variable otherwise
53
+ $ separator = $ pos !== $ len ? $ pattern [ $ pos ] : $ match [ 0 ][ 0 ][ 0 ];
54
+ $ regexp = sprintf ('[^%s]+? ' , preg_quote ($ separator , self ::REGEX_DELIMITER ));
53
55
}
54
56
55
57
$ tokens [] = array ('variable ' , $ match [0 ][0 ][0 ], $ regexp , $ var );
0 commit comments