d887a1364fdbc2a0fe9a3ee4a30841cf1464627a
[catagits/Catalyst-Runtime.git] / lib / Catalyst / DispatchType / Path.pm
1 package Catalyst::DispatchType::Path;
2
3 use strict;
4 use base qw/Catalyst::DispatchType/;
5 use Text::SimpleTable;
6 use URI;
7
8 =head1 NAME
9
10 Catalyst::DispatchType::Path - Path DispatchType
11
12 =head1 SYNOPSIS
13
14 See L<Catalyst>.
15
16 =head1 DESCRIPTION
17
18 =head1 METHODS
19
20 =head2 $self->list($c)
21
22 Debug output for Path dispatch points
23
24 =cut
25
26 sub list {
27     my ( $self, $c ) = @_;
28     my $paths = Text::SimpleTable->new( [ 36, 'Path' ], [ 37, 'Private' ] );
29     for my $path ( sort keys %{ $self->{paths} } ) {
30         my $action = $self->{paths}->{$path};
31         $path = "/$path" unless $path eq '/';
32         $paths->row( "$path", "/$action" );
33     }
34     $c->log->debug( "Loaded Path actions:\n" . $paths->draw )
35       if ( keys %{ $self->{paths} } );
36 }
37
38 =head2 $self->match( $c, $path )
39
40 Check for paths that match the given path.
41
42 =cut
43
44 sub match {
45     my ( $self, $c, $path ) = @_;
46
47     $path ||= '/';
48     if ( my $action = $self->{paths}->{$path} ) {
49         return 0 unless $action->match($c);
50         $c->req->action($path);
51         $c->req->match($path);
52         $c->action($action);
53         $c->namespace( $action->namespace );
54         return 1;
55     }
56
57     return 0;
58 }
59
60 =head2 $self->register( $c, $action )
61
62 Call register_path for every path attribute in the given $action.
63
64 =cut
65
66 sub register {
67     my ( $self, $c, $action ) = @_;
68
69     my @register = @{ $action->attributes->{Path} || [] };
70
71     $self->register_path( $c, $_, $action ) for @register;
72
73     return 1 if @register;
74     return 0;
75 }
76
77 =head2 $self->register_path($c, $path, $action)
78
79 register an action at a given path.
80
81 =cut
82
83 sub register_path {
84     my ( $self, $c, $path, $action ) = @_;
85     $path =~ s!^/!!;
86     $path = '/' unless length $path;
87     $path = URI->new($path)->canonical;
88
89     $self->{paths}{$path} = $action;
90 }
91
92 =head1 AUTHOR
93
94 Matt S Trout
95 Sebastian Riedel, C<sri@cpan.org>
96
97 =head1 COPYRIGHT
98
99 This program is free software, you can redistribute it and/or modify it under
100 the same terms as Perl itself.
101
102 =cut
103
104 1;