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