Text::SimpleTable's now go as wide as $ENV{COLUMNS}
[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>.
26
27 =head1 DESCRIPTION
28
29 =head1 METHODS
30
31 =head2 $self->list($c)
32
33 Debug output for Path dispatch points
34
35 =cut
36
37 sub list {
38     my ( $self, $c ) = @_;
39     my $column_width = Catalyst::Utils::term_width() - 35 - 9;
40     my $paths = Text::SimpleTable->new( 
41        [ 35, 'Path' ], [ $column_width, 'Private' ]
42     );
43     foreach my $path ( sort keys %{ $self->_paths } ) {
44         my $display_path = $path eq '/' ? $path : "/$path";
45         foreach my $action ( @{ $self->_paths->{$path} } ) {
46             $paths->row( $display_path, "/$action" );
47         }
48     }
49     $c->log->debug( "Loaded Path actions:\n" . $paths->draw . "\n" )
50       if ( keys %{ $self->_paths } );
51 }
52
53 =head2 $self->match( $c, $path )
54
55 For each action registered to this exact path, offers the action a chance to
56 match the path (in the order in which they were registered). Succeeds on the
57 first action that matches, if any; if not, returns 0.
58
59 =cut
60
61 sub match {
62     my ( $self, $c, $path ) = @_;
63
64     $path = '/' if !defined $path || !length $path;
65
66     foreach my $action ( @{ $self->_paths->{$path} || [] } ) {
67         next unless $action->match($c);
68         $c->req->action($path);
69         $c->req->match($path);
70         $c->action($action);
71         $c->namespace( $action->namespace );
72         return 1;
73     }
74
75     return 0;
76 }
77
78 =head2 $self->register( $c, $action )
79
80 Calls register_path for every Path attribute for the given $action.
81
82 =cut
83
84 sub register {
85     my ( $self, $c, $action ) = @_;
86
87     my @register = @{ $action->attributes->{Path} || [] };
88
89     $self->register_path( $c, $_, $action ) for @register;
90
91     return 1 if @register;
92     return 0;
93 }
94
95 =head2 $self->register_path($c, $path, $action)
96
97 Registers an action at a given path.
98
99 =cut
100
101 sub register_path {
102     my ( $self, $c, $path, $action ) = @_;
103     $path =~ s!^/!!;
104     $path = '/' unless length $path;
105     $path = URI->new($path)->canonical;
106
107     unshift( @{ $self->_paths->{$path} ||= [] }, $action);
108
109     return 1;
110 }
111
112 =head2 $self->uri_for_action($action, $captures)
113
114 get a URI part for an action; always returns undef is $captures is set
115 since Path actions don't have captures
116
117 =cut
118
119 sub uri_for_action {
120     my ( $self, $action, $captures ) = @_;
121
122     return undef if @$captures;
123
124     if (my $paths = $action->attributes->{Path}) {
125         my $path = $paths->[0];
126         $path = '/' unless length($path);
127         $path = "/${path}" unless ($path =~ m/^\//);
128         $path = URI->new($path)->canonical;
129         return $path;
130     } else {
131         return undef;
132     }
133 }
134
135 =head1 AUTHORS
136
137 Catalyst Contributors, see Catalyst.pm
138
139 =head1 COPYRIGHT
140
141 This program is free software, you can redistribute it and/or modify it under
142 the same terms as Perl itself.
143
144 =cut
145
146 __PACKAGE__->meta->make_immutable;
147
148 1;