c13f73c0eadbfbab058165c1e144af0256e3a1bb
[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         foreach my $action ( @{ $self->{paths}->{$path} } ) {
31             $path = "/$path" unless $path eq '/';
32             $paths->row( "$path", "/$action" );
33         }
34     }
35     $c->log->debug( "Loaded Path actions:\n" . $paths->draw )
36       if ( keys %{ $self->{paths} } );
37 }
38
39 =head2 $self->match( $c, $path )
40
41 Check for paths that match the given path.
42
43 =cut
44
45 sub match {
46     my ( $self, $c, $path ) = @_;
47
48     $path ||= '/';
49
50     foreach my $action ( @{ $self->{paths}->{$path} || [] } ) {
51         next unless $action->match($c);
52         $c->req->action($path);
53         $c->req->match($path);
54         $c->action($action);
55         $c->namespace( $action->namespace );
56         return 1;
57     }
58
59     return 0;
60 }
61
62 =head2 $self->register( $c, $action )
63
64 Call register_path for every path attribute in the given $action.
65
66 =cut
67
68 sub register {
69     my ( $self, $c, $action ) = @_;
70
71     my @register = @{ $action->attributes->{Path} || [] };
72
73     $self->register_path( $c, $_, $action ) for @register;
74
75     return 1 if @register;
76     return 0;
77 }
78
79 =head2 $self->register_path($c, $path, $action)
80
81 register an action at a given path.
82
83 =cut
84
85 sub register_path {
86     my ( $self, $c, $path, $action ) = @_;
87     $path =~ s!^/!!;
88     $path = '/' unless length $path;
89     $path = URI->new($path)->canonical;
90
91     unshift( @{ $self->{paths}{$path} ||= [] }, $action);
92
93     return 1;
94 }
95
96 =head2 $self->uri_for_action($action, $captures)
97
98 get a URI part for an action; always returns undef is $captures is set
99 since Path actions don't have captures
100
101 =cut
102
103 sub uri_for_action {
104     my ( $self, $action, $captures ) = @_;
105
106     return undef if @$captures;
107
108     if (my $paths = $action->attributes->{Path}) {
109         my $path = $paths->[0];
110         $path = '/' unless length($path);
111         $path = "/${path}" unless ($path =~ m/^\//);
112         $path = URI->new($path)->canonical;
113         return $path;
114     } else {
115         return undef;
116     }
117 }
118
119 =head1 AUTHOR
120
121 Matt S Trout
122 Sebastian Riedel, C<sri@cpan.org>
123
124 =head1 COPYRIGHT
125
126 This program is free software, you can redistribute it and/or modify it under
127 the same terms as Perl itself.
128
129 =cut
130
131 1;