5c1e4ed4f7d956c8dc7a89ad40c0f820f449f722
[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     my @actions = @{ $self->_paths->{$path} || [] };
78
79     foreach my $action ( @actions ) {
80         next unless $action->match($c);
81         $c->req->action($path);
82         $c->req->match($path);
83         $c->action($action);
84         $c->namespace( $action->namespace );
85         return 1;
86     }
87
88     return 0;
89 }
90
91 =head2 $self->register( $c, $action )
92
93 Calls register_path for every Path attribute for the given $action.
94
95 =cut
96
97 sub register {
98     my ( $self, $c, $action ) = @_;
99
100     my @register = @{ $action->attributes->{Path} || [] };
101
102     $self->register_path( $c, $_, $action ) for @register;
103
104     return 1 if @register;
105     return 0;
106 }
107
108 =head2 $self->register_path($c, $path, $action)
109
110 Registers an action at a given path.
111
112 =cut
113
114 sub register_path {
115     my ( $self, $c, $path, $action ) = @_;
116     $path =~ s!^/!!;
117     $path = '/' unless length $path;
118     $path = URI->new($path)->canonical;
119
120     $self->_paths->{$path} = [
121         sort { $a <=> $b } ($action, @{ $self->_paths->{$path} || [] })
122     ];
123
124     return 1;
125 }
126
127 =head2 $self->uri_for_action($action, $captures)
128
129 get a URI part for an action; always returns undef is $captures is set
130 since Path actions don't have captures
131
132 =cut
133
134 sub uri_for_action {
135     my ( $self, $action, $captures ) = @_;
136
137     return undef if @$captures;
138
139     if (my $paths = $action->attributes->{Path}) {
140         my $path = $paths->[0];
141         $path = '/' unless length($path);
142         $path = "/${path}" unless ($path =~ m/^\//);
143         $path = URI->new($path)->canonical;
144         return $path;
145     } else {
146         return undef;
147     }
148 }
149
150 =head1 AUTHORS
151
152 Catalyst Contributors, see Catalyst.pm
153
154 =head1 COPYRIGHT
155
156 This library is free software. You can redistribute it and/or modify it under
157 the same terms as Perl itself.
158
159 =cut
160
161 __PACKAGE__->meta->make_immutable;
162
163 1;