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