Commit | Line | Data |
6b239949 |
1 | package Catalyst::DispatchType::Path; |
2 | |
3 | use strict; |
4 | use base qw/Catalyst::DispatchType/; |
8c113188 |
5 | use Text::SimpleTable; |
595f3872 |
6 | use URI; |
6b239949 |
7 | |
2633d7dc |
8 | =head1 NAME |
9 | |
10 | Catalyst::DispatchType::Path - Path DispatchType |
11 | |
12 | =head1 SYNOPSIS |
13 | |
14 | See L<Catalyst>. |
15 | |
16 | =head1 DESCRIPTION |
17 | |
18 | =head1 METHODS |
19 | |
b5ecfcf0 |
20 | =head2 $self->list($c) |
a9cbd748 |
21 | |
4ab87e27 |
22 | Debug output for Path dispatch points |
23 | |
a9cbd748 |
24 | =cut |
25 | |
26 | sub list { |
27 | my ( $self, $c ) = @_; |
34d28dfd |
28 | my $paths = Text::SimpleTable->new( [ 35, 'Path' ], [ 36, 'Private' ] ); |
0bd5f8a2 |
29 | foreach my $path ( sort keys %{ $self->{paths} } ) { |
c5b74a51 |
30 | my $display_path = $path eq '/' ? $path : "/$path"; |
0bd5f8a2 |
31 | foreach my $action ( @{ $self->{paths}->{$path} } ) { |
c5b74a51 |
32 | $paths->row( $display_path, "/$action" ); |
0bd5f8a2 |
33 | } |
a9cbd748 |
34 | } |
1cf0345b |
35 | $c->log->debug( "Loaded Path actions:\n" . $paths->draw . "\n" ) |
8c113188 |
36 | if ( keys %{ $self->{paths} } ); |
a9cbd748 |
37 | } |
38 | |
b5ecfcf0 |
39 | =head2 $self->match( $c, $path ) |
2633d7dc |
40 | |
b2b90ec2 |
41 | For each action registered to this exact path, offers the action a chance to |
42 | match the path (in the order in which they were registered). Succeeds on the |
43 | first action that matches, if any; if not, returns 0. |
4ab87e27 |
44 | |
2633d7dc |
45 | =cut |
46 | |
47 | sub match { |
48 | my ( $self, $c, $path ) = @_; |
6b239949 |
49 | |
61a9002d |
50 | $path ||= '/'; |
0bd5f8a2 |
51 | |
52 | foreach my $action ( @{ $self->{paths}->{$path} || [] } ) { |
53 | next unless $action->match($c); |
6b239949 |
54 | $c->req->action($path); |
55 | $c->req->match($path); |
56 | $c->action($action); |
11bd4e3e |
57 | $c->namespace( $action->namespace ); |
6b239949 |
58 | return 1; |
59 | } |
60 | |
61 | return 0; |
62 | } |
63 | |
b5ecfcf0 |
64 | =head2 $self->register( $c, $action ) |
2633d7dc |
65 | |
b2b90ec2 |
66 | Calls register_path for every Path attribute for the given $action. |
4ab87e27 |
67 | |
2633d7dc |
68 | =cut |
69 | |
70 | sub register { |
6b239949 |
71 | my ( $self, $c, $action ) = @_; |
22f3a8dd |
72 | |
27708fc5 |
73 | my @register = @{ $action->attributes->{Path} || [] }; |
6b239949 |
74 | |
694d15f1 |
75 | $self->register_path( $c, $_, $action ) for @register; |
5b707014 |
76 | |
694d15f1 |
77 | return 1 if @register; |
78 | return 0; |
081def36 |
79 | } |
80 | |
b5ecfcf0 |
81 | =head2 $self->register_path($c, $path, $action) |
081def36 |
82 | |
b2b90ec2 |
83 | Registers an action at a given path. |
4ab87e27 |
84 | |
081def36 |
85 | =cut |
86 | |
87 | sub register_path { |
694d15f1 |
88 | my ( $self, $c, $path, $action ) = @_; |
081def36 |
89 | $path =~ s!^/!!; |
0ba80bce |
90 | $path = '/' unless length $path; |
595f3872 |
91 | $path = URI->new($path)->canonical; |
27708fc5 |
92 | |
0bd5f8a2 |
93 | unshift( @{ $self->{paths}{$path} ||= [] }, $action); |
94 | |
95 | return 1; |
6b239949 |
96 | } |
97 | |
ea0e58d9 |
98 | =head2 $self->uri_for_action($action, $captures) |
99 | |
100 | get a URI part for an action; always returns undef is $captures is set |
101 | since Path actions don't have captures |
102 | |
103 | =cut |
104 | |
105 | sub uri_for_action { |
106 | my ( $self, $action, $captures ) = @_; |
107 | |
108 | return undef if @$captures; |
109 | |
110 | if (my $paths = $action->attributes->{Path}) { |
111 | my $path = $paths->[0]; |
112 | $path = '/' unless length($path); |
113 | $path = "/${path}" unless ($path =~ m/^\//); |
114 | $path = URI->new($path)->canonical; |
115 | return $path; |
116 | } else { |
117 | return undef; |
118 | } |
119 | } |
120 | |
2633d7dc |
121 | =head1 AUTHOR |
122 | |
123 | Matt S Trout |
124 | Sebastian Riedel, C<sri@cpan.org> |
125 | |
126 | =head1 COPYRIGHT |
127 | |
128 | This program is free software, you can redistribute it and/or modify it under |
129 | the same terms as Perl itself. |
130 | |
131 | =cut |
132 | |
6b239949 |
133 | 1; |