| 1 | package Mojolicious::Routes::Pattern; | ||||||||||||||||||||||||||||||||||||||||||||||
| 2 | 22 | ▶ | use Mojo::Base -base; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||||||||||||||||||||
| 4 | 22 | ▶ | use Carp qw(croak); | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||||||||||||||||||||
| 6 | 1385 | ▶ | has [qw(constraints defaults types)] => sub { {} }; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 7 | 22 | has [qw(placeholder_start type_start)] => ':'; | |||||||||||||||||||||||||||||||||||||||||||||
| 8 | 1289 | ▶ | has [qw(placeholders tree)] => sub { [] }; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 9 | 22 | has quote_end => '>'; | |||||||||||||||||||||||||||||||||||||||||||||
| 10 | 22 | has quote_start => '<'; | |||||||||||||||||||||||||||||||||||||||||||||
| 11 | 22 | has [qw(regex unparsed)]; | |||||||||||||||||||||||||||||||||||||||||||||
| 12 | 22 | has relaxed_start => '#'; | |||||||||||||||||||||||||||||||||||||||||||||
| 13 | 22 | has wildcard_start => '*'; | |||||||||||||||||||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||||||||||||||||||||
| 15 | 583 | ▶ | sub match { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
match: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 16 | 583 | my ($self, $path, $detect) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 17 | 583 | my $captures = $self->match_partial(\$path, $detect); | |||||||||||||||||||||||||||||||||||||||||||||
| 18 | 583 | ▶ | return !$path || $path eq '/' ? $captures : undef; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
not $path or $path eq "/"
MC/DC100%2/2
not $path or $path eq "/"
not $path
$path eq "/"
Truth table100%3/3
not(A) or B
A= not $pathB= $path eq "/"
| |||||||||||||||||||||||||||||||||||||||||||||||
| 19 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||||||||||||||||||||
| 21 | 1736 | ▶ | sub match_partial { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
match_partial: called CC=7 SCAR=19.5
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 22 | 1736 | my ($self, $pathref, $detect) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||||||||||||||||||||
| 24 | # Compile on demand | ||||||||||||||||||||||||||||||||||||||||||||||
| 25 | 1736 | ▶ | $self->_compile($detect) unless $self->{regex}; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||||||||||||||||||||
| 27 | 1736 | ▶ | return undef unless my @captures = $$pathref =~ $self->regex; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 28 | 911 | $$pathref = ${^POSTMATCH}; | |||||||||||||||||||||||||||||||||||||||||||||
| 29 | 911 | ▶ | @captures = () if $#+ == 0; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 30 | 911 | my $captures = {%{$self->defaults}}; | |||||||||||||||||||||||||||||||||||||||||||||
| 31 | 911 | for my $placeholder (@{$self->placeholders}, 'format') { | |||||||||||||||||||||||||||||||||||||||||||||
| 32 | 1076 | ▶ | last unless @captures; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 33 | 208 | my $capture = shift @captures; | |||||||||||||||||||||||||||||||||||||||||||||
| 34 | 208 | ▶ | $captures->{$placeholder} = $capture if defined $capture; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 35 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||||||||||||||||||||
| 37 | 911 | return $captures; | |||||||||||||||||||||||||||||||||||||||||||||
| 38 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||||||||||||||||||||
| 40 | 1280 | ▶ | sub new { @_ > 1 ? shift->SUPER::new->parse(@_) : shift->SUPER::new } | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
new: called CC=2 SCAR=6.9
Pod
documented
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||||||||||||||||||||
| 42 | 1222 | ▶ | sub parse { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
parse: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 43 | 1222 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||||||||||||||||||||
| 45 | 1222 | ▶ | my $pattern = @_ % 2 ? (shift // '/') : '/'; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%2/2
shift() // "/"
MC/DC100%1/1
shift() // "/"
shift()
Truth table100%2/2
A
A= shift()
| |||||||||||||||||||||||||||||||||||||||||||||||
| 46 | 1222 | $pattern =~ s!^/*|/+!/!g; | |||||||||||||||||||||||||||||||||||||||||||||
| 47 | 1222 | ▶ | return $self->constraints({@_}) if $pattern eq '/'; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||||||||||||||||||||
| 49 | 1138 | $pattern =~ s!/$!!; | |||||||||||||||||||||||||||||||||||||||||||||
| 50 | 1138 | return $self->constraints({@_})->_tokenize($pattern); | |||||||||||||||||||||||||||||||||||||||||||||
| 51 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||||||||||||||||||||
| 53 | 406 | ▶ | sub render { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
render: called CC=12 SCAR=24.8
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 54 | 406 | my ($self, $values, $endpoint) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||||||||||||||||||||
| 56 | # Placeholders can only be optional without a format | ||||||||||||||||||||||||||||||||||||||||||||||
| 57 | 406 | my $optional = !(my $format = $values->{format}); | |||||||||||||||||||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||||||||||||||||||||
| 59 | 406 | my $str = ''; | |||||||||||||||||||||||||||||||||||||||||||||
| 60 | 406 | for my $token (reverse @{$self->tree}) { | |||||||||||||||||||||||||||||||||||||||||||||
| 61 | 571 | my ($op, $value) = @$token; | |||||||||||||||||||||||||||||||||||||||||||||
| 62 | 571 | my $part = ''; | |||||||||||||||||||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||||||||||||||||||||
| 64 | # Text | ||||||||||||||||||||||||||||||||||||||||||||||
| 65 | 571 | ▶ | if ($op eq 'text') { ($part, $optional) = ($value, 0) } | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||||||||||||||||||||
| 67 | # Slash | ||||||||||||||||||||||||||||||||||||||||||||||
| 68 | 177 | ▶ | elsif ($op eq 'slash') { $part = '/' unless $optional } | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||||||||||||||||||||
| 70 | # Placeholder | ||||||||||||||||||||||||||||||||||||||||||||||
| 71 | else { | ||||||||||||||||||||||||||||||||||||||||||||||
| 72 | 152 | my $name = $value->[0]; | |||||||||||||||||||||||||||||||||||||||||||||
| 73 | 152 | my $default = $self->defaults->{$name}; | |||||||||||||||||||||||||||||||||||||||||||||
| 74 | 152 | ▶ | $part = $values->{$name} // $default // ''; | ||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%5/5
($values->{$name} // $default) // ""
$values->{$name} // $default
MC/DC100%2/2
($values->{$name} // $default) // ""
$values->{$name}
$default
Truth table100%3/3
A // B
A= $values->{$name}B= $default
| |||||||||||||||||||||||||||||||||||||||||||||||
| 75 | 152 | ▶ | if (!defined $default || ($default ne $part)) { $optional = 0 } | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition100%3/3
not defined $default or $default ne $part
MC/DC100%2/2
not defined $default or $default ne $part
not defined $default
$default ne $part
Truth table100%3/3
not(A) or B
A= not defined $defaultB= $default ne $part
| |||||||||||||||||||||||||||||||||||||||||||||||
| 76 | 13 | elsif ($optional) { $part = '' } | |||||||||||||||||||||||||||||||||||||||||||||
| 77 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||||||||||||||||||||
| 79 | 571 | $str = $part . $str; | |||||||||||||||||||||||||||||||||||||||||||||
| 80 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||||||||||||||||||||
| 82 | # Format can be optional | ||||||||||||||||||||||||||||||||||||||||||||||
| 83 | 406 | ▶ | return $endpoint && $format ? "$str.$format" : $str; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$endpoint and $format
MC/DC100%2/2
$endpoint and $format
$endpoint
$format
Truth table100%3/3
A and B
A= $endpointB= $format
| |||||||||||||||||||||||||||||||||||||||||||||||
| 84 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||||||||||||||||||||
| 86 | 319 | ▶ | sub _compile { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_compile: called CC=13 SCAR=25.6
| |||||||||||||||||||||||||||||||||||||||||||||||
| 87 | 319 | my ($self, $detect) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||||||||||||||||||||
| 89 | 319 | my $constraints = $self->constraints; | |||||||||||||||||||||||||||||||||||||||||||||
| 90 | 319 | my $defaults = $self->defaults; | |||||||||||||||||||||||||||||||||||||||||||||
| 91 | 319 | my $types = $self->types; | |||||||||||||||||||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||||||||||||||||||||
| 93 | 319 | my $block = my $regex = ''; | |||||||||||||||||||||||||||||||||||||||||||||
| 94 | 319 | my $optional = 1; | |||||||||||||||||||||||||||||||||||||||||||||
| 95 | 319 | for my $token (reverse @{$self->tree}) { | |||||||||||||||||||||||||||||||||||||||||||||
| 96 | 445 | my ($op, $value, $type) = @$token; | |||||||||||||||||||||||||||||||||||||||||||||
| 97 | 445 | my $part = ''; | |||||||||||||||||||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||||||||||||||||||||
| 99 | # Text | ||||||||||||||||||||||||||||||||||||||||||||||
| 100 | 445 | ▶ | if ($op eq 'text') { ($part, $optional) = (quotemeta $value, 0) } | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||||||||||||||||||||
| 102 | # Slash | ||||||||||||||||||||||||||||||||||||||||||||||
| 103 | elsif ($op eq 'slash') { | ||||||||||||||||||||||||||||||||||||||||||||||
| 104 | 99 | ▶ | $regex = ($optional ? "(?:/$block)?" : "/$block") . $regex; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 105 | 99 | ($block, $optional) = ('', 1); | |||||||||||||||||||||||||||||||||||||||||||||
| 106 | 99 | next; | |||||||||||||||||||||||||||||||||||||||||||||
| 107 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||||||||||||||||||||
| 109 | # Placeholder | ||||||||||||||||||||||||||||||||||||||||||||||
| 110 | else { | ||||||||||||||||||||||||||||||||||||||||||||||
| 111 | 85 | ▶ | if ($value->[1]) { $part = _compile_req($types->{$value->[1]} // '?!') } | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%2/2
$$types{$value->[1]} // "?!"
MC/DC100%1/1
$$types{$value->[1]} // "?!"
$$types{$value->[1]}
Truth table100%2/2
A
A= $$types{$value->[1]}
| |||||||||||||||||||||||||||||||||||||||||||||||
| 112 | 80 | ▶ | else { $part = $type ? $type eq 'relaxed' ? '([^/]+)' : '(.+)' : '([^/.]+)' } | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||||||||||||||||||||
| 114 | # Custom regex | ||||||||||||||||||||||||||||||||||||||||||||||
| 115 | 85 | ▶ | if (my $c = $constraints->{$value->[0]}) { $part = _compile_req($c) } | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||||||||||||||||||||
| 117 | # Optional placeholder | ||||||||||||||||||||||||||||||||||||||||||||||
| 118 | 85 | ▶ | exists $defaults->{$value->[0]} ? ($part .= '?') : ($optional = 0); | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 119 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||||||||||||||||||||
| 121 | 346 | $block = $part . $block; | |||||||||||||||||||||||||||||||||||||||||||||
| 122 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||||||||||||||||||||
| 124 | # Not rooted with a slash | ||||||||||||||||||||||||||||||||||||||||||||||
| 125 | 319 | ▶ | $regex = $block . $regex if $block; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||||||||||||||||||||
| 127 | # Format | ||||||||||||||||||||||||||||||||||||||||||||||
| 128 | 319 | ▶ | $regex .= _compile_format($constraints->{format}, exists $defaults->{format}) if $detect; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||||||||||||||||||||
| 130 | 319 | $self->{compiled} = $self->regex(qr/^$regex/ps)->regex; | |||||||||||||||||||||||||||||||||||||||||||||
| 131 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||||||||||||||||||||
| 133 | 242 | ▶ | sub _compile_format { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_compile_format: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||||||||||||||||||||
| 134 | 242 | my ($format, $has_default) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||||||||||||||||||||
| 136 | # No regex | ||||||||||||||||||||||||||||||||||||||||||||||
| 137 | 242 | ▶ | return '' unless $format; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||||||||||||||||||||
| 139 | # Default regex | ||||||||||||||||||||||||||||||||||||||||||||||
| 140 | 24 | ▶ | return '/?(?:\.([^/]+))?$' if $format eq '1'; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||||||||||||||||||||
| 142 | # Compile custom regex | ||||||||||||||||||||||||||||||||||||||||||||||
| 143 | 20 | my $regex = '\.' . _compile_req($format); | |||||||||||||||||||||||||||||||||||||||||||||
| 144 | 20 | ▶ | return $has_default ? "/?(?:$regex)?\$" : "/?$regex\$"; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 145 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||||||||||||||||||||
| 147 | 39 | ▶ | sub _compile_req { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_compile_req: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||||||
| 148 | 39 | my $req = shift; | |||||||||||||||||||||||||||||||||||||||||||||
| 149 | 39 | ▶ | return "($req)" if ref $req ne 'ARRAY'; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 150 | 30 | return '(' . join('|', map {quotemeta} reverse sort @$req) . ')'; | |||||||||||||||||||||||||||||||||||||||||||||
| 151 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||||||||||||||||||||
| 153 | 1138 | ▶ | sub _tokenize { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_tokenize: called CC=26 SCAR=32.6
| |||||||||||||||||||||||||||||||||||||||||||||||
| 154 | 1138 | my ($self, $pattern) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||||||||||||||||||||
| 156 | 1138 | my $placeholders = $self->placeholders; | |||||||||||||||||||||||||||||||||||||||||||||
| 157 | 1138 | my $type_start = $self->type_start; | |||||||||||||||||||||||||||||||||||||||||||||
| 158 | 1138 | my $quote_end = $self->quote_end; | |||||||||||||||||||||||||||||||||||||||||||||
| 159 | 1138 | my $quote_start = $self->quote_start; | |||||||||||||||||||||||||||||||||||||||||||||
| 160 | 1138 | my $start = $self->placeholder_start; | |||||||||||||||||||||||||||||||||||||||||||||
| 161 | 1138 | my $relaxed = $self->relaxed_start; | |||||||||||||||||||||||||||||||||||||||||||||
| 162 | 1138 | my $wildcard = $self->wildcard_start; | |||||||||||||||||||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||||||||||||||||||||
| 164 | 1138 | my (@tree, $spec, $more); | |||||||||||||||||||||||||||||||||||||||||||||
| 165 | 1138 | for my $char (split //, $pattern) { | |||||||||||||||||||||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||||||||||||||||||||
| 167 | # Quoted | ||||||||||||||||||||||||||||||||||||||||||||||
| 168 | 13436 | ▶ | if ($char eq $quote_start) { push @tree, ['placeholder', ''] if ++$spec } | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 10 branches
| |||||||||||||||||||||||||||||||||||||||||||||||
| 169 | 27 | elsif ($char eq $quote_end) { $spec = $more = 0 } | |||||||||||||||||||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||||||||||||||||||||
| 171 | # Placeholder | ||||||||||||||||||||||||||||||||||||||||||||||
| 172 | 87 | ▶ | elsif (!$more && $char eq $start) { push @tree, ['placeholder', ''] unless $spec++ } | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
not $more and $char eq $start
MC/DC100%2/2
not $more and $char eq $start
not $more
$char eq $start
Truth table100%3/3
not(A) and B
A= not $moreB= $char eq $start
| |||||||||||||||||||||||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||||||||||||||||||||
| 174 | # Relaxed or wildcard (upgrade when quoted) | ||||||||||||||||||||||||||||||||||||||||||||||
| 175 | ▶ | elsif (!$more && ($char eq $relaxed || $char eq $wildcard)) { | |||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%6/6
not $more and $char eq $relaxed || $char eq $wildcard
$char eq $relaxed or $char eq $wildcard
MC/DC100%3/3
not $more and $char eq $relaxed || $char eq $wildcard
not $more
$char eq $relaxed
$char eq $wildcard
Truth table100%4/4
not(A) and B or C
A= not $moreB= $char eq $relaxedC= $char eq $wildcard
| |||||||||||||||||||||||||||||||||||||||||||||||
| 176 | 40 | ▶ | push @tree, ['placeholder', ''] unless $spec++; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 177 | 40 | ▶ | $tree[-1][2] = $char eq $relaxed ? 'relaxed' : 'wildcard'; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 178 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||||||||||||||||||||
| 180 | # Slash | ||||||||||||||||||||||||||||||||||||||||||||||
| 181 | elsif ($char eq '/') { | ||||||||||||||||||||||||||||||||||||||||||||||
| 182 | 1712 | push @tree, ['slash']; | |||||||||||||||||||||||||||||||||||||||||||||
| 183 | 1712 | $spec = $more = 0; | |||||||||||||||||||||||||||||||||||||||||||||
| 184 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 185 | |||||||||||||||||||||||||||||||||||||||||||||||
| 186 | # Placeholder | ||||||||||||||||||||||||||||||||||||||||||||||
| 187 | 804 | ▶ | elsif ($spec && ++$more) { $tree[-1][1] .= $char } | ||||||||||||||||||||||||||||||||||||||||||||
|
Condition66%2/3
$spec and ++$more
MC/DC50%1/2
$spec and ++$more
$spec
++$more
Truth table66%2/3
A and B
A= $specB= ++$more
| |||||||||||||||||||||||||||||||||||||||||||||||
| 188 | |||||||||||||||||||||||||||||||||||||||||||||||
| 189 | # Text (optimize slash+text and *+text+slash+text) | ||||||||||||||||||||||||||||||||||||||||||||||
| 190 | 9160 | elsif ($tree[-1][0] eq 'text') { $tree[-1][-1] .= $char } | |||||||||||||||||||||||||||||||||||||||||||||
| 191 | 1066 | ▶ | elsif (!$tree[-2] && $tree[-1][0] eq 'slash') { @tree = (['text', "/$char"]) } | ||||||||||||||||||||||||||||||||||||||||||||
|
Condition66%2/3
not $tree[-2] and $tree[-1][0] eq "slash"
MC/DC50%1/2
not $tree[-2] and $tree[-1][0] eq "slash"
not $tree[-2]
$tree[-1][0] eq "slash"
Truth table66%2/3
not(A) and B
A= not $tree[-2]B= $tree[-1][0] eq "slash"
| |||||||||||||||||||||||||||||||||||||||||||||||
| 192 | 473 | ▶ | elsif ($tree[-2] && $tree[-2][0] eq 'text' && $tree[-1][0] eq 'slash') { pop @tree && ($tree[-1][-1] .= "/$char") } | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition83%5/6
$tree[-2] and $tree[-2][0] eq "text" and $tree[-1][0] eq "slash"
$tree[-2] and $tree[-2][0] eq "text"
MC/DC66%2/3
$tree[-2] and $tree[-2][0] eq "text" and $tree[-1][0] eq "slash"
$tree[-2]
$tree[-2][0] eq "text"
$tree[-1][0] eq "slash"
Truth table75%3/4
A and B and C
A= $tree[-2]B= $tree[-2][0] eq "text"C= $tree[-1][0] eq "slash"
| |||||||||||||||||||||||||||||||||||||||||||||||
| 193 | 40 | else { push @tree, ['text', $char] } | |||||||||||||||||||||||||||||||||||||||||||||
| 194 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 195 | |||||||||||||||||||||||||||||||||||||||||||||||
| 196 | # Placeholder types | ||||||||||||||||||||||||||||||||||||||||||||||
| 197 | 1138 | for my $token (reverse @tree) { | |||||||||||||||||||||||||||||||||||||||||||||
| 198 | 1422 | ▶ | next unless $token->[0] eq 'placeholder'; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 199 | 143 | ▶ | $token->[1] = $token->[1] =~ /^(.+)\Q$type_start\E(.+)$/ ? [$1, $2] : [$token->[1]]; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 200 | 143 | unshift @$placeholders, $token->[1][0]; | |||||||||||||||||||||||||||||||||||||||||||||
| 201 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 202 | |||||||||||||||||||||||||||||||||||||||||||||||
| 203 | 1138 | return $self->unparsed($pattern)->tree(\@tree); | |||||||||||||||||||||||||||||||||||||||||||||
| 204 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 205 | |||||||||||||||||||||||||||||||||||||||||||||||
| 206 | 22 | 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<Mojolicious::Routes::Pattern> is the core of L<Mojolicious::Routes>. | ||||||||||||||||||||||||||||||||||||||||||||||
| 228 | |||||||||||||||||||||||||||||||||||||||||||||||
| 229 | =head1 ATTRIBUTES | ||||||||||||||||||||||||||||||||||||||||||||||
| 230 | |||||||||||||||||||||||||||||||||||||||||||||||
| 231 | L<Mojolicious::Routes::Pattern> 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<E<gt>>. | ||||||||||||||||||||||||||||||||||||||||||||||
| 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<E<lt>>. | ||||||||||||||||||||||||||||||||||||||||||||||
| 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<Mojolicious::Routes::Pattern> inherits all methods from L<Mojo::Base> 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<Mojolicious::Routes::Pattern> object and L</"parse"> 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<Mojolicious>, L<Mojolicious::Guides>, L<https://mojolicious.org>. | ||||||||||||||||||||||||||||||||||||||||||||||
| 369 | |||||||||||||||||||||||||||||||||||||||||||||||
| 370 | =cut | ||||||||||||||||||||||||||||||||||||||||||||||