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