| 1 | package Gears::Router::Pattern::SigilMatch; | ||||||||||||||||||||||||||||
| 2 | 2 | $Gears::Router::Pattern::SigilMatch::VERSION = '0.104'; | |||||||||||||||||||||||||||
| 3 | 2 | ▶ | use v5.40; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 2 | ▶ | use Mooish::Base -standard; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | 2 | ▶ | use Gears::X; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 2 | ▶ | use URI::Escape; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 2 | extends 'Gears::Router::Pattern'; | |||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | 2 | has extended 'location' => ( | |||||||||||||||||||||||||||
| 12 | handles => [ | ||||||||||||||||||||||||||||
| 13 | qw( | ||||||||||||||||||||||||||||
| 14 | checks | ||||||||||||||||||||||||||||
| 15 | defaults | ||||||||||||||||||||||||||||
| 16 | ) | ||||||||||||||||||||||||||||
| 17 | ], | ||||||||||||||||||||||||||||
| 18 | ); | ||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | 2 | has field '_regex' => ( | |||||||||||||||||||||||||||
| 21 | isa => RegexpRef, | ||||||||||||||||||||||||||||
| 22 | lazy => 1, | ||||||||||||||||||||||||||||
| 23 | ); | ||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | has field 'tokens' => ( | ||||||||||||||||||||||||||||
| 26 | isa => ArrayRef, | ||||||||||||||||||||||||||||
| 27 | 33 | ▶ | default => sub { [] }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 28 | 2 | ); | |||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | # helpers for matching different types of wildcards | ||||||||||||||||||||||||||||
| 31 | 61 | ▶ | my sub noslash ($sigil) | ||||||||||||||||||||||||||
|
Subroutine
noslash: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 32 | { | ||||||||||||||||||||||||||||
| 33 | 61 | return 1 == grep { $sigil eq $_ } ':', '?'; | |||||||||||||||||||||||||||
| 34 | } | ||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | 11 | ▶ | my sub matchall ($sigil) | ||||||||||||||||||||||||||
|
Subroutine
matchall: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 37 | { | ||||||||||||||||||||||||||||
| 38 | 11 | return 1 == grep { $sigil eq $_ } '*', '>'; | |||||||||||||||||||||||||||
| 39 | } | ||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | 56 | ▶ | my sub optional ($sigil) | ||||||||||||||||||||||||||
|
Subroutine
optional: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 42 | { | ||||||||||||||||||||||||||||
| 43 | 56 | return 1 == grep { $sigil eq $_ } '?', '>'; | |||||||||||||||||||||||||||
| 44 | } | ||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | sub _rep_regex | ||||||||||||||||||||||||||||
| 47 | 52 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
_rep_regex: called CC=7 SCAR=19.5
| |||||||||||||||||||||||||||||
| 48 | 52 | my ($self, $char, $switch, $token, $out) = @_; | |||||||||||||||||||||||||||
| 49 | 52 | my $qchar = quotemeta $char; | |||||||||||||||||||||||||||
| 50 | 52 | my $re; | |||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | 52 | push $self->tokens->@*, { | |||||||||||||||||||||||||||
| 53 | sigil => $switch, | ||||||||||||||||||||||||||||
| 54 | label => $token, | ||||||||||||||||||||||||||||
| 55 | }; | ||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | 52 | my ($prefix, $suffix) = ("(?<$token>", ')'); | |||||||||||||||||||||||||||
| 58 | 52 | ▶ | if (noslash($switch)) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 59 | 41 | ▶ | $re = $qchar . $prefix . ($self->checks->{$token} // '[^\/]+') . $suffix; | ||||||||||||||||||||||||||
|
Condition100%2/2
$self->checks->{$token} // "[^\\/]+"
MC/DC100%1/1
$self->checks->{$token} // "[^\\/]+"
$self->checks->{$token}
Truth table100%2/2
A
A= $self->checks->{$token}
| |||||||||||||||||||||||||||||
| 60 | } | ||||||||||||||||||||||||||||
| 61 | elsif (matchall($switch)) { | ||||||||||||||||||||||||||||
| 62 | 11 | ▶ | $re = $qchar . $prefix . ($self->checks->{$token} // '.+') . $suffix; | ||||||||||||||||||||||||||
|
Condition100%2/2
$self->checks->{$token} // ".+"
MC/DC100%1/1
$self->checks->{$token} // ".+"
$self->checks->{$token}
Truth table100%2/2
A
A= $self->checks->{$token}
| |||||||||||||||||||||||||||||
| 63 | } | ||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | 52 | ▶ | if (optional($switch)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 66 | 15 | ▶ | $re = "(?:$re)" if $char eq '/'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 67 | 15 | $re .= '?'; | |||||||||||||||||||||||||||
| 68 | } | ||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | 52 | push $out->@*, $re; | |||||||||||||||||||||||||||
| 71 | 52 | return '{}'; | |||||||||||||||||||||||||||
| 72 | } | ||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | 33 | ▶ | sub _build_regex ($self) | ||||||||||||||||||||||||||
|
Subroutine
_build_regex: called CC=6 SCAR=18.3
| |||||||||||||||||||||||||||||
| 75 | { | ||||||||||||||||||||||||||||
| 76 | 33 | my $pattern = $self->pattern; | |||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | 33 | my $placeholder_pattern = qr{ | |||||||||||||||||||||||||||
| 79 | ( [^\0]? ) # preceding char, may change behavior of some placeholders | ||||||||||||||||||||||||||||
| 80 | ( [:*?>] ) # placeholder sigil | ||||||||||||||||||||||||||||
| 81 | ( \w+ ) # placeholder label | ||||||||||||||||||||||||||||
| 82 | }x; | ||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | # Curly braces and brackets are only used for separation. | ||||||||||||||||||||||||||||
| 85 | # We replace all of them with \0, then convert the pattern | ||||||||||||||||||||||||||||
| 86 | # into a regular expression. This way if the regular expression | ||||||||||||||||||||||||||||
| 87 | # contains curlies, they won't be removed. | ||||||||||||||||||||||||||||
| 88 | 33 | $pattern =~ s/[{}]/\0/g; | |||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | 33 | my @rep_regex_parts; | |||||||||||||||||||||||||||
| 91 | 33 | $pattern =~ s{ | |||||||||||||||||||||||||||
| 92 | $placeholder_pattern | ||||||||||||||||||||||||||||
| 93 | }{ | ||||||||||||||||||||||||||||
| 94 | 52 | $self->_rep_regex($1, $2, $3, \@rep_regex_parts) | |||||||||||||||||||||||||||
| 95 | }egx; | ||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | # Now remove all curlies remembered as \0 - We will use curlies again for | ||||||||||||||||||||||||||||
| 98 | # special behavior in a moment | ||||||||||||||||||||||||||||
| 99 | 33 | $pattern =~ s/\0//g; | |||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | # remember if the pattern has a trailing slash before we quote it | ||||||||||||||||||||||||||||
| 102 | 33 | my $trailing_slash = $pattern =~ m{/$}; | |||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | # _rep_regex reused curies for {} placeholders, so we want to split the | ||||||||||||||||||||||||||||
| 105 | # string by that (and include them in the result by capturing the | ||||||||||||||||||||||||||||
| 106 | # separator) | ||||||||||||||||||||||||||||
| 107 | 33 | my @parts = split /(\Q{}\E)/, $pattern, -1; | |||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | # If we have a placeholder, replace it with next part. If not, quote it to | ||||||||||||||||||||||||||||
| 110 | # avoid misusing regex in patterns. | ||||||||||||||||||||||||||||
| 111 | 33 | foreach my $part (@parts) { | |||||||||||||||||||||||||||
| 112 | 137 | ▶ | if ($part eq '{}') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 113 | 52 | $part = shift @rep_regex_parts; | |||||||||||||||||||||||||||
| 114 | } | ||||||||||||||||||||||||||||
| 115 | else { | ||||||||||||||||||||||||||||
| 116 | 85 | $part = quotemeta $part; | |||||||||||||||||||||||||||
| 117 | } | ||||||||||||||||||||||||||||
| 118 | } | ||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | 33 | $pattern = join '', @parts; | |||||||||||||||||||||||||||
| 121 | 33 | ▶ | if ($self->is_bridge) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | # bridge must be followed by a slash or end of string, so that: | ||||||||||||||||||||||||||||
| 124 | # - /test matches | ||||||||||||||||||||||||||||
| 125 | # - /test/ matches | ||||||||||||||||||||||||||||
| 126 | # - /test/something matches | ||||||||||||||||||||||||||||
| 127 | # - /testsomething does not match | ||||||||||||||||||||||||||||
| 128 | # if the bridge is already followed by a trailing slash, it's not a | ||||||||||||||||||||||||||||
| 129 | # concern | ||||||||||||||||||||||||||||
| 130 | 0 | ▶ | $pattern .= '(?:/|$)' unless $trailing_slash; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 131 | } | ||||||||||||||||||||||||||||
| 132 | else { | ||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||
| 134 | # regular pattern must end immediately | ||||||||||||||||||||||||||||
| 135 | 33 | ▶ | $pattern .= quotemeta('/') . '?' unless $trailing_slash; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 136 | 33 | $pattern .= '$'; | |||||||||||||||||||||||||||
| 137 | } | ||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||
| 139 | 33 | return qr{^$pattern}; | |||||||||||||||||||||||||||
| 140 | } | ||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||
| 142 | 33 | ▶ | sub BUILD ($self, $) | ||||||||||||||||||||||||||
|
Subroutine
BUILD: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 143 | { | ||||||||||||||||||||||||||||
| 144 | 33 | $self->_regex; # ensure tokens are created | |||||||||||||||||||||||||||
| 145 | } | ||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||
| 147 | 150 | ▶ | sub compare ($self, $request_path) | ||||||||||||||||||||||||||
|
Subroutine
compare: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 148 | { | ||||||||||||||||||||||||||||
| 149 | 150 | ▶ | return undef unless $request_path =~ $self->_regex; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||
| 151 | # initialize the named parameters hash and its default values | ||||||||||||||||||||||||||||
| 152 | 97 | my %named = ($self->defaults->%*, %+); | |||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||
| 154 | # transform into a list of parameters | ||||||||||||||||||||||||||||
| 155 | 97 | return [map { $named{$_->{label}} } $self->tokens->@*]; | |||||||||||||||||||||||||||
| 156 | } | ||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||
| 158 | 12 | ▶ | sub build ($self, %args) | ||||||||||||||||||||||||||
|
Subroutine
build: called CC=9 SCAR=22.0
Pod
documented
| |||||||||||||||||||||||||||||
| 159 | { | ||||||||||||||||||||||||||||
| 160 | 12 | my $pattern = $self->pattern; | |||||||||||||||||||||||||||
| 161 | 12 | my $checks = $self->checks; | |||||||||||||||||||||||||||
| 162 | 12 | %args = ($self->defaults->%*, %args); | |||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||
| 164 | 12 | foreach my $token ($self->tokens->@*) { | |||||||||||||||||||||||||||
| 165 | 13 | my $value = $args{$token->{label}}; | |||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||
| 167 | Gears::X->raise("no value for placeholder $token->{sigil}$token->{label}") | ||||||||||||||||||||||||||||
| 168 | 13 | ▶ | unless defined $value || optional $token->{sigil}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $value or optional $token->{"sigil"}
MC/DC100%2/2
defined $value or optional $token->{"sigil"}
defined $value
optional $token->{"sigil"}
Truth table100%3/3
A or B
A= defined $valueB= optional $token->{"sigil"}
| |||||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||
| 170 | 12 | ▶ | if (defined $value) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 171 | 9 | my $safe = '^A-Za-z0-9\-\._~'; | |||||||||||||||||||||||||||
| 172 | 9 | ▶ | $safe .= '/' unless noslash $token->{sigil}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 173 | 9 | $value = uri_escape_utf8 $value, "^$safe"; | |||||||||||||||||||||||||||
| 174 | } | ||||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||
| 176 | 12 | my $to_replace = qr{ | |||||||||||||||||||||||||||
| 177 | \Q$token->{sigil}\E | ||||||||||||||||||||||||||||
| 178 | $token->{label} | ||||||||||||||||||||||||||||
| 179 | }x; | ||||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||
| 181 | 12 | ▶ | if (defined $value) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 182 | 9 | my $check = $checks->{$token->{label}}; | |||||||||||||||||||||||||||
| 183 | 9 | ▶ | Gears::X->raise("bad value for placeholder $token->{sigil}$token->{label}") | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$check and not $value =~ /^$check$/u
MC/DC50%1/2
$check and not $value =~ /^$check$/u
$check
not $value =~ /^$check$/u
Truth table66%2/3
A and not(B)
A= $checkB= not $value =~ /^$check$/u
| |||||||||||||||||||||||||||||
| 184 | if $check && $value !~ /^$check$/; | ||||||||||||||||||||||||||||
| 185 | |||||||||||||||||||||||||||||
| 186 | 7 | $pattern =~ s{\{?$to_replace\}?}{$value}; | |||||||||||||||||||||||||||
| 187 | } | ||||||||||||||||||||||||||||
| 188 | else { | ||||||||||||||||||||||||||||
| 189 | # slash should be removed as well for optional placeholders (if no brackets) | ||||||||||||||||||||||||||||
| 190 | 3 | $pattern =~ s{/$to_replace|\{?$to_replace\}?}{}; | |||||||||||||||||||||||||||
| 191 | } | ||||||||||||||||||||||||||||
| 192 | } | ||||||||||||||||||||||||||||
| 193 | |||||||||||||||||||||||||||||
| 194 | 9 | return $pattern; | |||||||||||||||||||||||||||
| 195 | } | ||||||||||||||||||||||||||||
| 196 | |||||||||||||||||||||||||||||
| 197 | __END__ | ||||||||||||||||||||||||||||