Added msts message
[catagits/Catalyst-Runtime.git] / lib / Catalyst / DispatchType / Path.pm
CommitLineData
6b239949 1package Catalyst::DispatchType::Path;
2
3use strict;
4use base qw/Catalyst::DispatchType/;
8c113188 5use Text::SimpleTable;
595f3872 6use URI;
6b239949 7
2633d7dc 8=head1 NAME
9
10Catalyst::DispatchType::Path - Path DispatchType
11
12=head1 SYNOPSIS
13
14See L<Catalyst>.
15
16=head1 DESCRIPTION
17
18=head1 METHODS
19
b5ecfcf0 20=head2 $self->list($c)
a9cbd748 21
4ab87e27 22Debug output for Path dispatch points
23
a9cbd748 24=cut
25
26sub 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} } ) {
30 foreach my $action ( @{ $self->{paths}->{$path} } ) {
31 $path = "/$path" unless $path eq '/';
32 $paths->row( "$path", "/$action" );
33 }
a9cbd748 34 }
35 $c->log->debug( "Loaded Path actions:\n" . $paths->draw )
8c113188 36 if ( keys %{ $self->{paths} } );
a9cbd748 37}
38
b5ecfcf0 39=head2 $self->match( $c, $path )
2633d7dc 40
b2b90ec2 41For each action registered to this exact path, offers the action a chance to
42match the path (in the order in which they were registered). Succeeds on the
43first action that matches, if any; if not, returns 0.
4ab87e27 44
2633d7dc 45=cut
46
47sub 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 66Calls register_path for every Path attribute for the given $action.
4ab87e27 67
2633d7dc 68=cut
69
70sub 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 83Registers an action at a given path.
4ab87e27 84
081def36 85=cut
86
87sub 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
100get a URI part for an action; always returns undef is $captures is set
101since Path actions don't have captures
102
103=cut
104
105sub 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
123Matt S Trout
124Sebastian Riedel, C<sri@cpan.org>
125
126=head1 COPYRIGHT
127
128This program is free software, you can redistribute it and/or modify it under
129the same terms as Perl itself.
130
131=cut
132
6b239949 1331;