acf0f3a4be00107193bb730094f16ed517c40602
[catagits/Catalyst-Runtime.git] / lib / Catalyst / DispatchType / Path.pm
1 package Catalyst::DispatchType::Path;
2
3 use Moose;
4 extends 'Catalyst::DispatchType';
5
6 use Text::SimpleTable;
7 use Catalyst::Utils;
8 use URI;
9 use Encode 2.21 'decode_utf8';
10
11 has _paths => (
12                is => 'rw',
13                isa => 'HashRef',
14                required => 1,
15                default => sub { +{} },
16               );
17
18 no Moose;
19
20 =head1 NAME
21
22 Catalyst::DispatchType::Path - Path DispatchType
23
24 =head1 SYNOPSIS
25
26 See L<Catalyst::DispatchType>.
27
28 =head1 DESCRIPTION
29
30 Dispatch type managing full path matching behaviour.  For more information on
31 dispatch types, see:
32
33 =over 4
34
35 =item * L<Catalyst::Manual::Intro> for how they affect application authors
36
37 =item * L<Catalyst::DispatchType> for implementation information.
38
39 =back
40
41 =head1 METHODS
42
43 =head2 $self->list($c)
44
45 Debug output for Path dispatch points
46
47 =cut
48
49 sub list {
50     my ( $self, $c ) = @_;
51     my $avail_width = Catalyst::Utils::term_width() - 9;
52     my $col1_width = ($avail_width * .50) < 35 ? 35 : int($avail_width * .50);
53     my $col2_width = $avail_width - $col1_width;
54     my $paths = Text::SimpleTable->new(
55        [ $col1_width, 'Path' ], [ $col2_width, 'Private' ]
56     );
57     foreach my $path ( sort keys %{ $self->_paths } ) {
58         foreach my $action ( @{ $self->_paths->{$path} } ) {
59             my $args  = $action->attributes->{Args}->[0];
60             my $parts = defined($args) ? '/*' x $args : '/...';
61
62             my $display_path = "/$path/$parts";
63             $display_path =~ s{/{1,}}{/}g;
64             $display_path =~ s/%([0-9A-Fa-f]{2})/chr(hex($1))/eg; # deconvert urlencoded for pretty view 
65             $display_path = decode_utf8 $display_path;  # URI does encoding
66             $paths->row( $display_path, "/$action" );
67         }
68     }
69     $c->log->debug( "Loaded Path actions:\n" . $paths->draw . "\n" )
70       if ( keys %{ $self->_paths } );
71 }
72
73 =head2 $self->match( $c, $path )
74
75 For each action registered to this exact path, offers the action a chance to
76 match the path (in the order in which they were registered). Succeeds on the
77 first action that matches, if any; if not, returns 0.
78
79 =cut
80
81 sub match {
82     my ( $self, $c, $path ) = @_;
83
84     $path = '/' if !defined $path || !length $path;
85
86     my @actions = @{ $self->_paths->{$path} || [] };
87
88     foreach my $action ( @actions ) {
89         next unless $action->match($c);
90         $c->req->action($path);
91         $c->req->match($path);
92         $c->action($action);
93         $c->namespace( $action->namespace );
94         return 1;
95     }
96
97     return 0;
98 }
99
100 =head2 $self->register( $c, $action )
101
102 Calls register_path for every Path attribute for the given $action.
103
104 =cut
105
106 sub register {
107     my ( $self, $c, $action ) = @_;
108
109     my @register = @{ $action->attributes->{Path} || [] };
110
111     $self->register_path( $c, $_, $action ) for @register;
112
113     return 1 if @register;
114     return 0;
115 }
116
117 =head2 $self->register_path($c, $path, $action)
118
119 Registers an action at a given path.
120
121 =cut
122
123 sub register_path {
124     my ( $self, $c, $path, $action ) = @_;
125     $path =~ s!^/!!;
126     $path = '/' unless length $path;
127     $path = URI->new($path)->canonical;
128     $path =~ s{(?<=[^/])/+\z}{};
129
130     $self->_paths->{$path} = [
131         sort { $a->compare($b) } ($action, @{ $self->_paths->{$path} || [] })
132     ];
133
134     return 1;
135 }
136
137 =head2 $self->uri_for_action($action, $captures)
138
139 get a URI part for an action; always returns undef is $captures is set
140 since Path actions don't have captures
141
142 =cut
143
144 sub uri_for_action {
145     my ( $self, $action, $captures ) = @_;
146
147     return undef if @$captures;
148
149     if (my $paths = $action->attributes->{Path}) {
150         my $path = $paths->[0];
151         $path = '/' unless length($path);
152         $path = "/${path}" unless ($path =~ m/^\//);
153         $path = URI->new($path)->canonical;
154         return $path;
155     } else {
156         return undef;
157     }
158 }
159
160 =head1 AUTHORS
161
162 Catalyst Contributors, see Catalyst.pm
163
164 =head1 COPYRIGHT
165
166 This library is free software. You can redistribute it and/or modify it under
167 the same terms as Perl itself.
168
169 =cut
170
171 __PACKAGE__->meta->make_immutable;
172
173 1;