Fixes to model/view/controller methods from Brandon Black
[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} ) {
4082e678 45 return 0 unless $action->match($c);
6b239949 46 $c->req->action($path);
47 $c->req->match($path);
48 $c->action($action);
11bd4e3e 49 $c->namespace( $action->namespace );
6b239949 50 return 1;
51 }
52
53 return 0;
54}
55
b5ecfcf0 56=head2 $self->register( $c, $action )
2633d7dc 57
58=cut
59
60sub register {
6b239949 61 my ( $self, $c, $action ) = @_;
22f3a8dd 62
6b239949 63 my $attrs = $action->attributes;
64 my @register;
22f3a8dd 65
2633d7dc 66 foreach my $r ( @{ $attrs->{Path} || [] } ) {
694d15f1 67 unless ($r) {
621a5d5e 68 $r = $action->namespace;
0ba80bce 69 $r = '/' unless length $r;
621a5d5e 70 }
71 elsif ( $r !~ m!^/! ) { # It's a relative path
11bd4e3e 72 $r = $action->namespace . "/$r";
6b239949 73 }
2633d7dc 74 push( @register, $r );
6b239949 75 }
76
2633d7dc 77 if ( $attrs->{Global} || $attrs->{Absolute} ) {
78 push( @register, $action->name ); # Register sub name against root
6b239949 79 }
80
2633d7dc 81 if ( $attrs->{Local} || $attrs->{Relative} ) {
11bd4e3e 82 push( @register, join( '/', $action->namespace, $action->name ) );
2633d7dc 83
84 # Register sub name as a relative path
6b239949 85 }
86
694d15f1 87 $self->register_path( $c, $_, $action ) for @register;
88 return 1 if @register;
89 return 0;
081def36 90}
91
b5ecfcf0 92=head2 $self->register_path($c, $path, $action)
081def36 93
94=cut
95
96sub register_path {
694d15f1 97 my ( $self, $c, $path, $action ) = @_;
081def36 98 $path =~ s!^/!!;
0ba80bce 99 $path = '/' unless length $path;
595f3872 100 $path = URI->new($path)->canonical;
081def36 101 $self->{paths}{$path} = $action;
6b239949 102}
103
2633d7dc 104=head1 AUTHOR
105
106Matt S Trout
107Sebastian Riedel, C<sri@cpan.org>
108
109=head1 COPYRIGHT
110
111This program is free software, you can redistribute it and/or modify it under
112the same terms as Perl itself.
113
114=cut
115
6b239949 1161;