Commit | Line | Data |
6b239949 |
1 | package Catalyst::DispatchType::Path; |
2 | |
3 | use strict; |
4 | use base qw/Catalyst::DispatchType/; |
8c113188 |
5 | use Text::SimpleTable; |
595f3872 |
6 | use URI; |
6b239949 |
7 | |
2633d7dc |
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 | |
b5ecfcf0 |
20 | =head2 $self->list($c) |
a9cbd748 |
21 | |
4ab87e27 |
22 | Debug output for Path dispatch points |
23 | |
a9cbd748 |
24 | =cut |
25 | |
26 | sub list { |
27 | my ( $self, $c ) = @_; |
34d28dfd |
28 | my $paths = Text::SimpleTable->new( [ 35, 'Path' ], [ 36, 'Private' ] ); |
a9cbd748 |
29 | for my $path ( sort keys %{ $self->{paths} } ) { |
30 | my $action = $self->{paths}->{$path}; |
61a9002d |
31 | $path = "/$path" unless $path eq '/'; |
32 | $paths->row( "$path", "/$action" ); |
a9cbd748 |
33 | } |
34 | $c->log->debug( "Loaded Path actions:\n" . $paths->draw ) |
8c113188 |
35 | if ( keys %{ $self->{paths} } ); |
a9cbd748 |
36 | } |
37 | |
b5ecfcf0 |
38 | =head2 $self->match( $c, $path ) |
2633d7dc |
39 | |
4ab87e27 |
40 | Check for paths that match the given path. |
41 | |
2633d7dc |
42 | =cut |
43 | |
44 | sub match { |
45 | my ( $self, $c, $path ) = @_; |
6b239949 |
46 | |
61a9002d |
47 | $path ||= '/'; |
6b239949 |
48 | if ( my $action = $self->{paths}->{$path} ) { |
4082e678 |
49 | return 0 unless $action->match($c); |
6b239949 |
50 | $c->req->action($path); |
51 | $c->req->match($path); |
52 | $c->action($action); |
11bd4e3e |
53 | $c->namespace( $action->namespace ); |
6b239949 |
54 | return 1; |
55 | } |
56 | |
57 | return 0; |
58 | } |
59 | |
b5ecfcf0 |
60 | =head2 $self->register( $c, $action ) |
2633d7dc |
61 | |
4ab87e27 |
62 | Call register_path for every path attribute in the given $action. |
63 | |
2633d7dc |
64 | =cut |
65 | |
66 | sub register { |
6b239949 |
67 | my ( $self, $c, $action ) = @_; |
22f3a8dd |
68 | |
27708fc5 |
69 | my @register = @{ $action->attributes->{Path} || [] }; |
6b239949 |
70 | |
694d15f1 |
71 | $self->register_path( $c, $_, $action ) for @register; |
5b707014 |
72 | |
694d15f1 |
73 | return 1 if @register; |
74 | return 0; |
081def36 |
75 | } |
76 | |
b5ecfcf0 |
77 | =head2 $self->register_path($c, $path, $action) |
081def36 |
78 | |
4ab87e27 |
79 | register an action at a given path. |
80 | |
081def36 |
81 | =cut |
82 | |
83 | sub register_path { |
694d15f1 |
84 | my ( $self, $c, $path, $action ) = @_; |
081def36 |
85 | $path =~ s!^/!!; |
0ba80bce |
86 | $path = '/' unless length $path; |
595f3872 |
87 | $path = URI->new($path)->canonical; |
27708fc5 |
88 | |
081def36 |
89 | $self->{paths}{$path} = $action; |
6b239949 |
90 | } |
91 | |
2633d7dc |
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 | |
6b239949 |
104 | 1; |