Refactored Regex actions
[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
27708fc5 63 my @register = @{ $action->attributes->{Path} || [] };
6b239949 64
694d15f1 65 $self->register_path( $c, $_, $action ) for @register;
5b707014 66
694d15f1 67 return 1 if @register;
68 return 0;
081def36 69}
70
b5ecfcf0 71=head2 $self->register_path($c, $path, $action)
081def36 72
73=cut
74
75sub register_path {
694d15f1 76 my ( $self, $c, $path, $action ) = @_;
081def36 77 $path =~ s!^/!!;
0ba80bce 78 $path = '/' unless length $path;
595f3872 79 $path = URI->new($path)->canonical;
27708fc5 80
081def36 81 $self->{paths}{$path} = $action;
6b239949 82}
83
2633d7dc 84=head1 AUTHOR
85
86Matt S Trout
87Sebastian Riedel, C<sri@cpan.org>
88
89=head1 COPYRIGHT
90
91This program is free software, you can redistribute it and/or modify it under
92the same terms as Perl itself.
93
94=cut
95
6b239949 961;