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