extended uri_for, added uri_for_action to dispatcher
[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
4ab87e27 41Check for paths that match the given path.
42
2633d7dc 43=cut
44
45sub match {
46 my ( $self, $c, $path ) = @_;
6b239949 47
61a9002d 48 $path ||= '/';
0bd5f8a2 49
50 foreach my $action ( @{ $self->{paths}->{$path} || [] } ) {
51 next unless $action->match($c);
6b239949 52 $c->req->action($path);
53 $c->req->match($path);
54 $c->action($action);
11bd4e3e 55 $c->namespace( $action->namespace );
6b239949 56 return 1;
57 }
58
59 return 0;
60}
61
b5ecfcf0 62=head2 $self->register( $c, $action )
2633d7dc 63
4ab87e27 64Call register_path for every path attribute in the given $action.
65
2633d7dc 66=cut
67
68sub register {
6b239949 69 my ( $self, $c, $action ) = @_;
22f3a8dd 70
27708fc5 71 my @register = @{ $action->attributes->{Path} || [] };
6b239949 72
694d15f1 73 $self->register_path( $c, $_, $action ) for @register;
5b707014 74
694d15f1 75 return 1 if @register;
76 return 0;
081def36 77}
78
b5ecfcf0 79=head2 $self->register_path($c, $path, $action)
081def36 80
4ab87e27 81register an action at a given path.
82
081def36 83=cut
84
85sub register_path {
694d15f1 86 my ( $self, $c, $path, $action ) = @_;
081def36 87 $path =~ s!^/!!;
0ba80bce 88 $path = '/' unless length $path;
595f3872 89 $path = URI->new($path)->canonical;
27708fc5 90
0bd5f8a2 91 unshift( @{ $self->{paths}{$path} ||= [] }, $action);
92
93 return 1;
6b239949 94}
95
ea0e58d9 96=head2 $self->uri_for_action($action, $captures)
97
98get a URI part for an action; always returns undef is $captures is set
99since Path actions don't have captures
100
101=cut
102
103sub uri_for_action {
104 my ( $self, $action, $captures ) = @_;
105
106 return undef if @$captures;
107
108 if (my $paths = $action->attributes->{Path}) {
109 my $path = $paths->[0];
110 $path = '/' unless length($path);
111 $path = "/${path}" unless ($path =~ m/^\//);
112 $path = URI->new($path)->canonical;
113 return $path;
114 } else {
115 return undef;
116 }
117}
118
2633d7dc 119=head1 AUTHOR
120
121Matt S Trout
122Sebastian Riedel, C<sri@cpan.org>
123
124=head1 COPYRIGHT
125
126This program is free software, you can redistribute it and/or modify it under
127the same terms as Perl itself.
128
129=cut
130
6b239949 1311;