File Coverage

blib/lib/Catalyst/DispatchType/Path.pm
Criterion Covered Total %
statement 72 73 98.6
branch 26 28 92.8
condition 2 3 66.6
subroutine 11 11 100.0
pod 5 5 100.0
total 116 120 96.6


line stmt bran cond sub pod time code
1             package Catalyst::DispatchType::Path;
2              
3 154     154   138335 use Moose;
  154         985  
  154         1592  
4             extends 'Catalyst::DispatchType';
5              
6 154     154   1164685 use Text::SimpleTable;
  154         449  
  154         5154  
7 154     154   1159 use Catalyst::Utils;
  154         408  
  154         4454  
8 154     154   1118 use URI;
  154         413  
  154         6524  
9 154     154   1137 use Encode 2.21 'decode_utf8';
  154         5334  
  154         20240  
10              
11             has _paths => (
12             is => 'rw',
13             isa => 'HashRef',
14             required => 1,
15             default => sub { +{} },
16             );
17              
18 154     154   1406 no Moose;
  154         447  
  154         1377  
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             sub list {
50 7     7 1 33 my ( $self, $c ) = @_;
51 7         41 my $avail_width = Catalyst::Utils::term_width() - 9;
52 7 50       70 my $col1_width = ($avail_width * .50) < 35 ? 35 : int($avail_width * .50);
53 7         26 my $col2_width = $avail_width - $col1_width;
54 7         58 my $paths = Text::SimpleTable->new(
55             [ $col1_width, 'Path' ], [ $col2_width, 'Private' ]
56             );
57 7         662 foreach my $path ( sort keys %{ $self->_paths } ) {
  7         280  
58 14         52572 foreach my $action ( @{ $self->_paths->{$path} } ) {
  14         485  
59 14         416 my $args = $action->number_of_args;
60 14 100       77 my $parts = defined($args) ? '/*' x $args : '/...';
61              
62 14         57 my $display_path = "/$path/$parts";
63 14         112 $display_path =~ s{/{1,}}{/}g;
64 14         43 $display_path =~ s/%([0-9A-Fa-f]{2})/chr(hex($1))/eg; # deconvert urlencoded for pretty view
  0         0  
65 14         227 $display_path = decode_utf8 $display_path; # URI does encoding
66 14         168 $paths->row( $display_path, "/$action" );
67             }
68             }
69             $c->log->debug( "Loaded Path actions:\n" . $paths->draw . "\n" )
70 7 100       34268 if ( keys %{ $self->_paths } );
  7         282  
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             sub match {
82 1377     1377 1 3818 my ( $self, $c, $path ) = @_;
83              
84 1377 100 66     7241 $path = '/' if !defined $path || !length $path;
85              
86 1377 100       2497 my @actions = @{ $self->_paths->{$path} || [] };
  1377         44062  
87              
88 1377         3987 foreach my $action ( @actions ) {
89 627 100       3128 next unless $action->match($c);
90 587         2756 $c->req->action($path);
91 587         2322 $c->req->match($path);
92 587         16043 $c->action($action);
93 587         16544 $c->namespace( $action->namespace );
94 587         3267 return 1;
95             }
96              
97 790         3210 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             sub register {
107 148339     148339 1 295692 my ( $self, $c, $action ) = @_;
108              
109 148339 100       212363 my @register = @{ $action->attributes->{Path} || [] };
  148339         3956524  
110              
111 148339         376630 $self->register_path( $c, $_, $action ) for @register;
112              
113 148339 100       377494 return 1 if @register;
114 116106         341935 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             sub register_path {
124 32752     32752 1 74290 my ( $self, $c, $path, $action ) = @_;
125 32752         72065 $path =~ s!^/!!;
126 32752 100       74454 $path = '/' unless length $path;
127 32752         110027 $path = URI->new($path)->canonical;
128 32752         1983448 $path =~ s{(?<=[^/])/+\z}{};
129              
130             $self->_paths->{$path} = [
131 32752 100       223969 sort { $a->compare($b) } ($action, @{ $self->_paths->{$path} || [] })
  3496         24866  
  32752         1053093  
132             ];
133              
134 32752         247569 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             sub uri_for_action {
145 129     129 1 334 my ( $self, $action, $captures ) = @_;
146              
147 129 100       355 return undef if @$captures;
148              
149 34 100       972 if (my $paths = $action->attributes->{Path}) {
150 22         78 my $path = $paths->[0];
151 22 50       75 $path = '/' unless length($path);
152 22 100       142 $path = "/${path}" unless ($path =~ m/^\//);
153 22         176 $path = URI->new($path)->canonical;
154 22         2141 return $path;
155             } else {
156 12         39 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             __PACKAGE__->meta->make_immutable;
172              
173             1;