Commit | Line | Data |
6b239949 |
1 | package Catalyst::DispatchType::Path; |
2 | |
0fc2d522 |
3 | use Class::C3; |
3c0186f2 |
4 | use Moose; |
3c0186f2 |
5 | extends 'Catalyst::DispatchType'; |
6 | |
e8b9f2a9 |
7 | use Text::SimpleTable; |
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 ) = @_; |
e8b9f2a9 |
39 | my $paths = Text::SimpleTable->new( [ 35, 'Path' ], [ 36, 'Private' ] ); |
36dbb986 |
40 | foreach my $path ( sort keys %{ $self->_paths } ) { |
c5b74a51 |
41 | my $display_path = $path eq '/' ? $path : "/$path"; |
36dbb986 |
42 | foreach my $action ( @{ $self->_paths->{$path} } ) { |
e8b9f2a9 |
43 | $paths->row( $display_path, "/$action" ); |
0bd5f8a2 |
44 | } |
a9cbd748 |
45 | } |
e8b9f2a9 |
46 | $c->log->debug( "Loaded Path actions:\n" . $paths->draw . "\n" ) |
36dbb986 |
47 | if ( keys %{ $self->_paths } ); |
a9cbd748 |
48 | } |
49 | |
b5ecfcf0 |
50 | =head2 $self->match( $c, $path ) |
2633d7dc |
51 | |
b2b90ec2 |
52 | For each action registered to this exact path, offers the action a chance to |
53 | match the path (in the order in which they were registered). Succeeds on the |
54 | first action that matches, if any; if not, returns 0. |
4ab87e27 |
55 | |
2633d7dc |
56 | =cut |
57 | |
58 | sub match { |
59 | my ( $self, $c, $path ) = @_; |
6b239949 |
60 | |
61a9002d |
61 | $path ||= '/'; |
0bd5f8a2 |
62 | |
36dbb986 |
63 | foreach my $action ( @{ $self->_paths->{$path} || [] } ) { |
0bd5f8a2 |
64 | next unless $action->match($c); |
6b239949 |
65 | $c->req->action($path); |
66 | $c->req->match($path); |
67 | $c->action($action); |
11bd4e3e |
68 | $c->namespace( $action->namespace ); |
6b239949 |
69 | return 1; |
70 | } |
71 | |
72 | return 0; |
73 | } |
74 | |
b5ecfcf0 |
75 | =head2 $self->register( $c, $action ) |
2633d7dc |
76 | |
b2b90ec2 |
77 | Calls register_path for every Path attribute for the given $action. |
4ab87e27 |
78 | |
2633d7dc |
79 | =cut |
80 | |
81 | sub register { |
6b239949 |
82 | my ( $self, $c, $action ) = @_; |
22f3a8dd |
83 | |
27708fc5 |
84 | my @register = @{ $action->attributes->{Path} || [] }; |
6b239949 |
85 | |
694d15f1 |
86 | $self->register_path( $c, $_, $action ) for @register; |
5b707014 |
87 | |
694d15f1 |
88 | return 1 if @register; |
89 | return 0; |
081def36 |
90 | } |
91 | |
b5ecfcf0 |
92 | =head2 $self->register_path($c, $path, $action) |
081def36 |
93 | |
b2b90ec2 |
94 | Registers an action at a given path. |
4ab87e27 |
95 | |
081def36 |
96 | =cut |
97 | |
98 | sub register_path { |
694d15f1 |
99 | my ( $self, $c, $path, $action ) = @_; |
081def36 |
100 | $path =~ s!^/!!; |
0ba80bce |
101 | $path = '/' unless length $path; |
595f3872 |
102 | $path = URI->new($path)->canonical; |
27708fc5 |
103 | |
36dbb986 |
104 | unshift( @{ $self->_paths->{$path} ||= [] }, $action); |
0bd5f8a2 |
105 | |
106 | return 1; |
6b239949 |
107 | } |
108 | |
ea0e58d9 |
109 | =head2 $self->uri_for_action($action, $captures) |
110 | |
111 | get a URI part for an action; always returns undef is $captures is set |
112 | since Path actions don't have captures |
113 | |
114 | =cut |
115 | |
116 | sub uri_for_action { |
117 | my ( $self, $action, $captures ) = @_; |
118 | |
119 | return undef if @$captures; |
120 | |
121 | if (my $paths = $action->attributes->{Path}) { |
122 | my $path = $paths->[0]; |
123 | $path = '/' unless length($path); |
124 | $path = "/${path}" unless ($path =~ m/^\//); |
125 | $path = URI->new($path)->canonical; |
126 | return $path; |
127 | } else { |
128 | return undef; |
129 | } |
130 | } |
131 | |
2633d7dc |
132 | =head1 AUTHOR |
133 | |
134 | Matt S Trout |
135 | Sebastian Riedel, C<sri@cpan.org> |
136 | |
137 | =head1 COPYRIGHT |
138 | |
139 | This program is free software, you can redistribute it and/or modify it under |
140 | the same terms as Perl itself. |
141 | |
142 | =cut |
143 | |
e5ecd5bc |
144 | __PACKAGE__->meta->make_immutable; |
145 | |
6b239949 |
146 | 1; |