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