Commit | Line | Data |
6b239949 |
1 | package Catalyst::DispatchType::Path; |
2 | |
3 | use strict; |
4 | use base qw/Catalyst::DispatchType/; |
8c113188 |
5 | use Text::SimpleTable; |
6b239949 |
6 | |
2633d7dc |
7 | =head1 NAME |
8 | |
9 | Catalyst::DispatchType::Path - Path DispatchType |
10 | |
11 | =head1 SYNOPSIS |
12 | |
13 | See L<Catalyst>. |
14 | |
15 | =head1 DESCRIPTION |
16 | |
17 | =head1 METHODS |
18 | |
b5ecfcf0 |
19 | =head2 $self->list($c) |
a9cbd748 |
20 | |
21 | =cut |
22 | |
23 | sub list { |
24 | my ( $self, $c ) = @_; |
8c113188 |
25 | my $paths = Text::SimpleTable->new( [ 36, 'Path' ], [ 37, 'Private' ] ); |
a9cbd748 |
26 | for my $path ( sort keys %{ $self->{paths} } ) { |
27 | my $action = $self->{paths}->{$path}; |
8c113188 |
28 | $paths->row( "/$path", "/$action" ); |
a9cbd748 |
29 | } |
30 | $c->log->debug( "Loaded Path actions:\n" . $paths->draw ) |
8c113188 |
31 | if ( keys %{ $self->{paths} } ); |
a9cbd748 |
32 | } |
33 | |
b5ecfcf0 |
34 | =head2 $self->match( $c, $path ) |
2633d7dc |
35 | |
36 | =cut |
37 | |
38 | sub match { |
39 | my ( $self, $c, $path ) = @_; |
6b239949 |
40 | |
41 | if ( my $action = $self->{paths}->{$path} ) { |
42 | $c->req->action($path); |
43 | $c->req->match($path); |
44 | $c->action($action); |
11bd4e3e |
45 | $c->namespace( $action->namespace ); |
6b239949 |
46 | return 1; |
47 | } |
48 | |
49 | return 0; |
50 | } |
51 | |
b5ecfcf0 |
52 | =head2 $self->register( $c, $action ) |
2633d7dc |
53 | |
54 | =cut |
55 | |
56 | sub register { |
6b239949 |
57 | my ( $self, $c, $action ) = @_; |
22f3a8dd |
58 | |
6b239949 |
59 | my $attrs = $action->attributes; |
60 | my @register; |
22f3a8dd |
61 | |
2633d7dc |
62 | foreach my $r ( @{ $attrs->{Path} || [] } ) { |
694d15f1 |
63 | unless ($r) { |
621a5d5e |
64 | $r = $action->namespace; |
18339b15 |
65 | $r = '' if $r eq '/'; |
621a5d5e |
66 | } |
67 | elsif ( $r !~ m!^/! ) { # It's a relative path |
11bd4e3e |
68 | $r = $action->namespace . "/$r"; |
6b239949 |
69 | } |
2633d7dc |
70 | push( @register, $r ); |
6b239949 |
71 | } |
72 | |
2633d7dc |
73 | if ( $attrs->{Global} || $attrs->{Absolute} ) { |
74 | push( @register, $action->name ); # Register sub name against root |
6b239949 |
75 | } |
76 | |
2633d7dc |
77 | if ( $attrs->{Local} || $attrs->{Relative} ) { |
11bd4e3e |
78 | push( @register, join( '/', $action->namespace, $action->name ) ); |
2633d7dc |
79 | |
80 | # Register sub name as a relative path |
6b239949 |
81 | } |
82 | |
694d15f1 |
83 | $self->register_path( $c, $_, $action ) for @register; |
84 | return 1 if @register; |
85 | return 0; |
081def36 |
86 | } |
87 | |
b5ecfcf0 |
88 | =head2 $self->register_path($c, $path, $action) |
081def36 |
89 | |
90 | =cut |
91 | |
92 | sub register_path { |
694d15f1 |
93 | my ( $self, $c, $path, $action ) = @_; |
081def36 |
94 | $path =~ s!^/!!; |
95 | $self->{paths}{$path} = $action; |
6b239949 |
96 | } |
97 | |
2633d7dc |
98 | =head1 AUTHOR |
99 | |
100 | Matt S Trout |
101 | Sebastian Riedel, C<sri@cpan.org> |
102 | |
103 | =head1 COPYRIGHT |
104 | |
105 | This program is free software, you can redistribute it and/or modify it under |
106 | the same terms as Perl itself. |
107 | |
108 | =cut |
109 | |
6b239949 |
110 | 1; |