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