31f37b3e9790d5367aeb087c847c529d4edc8397
[catagits/Catalyst-Runtime.git] / lib / Catalyst / DispatchType / Path.pm
1 package Catalyst::DispatchType::Path;
2
3 use strict;
4 use base qw/Catalyst::DispatchType/;
5 use Text::SimpleTable;
6
7 =head1 NAME
8
9 Catalyst::DispatchType::Path - Path DispatchType
10
11 =head1 SYNOPSIS
12
13 See L<Catalyst>.
14
15 =head1 DESCRIPTION
16
17 =head1 METHODS
18
19 =over 4
20
21 =item $self->list($c)
22
23 =cut
24
25 sub list {
26     my ( $self, $c ) = @_;
27     my $paths = Text::SimpleTable->new( [ 36, 'Path' ], [ 37, 'Private' ] );
28     for my $path ( sort keys %{ $self->{paths} } ) {
29         my $action = $self->{paths}->{$path};
30         $paths->row( "/$path", "/$action" );
31     }
32     $c->log->debug( "Loaded Path actions:\n" . $paths->draw )
33       if ( keys %{ $self->{paths} } );
34 }
35
36 =item $self->match( $c, $path )
37
38 =cut
39
40 sub match {
41     my ( $self, $c, $path ) = @_;
42
43     if ( my $action = $self->{paths}->{$path} ) {
44         $c->req->action($path);
45         $c->req->match($path);
46         $c->action($action);
47         $c->namespace( $action->namespace );
48         return 1;
49     }
50
51     return 0;
52 }
53
54 =item $self->register( $c, $action )
55
56 =cut
57
58 sub register {
59     my ( $self, $c, $action ) = @_;
60
61     my $attrs = $action->attributes;
62     my @register;
63
64     foreach my $r ( @{ $attrs->{Path} || [] } ) {
65         unless ( $r =~ m!^/! ) {    # It's a relative path
66             $r = $action->namespace . "/$r";
67         }
68         push( @register, $r );
69     }
70
71     if ( $attrs->{Global} || $attrs->{Absolute} ) {
72         push( @register, $action->name );    # Register sub name against root
73     }
74
75     if ( $attrs->{Local} || $attrs->{Relative} ) {
76         push( @register, join( '/', $action->namespace, $action->name ) );
77
78         # Register sub name as a relative path
79     }
80
81     foreach my $r (@register) {
82         $r =~ s!^/!!;
83         $self->{paths}{$r} = $action;
84     }
85 }
86
87 =back
88
89 =head1 AUTHOR
90
91 Matt S Trout
92 Sebastian Riedel, C<sri@cpan.org>
93
94 =head1 COPYRIGHT
95
96 This program is free software, you can redistribute it and/or modify it under
97 the same terms as Perl itself.
98
99 =cut
100
101 1;