Fixed run-on sentence in COPYRIGHT and s/program/library/
[catagits/Catalyst-Runtime.git] / lib / Catalyst / DispatchType / Path.pm
CommitLineData
6b239949 1package Catalyst::DispatchType::Path;
2
3c0186f2 3use Moose;
3c0186f2 4extends 'Catalyst::DispatchType';
5
e8b9f2a9 6use Text::SimpleTable;
39fc2ce1 7use Catalyst::Utils;
e8b9f2a9 8use URI;
cdf573a6 9use Scalar::Util ();
3c0186f2 10
36dbb986 11has _paths => (
12 is => 'rw',
13 isa => 'HashRef',
14 required => 1,
15 default => sub { +{} },
16 );
17
0fc2d522 18no Moose;
19
2633d7dc 20=head1 NAME
21
22Catalyst::DispatchType::Path - Path DispatchType
23
24=head1 SYNOPSIS
25
26dc649a 26See L<Catalyst::DispatchType>.
2633d7dc 27
28=head1 DESCRIPTION
29
26dc649a 30Dispatch type managing full path matching behaviour. For more information on
31dispatch types, see:
32
33=over 4
34
b9b89145 35=item * L<Catalyst::Manual::Intro> for how they affect application authors
26dc649a 36
37=item * L<Catalyst::DispatchType> for implementation information.
38
39=back
40
2633d7dc 41=head1 METHODS
42
b5ecfcf0 43=head2 $self->list($c)
a9cbd748 44
4ab87e27 45Debug output for Path dispatch points
46
a9cbd748 47=cut
48
49sub list {
50 my ( $self, $c ) = @_;
39fc2ce1 51 my $column_width = Catalyst::Utils::term_width() - 35 - 9;
52 my $paths = Text::SimpleTable->new(
53 [ 35, 'Path' ], [ $column_width, 'Private' ]
54 );
36dbb986 55 foreach my $path ( sort keys %{ $self->_paths } ) {
c5b74a51 56 my $display_path = $path eq '/' ? $path : "/$path";
36dbb986 57 foreach my $action ( @{ $self->_paths->{$path} } ) {
e8b9f2a9 58 $paths->row( $display_path, "/$action" );
0bd5f8a2 59 }
a9cbd748 60 }
e8b9f2a9 61 $c->log->debug( "Loaded Path actions:\n" . $paths->draw . "\n" )
36dbb986 62 if ( keys %{ $self->_paths } );
a9cbd748 63}
64
cdf573a6 65sub _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
b5ecfcf0 75=head2 $self->match( $c, $path )
2633d7dc 76
b2b90ec2 77For each action registered to this exact path, offers the action a chance to
78match the path (in the order in which they were registered). Succeeds on the
79first action that matches, if any; if not, returns 0.
4ab87e27 80
2633d7dc 81=cut
82
83sub match {
84 my ( $self, $c, $path ) = @_;
6b239949 85
2f381252 86 $path = '/' if !defined $path || !length $path;
0bd5f8a2 87
391c455f 88 # sort from least args to most
cdf573a6 89 my @actions = sort { $self->_action_args_sort_order($a) <=>
90 $self->_action_args_sort_order($b) }
391c455f 91 @{ $self->_paths->{$path} || [] };
92
93 foreach my $action ( @actions ) {
0bd5f8a2 94 next unless $action->match($c);
6b239949 95 $c->req->action($path);
96 $c->req->match($path);
97 $c->action($action);
11bd4e3e 98 $c->namespace( $action->namespace );
6b239949 99 return 1;
100 }
101
102 return 0;
103}
104
b5ecfcf0 105=head2 $self->register( $c, $action )
2633d7dc 106
b2b90ec2 107Calls register_path for every Path attribute for the given $action.
4ab87e27 108
2633d7dc 109=cut
110
111sub register {
6b239949 112 my ( $self, $c, $action ) = @_;
22f3a8dd 113
27708fc5 114 my @register = @{ $action->attributes->{Path} || [] };
6b239949 115
694d15f1 116 $self->register_path( $c, $_, $action ) for @register;
5b707014 117
694d15f1 118 return 1 if @register;
119 return 0;
081def36 120}
121
b5ecfcf0 122=head2 $self->register_path($c, $path, $action)
081def36 123
b2b90ec2 124Registers an action at a given path.
4ab87e27 125
081def36 126=cut
127
128sub register_path {
694d15f1 129 my ( $self, $c, $path, $action ) = @_;
081def36 130 $path =~ s!^/!!;
0ba80bce 131 $path = '/' unless length $path;
595f3872 132 $path = URI->new($path)->canonical;
27708fc5 133
36dbb986 134 unshift( @{ $self->_paths->{$path} ||= [] }, $action);
0bd5f8a2 135
136 return 1;
6b239949 137}
138
ea0e58d9 139=head2 $self->uri_for_action($action, $captures)
140
141get a URI part for an action; always returns undef is $captures is set
142since Path actions don't have captures
143
144=cut
145
146sub 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
2f381252 162=head1 AUTHORS
2633d7dc 163
2f381252 164Catalyst Contributors, see Catalyst.pm
2633d7dc 165
166=head1 COPYRIGHT
167
536bee89 168This library is free software. You can redistribute it and/or modify it under
2633d7dc 169the same terms as Perl itself.
170
171=cut
172
e5ecd5bc 173__PACKAGE__->meta->make_immutable;
174
6b239949 1751;