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