authors cleanup
[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 Debug output for Path dispatch points
23
24 =cut
25
26 sub list {
27     my ( $self, $c ) = @_;
28     my $paths = Text::SimpleTable->new( [ 35, 'Path' ], [ 36, 'Private' ] );
29     foreach my $path ( sort keys %{ $self->{paths} } ) {
30         my $display_path = $path eq '/' ? $path : "/$path";
31         foreach my $action ( @{ $self->{paths}->{$path} } ) {
32             $paths->row( $display_path, "/$action" );
33         }
34     }
35     $c->log->debug( "Loaded Path actions:\n" . $paths->draw . "\n" )
36       if ( keys %{ $self->{paths} } );
37 }
38
39 =head2 $self->match( $c, $path )
40
41 For each action registered to this exact path, offers the action a chance to
42 match the path (in the order in which they were registered). Succeeds on the
43 first action that matches, if any; if not, returns 0.
44
45 =cut
46
47 sub match {
48     my ( $self, $c, $path ) = @_;
49
50     $path = '/' if !defined $path || !length $path;
51
52     foreach my $action ( @{ $self->{paths}->{$path} || [] } ) {
53         next unless $action->match($c);
54         $c->req->action($path);
55         $c->req->match($path);
56         $c->action($action);
57         $c->namespace( $action->namespace );
58         return 1;
59     }
60
61     return 0;
62 }
63
64 =head2 $self->register( $c, $action )
65
66 Calls register_path for every Path attribute for the given $action.
67
68 =cut
69
70 sub register {
71     my ( $self, $c, $action ) = @_;
72
73     my @register = @{ $action->attributes->{Path} || [] };
74
75     $self->register_path( $c, $_, $action ) for @register;
76
77     return 1 if @register;
78     return 0;
79 }
80
81 =head2 $self->register_path($c, $path, $action)
82
83 Registers an action at a given path.
84
85 =cut
86
87 sub register_path {
88     my ( $self, $c, $path, $action ) = @_;
89     $path =~ s!^/!!;
90     $path = '/' unless length $path;
91     $path = URI->new($path)->canonical;
92
93     unshift( @{ $self->{paths}{$path} ||= [] }, $action);
94
95     return 1;
96 }
97
98 =head2 $self->uri_for_action($action, $captures)
99
100 get a URI part for an action; always returns undef is $captures is set
101 since Path actions don't have captures
102
103 =cut
104
105 sub uri_for_action {
106     my ( $self, $action, $captures ) = @_;
107
108     return undef if @$captures;
109
110     if (my $paths = $action->attributes->{Path}) {
111         my $path = $paths->[0];
112         $path = '/' unless length($path);
113         $path = "/${path}" unless ($path =~ m/^\//);
114         $path = URI->new($path)->canonical;
115         return $path;
116     } else {
117         return undef;
118     }
119 }
120
121 =head1 AUTHORS
122
123 Catalyst Contributors, see Catalyst.pm
124
125 =head1 COPYRIGHT
126
127 This program is free software, you can redistribute it and/or modify it under
128 the same terms as Perl itself.
129
130 =cut
131
132 1;