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