lib/Dancer2/Core/Route.pm

filter: Statementstmt 99.3% 142 / 143 Branchbran 92.3% 72 / 78 Conditioncond 87.5% 7 / 8 MC/DCmcdc 80.0% 4 / 5 Subroutinesub 100% 18 / 18 Podpod 50.0% 2 / 4 totaltotal 95.7% 245 / 256 CC 46 SCAR 21.5
CC
46
Cov
97%
match
15.0
_build_regexp_from_string
11.0
BUILDARGS
9.0
CRAP 8.6
« lib/Dancer2/ConfigReader.pm ↑ Summary blib/lib/Dancer2/Handler/File.pm »
1 package Dancer2::Core::Route;
2 # ABSTRACT: Dancer2's route handler
3 187 $Dancer2::Core::Route::VERSION = '2.3.0';
4 187 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 187 ▶use Dancer2::Core::Types;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 187 ▶use Module::Runtime 'use_module';
Subroutine BEGIN: called CC=1 SCAR=0.0
7 187 ▶use Carp 'croak';
Subroutine BEGIN: called CC=1 SCAR=0.0
8 187 ▶use List::Util 'first';
Subroutine BEGIN: called CC=1 SCAR=0.0
9 187 ▶use Scalar::Util 'blessed';
Subroutine BEGIN: called CC=1 SCAR=0.0
10 187 ▶use Ref::Util qw< is_regexpref >;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 187 ▶use Type::Registry;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 187 our ( $REQUEST, $RESPONSE, $RESPONDER, $WRITER, $ERROR_HANDLER );
14  
15 187 has name => (
16 is => 'ro',
17 isa => Str,
18 predicate => 'has_name',
19 );
20  
21 187 has method => (
22 is => 'ro',
23 isa => Dancer2Method,
24 required => 1,
25 );
26  
27 187 has code => (
28 is => 'ro',
29 required => 1,
30 isa => CodeRef,
31 );
32  
33 187 has regexp => (
34 is => 'ro',
35 required => 1,
36 );
37  
38 187 has spec_route => ( is => 'ro' );
39  
40 187 has prefix => (
41 is => 'ro',
42 isa => Maybe [Dancer2Prefix],
43 predicate => 1,
44 );
45  
46 187 has options => (
47 is => 'ro',
48 isa => HashRef,
49 trigger => \&_check_options,
50 predicate => 1,
51 );
52  
53 12 ▶sub _check_options {
Subroutine _check_options: called CC=4 SCAR=13.9
54 12 my ( $self, $options ) = @_;
55 12 ▶ return 1 unless defined $options;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $options 0 12 12
56  
57 12 my @supported_options = (
58 qw/content_type agent user_agent content_length
59 path_info/
60 );
61 12 for my $opt ( keys %{$options} ) {
62 croak "Not a valid option for route matching: `$opt'"
63 11 ▶ if not( grep {/^$opt$/} @supported_options );
Branch: 1 branch
BranchTrueFalseTotal
unless grep {/^$opt$/;} @supported_options 1 10 11
64 }
65 11 return 1;
66 }
67  
68 # private attributes
69  
70 187 has _should_capture => (
71 is => 'ro',
72 isa => Bool,
73 );
74  
75 187 has _match_data => (
76 is => 'rw',
77 isa => HashRef,
78 );
79  
80 has _params => (
81 is => 'ro',
82 isa => ArrayRef,
83 17 ▶ default => sub { [] },
Subroutine __ANON__: called CC=1 SCAR=0.0
84 187 );
85  
86 has _typed_params => (
87 is => 'ro',
88 isa => ArrayRef,
89 17 ▶ default => sub { [] },
Subroutine __ANON__: called CC=1 SCAR=0.0
90 187 );
91  
92 1978 ▶sub match {
Subroutine match: called CC=15 SCAR=27.1
Pod documented
93 1978 my ( $self, $request ) = @_;
94  
95 1978 ▶ if ( $self->has_options ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_options) 18 1960 1978
96 18 ▶ return unless $self->validate_options($request);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->validate_options($request) 12 6 18
97 }
98  
99 1966 my @values = $request->path =~ $self->regexp;
100  
101 1966 ▶ return unless @values;
Branch: 1 branch
BranchTrueFalseTotal
unless @values 1162 804 1966
102  
103 # if some named captures are found, return captures
104 # - Note no @values implies no named captures
105 804 ▶ if (my %captures = %+ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(%captures) = %+) 3 801 804
106 3 return $self->_match_data( { captures => \%captures } );
107 }
108  
109 # regex comments are how we know if we captured a token,
110 # splat or a megasplat
111 801 my @token_or_splat =
112 $self->regexp =~ /\(\?#((?:typed_)?token|(?:mega)?splat)\)/g;
113  
114 801 ▶ if (@token_or_splat) {
Branch: 1 branch
BranchTrueFalseTotal
if (@token_or_splat) 114 687 801
115 # our named tokens
116 114 my @tokens = @{ $self->_params };
117 114 my @typed_tokens = @{ $self->_typed_params };
118  
119 114 my %params;
120 my @splat;
121 114 ▶ for ( my $i = 0; $i < @values; $i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @values) 136 110 246
122 # Is this value from a token?
123 136 ▶ if ( defined $token_or_splat[$i] ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $token_or_splat[$i]) 135 1 136
124 135 ▶ if ( $token_or_splat[$i] eq 'typed_token' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($token_or_splat[$i] eq "typed_token") 15 120 135
125 15 my ( $token, $type ) = @{ shift @typed_tokens };
126  
127 15 ▶ if (defined $values[$i]) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $values[$i]) 13 2 15
128 # undef value mean that token was marked as optional so
129 # we only do type check on defined value
130 return
131 13 ▶ unless $type->check($values[$i]);
Branch: 1 branch
BranchTrueFalseTotal
unless $type->check($values[$i]) 4 9 13
132 }
133 11 $params{$token} = $values[$i];
134 11 next;
135 }
136 120 ▶ if ( $token_or_splat[$i] eq 'token' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($token_or_splat[$i] eq "token") 33 87 120
137 33 $params{ shift @tokens } = $values[$i];
138 33 next;
139 }
140  
141 # megasplat values are split on '/'
142 87 ▶ if ($token_or_splat[$i] eq 'megasplat') {
Branch: 1 branch
BranchTrueFalseTotal
if ($token_or_splat[$i] eq "megasplat") 45 42 87
143 45 ▶ $values[$i] = [
Branch: 1 branch
BranchTrueFalseTotal
defined $values[$i] ? : 44 1 45
144 defined $values[$i] ? split( m{/} , $values[$i], -1 ) : ()
145 ];
146 }
147 }
148 88 push @splat, $values[$i];
149 }
150 110 return $self->_match_data( {
151 %params,
152 (splat => \@splat)x!! @splat,
153 });
154 }
155  
156 687 ▶ if ( $self->_should_capture ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_should_capture) 5 682 687
157 5 return $self->_match_data( { splat => \@values } );
158 }
159  
160 682 return $self->_match_data( {} );
161 }
162  
163 755 ▶sub execute {
Subroutine execute: called CC=8 SCAR=21.7
Pod documented
164 755 my ( $self, $app, @args ) = @_;
165 755 local $REQUEST = $app->request;
166 755 local $RESPONSE = $app->response;
167  
168 755 my $content = $self->code->( $app, @args );
169  
170 # users may set content in the response. If the response has
171 # content, and the returned value from the route code is not
172 # an object (well, reference) we ignore the returned value
173 # and use the existing content in the response instead.
174 598 ▶ $RESPONSE->has_content && !ref $content
Branch: 1 branch
BranchTrueFalseTotal
$Dancer2::Core::Route::RESPONSE->has_content and not ref $content and return $app->_prep_response($Dancer2::Core::Route::RESPONSE) 3 595 598
Condition66%2/3
$Dancer2::Core::Route::RESPONSE->has_content and not ref $content
!ll&&!rl&&r
59503
MC/DC50%1/2
$Dancer2::Core::Route::RESPONSE->has_content and not ref $content
$Dancer2::Core::Route::RESPONSE->has_content not ref $content
Truth table66%2/3
A and not(B)
A= $Dancer2::Core::Route::RESPONSE->has_contentB= not ref $content
ABresult
0X0
100
111
175 and return $app->_prep_response( $RESPONSE );
176  
177 595 ▶ my $type = blessed($content)
Branch: 1 branch
BranchTrueFalseTotal
my $type = builtin::blessed($content) or return $app->_prep_response($Dancer2::Core::Route::RESPONSE, $content) 1 594 595
178 or return $app->_prep_response( $RESPONSE, $content );
179  
180 # Plack::Response: proper ArrayRef-style response
181 1 ▶ $type eq 'Plack::Response'
Branch: 1 branch
BranchTrueFalseTotal
$type eq "Plack::Response" and $Dancer2::Core::Route::RESPONSE = "Dancer2::Core::Response"->new_from_plack($Dancer2::Core::Route::RESPONSE) 0 1 1
182 and $RESPONSE = Dancer2::Core::Response->new_from_plack($RESPONSE);
183  
184 # CodeRef: raw PSGI response
185 # do we want to allow it and forward it back?
186 # do we want to upgrade it to an asynchronous response?
187 1 ▶ $type eq 'CODE'
Branch: 1 branch
BranchTrueFalseTotal
$type eq "CODE" and die "We do not support returning code references from routes.\n" 0 1 1
188 and die "We do not support returning code references from routes.\n";
189  
190 # Dancer2::Core::Response, Dancer2::Core::Response::Delayed:
191 # proper responses
192 1 ▶ $type eq 'Dancer2::Core::Response'
Branch: 1 branch
BranchTrueFalseTotal
$type eq "Dancer2::Core::Response" and return $Dancer2::Core::Route::RESPONSE 0 1 1
193 and return $RESPONSE;
194  
195 1 ▶ $type eq 'Dancer2::Core::Response::Delayed'
Branch: 1 branch
BranchTrueFalseTotal
$type eq "Dancer2::Core::Response::Delayed" and return $content 1 0 1
196 and return $content;
197  
198 # we can't handle arrayref or hashref
199 # because those might be serialized back
200 0 die "Unrecognized response type from route: $type.\n";
201 }
202  
203 # private subs
204  
205 978 ▶sub BUILDARGS {
Subroutine BUILDARGS: called CC=9 SCAR=22.0
Pod undocumented
206 978 my ( $class, %args ) = @_;
207  
208 978 my $prefix = $args{prefix};
209 978 my $regexp = $args{regexp};
210  
211 978 my $type_library = delete $args{type_library};
212 978 ▶ if ( $type_library) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type_library) 3 975 978
213 3 ▶ eval { use_module($type_library); 1 }
Branch: 1 branch
BranchTrueFalseTotal
eval { do { &use_module($type_library); 1 } } or croak("type_library $type_library cannot be loaded") 3 0 3
214 or croak "type_library $type_library cannot be loaded";
215 }
216 978 ▶ $type_library ||= 'Dancer2::Core::Types';
Condition100%2/2
$type_library ||= "Dancer2::Core::Types"
l!l
3975
MC/DC100%1/1
$type_library ||= "Dancer2::Core::Types"
$type_library
Truth table100%2/2
A
A= $type_library
Aresult
11
00
217  
218 # init prefix
219 978 ▶ if ( $prefix ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($prefix) { } 35 943 978
elsif (not is_regexpref($regexp)) { } 927 16 943
220 $args{regexp} =
221 35 ▶ is_regexpref($regexp) ? qr{^\Q${prefix}\E${regexp}$} :
Branch: 1 branch
BranchTrueFalseTotal
is_regexpref($regexp) ? : 1 34 35
222 $prefix . $regexp;
223 }
224 elsif ( !is_regexpref($regexp) ) {
225 # No prefix, so ensure regexp begins with a '/'
226 927 ▶ index( $regexp, '/', 0 ) == 0 or $args{regexp} = "/$regexp";
Branch: 1 branch
BranchTrueFalseTotal
index($regexp, "/", 0) == 0 or $args{"regexp"} = "/$regexp" 920 7 927
227 }
228  
229 # init regexp
230 978 $regexp = $args{regexp}; # updated value
231 978 $args{spec_route} = $regexp;
232  
233 978 ▶ if ( is_regexpref($regexp)) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_regexpref($regexp)) { } 17 961 978
234 17 $args{_should_capture} = 1;
235 }
236 else {
237 @args{qw/ regexp _params _typed_params _should_capture/} =
238 961 @{ _build_regexp_from_string($regexp, $type_library) };
239 }
240  
241 973 return \%args;
242 }
243  
244 961 ▶sub _build_regexp_from_string {
Subroutine _build_regexp_from_string: called CC=11 SCAR=24.0
245 961 my ($string, $type_library) = @_;
246  
247 961 my $capture = 0;
248 961 my ( @params, @typed_params );
249  
250 961 my $type_registry = Type::Registry->new;
251 961 $type_registry->add_types($type_library);
252  
253 # look for route with tokens [aka params] (/hello/:foo)
254 961 ▶ if ( $string =~ /:/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($string =~ /:/) 70 891 961
255 70 my @found = $string =~ m|:([^/.\?]+)|g;
256 70 foreach my $token ( @found ) {
257 80 ▶ if ( $token =~ s/\[(.+)\]$// ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($token =~ s/\[(.+)\]$//) { } 18 62 80
258  
259 # typed token
260 18 my $type = $type_registry->lookup($1);
261 17 push @typed_params, [ $token, $type ];
262 }
263 else {
264 62 push @params, $token;
265 }
266 }
267 69 ▶ if (@typed_params) {
Branch: 1 branch
BranchTrueFalseTotal
if (@typed_params) 16 53 69
268 16 $string =~ s!(:[^/.\?]+\[[^/.\?]+\])!(?#typed_token)([^/]+)!g;
269 16 $capture = 1;
270 }
271 69 ▶ if (@params) {
Branch: 1 branch
BranchTrueFalseTotal
if (@params) 56 13 69
272 62 ▶ first { $_ eq 'splat' } @params
Subroutine __ANON__: called CC=1 SCAR=0.0
273 56 ▶ and croak q{Named placeholder 'splat' is deprecated};
Branch: 1 branch
BranchTrueFalseTotal
&first(sub { $_ eq "splat"; } , @params) and croak("Named placeholder 'splat' is deprecated") 2 54 56
274  
275 60 ▶ first { $_ eq 'captures' } @params
Subroutine __ANON__: called CC=1 SCAR=0.0
276 54 ▶ and croak q{Named placeholder 'captures' is deprecated};
Branch: 1 branch
BranchTrueFalseTotal
&first(sub { $_ eq "captures"; } , @params) and croak("Named placeholder 'captures' is deprecated") 2 52 54
277  
278 52 $string =~ s!(:[^\/\.\?]+)!(?#token)([^/]+)!g;
279 52 $capture = 1;
280 }
281 }
282  
283 # parse megasplat
284 # we use {0,} instead of '*' not to fall in the splat rule
285 # same logic for [^\n] instead of '.'
286 956 ▶ $capture = 1 if $string =~ s!\Q**\E!(?#megasplat)([^\n]+)!g;
Branch: 1 branch
BranchTrueFalseTotal
if $string =~ s/\*\*/(?#megasplat)([^\n]+)/g 46 910 956
287  
288 # parse wildcards
289 956 ▶ $capture = 1 if $string =~ s!\*!(?#splat)([^/]+)!g;
Branch: 1 branch
BranchTrueFalseTotal
if $string =~ s[\*][(?#splat)([^/]+)]g 35 921 956
290  
291 # escape dots
292 956 ▶ $string =~ s/\./\\\./g if $string =~ /\./;
Branch: 1 branch
BranchTrueFalseTotal
if $string =~ /\./ 8 948 956
293  
294 # escape slashes
295 956 $string =~ s/\//\\\//g;
296  
297 956 return [ "^$string\$", \@params, \@typed_params, $capture ];
298 }
299  
300 18 ▶sub validate_options {
Subroutine validate_options: called CC=4 SCAR=13.9
Pod undocumented
301 18 my ( $self, $request ) = @_;
302  
303 18 for my $option ( keys %{ $self->options } ) {
304 return 0
305 if (
306 ( not $request->$option )
307 19 ▶ || ( $request->$option !~ $self->options->{ $option } )
Branch: 1 branch
BranchTrueFalseTotal
unless $request->$option and $request->$option =~ /$self->options->{$option}/ 12 7 19
Condition100%3/3
$request->$option and $request->$option =~ /$self->options->{$option}/
!ll&&!rl&&r
937
MC/DC100%2/2
$request->$option and $request->$option =~ /$self->options->{$option}/
$request->$option $request->$option =~ /$self->options->{$option}/
Truth table100%3/3
A and B
A= $request->$optionB= $request->$option =~ /$self->options->{$option}/
ABresult
0X0
100
111
308 )
309 }
310 6 return 1;
311 }
312  
313 187 1;
314  
315 __END__
« lib/Dancer2/ConfigReader.pm ↑ Summary blib/lib/Dancer2/Handler/File.pm »