make debug output prettier with large widths
[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         my $display_path = $path eq '/' ? $path : "/$path";
58         foreach my $action ( @{ $self->_paths->{$path} } ) {
59             $paths->row( $display_path, "/$action" );
60         }
61     }
62     $c->log->debug( "Loaded Path actions:\n" . $paths->draw . "\n" )
63       if ( keys %{ $self->_paths } );
64 }
65
66 =head2 $self->match( $c, $path )
67
68 For each action registered to this exact path, offers the action a chance to
69 match the path (in the order in which they were registered). Succeeds on the
70 first action that matches, if any; if not, returns 0.
71
72 =cut
73
74 sub match {
75     my ( $self, $c, $path ) = @_;
76
77     $path = '/' if !defined $path || !length $path;
78
79     my @actions = @{ $self->_paths->{$path} || [] };
80
81     foreach my $action ( @actions ) {
82         next unless $action->match($c);
83         $c->req->action($path);
84         $c->req->match($path);
85         $c->action($action);
86         $c->namespace( $action->namespace );
87         return 1;
88     }
89
90     return 0;
91 }
92
93 =head2 $self->register( $c, $action )
94
95 Calls register_path for every Path attribute for the given $action.
96
97 =cut
98
99 sub register {
100     my ( $self, $c, $action ) = @_;
101
102     my @register = @{ $action->attributes->{Path} || [] };
103
104     $self->register_path( $c, $_, $action ) for @register;
105
106     return 1 if @register;
107     return 0;
108 }
109
110 =head2 $self->register_path($c, $path, $action)
111
112 Registers an action at a given path.
113
114 =cut
115
116 sub register_path {
117     my ( $self, $c, $path, $action ) = @_;
118     $path =~ s!^/!!;
119     $path = '/' unless length $path;
120     $path = URI->new($path)->canonical;
121     $path =~ s{(?<=[^/])/+\z}{};
122
123     $self->_paths->{$path} = [
124         sort { $a->compare($b) } ($action, @{ $self->_paths->{$path} || [] })
125     ];
126
127     return 1;
128 }
129
130 =head2 $self->uri_for_action($action, $captures)
131
132 get a URI part for an action; always returns undef is $captures is set
133 since Path actions don't have captures
134
135 =cut
136
137 sub uri_for_action {
138     my ( $self, $action, $captures ) = @_;
139
140     return undef if @$captures;
141
142     if (my $paths = $action->attributes->{Path}) {
143         my $path = $paths->[0];
144         $path = '/' unless length($path);
145         $path = "/${path}" unless ($path =~ m/^\//);
146         $path = URI->new($path)->canonical;
147         return $path;
148     } else {
149         return undef;
150     }
151 }
152
153 =head1 AUTHORS
154
155 Catalyst Contributors, see Catalyst.pm
156
157 =head1 COPYRIGHT
158
159 This library is free software. You can redistribute it and/or modify it under
160 the same terms as Perl itself.
161
162 =cut
163
164 __PACKAGE__->meta->make_immutable;
165
166 1;