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