added Catalyst::Component->action_for
[catagits/Catalyst-Runtime.git] / lib / Catalyst / DispatchType / Regex.pm
CommitLineData
b96f127f 1package Catalyst::DispatchType::Regex;
2
3use strict;
6b239949 4use base qw/Catalyst::DispatchType::Path/;
8c113188 5use Text::SimpleTable;
b96f127f 6
2633d7dc 7=head1 NAME
b96f127f 8
2633d7dc 9Catalyst::DispatchType::Regex - Regex DispatchType
b96f127f 10
2633d7dc 11=head1 SYNOPSIS
12
13See L<Catalyst>.
14
15=head1 DESCRIPTION
16
17=head1 METHODS
18
b5ecfcf0 19=head2 $self->list($c)
a9cbd748 20
4ab87e27 21Output a table of all regex actions, and their private equivalent.
22
a9cbd748 23=cut
24
25sub list {
26 my ( $self, $c ) = @_;
34d28dfd 27 my $re = Text::SimpleTable->new( [ 35, 'Regex' ], [ 36, 'Private' ] );
a9cbd748 28 for my $regex ( @{ $self->{compiled} } ) {
694d15f1 29 my $action = $regex->{action};
dcc61a75 30 $re->row( $regex->{path}, "/$action" );
a9cbd748 31 }
32 $c->log->debug( "Loaded Regex actions:\n" . $re->draw )
8c113188 33 if ( @{ $self->{compiled} } );
a9cbd748 34}
35
b5ecfcf0 36=head2 $self->match( $c, $path )
2633d7dc 37
4ab87e27 38Check path against compiled regexes, and set action to any matching
39action. Returns 1 on success and 0 on failure.
40
2633d7dc 41=cut
42
43sub match {
44 my ( $self, $c, $path ) = @_;
45
46 return if $self->SUPER::match( $c, $path );
47
48 # Check path against plain text first
49
50 foreach my $compiled ( @{ $self->{compiled} || [] } ) {
2982e768 51 if ( my @captures = ( $path =~ $compiled->{re} ) ) {
4082e678 52 next unless $compiled->{action}->match($c);
2633d7dc 53 $c->req->action( $compiled->{path} );
b96f127f 54 $c->req->match($path);
2982e768 55 $c->req->captures( \@captures );
2633d7dc 56 $c->action( $compiled->{action} );
11bd4e3e 57 $c->namespace( $compiled->{action}->namespace );
b96f127f 58 return 1;
59 }
60 }
61
62 return 0;
63}
64
b5ecfcf0 65=head2 $self->register( $c, $action )
2633d7dc 66
4ab87e27 67Registers one or more regex actions for an action object.\
68Also registers them as literal paths.
69
70Returns 1 on if any regexps were registered.
71
2633d7dc 72=cut
73
74sub register {
b96f127f 75 my ( $self, $c, $action ) = @_;
34d28dfd 76 my $attrs = $action->attributes;
27708fc5 77 my @register = @{ $attrs->{'Regex'} || [] };
081def36 78
b96f127f 79 foreach my $r (@register) {
081def36 80 $self->register_path( $c, $r, $action );
81 $self->register_regex( $c, $r, $action );
b96f127f 82 }
27708fc5 83
694d15f1 84 return 1 if @register;
85 return 0;
b96f127f 86}
87
b5ecfcf0 88=head2 $self->register_regex($c, $re, $action)
081def36 89
4ab87e27 90Register an individual regex on the action. Usually called from the
91register action.
92
081def36 93=cut
94
95sub register_regex {
96 my ( $self, $c, $re, $action ) = @_;
97 push(
98 @{ $self->{compiled} }, # and compiled regex for us
99 {
100 re => qr#$re#,
101 action => $action,
102 path => $re,
103 }
104 );
105}
106
2633d7dc 107=head1 AUTHOR
108
109Matt S Trout
110Sebastian Riedel, C<sri@cpan.org>
111
112=head1 COPYRIGHT
113
114This program is free software, you can redistribute it and/or modify it under
115the same terms as Perl itself.
116
117=cut
118
b96f127f 1191;