@@ -130,18 +130,7 @@ public function getPath()
130
130
*/
131
131
public function setPath (string $ pattern )
132
132
{
133
- if (false !== strpbrk ($ pattern , '?< ' )) {
134
- $ pattern = preg_replace_callback ('#\{(\w++)(<.*?>)?(\?[^\}]*+)?\}# ' , function ($ m ) {
135
- if (isset ($ m [3 ][0 ])) {
136
- $ this ->setDefault ($ m [1 ], '? ' !== $ m [3 ] ? substr ($ m [3 ], 1 ) : null );
137
- }
138
- if (isset ($ m [2 ][0 ])) {
139
- $ this ->setRequirement ($ m [1 ], substr ($ m [2 ], 1 , -1 ));
140
- }
141
-
142
- return '{ ' .$ m [1 ].'} ' ;
143
- }, $ pattern );
144
- }
133
+ $ pattern = $ this ->extractInlineDefaultsAndRequirements ($ pattern );
145
134
146
135
// A pattern must start with a slash and must not have multiple slashes at the beginning because the
147
136
// generated path for this route would be confused with a network path, e.g. '//domain.com/path'.
@@ -170,7 +159,7 @@ public function getHost()
170
159
*/
171
160
public function setHost (?string $ pattern )
172
161
{
173
- $ this ->host = ( string ) $ pattern ;
162
+ $ this ->host = $ this -> extractInlineDefaultsAndRequirements (( string ) $ pattern) ;
174
163
$ this ->compiled = null ;
175
164
176
165
return $ this ;
@@ -544,6 +533,24 @@ public function compile()
544
533
return $ this ->compiled = $ class ::compile ($ this );
545
534
}
546
535
536
+ private function extractInlineDefaultsAndRequirements (string $ pattern ): string
537
+ {
538
+ if (false === strpbrk ($ pattern , '?< ' )) {
539
+ return $ pattern ;
540
+ }
541
+
542
+ return preg_replace_callback ('#\{(\w++)(<.*?>)?(\?[^\}]*+)?\}# ' , function ($ m ) {
543
+ if (isset ($ m [3 ][0 ])) {
544
+ $ this ->setDefault ($ m [1 ], '? ' !== $ m [3 ] ? substr ($ m [3 ], 1 ) : null );
545
+ }
546
+ if (isset ($ m [2 ][0 ])) {
547
+ $ this ->setRequirement ($ m [1 ], substr ($ m [2 ], 1 , -1 ));
548
+ }
549
+
550
+ return '{ ' .$ m [1 ].'} ' ;
551
+ }, $ pattern );
552
+ }
553
+
547
554
private function sanitizeRequirement (string $ key , string $ regex )
548
555
{
549
556
if ('' !== $ regex && '^ ' === $ regex [0 ]) {
0 commit comments