File Coverage

blib/lib/Mojolicious/Routes/Pattern.pm
Criterion Covered Total %
statement 109 109 100.0
branch 88 90 97.7
condition 35 39 89.7
subroutine 11 11 100.0
pod 5 5 100.0
total 248 254 97.6


line stmt bran cond sub pod time code
1             package Mojolicious::Routes::Pattern;
2 56     56   1214 use Mojo::Base -base;
  56         174  
  56         504  
3              
4 56     56   494 use Carp qw(croak);
  56         173  
  56         174957  
5              
6             has [qw(constraints defaults types)] => sub { {} };
7             has [qw(placeholder_start type_start)] => ':';
8             has [qw(placeholders tree)] => sub { [] };
9             has quote_end => '>';
10             has quote_start => '<';
11             has [qw(regex unparsed)];
12             has relaxed_start => '#';
13             has wildcard_start => '*';
14              
15             sub match {
16 187     187 1 510 my ($self, $path, $detect) = @_;
17 187         536 my $captures = $self->match_partial(\$path, $detect);
18 187 100 100     1027 return !$path || $path eq '/' ? $captures : undef;
19             }
20              
21             sub match_partial {
22 17739     17739 1 31974 my ($self, $pathref, $detect) = @_;
23              
24             # Compile on demand
25 17739 100       45200 $self->_compile($detect) unless $self->{regex};
26              
27 17739 100       39814 return undef unless my @captures = $$pathref =~ $self->regex;
28 2699         9264 $$pathref = ${^POSTMATCH};
29 2699 100       9626 @captures = () if $#+ == 0;
30 2699         4508 my $captures = {%{$self->defaults}};
  2699         7813  
31 2699         5066 for my $placeholder (@{$self->placeholders}, 'format') {
  2699         7286  
32 2967 100       7930 last unless @captures;
33 371         845 my $capture = shift @captures;
34 371 100       1617 $captures->{$placeholder} = $capture if defined $capture;
35             }
36              
37 2699         9128 return $captures;
38             }
39              
40 1141 100   1141 1 173043 sub new { @_ > 1 ? shift->SUPER::new->parse(@_) : shift->SUPER::new }
41              
42             sub parse {
43 1050     1050 1 1915 my $self = shift;
44              
45 1050 100 100     6225 my $pattern = @_ % 2 ? (shift // '/') : '/';
46 1050         10201 $pattern =~ s!^/*|/+!/!g;
47 1050 100       3649 return $self->constraints({@_}) if $pattern eq '/';
48              
49 949         2157 $pattern =~ s!/$!!;
50 949         3150 return $self->constraints({@_})->_tokenize($pattern);
51             }
52              
53             sub render {
54 720     720 1 19714 my ($self, $values, $endpoint) = @_;
55              
56             # Placeholders can only be optional without a format
57 720         1529 my $optional = !(my $format = $values->{format});
58              
59 720         1371 my $str = '';
60 720         1092 for my $token (reverse @{$self->tree}) {
  720         1871  
61 787         1796 my ($op, $value) = @$token;
62 787         1269 my $part = '';
63              
64             # Text
65 787 100       1903 if ($op eq 'text') { ($part, $optional) = ($value, 0) }
  381 100       858  
66              
67             # Slash
68 216 100       569 elsif ($op eq 'slash') { $part = '/' unless $optional }
69              
70             # Placeholder
71             else {
72 190         434 my $name = $value->[0];
73 190         478 my $default = $self->defaults->{$name};
74 190   100     658 $part = $values->{$name} // $default // '';
      100        
75 190 100 100     699 if (!defined $default || ($default ne $part)) { $optional = 0 }
  154 100       308  
76 22         42 elsif ($optional) { $part = '' }
77             }
78              
79 787         4386 $str = $part . $str;
80             }
81              
82             # Format can be optional
83 720 100 100     4543 return $endpoint && $format ? "$str.$format" : $str;
84             }
85              
86             sub _compile {
87 712     712   1530 my ($self, $detect) = @_;
88              
89 712         1941 my $constraints = $self->constraints;
90 712         1876 my $defaults = $self->defaults;
91 712         3983 my $types = $self->types;
92              
93 712         1604 my $block = my $regex = '';
94 712         1215 my $optional = 1;
95 712         1163 for my $token (reverse @{$self->tree}) {
  712         2152  
96 794         2148 my ($op, $value, $type) = @$token;
97 794         1271 my $part = '';
98              
99             # Text
100 794 100       1858 if ($op eq 'text') { ($part, $optional) = (quotemeta $value, 0) }
  569 100       4528  
101              
102             # Slash
103             elsif ($op eq 'slash') {
104 119 100       376 $regex = ($optional ? "(?:/$block)?" : "/$block") . $regex;
105 119         207 ($block, $optional) = ('', 1);
106 119         261 next;
107             }
108              
109             # Placeholder
110             else {
111 106 100 100     234 if ($value->[1]) { $part = _compile_req($types->{$value->[1]} // '?!') }
  7         44  
112 99 100       245 else { $part = $type ? $type eq 'relaxed' ? '([^/]+)' : '(.+)' : '([^/.]+)' }
    100          
113              
114             # Custom regex
115 106 100       301 if (my $c = $constraints->{$value->[0]}) { $part = _compile_req($c) }
  14         37  
116              
117             # Optional placeholder
118 106 100       374 exists $defaults->{$value->[0]} ? ($part .= '?') : ($optional = 0);
119             }
120              
121 675         1771 $block = $part . $block;
122             }
123              
124             # Not rooted with a slash
125 712 100       1925 $regex = $block . $regex if $block;
126              
127             # Format
128 712 100       3658 $regex .= _compile_format($constraints->{format}, exists $defaults->{format}) if $detect;
129              
130 712         16174 $self->regex(qr/^$regex/ps);
131             }
132              
133             sub _compile_format {
134 564     564   1666 my ($format, $has_default) = @_;
135              
136             # No regex
137 564 100       1944 return '' unless $format;
138              
139             # Default regex
140 45 100       212 return '/?(?:\.([^/]+))?$' if $format eq '1';
141              
142             # Compile custom regex
143 41         147 my $regex = '\.' . _compile_req($format);
144 41 100       188 return $has_default ? "/?(?:$regex)?\$" : "/?$regex\$";
145             }
146              
147             sub _compile_req {
148 62     62   149 my $req = shift;
149 62 100       271 return "($req)" if ref $req ne 'ARRAY';
150 47         208 return '(' . join('|', map {quotemeta} reverse sort @$req) . ')';
  75         363  
151             }
152              
153             sub _tokenize {
154 949     949   2283 my ($self, $pattern) = @_;
155              
156 949         2384 my $placeholders = $self->placeholders;
157 949         2574 my $type_start = $self->type_start;
158 949         2502 my $quote_end = $self->quote_end;
159 949         2431 my $quote_start = $self->quote_start;
160 949         2348 my $start = $self->placeholder_start;
161 949         2387 my $relaxed = $self->relaxed_start;
162 949         2432 my $wildcard = $self->wildcard_start;
163              
164 949         1919 my (@tree, $spec, $more);
165 949         5326 for my $char (split //, $pattern) {
166              
167             # Quoted
168 10399 50 100     76786 if ($char eq $quote_start) { push @tree, ['placeholder', ''] if ++$spec }
  30 100 100     144  
    100 66        
    100 66        
    100 66        
    100 66        
    100 100        
    100          
    100          
    100          
169 30         73 elsif ($char eq $quote_end) { $spec = $more = 0 }
170              
171             # Placeholder
172 83 100       345 elsif (!$more && $char eq $start) { push @tree, ['placeholder', ''] unless $spec++ }
173              
174             # Relaxed or wildcard (upgrade when quoted)
175             elsif (!$more && ($char eq $relaxed || $char eq $wildcard)) {
176 46 100       252 push @tree, ['placeholder', ''] unless $spec++;
177 46 100       203 $tree[-1][2] = $char eq $relaxed ? 'relaxed' : 'wildcard';
178             }
179              
180             # Slash
181             elsif ($char eq '/') {
182 1339         4231 push @tree, ['slash'];
183 1339         2608 $spec = $more = 0;
184             }
185              
186             # Placeholder
187 786         1379 elsif ($spec && ++$more) { $tree[-1][1] .= $char }
188              
189             # Text (optimize slash+text and *+text+slash+text)
190 6883         12066 elsif ($tree[-1][0] eq 'text') { $tree[-1][-1] .= $char }
191 891         3949 elsif (!$tree[-2] && $tree[-1][0] eq 'slash') { @tree = (['text', "/$char"]) }
192 281 50       1128 elsif ($tree[-2] && $tree[-2][0] eq 'text' && $tree[-1][0] eq 'slash') { pop @tree && ($tree[-1][-1] .= "/$char") }
193 30         90 else { push @tree, ['text', $char] }
194             }
195              
196             # Placeholder types
197 949         2809 for my $token (reverse @tree) {
198 1235 100       3315 next unless $token->[0] eq 'placeholder';
199 147 100       1789 $token->[1] = $token->[1] =~ /^(.+)\Q$type_start\E(.+)$/ ? [$1, $2] : [$token->[1]];
200 147         473 unshift @$placeholders, $token->[1][0];
201             }
202              
203 949         3693 return $self->unparsed($pattern)->tree(\@tree);
204             }
205              
206             1;
207              
208             =encoding utf8
209              
210             =head1 NAME
211              
212             Mojolicious::Routes::Pattern - Route pattern
213              
214             =head1 SYNOPSIS
215              
216             use Mojolicious::Routes::Pattern;
217              
218             # Create pattern
219             my $pattern = Mojolicious::Routes::Pattern->new('/test/:name');
220              
221             # Match routes
222             my $captures = $pattern->match('/test/sebastian');
223             say $captures->{name};
224              
225             =head1 DESCRIPTION
226              
227             L is the core of L.
228              
229             =head1 ATTRIBUTES
230              
231             L implements the following attributes.
232              
233             =head2 constraints
234              
235             my $constraints = $pattern->constraints;
236             $pattern = $pattern->constraints({foo => qr/\w+/});
237              
238             Regular expression constraints.
239              
240             =head2 defaults
241              
242             my $defaults = $pattern->defaults;
243             $pattern = $pattern->defaults({foo => 'bar'});
244              
245             Default parameters.
246              
247             =head2 placeholder_start
248              
249             my $start = $pattern->placeholder_start;
250             $pattern = $pattern->placeholder_start(':');
251              
252             Character indicating a placeholder, defaults to C<:>.
253              
254             =head2 placeholders
255              
256             my $placeholders = $pattern->placeholders;
257             $pattern = $pattern->placeholders(['foo', 'bar']);
258              
259             Placeholder names.
260              
261             =head2 quote_end
262              
263             my $end = $pattern->quote_end;
264             $pattern = $pattern->quote_end('}');
265              
266             Character indicating the end of a quoted placeholder, defaults to C>.
267              
268             =head2 quote_start
269              
270             my $start = $pattern->quote_start;
271             $pattern = $pattern->quote_start('{');
272              
273             Character indicating the start of a quoted placeholder, defaults to C>.
274              
275             =head2 regex
276              
277             my $regex = $pattern->regex;
278             $pattern = $pattern->regex($regex);
279              
280             Pattern in compiled regular expression form.
281              
282             =head2 relaxed_start
283              
284             my $start = $pattern->relaxed_start;
285             $pattern = $pattern->relaxed_start('*');
286              
287             Character indicating a relaxed placeholder, defaults to C<#>.
288              
289             =head2 tree
290              
291             my $tree = $pattern->tree;
292             $pattern = $pattern->tree([['text', '/foo']]);
293              
294             Pattern in parsed form. Note that this structure should only be used very carefully since it is very dynamic.
295              
296             =head2 type_start
297              
298             my $start = $pattern->type_start;
299             $pattern = $pattern->type_start('|');
300              
301             Character indicating the start of a placeholder type, defaults to C<:>.
302              
303             =head2 types
304              
305             my $types = $pattern->types;
306             $pattern = $pattern->types({int => qr/[0-9]+/});
307              
308             Placeholder types.
309              
310             =head2 unparsed
311              
312             my $unparsed = $pattern->unparsed;
313             $pattern = $pattern->unparsed('/:foo/:bar');
314              
315             Raw unparsed pattern.
316              
317             =head2 wildcard_start
318              
319             my $start = $pattern->wildcard_start;
320             $pattern = $pattern->wildcard_start('*');
321              
322             Character indicating the start of a wildcard placeholder, defaults to C<*>.
323              
324             =head1 METHODS
325              
326             L inherits all methods from L and implements the following new ones.
327              
328             =head2 match
329              
330             my $captures = $pattern->match('/foo/bar');
331             my $captures = $pattern->match('/foo/bar', 1);
332              
333             Match pattern against entire path, format detection is disabled by default.
334              
335             =head2 match_partial
336              
337             my $captures = $pattern->match_partial(\$path);
338             my $captures = $pattern->match_partial(\$path, 1);
339              
340             Match pattern against path and remove matching parts, format detection is disabled by default.
341              
342             =head2 new
343              
344             my $pattern = Mojolicious::Routes::Pattern->new;
345             my $pattern = Mojolicious::Routes::Pattern->new('/users/:id');
346             my $pattern = Mojolicious::Routes::Pattern->new('/user/:id', id => qr/\d+/);
347             my $pattern = Mojolicious::Routes::Pattern->new(format => ['json', 'yaml']);
348              
349             Construct a new L object and L pattern if necessary.
350              
351             =head2 parse
352              
353             $pattern = $pattern->parse('/user/:id');
354             $pattern = $pattern->parse('/user/:id', id=> qr/\d+/);
355             $pattern = $pattern->parse(format => ['json', 'yaml']);
356              
357             Parse pattern.
358              
359             =head2 render
360              
361             my $path = $pattern->render({id => 24});
362             my $path = $pattern->render({id => 24}, 1);
363              
364             Render pattern into a path with parameters, format rendering is disabled by default.
365              
366             =head1 SEE ALSO
367              
368             L, L, L.
369              
370             =cut