- Fixed http://dev.catalyst.perl.org/ticket/62
[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
22=cut
23
24sub list {
25 my ( $self, $c ) = @_;
8c113188 26 my $paths = Text::SimpleTable->new( [ 36, 'Path' ], [ 37, 'Private' ] );
a9cbd748 27 for my $path ( sort keys %{ $self->{paths} } ) {
28 my $action = $self->{paths}->{$path};
61a9002d 29 $path = "/$path" unless $path eq '/';
30 $paths->row( "$path", "/$action" );
a9cbd748 31 }
32 $c->log->debug( "Loaded Path actions:\n" . $paths->draw )
8c113188 33 if ( keys %{ $self->{paths} } );
a9cbd748 34}
35
b5ecfcf0 36=head2 $self->match( $c, $path )
2633d7dc 37
38=cut
39
40sub match {
41 my ( $self, $c, $path ) = @_;
6b239949 42
61a9002d 43 $path ||= '/';
6b239949 44 if ( my $action = $self->{paths}->{$path} ) {
45 $c->req->action($path);
46 $c->req->match($path);
47 $c->action($action);
11bd4e3e 48 $c->namespace( $action->namespace );
6b239949 49 return 1;
50 }
51
52 return 0;
53}
54
b5ecfcf0 55=head2 $self->register( $c, $action )
2633d7dc 56
57=cut
58
59sub register {
6b239949 60 my ( $self, $c, $action ) = @_;
22f3a8dd 61
6b239949 62 my $attrs = $action->attributes;
63 my @register;
22f3a8dd 64
2633d7dc 65 foreach my $r ( @{ $attrs->{Path} || [] } ) {
694d15f1 66 unless ($r) {
621a5d5e 67 $r = $action->namespace;
0ba80bce 68 $r = '/' unless length $r;
621a5d5e 69 }
70 elsif ( $r !~ m!^/! ) { # It's a relative path
11bd4e3e 71 $r = $action->namespace . "/$r";
6b239949 72 }
2633d7dc 73 push( @register, $r );
6b239949 74 }
75
2633d7dc 76 if ( $attrs->{Global} || $attrs->{Absolute} ) {
77 push( @register, $action->name ); # Register sub name against root
6b239949 78 }
79
2633d7dc 80 if ( $attrs->{Local} || $attrs->{Relative} ) {
11bd4e3e 81 push( @register, join( '/', $action->namespace, $action->name ) );
2633d7dc 82
83 # Register sub name as a relative path
6b239949 84 }
85
694d15f1 86 $self->register_path( $c, $_, $action ) for @register;
87 return 1 if @register;
88 return 0;
081def36 89}
90
b5ecfcf0 91=head2 $self->register_path($c, $path, $action)
081def36 92
93=cut
94
95sub register_path {
694d15f1 96 my ( $self, $c, $path, $action ) = @_;
081def36 97 $path =~ s!^/!!;
0ba80bce 98 $path = '/' unless length $path;
595f3872 99 $path = URI->new($path)->canonical;
081def36 100 $self->{paths}{$path} = $action;
6b239949 101}
102
2633d7dc 103=head1 AUTHOR
104
105Matt S Trout
106Sebastian Riedel, C<sri@cpan.org>
107
108=head1 COPYRIGHT
109
110This program is free software, you can redistribute it and/or modify it under
111the same terms as Perl itself.
112
113=cut
114
6b239949 1151;