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