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