converting the engines. i had to add use NEXT to some of the test files to make it...
[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 6#use strict;
7#use base qw/Catalyst::DispatchType/;
8use Text::SimpleTable;
9use URI;
3c0186f2 10
36dbb986 11has _paths => (
12 is => 'rw',
13 isa => 'HashRef',
14 required => 1,
15 default => sub { +{} },
16 );
17
2633d7dc 18=head1 NAME
19
20Catalyst::DispatchType::Path - Path DispatchType
21
22=head1 SYNOPSIS
23
24See L<Catalyst>.
25
26=head1 DESCRIPTION
27
28=head1 METHODS
29
b5ecfcf0 30=head2 $self->list($c)
a9cbd748 31
4ab87e27 32Debug output for Path dispatch points
33
a9cbd748 34=cut
35
36sub list {
37 my ( $self, $c ) = @_;
e8b9f2a9 38 my $paths = Text::SimpleTable->new( [ 35, 'Path' ], [ 36, 'Private' ] );
36dbb986 39 foreach my $path ( sort keys %{ $self->_paths } ) {
c5b74a51 40 my $display_path = $path eq '/' ? $path : "/$path";
36dbb986 41 foreach my $action ( @{ $self->_paths->{$path} } ) {
e8b9f2a9 42 $paths->row( $display_path, "/$action" );
0bd5f8a2 43 }
a9cbd748 44 }
e8b9f2a9 45 $c->log->debug( "Loaded Path actions:\n" . $paths->draw . "\n" )
36dbb986 46 if ( keys %{ $self->_paths } );
a9cbd748 47}
48
b5ecfcf0 49=head2 $self->match( $c, $path )
2633d7dc 50
b2b90ec2 51For each action registered to this exact path, offers the action a chance to
52match the path (in the order in which they were registered). Succeeds on the
53first action that matches, if any; if not, returns 0.
4ab87e27 54
2633d7dc 55=cut
56
57sub match {
58 my ( $self, $c, $path ) = @_;
6b239949 59
61a9002d 60 $path ||= '/';
0bd5f8a2 61
36dbb986 62 foreach my $action ( @{ $self->_paths->{$path} || [] } ) {
0bd5f8a2 63 next unless $action->match($c);
6b239949 64 $c->req->action($path);
65 $c->req->match($path);
66 $c->action($action);
11bd4e3e 67 $c->namespace( $action->namespace );
6b239949 68 return 1;
69 }
70
71 return 0;
72}
73
b5ecfcf0 74=head2 $self->register( $c, $action )
2633d7dc 75
b2b90ec2 76Calls register_path for every Path attribute for the given $action.
4ab87e27 77
2633d7dc 78=cut
79
80sub register {
6b239949 81 my ( $self, $c, $action ) = @_;
22f3a8dd 82
27708fc5 83 my @register = @{ $action->attributes->{Path} || [] };
6b239949 84
694d15f1 85 $self->register_path( $c, $_, $action ) for @register;
5b707014 86
694d15f1 87 return 1 if @register;
88 return 0;
081def36 89}
90
b5ecfcf0 91=head2 $self->register_path($c, $path, $action)
081def36 92
b2b90ec2 93Registers an action at a given path.
4ab87e27 94
081def36 95=cut
96
97sub register_path {
694d15f1 98 my ( $self, $c, $path, $action ) = @_;
081def36 99 $path =~ s!^/!!;
0ba80bce 100 $path = '/' unless length $path;
595f3872 101 $path = URI->new($path)->canonical;
27708fc5 102
36dbb986 103 unshift( @{ $self->_paths->{$path} ||= [] }, $action);
0bd5f8a2 104
105 return 1;
6b239949 106}
107
ea0e58d9 108=head2 $self->uri_for_action($action, $captures)
109
110get a URI part for an action; always returns undef is $captures is set
111since Path actions don't have captures
112
113=cut
114
115sub uri_for_action {
116 my ( $self, $action, $captures ) = @_;
117
118 return undef if @$captures;
119
120 if (my $paths = $action->attributes->{Path}) {
121 my $path = $paths->[0];
122 $path = '/' unless length($path);
123 $path = "/${path}" unless ($path =~ m/^\//);
124 $path = URI->new($path)->canonical;
125 return $path;
126 } else {
127 return undef;
128 }
129}
130
2633d7dc 131=head1 AUTHOR
132
133Matt S Trout
134Sebastian Riedel, C<sri@cpan.org>
135
136=head1 COPYRIGHT
137
138This program is free software, you can redistribute it and/or modify it under
139the same terms as Perl itself.
140
141=cut
142
6b239949 1431;