fix stackoverflow Path bug
[catagits/Catalyst-Runtime.git] / lib / Catalyst / DispatchType / Path.pm
1 package Catalyst::DispatchType::Path;
2
3 use Moose;
4 extends 'Catalyst::DispatchType';
5
6 use Text::SimpleTable;
7 use Catalyst::Utils;
8 use URI;
9
10 has _paths => (
11                is => 'rw',
12                isa => 'HashRef',
13                required => 1,
14                default => sub { +{} },
15               );
16
17 no Moose;
18
19 =head1 NAME
20
21 Catalyst::DispatchType::Path - Path DispatchType
22
23 =head1 SYNOPSIS
24
25 See L<Catalyst::DispatchType>.
26
27 =head1 DESCRIPTION
28
29 Dispatch type managing full path matching behaviour.  For more information on
30 dispatch types, see:
31
32 =over 4
33
34 =item * L<Catalyst::Manual::Intro> for how they affect application authors
35
36 =item * L<Catalyst::DispatchType> for implementation information.
37
38 =back
39
40 =head1 METHODS
41
42 =head2 $self->list($c)
43
44 Debug output for Path dispatch points
45
46 =cut
47
48 sub list {
49     my ( $self, $c ) = @_;
50     my $column_width = Catalyst::Utils::term_width() - 35 - 9;
51     my $paths = Text::SimpleTable->new( 
52        [ 35, 'Path' ], [ $column_width, 'Private' ]
53     );
54     foreach my $path ( sort keys %{ $self->_paths } ) {
55         my $display_path = $path eq '/' ? $path : "/$path";
56         foreach my $action ( @{ $self->_paths->{$path} } ) {
57             $paths->row( $display_path, "/$action" );
58         }
59     }
60     $c->log->debug( "Loaded Path actions:\n" . $paths->draw . "\n" )
61       if ( keys %{ $self->_paths } );
62 }
63
64 =head2 $self->match( $c, $path )
65
66 For each action registered to this exact path, offers the action a chance to
67 match the path (in the order in which they were registered). Succeeds on the
68 first action that matches, if any; if not, returns 0.
69
70 =cut
71
72 sub match {
73     my ( $self, $c, $path ) = @_;
74
75     $path = '/' if !defined $path || !length $path;
76
77     # sort from least args to most
78     my @actions = sort { ($b->attributes->{Args}||0) <=>
79                       ($a->attributes->{Args}||0) }
80             @{ $self->_paths->{$path} || [] };
81
82     foreach my $action ( @actions ) {
83         next unless $action->match($c);
84         $c->req->action($path);
85         $c->req->match($path);
86         $c->action($action);
87         $c->namespace( $action->namespace );
88         return 1;
89     }
90
91     return 0;
92 }
93
94 =head2 $self->register( $c, $action )
95
96 Calls register_path for every Path attribute for the given $action.
97
98 =cut
99
100 sub register {
101     my ( $self, $c, $action ) = @_;
102
103     my @register = @{ $action->attributes->{Path} || [] };
104
105     $self->register_path( $c, $_, $action ) for @register;
106
107     return 1 if @register;
108     return 0;
109 }
110
111 =head2 $self->register_path($c, $path, $action)
112
113 Registers an action at a given path.
114
115 =cut
116
117 sub register_path {
118     my ( $self, $c, $path, $action ) = @_;
119     $path =~ s!^/!!;
120     $path = '/' unless length $path;
121     $path = URI->new($path)->canonical;
122
123     unshift( @{ $self->_paths->{$path} ||= [] }, $action);
124
125     return 1;
126 }
127
128 =head2 $self->uri_for_action($action, $captures)
129
130 get a URI part for an action; always returns undef is $captures is set
131 since Path actions don't have captures
132
133 =cut
134
135 sub uri_for_action {
136     my ( $self, $action, $captures ) = @_;
137
138     return undef if @$captures;
139
140     if (my $paths = $action->attributes->{Path}) {
141         my $path = $paths->[0];
142         $path = '/' unless length($path);
143         $path = "/${path}" unless ($path =~ m/^\//);
144         $path = URI->new($path)->canonical;
145         return $path;
146     } else {
147         return undef;
148     }
149 }
150
151 =head1 AUTHORS
152
153 Catalyst Contributors, see Catalyst.pm
154
155 =head1 COPYRIGHT
156
157 This program is free software, you can redistribute it and/or modify it under
158 the same terms as Perl itself.
159
160 =cut
161
162 __PACKAGE__->meta->make_immutable;
163
164 1;