Commit | Line | Data |
6b239949 |
1 | package Catalyst::DispatchType::Path; |
2 | |
3c0186f2 |
3 | use Moose; |
3c0186f2 |
4 | extends 'Catalyst::DispatchType'; |
5 | |
e8b9f2a9 |
6 | use Text::SimpleTable; |
39fc2ce1 |
7 | use Catalyst::Utils; |
e8b9f2a9 |
8 | use URI; |
0ca510f0 |
9 | use Encode 2.21 'decode_utf8'; |
3c0186f2 |
10 | |
36dbb986 |
11 | has _paths => ( |
12 | is => 'rw', |
13 | isa => 'HashRef', |
14 | required => 1, |
15 | default => sub { +{} }, |
16 | ); |
17 | |
0fc2d522 |
18 | no Moose; |
19 | |
2633d7dc |
20 | =head1 NAME |
21 | |
22 | Catalyst::DispatchType::Path - Path DispatchType |
23 | |
24 | =head1 SYNOPSIS |
25 | |
26dc649a |
26 | See L<Catalyst::DispatchType>. |
2633d7dc |
27 | |
28 | =head1 DESCRIPTION |
29 | |
26dc649a |
30 | Dispatch type managing full path matching behaviour. For more information on |
31 | dispatch types, see: |
32 | |
33 | =over 4 |
34 | |
b9b89145 |
35 | =item * L<Catalyst::Manual::Intro> for how they affect application authors |
26dc649a |
36 | |
37 | =item * L<Catalyst::DispatchType> for implementation information. |
38 | |
39 | =back |
40 | |
2633d7dc |
41 | =head1 METHODS |
42 | |
b5ecfcf0 |
43 | =head2 $self->list($c) |
a9cbd748 |
44 | |
4ab87e27 |
45 | Debug output for Path dispatch points |
46 | |
a9cbd748 |
47 | =cut |
48 | |
49 | sub list { |
50 | my ( $self, $c ) = @_; |
48d435ba |
51 | my $avail_width = Catalyst::Utils::term_width() - 9; |
52 | my $col1_width = ($avail_width * .50) < 35 ? 35 : int($avail_width * .50); |
53 | my $col2_width = $avail_width - $col1_width; |
b0ad47c1 |
54 | my $paths = Text::SimpleTable->new( |
48d435ba |
55 | [ $col1_width, 'Path' ], [ $col2_width, 'Private' ] |
39fc2ce1 |
56 | ); |
36dbb986 |
57 | foreach my $path ( sort keys %{ $self->_paths } ) { |
36dbb986 |
58 | foreach my $action ( @{ $self->_paths->{$path} } ) { |
6d62355b |
59 | my $args = $action->number_of_args; |
fb495632 |
60 | my $parts = defined($args) ? '/*' x $args : '/...'; |
91cb8b9a |
61 | |
fb495632 |
62 | my $display_path = "/$path/$parts"; |
63 | $display_path =~ s{/{1,}}{/}g; |
0ca510f0 |
64 | $display_path =~ s/%([0-9A-Fa-f]{2})/chr(hex($1))/eg; # deconvert urlencoded for pretty view |
65 | $display_path = decode_utf8 $display_path; # URI does encoding |
e8b9f2a9 |
66 | $paths->row( $display_path, "/$action" ); |
0bd5f8a2 |
67 | } |
a9cbd748 |
68 | } |
e8b9f2a9 |
69 | $c->log->debug( "Loaded Path actions:\n" . $paths->draw . "\n" ) |
36dbb986 |
70 | if ( keys %{ $self->_paths } ); |
a9cbd748 |
71 | } |
72 | |
b5ecfcf0 |
73 | =head2 $self->match( $c, $path ) |
2633d7dc |
74 | |
b2b90ec2 |
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. |
4ab87e27 |
78 | |
2633d7dc |
79 | =cut |
80 | |
81 | sub match { |
82 | my ( $self, $c, $path ) = @_; |
6b239949 |
83 | |
2f381252 |
84 | $path = '/' if !defined $path || !length $path; |
0bd5f8a2 |
85 | |
91955398 |
86 | my @actions = @{ $self->_paths->{$path} || [] }; |
87 | |
88 | foreach my $action ( @actions ) { |
0bd5f8a2 |
89 | next unless $action->match($c); |
6b239949 |
90 | $c->req->action($path); |
91 | $c->req->match($path); |
92 | $c->action($action); |
11bd4e3e |
93 | $c->namespace( $action->namespace ); |
6b239949 |
94 | return 1; |
95 | } |
96 | |
97 | return 0; |
98 | } |
99 | |
b5ecfcf0 |
100 | =head2 $self->register( $c, $action ) |
2633d7dc |
101 | |
b2b90ec2 |
102 | Calls register_path for every Path attribute for the given $action. |
4ab87e27 |
103 | |
2633d7dc |
104 | =cut |
105 | |
106 | sub register { |
6b239949 |
107 | my ( $self, $c, $action ) = @_; |
22f3a8dd |
108 | |
27708fc5 |
109 | my @register = @{ $action->attributes->{Path} || [] }; |
6b239949 |
110 | |
694d15f1 |
111 | $self->register_path( $c, $_, $action ) for @register; |
5b707014 |
112 | |
694d15f1 |
113 | return 1 if @register; |
114 | return 0; |
081def36 |
115 | } |
116 | |
b5ecfcf0 |
117 | =head2 $self->register_path($c, $path, $action) |
081def36 |
118 | |
b2b90ec2 |
119 | Registers an action at a given path. |
4ab87e27 |
120 | |
081def36 |
121 | =cut |
122 | |
123 | sub register_path { |
694d15f1 |
124 | my ( $self, $c, $path, $action ) = @_; |
081def36 |
125 | $path =~ s!^/!!; |
0ba80bce |
126 | $path = '/' unless length $path; |
595f3872 |
127 | $path = URI->new($path)->canonical; |
5299fff8 |
128 | $path =~ s{(?<=[^/])/+\z}{}; |
27708fc5 |
129 | |
91955398 |
130 | $self->_paths->{$path} = [ |
cb3d811c |
131 | sort { $a->compare($b) } ($action, @{ $self->_paths->{$path} || [] }) |
91955398 |
132 | ]; |
05b47f2e |
133 | |
0bd5f8a2 |
134 | return 1; |
6b239949 |
135 | } |
136 | |
ea0e58d9 |
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 | my ( $self, $action, $captures ) = @_; |
146 | |
147 | return undef if @$captures; |
148 | |
149 | if (my $paths = $action->attributes->{Path}) { |
150 | my $path = $paths->[0]; |
151 | $path = '/' unless length($path); |
152 | $path = "/${path}" unless ($path =~ m/^\//); |
153 | $path = URI->new($path)->canonical; |
154 | return $path; |
155 | } else { |
156 | return undef; |
157 | } |
158 | } |
159 | |
2f381252 |
160 | =head1 AUTHORS |
2633d7dc |
161 | |
2f381252 |
162 | Catalyst Contributors, see Catalyst.pm |
2633d7dc |
163 | |
164 | =head1 COPYRIGHT |
165 | |
536bee89 |
166 | This library is free software. You can redistribute it and/or modify it under |
2633d7dc |
167 | the same terms as Perl itself. |
168 | |
169 | =cut |
170 | |
e5ecd5bc |
171 | __PACKAGE__->meta->make_immutable; |
172 | |
6b239949 |
173 | 1; |