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