lib/Catalyst/DispatchType/Path.pm

filter: Statementstmt 98.7% 77 / 78 Branchbran 90.9% 20 / 22 Conditioncond 88.8% 8 / 9 MC/DCmcdc 80.0% 4 / 5 Subroutinesub 100% 12 / 12 Podpod 100% 5 / 5 totaltotal 96.1% 126 / 131 CC 20 SCAR 14.3
CC
20
Cov
96%
list
6.0
match
6.0
uri_for_action
5.0
CRAP 4.2
« lib/Catalyst/Component.pm ↑ Summary lib/Catalyst/ActionRole/Scheme.pm »
1 package Catalyst::DispatchType::Path;
2  
3 153 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 153 extends 'Catalyst::DispatchType';
5  
6 153 ▶use Text::SimpleTable;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 153 ▶use Catalyst::Utils;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 153 ▶use URI;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 153 ▶use Encode 2.21 'decode_utf8';
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 has _paths => (
12 is => 'rw',
13 isa => 'HashRef',
14 required => 1,
15 317 ▶ default => sub { +{} },
Subroutine __ANON__: called CC=1 SCAR=0.0
16 153 );
17  
18 153 ▶no Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 =head1 NAME
21  
22 Catalyst::DispatchType::Path - Path DispatchType
23  
24 =head1 SYNOPSIS
25  
26 See L<Catalyst::DispatchType>.
27  
28 =head1 DESCRIPTION
29  
30 Dispatch type managing full path matching behaviour. For more information on
31 dispatch types, see:
32  
33 =over 4
34  
35 =item * L<Catalyst::Manual::Intro> for how they affect application authors
36  
37 =item * L<Catalyst::DispatchType> for implementation information.
38  
39 =back
40  
41 =head1 METHODS
42  
43 =head2 $self->list($c)
44  
45 Debug output for Path dispatch points
46  
47 =cut
48  
49 7 ▶sub list {
Subroutine list: called CC=6 SCAR=17.9
Pod documented
50 7 my ( $self, $c ) = @_;
51 7 my $avail_width = Catalyst::Utils::term_width() - 9;
52 7 ▶ my $col1_width = ($avail_width * .50) < 35 ? 35 : int($avail_width * .50);
Branch: 1 branch
BranchTrueFalseTotal
$avail_width * 0.5 < 35 ? : 0 7 7
53 7 my $col2_width = $avail_width - $col1_width;
54 7 my $paths = Text::SimpleTable->new(
55 [ $col1_width, 'Path' ], [ $col2_width, 'Private' ]
56 );
57 7 foreach my $path ( sort keys %{ $self->_paths } ) {
58 14 foreach my $action ( @{ $self->_paths->{$path} } ) {
59 14 my $args = $action->number_of_args;
60 14 ▶ my $parts = defined($args) ? '/*' x $args : '/...';
Branch: 1 branch
BranchTrueFalseTotal
defined $args ? : 1 13 14
61  
62 14 my $display_path = "/$path/$parts";
63 14 $display_path =~ s{/{1,}}{/}g;
64 0 $display_path =~ s/%([0-9A-Fa-f]{2})/chr(hex($1))/eg; # deconvert urlencoded for pretty view
65 14 $display_path = decode_utf8 $display_path; # URI does encoding
66 14 $paths->row( $display_path, "/$action" );
67 }
68 }
69 $c->log->debug( "Loaded Path actions:\n" . $paths->draw . "\n" )
70 7 ▶ if ( keys %{ $self->_paths } );
Branch: 1 branch
BranchTrueFalseTotal
if keys %{$self->_paths;} 5 2 7
71 }
72  
73 =head2 $self->match( $c, $path )
74  
75 For each action registered to this exact path, offers the action a chance to
76 match the path (in the order in which they were registered). Succeeds on the
77 first action that matches, if any; if not, returns 0.
78  
79 =cut
80  
81 1381 ▶sub match {
Subroutine match: called CC=6 SCAR=17.9
Pod documented
82 1381 my ( $self, $c, $path ) = @_;
83  
84 1381 ▶ $path = '/' if !defined $path || !length $path;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $path and length $path 108 1273 1381
Condition66%2/3
defined $path and length $path
!ll&&!rl&&r
01081273
MC/DC50%1/2
defined $path and length $path
defined $path length $path
Truth table66%2/3
A and B
A= defined $pathB= length $path
ABresult
0X0
100
111
85  
86 1381 ▶ my @actions = @{ $self->_paths->{$path} || [] };
Condition100%2/2
$self->_paths->{$path} || []
l!l
617764
MC/DC100%1/1
$self->_paths->{$path} || []
$self->_paths->{$path}
Truth table100%2/2
A
A= $self->_paths->{$path}
Aresult
11
00
87  
88 1381 foreach my $action ( @actions ) {
89 631 ▶ next unless $action->match($c);
Branch: 1 branch
BranchTrueFalseTotal
unless $action->match($c) 40 591 631
90 591 $c->req->action($path);
91 591 $c->req->match($path);
92 591 $c->action($action);
93 591 $c->namespace( $action->namespace );
94 591 return 1;
95 }
96  
97 790 return 0;
98 }
99  
100 =head2 $self->register( $c, $action )
101  
102 Calls register_path for every Path attribute for the given $action.
103  
104 =cut
105  
106 130468 ▶sub register {
Subroutine register: called CC=4 SCAR=13.9
Pod documented
107 130468 my ( $self, $c, $action ) = @_;
108  
109 130468 ▶ my @register = @{ $action->attributes->{Path} || [] };
Condition100%2/2
$action->attributes->{'Path'} || []
l!l
28855101613
MC/DC100%1/1
$action->attributes->{'Path'} || []
$action->attributes->{'Path'}
Truth table100%2/2
A
A= $action->attributes->{'Path'}
Aresult
11
00
110  
111 130468 $self->register_path( $c, $_, $action ) for @register;
112  
113 130468 ▶ return 1 if @register;
Branch: 1 branch
BranchTrueFalseTotal
if @register 28855 101613 130468
114 101613 return 0;
115 }
116  
117 =head2 $self->register_path($c, $path, $action)
118  
119 Registers an action at a given path.
120  
121 =cut
122  
123 29320 ▶sub register_path {
Subroutine register_path: called CC=3 SCAR=11.0
Pod documented
124 29320 my ( $self, $c, $path, $action ) = @_;
125 29320 $path =~ s!^/!!;
126 29320 ▶ $path = '/' unless length $path;
Branch: 1 branch
BranchTrueFalseTotal
unless length $path 19 29301 29320
127 29320 $path = URI->new($path)->canonical;
128 29320 $path =~ s{(?<=[^/])/+\z}{};
129  
130 $self->_paths->{$path} = [
131 29320 ▶ sort { $a->compare($b) } ($action, @{ $self->_paths->{$path} || [] })
Condition100%2/2
$self->_paths->{$path} || []
l!l
93528385
MC/DC100%1/1
$self->_paths->{$path} || []
$self->_paths->{$path}
Truth table100%2/2
A
A= $self->_paths->{$path}
Aresult
11
00
132 ];
133  
134 29320 return 1;
135 }
136  
137 =head2 $self->uri_for_action($action, $captures)
138  
139 get a URI part for an action; always returns undef is $captures is set
140 since Path actions don't have captures
141  
142 =cut
143  
144 129 ▶sub uri_for_action {
Subroutine uri_for_action: called CC=5 SCAR=16.1
Pod documented
145 129 my ( $self, $action, $captures ) = @_;
146  
147 129 ▶ return undef if @$captures;
Branch: 1 branch
BranchTrueFalseTotal
if @$captures 95 34 129
148  
149 34 ▶ if (my $paths = $action->attributes->{Path}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $paths = $action->attributes->{'Path'}) { } 22 12 34
150 22 my $path = $paths->[0];
151 22 ▶ $path = '/' unless length($path);
Branch: 1 branch
BranchTrueFalseTotal
unless length $path 0 22 22
152 22 ▶ $path = "/${path}" unless ($path =~ m/^\//);
Branch: 1 branch
BranchTrueFalseTotal
unless $path =~ m[^/] 12 10 22
153 22 $path = URI->new($path)->canonical;
154 22 return $path;
155 } else {
156 12 return undef;
157 }
158 }
159  
160 =head1 AUTHORS
161  
162 Catalyst Contributors, see Catalyst.pm
163  
164 =head1 COPYRIGHT
165  
166 This library is free software. You can redistribute it and/or modify it under
167 the same terms as Perl itself.
168  
169 =cut
170  
171 153 __PACKAGE__->meta->make_immutable;
172  
173 153 1;
« lib/Catalyst/Component.pm ↑ Summary lib/Catalyst/ActionRole/Scheme.pm »