- Fixes for rt.cpan #17322 and #17331
[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
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
19 =head2 $self->list($c)
20
21 =cut
22
23 sub list {
24     my ( $self, $c ) = @_;
25     my $paths = Text::SimpleTable->new( [ 36, 'Path' ], [ 37, 'Private' ] );
26     for my $path ( sort keys %{ $self->{paths} } ) {
27         my $action = $self->{paths}->{$path};
28         $path = "/$path" unless $path eq '/';
29         $paths->row( "$path", "/$action" );
30     }
31     $c->log->debug( "Loaded Path actions:\n" . $paths->draw )
32       if ( keys %{ $self->{paths} } );
33 }
34
35 =head2 $self->match( $c, $path )
36
37 =cut
38
39 sub match {
40     my ( $self, $c, $path ) = @_;
41
42     $path ||= '/';
43     if ( my $action = $self->{paths}->{$path} ) {
44         $c->req->action($path);
45         $c->req->match($path);
46         $c->action($action);
47         $c->namespace( $action->namespace );
48         return 1;
49     }
50
51     return 0;
52 }
53
54 =head2 $self->register( $c, $action )
55
56 =cut
57
58 sub register {
59     my ( $self, $c, $action ) = @_;
60
61     my $attrs = $action->attributes;
62     my @register;
63
64     foreach my $r ( @{ $attrs->{Path} || [] } ) {
65         unless ($r) {
66             $r = $action->namespace;
67             $r = '/' unless length $r;
68         }
69         elsif ( $r !~ m!^/! ) {    # It's a relative path
70             $r = $action->namespace . "/$r";
71         }
72         push( @register, $r );
73     }
74
75     if ( $attrs->{Global} || $attrs->{Absolute} ) {
76         push( @register, $action->name );    # Register sub name against root
77     }
78
79     if ( $attrs->{Local} || $attrs->{Relative} ) {
80         push( @register, join( '/', $action->namespace, $action->name ) );
81
82         # Register sub name as a relative path
83     }
84
85     $self->register_path( $c, $_, $action ) for @register;
86     return 1 if @register;
87     return 0;
88 }
89
90 =head2 $self->register_path($c, $path, $action)
91
92 =cut
93
94 sub register_path {
95     my ( $self, $c, $path, $action ) = @_;
96     $path =~ s!^/!!;
97     $path = '/' unless length $path;
98     $self->{paths}{$path} = $action;
99 }
100
101 =head1 AUTHOR
102
103 Matt S Trout
104 Sebastian Riedel, C<sri@cpan.org>
105
106 =head1 COPYRIGHT
107
108 This program is free software, you can redistribute it and/or modify it under
109 the same terms as Perl itself.
110
111 =cut
112
113 1;