DispatchType stripped of CAF, SUPER and substituted instances of $self->{*} with...
[catagits/Catalyst-Runtime.git] / lib / Catalyst / DispatchType / Path.pm
CommitLineData
6b239949 1package Catalyst::DispatchType::Path;
2
3c0186f2 3use Moose;
8c113188 4use Text::SimpleTable;
595f3872 5use URI;
6b239949 6
3c0186f2 7extends 'Catalyst::DispatchType';
8
9has _paths => (
10 is => 'rw',
11 isa => 'HashRef',
12 required => 1,
13 default => sub {{}}
14 );
15
2633d7dc 16=head1 NAME
17
18Catalyst::DispatchType::Path - Path DispatchType
19
20=head1 SYNOPSIS
21
22See L<Catalyst>.
23
24=head1 DESCRIPTION
25
26=head1 METHODS
27
b5ecfcf0 28=head2 $self->list($c)
a9cbd748 29
4ab87e27 30Debug output for Path dispatch points
31
a9cbd748 32=cut
33
34sub list {
35 my ( $self, $c ) = @_;
3c0186f2 36 my %paths = %{ $self->_paths };
37 my @keys = sort keys %paths;
38 return unless @keys;
39 my $paths_table = Text::SimpleTable->new( [ 35, 'Path' ], [ 36, 'Private' ] );
40 foreach my $path ( @keys ) {
c5b74a51 41 my $display_path = $path eq '/' ? $path : "/$path";
3c0186f2 42 foreach my $action ( @{ $paths{$path} } ) {
43 $paths_table->row( $display_path, "/$action" );
0bd5f8a2 44 }
a9cbd748 45 }
3c0186f2 46 $c->log->debug( "Loaded Path actions:\n" . $paths_table->draw . "\n" );
a9cbd748 47}
48
b5ecfcf0 49=head2 $self->match( $c, $path )
2633d7dc 50
b2b90ec2 51For each action registered to this exact path, offers the action a chance to
52match the path (in the order in which they were registered). Succeeds on the
53first action that matches, if any; if not, returns 0.
4ab87e27 54
2633d7dc 55=cut
56
57sub match {
58 my ( $self, $c, $path ) = @_;
6b239949 59
61a9002d 60 $path ||= '/';
0bd5f8a2 61
3c0186f2 62 foreach my $action ( @{ $self->_paths->{$path} || [] } ) {
0bd5f8a2 63 next unless $action->match($c);
6b239949 64 $c->req->action($path);
65 $c->req->match($path);
66 $c->action($action);
11bd4e3e 67 $c->namespace( $action->namespace );
6b239949 68 return 1;
69 }
70
71 return 0;
72}
73
b5ecfcf0 74=head2 $self->register( $c, $action )
2633d7dc 75
b2b90ec2 76Calls register_path for every Path attribute for the given $action.
4ab87e27 77
2633d7dc 78=cut
79
80sub register {
6b239949 81 my ( $self, $c, $action ) = @_;
22f3a8dd 82
27708fc5 83 my @register = @{ $action->attributes->{Path} || [] };
6b239949 84
694d15f1 85 $self->register_path( $c, $_, $action ) for @register;
5b707014 86
694d15f1 87 return 1 if @register;
88 return 0;
081def36 89}
90
b5ecfcf0 91=head2 $self->register_path($c, $path, $action)
081def36 92
b2b90ec2 93Registers an action at a given path.
4ab87e27 94
081def36 95=cut
96
97sub register_path {
694d15f1 98 my ( $self, $c, $path, $action ) = @_;
081def36 99 $path =~ s!^/!!;
0ba80bce 100 $path = '/' unless length $path;
595f3872 101 $path = URI->new($path)->canonical;
27708fc5 102
3c0186f2 103 unshift( @{ $self->_paths->{$path} ||= [] }, $action);
0bd5f8a2 104
105 return 1;
6b239949 106}
107
ea0e58d9 108=head2 $self->uri_for_action($action, $captures)
109
110get a URI part for an action; always returns undef is $captures is set
111since Path actions don't have captures
112
113=cut
114
115sub uri_for_action {
116 my ( $self, $action, $captures ) = @_;
117
118 return undef if @$captures;
119
120 if (my $paths = $action->attributes->{Path}) {
121 my $path = $paths->[0];
122 $path = '/' unless length($path);
123 $path = "/${path}" unless ($path =~ m/^\//);
124 $path = URI->new($path)->canonical;
125 return $path;
126 } else {
127 return undef;
128 }
129}
130
2633d7dc 131=head1 AUTHOR
132
133Matt S Trout
134Sebastian Riedel, C<sri@cpan.org>
135
136=head1 COPYRIGHT
137
138This program is free software, you can redistribute it and/or modify it under
139the same terms as Perl itself.
140
141=cut
142
6b239949 1431;