PathInfo.pm

filter: Statementstmt 90.4% 66 / 73 Branchbran 58.3% 21 / 36 Conditioncond 60.0% 15 / 25 MC/DCmcdc 37.5% 6 / 16 Subroutinesub 83.3% 10 / 12 Podpod 60.0% 3 / 5 totaltotal 72.4% 121 / 167 CC 28 SCAR 24.7
CC
28
Cov
76%
match
19.1
new
12.6
instance
6.0
CRAP 11.8
« PathInfo/Controller.pm ↑ Summary PathInfo/Static.pm »
1 package Router::PathInfo;
2 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 1 our $VERSION = '0.05';
6  
7 1 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 1 ▶use Router::PathInfo::Controller;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use Router::PathInfo::Static;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 =head1 NAME
14  
15 B<Router::PathInfo> - PATH_INFO router, based on search trees
16  
17 =head1 DESCRIPTION
18  
19 Allows balancing PATH_INFO to static and controllers.
20 It has a simple and intuitive interface.
21  
22 =head1 WARNING
23  
24 Version less than 0.05 is depricated.
25  
26 =head1 SYNOPSIS
27  
28 use Router::PathInfo;
29
30 # or
31 use Router::PathInfo as => singletone;
32 # this allow to call after new: instance, clear_singleton
33
34 my $r = Router::PathInfo->new(
35 static => {
36 allready => {
37 path => '/path/to/static',
38 first_uri_segment => 'static'
39 }
40 },
41 cache_limit => 300
42 );
43
44 $r->add_rule(
45 connect => '/foo/:enum(bar|baz)/:name(year):re(^\d{4}$)/:any',
46 action => $some_thing,
47 mthods => ['GET','DELETE'],
48 match_callback => $code_ref
49 );
50
51 my $env = {PATH_INFO => '/foo/bar/2011/baz', REQUEST_METHOD => 'GET'};
52
53 my $res = $r->match($env);
54 # or
55 my $res = $r->match('/foo/bar/2011/baz'); # GET by default
56
57 # $res = {
58 # type => 'controller',
59 # action => $some, # result call $code_ref->($match, $env)
60 # name_segments => {'year' => 2011}
61 # }
62
63
64 $env = {PATH_INFO => '/static/img/some.jpg'};
65
66 $res = $r->match($env);
67
68 # $res = {
69 # type => 'static',
70 # file => '/path/to/static/img/some.jpg',
71 # mime => 'image/jpeg'
72 # }
73  
74 See more details L<Router::PathInfo::Controller>, L<Router::PathInfo::Static>
75  
76 =head1 PACKAGE VARIABLES
77  
78 =head2 $Router::PathInfo::as_singleton
79  
80 Mode as singletone. By default - 0.
81 You can pick up directly, or:
82  
83 use Router::PathInfo as => singletone;
84 # or
85 require Router::PathInfo;
86 Router::PathInfo->import(as => singletone);
87 # or
88 $Router::PathInfo::as_singleton = 1
89
90 If you decide to work in singletone mode, raise the flag before the call to C<new>.
91  
92 =cut
93  
94 1 my $as_singletone = 0;
95  
96 1 ▶sub import {
Subroutine import: called CC=3 SCAR=12.5
97 1 my ($class, %param) = @_;
98 1 ▶ $as_singletone = 1 if ($param{as} and $param{as} eq 'singletone');
Branch: 1 branch
BranchTrueFalseTotal
if $param{"as"} and $param{"as"} eq "singletone" 0 1 1
Condition33%1/3
$param{"as"} and $param{"as"} eq "singletone"
!ll&&!rl&&r
100
MC/DC0%0/2
$param{"as"} and $param{"as"} eq "singletone"
$param{"as"} $param{"as"} eq "singletone"
Truth table33%1/3
A and B
A= $param{"as"}B= $param{"as"} eq "singletone"
ABresult
0X0
100
111
99 1 return;
100 }
101  
102 =head1 SINGLETON
103  
104 When you work in a mode singletone, you have access to methods: C<instance> and C<clear_singleton>
105  
106 =cut
107  
108  
109 =head1 METHODS
110  
111 =head2 new(static => $static, cache_limit => $cache_limit)
112  
113 Constructor. All arguments optsioanlny.
114  
115 static - it hashref arguments for the constructor L<Router::PathInfo::Static>
116  
117 cache_limit - limit of matches stored by the rules contain tokens C<:re> and C<:any>, statics and errors. By default - 200.
118 All matches (that occur on an accurate description) cached without limit.
119  
120 =cut
121  
122 1 my $singleton = undef;
123  
124 1 ▶sub new {
Subroutine new: called CC=8 SCAR=25.4
Pod documented
125 1 ▶ return $singleton if ($as_singletone and $singleton);
Branch: 1 branch
BranchTrueFalseTotal
if $as_singletone and $singleton 0 1 1
Condition33%1/3
$as_singletone and $singleton
!ll&&!rl&&r
100
MC/DC0%0/2
$as_singletone and $singleton
$as_singletone $singleton
Truth table33%1/3
A and B
A= $as_singletoneB= $singleton
ABresult
0X0
100
111
126
127 1 my $class = shift;
128 1 my $param = {@_};
129
130 my $self = bless {
131 1 ▶ static => UNIVERSAL::isa($param->{static}, 'HASH') ? Router::PathInfo::Static->new(%{delete $param->{static}}) : undef,
Branch: 1 branch
BranchTrueFalseTotal
UNIVERSAL::isa($param->{'controller'}, 'HASH') ? : 0 1 1
132 0 ▶ controller => UNIVERSAL::isa($param->{controller}, 'HASH') ? Router::PathInfo::Controller->new(%{delete $param->{controller}}) : Router::PathInfo::Controller->new(),
Branch: 1 branch
BranchTrueFalseTotal
(defined $param->{'cache_limit'} && $param->{'cache_limit'}) =~ /^\d+$/ ? : 0 1 1
Condition33%1/3
defined $param->{'cache_limit'} && $param->{'cache_limit'}
!ll&&!rl&&r
100
MC/DC0%0/2
defined $param->{'cache_limit'} && $param->{'cache_limit'}
defined $param->{'cache_limit'} $param->{'cache_limit'}
Truth table33%1/3
A && B
A= defined $param->{'cache_limit'}B= $param->{'cache_limit'}
ABresult
0X0
100
111
133 cache => {},
134 _hidden_cache => {},
135 1 ▶ cache_limit => (defined $param->{cache_limit} and $param->{cache_limit}) =~ /^\d+$/ ? $param->{cache_limit} : 200,
Branch: 1 branch
BranchTrueFalseTotal
UNIVERSAL::isa($param->{'static'}, 'HASH') ? : 1 0 1
136 cache_cnt => 0
137 }, $class;
138
139 1 ▶ $singleton = $self if $as_singletone;
Branch: 1 branch
BranchTrueFalseTotal
if $as_singletone 0 1 1
140
141 1 return $self;
142 }
143  
144 =head2 add_rule
145  
146 See C<add_rule> from L<Router::PathInfo::Controller>
147  
148 =cut
149 3 ▶sub add_rule {
Subroutine add_rule: called CC=2 SCAR=7.1
Pod documented
150 3 my $self = shift;
151 3 my $ret = 0;
152 3 ▶ if ($self->{controller}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'controller'}) { } 3 0 3
153 3 $self->{cache_cnt} = 0;
154 3 $self->{cache} = {};
155 3 $self->{controller}->add_rule(@_);
156 } else {
157 0 carp "controller not defined";
158 }
159 }
160  
161 0 ▶sub instance {$as_singletone ? $singleton : carp "singletone not allowed"}
Subroutine instance: not called CC=2 SCAR=17.9
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
$as_singletone ? : 0 0 0
162 0 ▶sub clear_singleton {undef $singleton}
Subroutine clear_singleton: not called CC=1 SCAR=6.9
Pod undocumented
163  
164 =head2 match({PATH_INFO => $path_info, REQUEST_METHOD => $method})
165  
166 Search match. Initially checked for matches on static, then according to the rules of the controllers.
167 In any event returns hashref coincidence or an error.
168  
169 Example:
170  
171 {
172 type => 'error',
173 code => 400,
174 desc => '$env->{PATH_INFO} not defined'
175 }
176
177 {
178 type => 'error',
179 code => 404,
180 desc => sprintf('not found for PATH_INFO = %s with REQUEST_METHOD = %s', $env->{PATH_INFO}, $env->{REQUEST_METHOD})
181 }
182
183 {
184 type => 'controller',
185 action => $action,
186 name_segments => $hashref_of_names_segments
187 }
188
189 {
190 type => 'static',
191 file => $serch_file,
192 mime => $mime_type
193 }
194  
195 =cut
196 7 ▶sub match {
Subroutine match: called CC=17 SCAR=29.5
Pod documented
197 7 my $self = shift;
198 7 my $env = shift;
199
200 7 ▶ unless (ref $env) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $env) { } 7 0 7
201 0 $env = {PATH_INFO => $env, REQUEST_METHOD => 'GET'};
202 } else {
203 7 ▶ $env->{REQUEST_METHOD} ||= 'GET';
Condition100%2/2
$env->{"REQUEST_METHOD"} ||= "GET"
l!l
34
MC/DC100%1/1
$env->{"REQUEST_METHOD"} ||= "GET"
$env->{"REQUEST_METHOD"}
Truth table100%2/2
A
A= $env->{"REQUEST_METHOD"}
Aresult
11
00
204 }
205
206 7 my $match = undef;
207
208 $match = {
209 type => 'error',
210 code => 400,
211 desc => '$env->{PATH_INFO} not defined'
212 7 ▶ } unless $env->{PATH_INFO};
Branch: 1 branch
BranchTrueFalseTotal
unless $env->{"PATH_INFO"} 0 7 7
213
214 # find in cache
215 7 my $cache_key = join('#',$env->{PATH_INFO}, $env->{REQUEST_METHOD});
216 7 ▶ my $cache_match = $self->{cache}->{$cache_key} || $self->{_hidden_cache}->{$cache_key};
Condition66%2/3
$self->{'cache'}{$cache_key} || $self->{'_hidden_cache'}{$cache_key}
l!l&&r!l&&!r
106
MC/DC50%1/2
$self->{'cache'}{$cache_key} || $self->{'_hidden_cache'}{$cache_key}
$self->{'cache'}{$cache_key} $self->{'_hidden_cache'}{$cache_key}
Truth table66%2/3
A || B
A= $self->{'cache'}{$cache_key}B= $self->{'_hidden_cache'}{$cache_key}
ABresult
1X1
011
000
217 7 ▶ if ($cache_match) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cache_match) 1 6 7
218 # only for controller
219 1 ▶ $cache_match = $cache_match->{_callback}->({%$cache_match},$env) if exists $cache_match->{_callback};
Branch: 1 branch
BranchTrueFalseTotal
if exists $cache_match->{"_callback"} 1 0 1
220 1 return $cache_match;
221 };
222
223 6 my @segment = split '/', $env->{PATH_INFO}, -1; shift @segment;
224 6 $env->{'psgix.tmp.RouterPathInfo'} = {
225 segments => [@segment],
226 depth => scalar @segment
227 };
228
229 # check in static
230 6 ▶ if (not $match and $self->{static}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $match and $self->{"static"}) 6 0 6
Condition33%1/3
not $match and $self->{"static"}
!ll&&!rl&&r
006
MC/DC0%0/2
not $match and $self->{"static"}
not $match $self->{"static"}
Truth table33%1/3
not(A) and B
A= not $matchB= $self->{"static"}
ABresult
0X0
100
111
231 6 $match = $self->{static}->match($env);
232 }
233
234 # check in controllers
235 # $not_exactly - match with regexp
236 6 my $not_exactly = 0;
237 6 ▶ if (not $match and $self->{controller}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $match and $self->{"controller"}) 4 2 6
Condition66%2/3
not $match and $self->{"controller"}
!ll&&!rl&&r
204
MC/DC50%1/2
not $match and $self->{"controller"}
not $match $self->{"controller"}
Truth table66%2/3
not(A) and B
A= not $matchB= $self->{"controller"}
ABresult
0X0
100
111
238 4 ($not_exactly, $match) = $self->{controller}->match($env);
239 }
240
241 # not found?
242 $match ||= {
243 type => 'error',
244 code => 404,
245 desc => sprintf('not found for PATH_INFO = %s with REQUEST_METHOD = %s', $env->{PATH_INFO}, $env->{REQUEST_METHOD})
246 6 ▶ };
Condition100%2/2
$match ||= {"type", "error", "code", 404, "desc", sprintf('not found for PATH_INFO = %s with REQUEST_METHOD = %s', $env->{"PATH_INFO"}, $env->{"REQUEST_METHOD"})}
l!l
51
MC/DC100%1/1
$match ||= {"type", "error", "code", 404, "desc", sprintf('not found for PATH_INFO = %s with REQUEST_METHOD = %s', $env->{"PATH_INFO"}, $env->{"REQUEST_METHOD"})}
$match
Truth table100%2/2
A
A= $match
Aresult
11
00
247
248 6 delete $env->{'psgix.tmp.RouterPathInfo'};
249
250 # cache!
251 6 ▶ if (not $not_exactly and $match->{type} eq 'controller') {
Branch: 2 branches
BranchTrueFalseTotal
if (not $not_exactly and $match->{'type'} eq 'controller') { } 1 5 6
elsif ($self->{'cache_limit'}) { } 5 0 5
Condition100%3/3
not $not_exactly and $match->{"type"} eq "controller"
!ll&&!rl&&r
231
MC/DC100%2/2
not $not_exactly and $match->{"type"} eq "controller"
not $not_exactly $match->{"type"} eq "controller"
Truth table100%3/3
not(A) and B
A= not $not_exactlyB= $match->{"type"} eq "controller"
ABresult
0X0
100
111
252 1 $self->{_hidden_cache}->{$cache_key} = $match;
253 } elsif ($self->{cache_limit}) {
254 5 ▶ if ($self->{cache_cnt} > $self->{cache_limit}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'cache_cnt'} > $self->{'cache_limit'}) { } 0 5 5
255 0 $self->{cache_cnt} = 0;
256 0 $self->{cache} = {};
257 } else {
258 5 $self->{cache_cnt}++;
259 }
260 5 $self->{cache}->{$cache_key} = $match;
261 }
262
263 # only for controller
264 6 ▶ $match = $match->{_callback}->({%$match},$env) if exists $match->{_callback};
Branch: 1 branch
BranchTrueFalseTotal
if exists $match->{"_callback"} 1 5 6
265
266 # match is done
267 6 return $match;
268 }
269  
270 =head1 SOURSE
271  
272 git@github.com:mrRico/p5-Router-Path-Info.git
273  
274 =head1 SEE ALSO
275  
276 L<Router::PathInfo::Static>, L<Router::PathInfo::Controller>
277  
278 =head1 AUTHOR
279  
280 mr.Rico <catamoose at yandex.ru>
281  
282 =cut
283 1 1;
284 __END__
« PathInfo/Controller.pm ↑ Summary PathInfo/Static.pm »