PathInfo/Controller.pm

filter: Statementstmt 93.9% 93 / 99 Branchbran 77.5% 45 / 58 Conditioncond 66.6% 12 / 18 MC/DCmcdc 40.0% 4 / 10 Subroutinesub 100% 8 / 8 Podpod 100% 3 / 3 totaltotal 84.1% 165 / 196 CC 49 SCAR 33.5
CC
49
Cov
86%
add_rule
41.7
_match
12.5
match
5.0
CRAP 28.5
↑ Summary PathInfo.pm »
1 package Router::PathInfo::Controller;
2 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 =head1 NAME
6  
7 B<Router::PathInfo::Controller> provides a mapping PATH_INFO to controllers.
8  
9 =head1 SYNOPSIS
10
11 # create instance
12 my $r = Router::PathInfo::Controller->new();
13
14 # describe connect
15 $r->add_rule(connect => '/foo/:enum(bar|baz)/:any', action => ['some','bar']);
16
17 # prepare arguments (this action to prepare $env hidden from you in the module Router::PathInfo)
18 my $env = {PATH_INFO => '/foo/baz/bar', REQUEST_METHOD => 'GET'};
19 my @segment = split '/', $env->{PATH_INFO}, -1;
20 shift @segment;
21 $env->{'psgix.tmp.RouterPathInfo'} = {
22 segments => [@segment],
23 depth => scalar @segment
24 };
25
26 # match
27 my $res = $r->match($env);
28 # $res = HASH(0x93d74d8)
29 # 'action' => ARRAY(0x99294e8)
30 # 0 'some'
31 # 1 'bar'
32 # 'segment' => ARRAY(0x93d8038)
33 # 0 'baz'
34 # 1 'bar'
35 # 'type' => 'controller'
36  
37 # or $res may by undef
38  
39 =head1 DESCRIPTION
40  
41 C<Router::PathInfo::Controller> is used for matching sets of trees.
42 Therefore, search matching is faster and more efficient,
43 than a simple enumeration of regular expressions to search for a suitable result.
44  
45 In the descriptions of 'C<connect>' by adding rules, you can use these tokens:
46  
47 :any - match with any segment
48 :re(...some regular expression...) - match with the specified regular expression
49 :enum(...|...) - match with a segment from the set
50  
51 and sub-attribute for rules
52
53 :name(...)
54  
55 For example
56
57 '/foo/:name(some_name)bar/:any'
58 '/foo/:re(^\d{4}\w{4}$)/:name(my_token):any'
59 '/:enum(foo|bar|baz)/:re(^\d{4}\w{4}$)/:any'
60
61 All descriptions of the segments have a certain weight.
62 Thus, the description C<:enum> has the greatest weight, a description of C<:re> weighs even less. Weakest coincidence is C<:any>.
63  
64 For all descriptions 'C<connect>' using these tokens in the match will be returned to a special key 'C<segment>'
65 in which stores a list of all segments C<PATH_INFO> they are responsible.
66  
67 An important point: description 'C<connect>' dominates over http method. Example:
68
69 $r->add_rule(connect => '/foo/:any/baz', action => 'one', methods => ['GET','DELETE']);
70 $r->add_rule(connect => '/foo/bar/:any', action => 'two');
71
72 for '/foo/bar/baz' with GET -> 'two'
73  
74 In C<action> you can pass any value: object, arrayref, hashref or a scalar.
75  
76 =head1 METHODS
77  
78 =cut
79  
80 2 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
81 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
82  
83 2 my $http_methods = {
84 GET => 1,
85 POST => 1,
86 PUT => 1,
87 OPTIONS => 1,
88 DELETE => 1,
89 HEAD => 1
90 };
91  
92 =head2 new()
93  
94 Simple constructor
95  
96 =cut
97 3 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
98 3 bless {
99 rule => {},
100 re_compile => {},
101 }, shift;
102 }
103  
104 =head2 add_rule(connect => $describe_connect, action => $action_token[, methods => $arrayref, match_callback => $code_ref])
105  
106 Add your description to match.
107  
108 'C<methods>' - arrayref of items GET, POST, PUT, OPTIONS, DELETE, HEAD
109  
110 'C<match_callback>' - coderef is called after match found. It takes two arguments: a match found and heshref passed parameters (see method C<match>).
111 Example:
112  
113 $r->add_rule(
114 connect => '/foo/:enum(bar|baz)/:any',
115 action => ['any thing'],
116 methods => ['POST'],
117 match_callback => sub {
118 my ($match, $env) = @_;
119
120 if ($env->{...} == ..) {
121 # $match->{action}->[0] eq 'any thing'
122 return $match;
123 } else {
124 return {
125 type => 'error',
126 code => 403,
127 desc => 'blah-blah'
128 };
129 }
130 }
131 );
132  
133 =cut
134 7 ▶sub add_rule {
Subroutine add_rule: called CC=34 SCAR=37.3
Pod documented
135 7 my ($self, %args) = @_;
136
137 7 for ( ('connect', 'action') ) {
138 14 ▶ unless ($args{$_}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($args{$_}) 0 14 14
139 0 carp "missing '$_'";
140 0 return;
141 };
142 }
143 7 ▶ $args{methods} = $args{methods} ? [grep {$http_methods->{$_}} (ref $args{methods} eq 'ARRAY' ? @{$args{methods}} : $args{methods})] : [];
Branch: 2 branches
BranchTrueFalseTotal
$args{'methods'} ? : 2 5 7
ref $args{'methods'} eq 'ARRAY' ? : 2 0 2
144 7 ▶ my @methods = $args{methods}->[0] ? @{$args{methods}} : keys %$http_methods;
Branch: 1 branch
BranchTrueFalseTotal
$args{'methods'}[0] ? : 2 5 7
145 7 my $methods_weight = $#methods;
146
147 7 ▶ my $sub_after_match = $args{match_callback} if ref $args{match_callback} eq 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
if ref $args{"match_callback"} eq "CODE" 1 6 7
148
149 7 my @depth = split '/',$args{connect},-1;
150
151 7 my $named_segment = {}; my $i = 0;
152
153 7 my $res = [];
154 7 for (@methods) {
155 33 ▶ $self->{rule}->{$_}->{$#depth} ||= {};
Condition100%2/2
$self->{"rule"}{$_}{$#depth} ||= {}
l!l
924
MC/DC100%1/1
$self->{"rule"}{$_}{$#depth} ||= {}
$self->{"rule"}{$_}{$#depth}
Truth table100%2/2
A
A= $self->{"rule"}{$_}{$#depth}
Aresult
11
00
156 33 push @$res, $self->{rule}->{$_}->{$#depth};
157 }
158
159 7 (my $tmp = $args{connect}) =~ s!
160 (/)(?=/) | # double slash
161 (/$) | # end slash
162 /(:name\(["']?(.*?)["']?\))?:enum\(([^/]+)\)(?= $|/) | # enum
163 /(:name\(["']?(.*?)["']?\))?:re\(([^/]+)\)(?= $|/) | # re
164 /(:name\(["']?(.*?)["']?\))?(:any)(?= $|/) | # any
165 /(:name\(["']?(.*?)["']?\))?([^/]+)(?= $|/) # eq
166 !
167 20 ▶ if ($1 or $2) {
Branch: 5 branches
BranchTrueFalseTotal
if ($1 or $2) { } 0 20 20
elsif ($5) { } 6 14 20
elsif ($8) { } 1 13 14
elsif ($11) { } 5 8 13
elsif ($14) { } 8 0 8
Condition33%1/3
$1 or $2
l!l&&r!l&&!r
0020
MC/DC0%0/2
$1 or $2
$1 $2
Truth table33%1/3
A or B
A= $1B= $2
ABresult
1X1
011
000
168 0 ▶ $_->{exactly}->{''} ||= {} for @$res;
Condition0%0/2
$_->{"exactly"}{""} ||= {}
l!l
00
MC/DC0%0/1
$_->{"exactly"}{""} ||= {}
$_->{"exactly"}{""}
Truth table0%0/2
A
A= $_->{"exactly"}{""}
Aresult
11
00
169 0 $res = [map {$_->{exactly}->{''}} @$res];
170 } elsif ($5) {
171 6 my @val = split('\|',$5);
172 6 my @tmp;
173 6 for my $val (@val) {
174 12 for (@$res) {
175 54 ▶ $_->{exactly}->{$val} ||= {};
Condition100%2/2
$_->{"exactly"}{$val} ||= {}
l!l
1836
MC/DC100%1/1
$_->{"exactly"}{$val} ||= {}
$_->{"exactly"}{$val}
Truth table100%2/2
A
A= $_->{"exactly"}{$val}
Aresult
11
00
176 54 push @tmp, $_->{exactly}->{$val};
177 };
178 }
179 6 $res = [@tmp];
180 6 ▶ $named_segment->{$i} = $4 if $4;
Branch: 1 branch
BranchTrueFalseTotal
if $4 1 5 6
181 } elsif ($8) {
182 1 $self->{re_compile}->{$8} = qr{$8}s;
183 1 ▶ $_->{regexp}->{$8} ||= {} for @$res;
Condition50%1/2
$_->{"regexp"}{$8} ||= {}
l!l
012
MC/DC0%0/1
$_->{"regexp"}{$8} ||= {}
$_->{"regexp"}{$8}
Truth table50%1/2
A
A= $_->{"regexp"}{$8}
Aresult
11
00
184 1 $res = [map {$_->{regexp}->{$8}} @$res];
185 1 ▶ $named_segment->{$i} = $7 if $7;
Branch: 1 branch
BranchTrueFalseTotal
if $7 1 0 1
186 } elsif ($11) {
187 5 ▶ $_->{default}->{''} ||= {} for @$res;
Condition100%2/2
$_->{"default"}{""} ||= {}
l!l
636
MC/DC100%1/1
$_->{"default"}{""} ||= {}
$_->{"default"}{""}
Truth table100%2/2
A
A= $_->{"default"}{""}
Aresult
11
00
188 5 $res = [map {$_->{default}->{''}} @$res];
189 5 ▶ $named_segment->{$i} = $10 if $10;
Branch: 1 branch
BranchTrueFalseTotal
if $10 1 4 5
190 } elsif ($14) {
191 8 ▶ $_->{exactly}->{$14} ||= {} for @$res;
Condition100%2/2
$_->{"exactly"}{$14} ||= {}
l!l
930
MC/DC100%1/1
$_->{"exactly"}{$14} ||= {}
$_->{"exactly"}{$14}
Truth table100%2/2
A
A= $_->{"exactly"}{$14}
Aresult
11
00
192 8 $res = [map {$_->{exactly}->{$14}} @$res];
193 8 ▶ $named_segment->{$i} = $13 if $13;
Branch: 1 branch
BranchTrueFalseTotal
if $13 0 8 8
194 } else {
195 # default as word
196 0 croak "cant't resolve connect '$args{connect}'"
197 }
198 20 $i++;
199 !gex;
200
201 7 for (@$res) {
202 60 ▶ if (not $_->{match} or $_->{match}->[3] >= $methods_weight) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $_->{"match"} or $_->{"match"}[3] >= $methods_weight) 60 0 60
Condition66%2/3
not $_->{"match"} or $_->{"match"}[3] >= $methods_weight
l!l&&r!l&&!r
5460
MC/DC0%0/2
not $_->{"match"} or $_->{"match"}[3] >= $methods_weight
not $_->{"match"} $_->{"match"}[3] >= $methods_weight
Truth table66%2/3
not(A) or B
A= not $_->{"match"}B= $_->{"match"}[3] >= $methods_weight
ABresult
1X1
011
000
203 # set only if no match or a match for a more accurate description
204 60 ▶ $_->{match} = [$args{action}, keys %$named_segment ? $named_segment : undef, $sub_after_match, $methods_weight];
Branch: 1 branch
BranchTrueFalseTotal
keys %$named_segment ? : 24 36 60
205 }
206 }
207  
208 7 return 1;
209 }
210  
211 22 ▶sub _match {
Subroutine _match: called CC=12 SCAR=25.3
212 22 my ($self, $reserch, $size_el, @el) = @_;
213 22 my $ret;
214 22 my $not_exactly = 0;
215 22 my $segment = shift @el;
216 22 $size_el--;
217 22 my $exactly = $reserch->{exactly}->{$segment};
218 22 ▶ if (defined $exactly) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $exactly) 14 8 22
219 14 ▶ ($ret, $not_exactly) = $size_el ? $self->_match($exactly, $size_el, @el) : $exactly->{match};
Branch: 1 branch
BranchTrueFalseTotal
$size_el ? : 13 1 14
220 14 ▶ return ($ret, $not_exactly) if $ret;
Branch: 1 branch
BranchTrueFalseTotal
if $ret 14 0 14
221 };
222
223 8 ▶ if ($reserch->{regexp}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($reserch->{"regexp"}) 1 7 8
224 1 for (keys %{$reserch->{regexp}}) {
225 1 ▶ if ($segment =~ $self->{re_compile}->{$_}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($segment =~ /$self->{"re_compile"}{$_}/s) 1 0 1
226 1 ▶ ($ret) = $size_el ? $self->_match($reserch->{regexp}->{$_}, $size_el, @el) : $reserch->{regexp}->{$_}->{match};
Branch: 1 branch
BranchTrueFalseTotal
$size_el ? : 0 1 1
227 1 ▶ return ($ret, 1) if $ret;
Branch: 1 branch
BranchTrueFalseTotal
if $ret 1 0 1
228 };
229 }
230 };
231
232 7 ▶ if ($reserch->{default}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($reserch->{"default"}) 5 2 7
233 5 ▶ ($ret) = $size_el ? $self->_match($reserch->{default}->{''}, $size_el, @el) : $reserch->{default}->{''}->{match};
Branch: 1 branch
BranchTrueFalseTotal
$size_el ? : 0 5 5
234 5 ▶ return ($ret, 1) if $ret;
Branch: 1 branch
BranchTrueFalseTotal
if $ret 5 0 5
235 }
236
237 2 return;
238 }
239  
240 =head2 match({REQUEST_METHOD => ..., 'psgix.tmp.RouterPathInfo' => ...})
241  
242 Search match. See SYNOPSIS.
243  
244 If a match is found, it returns hashref:
245  
246 {
247 type => 'controller',
248 action => $action,
249 name_segments => $arrayref
250 }
251  
252 Otherwise, undef.
253  
254 =cut
255 9 ▶sub match {
Subroutine match: called CC=5 SCAR=16.1
Pod documented
256 9 my $self = shift;
257 9 my $env = shift;
258
259 9 my $depth = $env->{'psgix.tmp.RouterPathInfo'}->{depth};
260
261 my ($match, $not_exactly) = $self->_match(
262 $self->{rule}->{$env->{REQUEST_METHOD}}->{$depth},
263 $depth,
264 9 @{$env->{'psgix.tmp.RouterPathInfo'}->{segments}}
265 );
266  
267 9 ▶ if ($match) {
Branch: 1 branch
BranchTrueFalseTotal
if ($match) { } 7 2 9
268 7 my $ret = {
269 type => 'controller',
270 action => $match->[0]
271 };
272 7 ▶ unless ($match->[1]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($match->[1]) { } 2 5 7
273 5 $ret->{name_segments} = {};
274 } else {
275 2 $ret->{name_segments}->{$match->[1]->{$_}} = $env->{'psgix.tmp.RouterPathInfo'}->{segments}->[$_] for keys %{$match->[1]};
276 }
277 7 ▶ $ret->{_callback} = $match->[2] if $match->[2];
Branch: 1 branch
BranchTrueFalseTotal
if $match->[2] 1 6 7
278 7 return ($not_exactly, $ret);
279 } else {
280 2 return;
281 }
282
283 # if ($match) {
284 # my $ret = {
285 # type => 'controller',
286 # action => $match->[0],
287 # segment => $match->[1] ? [map {$env->{'psgix.tmp.RouterPathInfo'}->{segments}->[$_]} @{$match->[1]}] : []
288 # };
289 # if ($match->[2]) {
290 # return ($not_exactly, $match->[2]->($ret,$env));
291 # } else {
292 # return ($not_exactly, $ret);
293 # }
294 # } else {
295 # return;
296 # }
297
298 }
299  
300 =head1 SEE ALSO
301  
302 L<Router::PathInfo>, L<Router::PathInfo::Static>
303  
304 =head1 AUTHOR
305  
306 mr.Rico <catamoose at yandex.ru>
307  
308 =cut
309  
310 2 1;
311 __END__
↑ Summary PathInfo.pm »