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