Looping and recursion tests plus a fix
[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
21=cut
22
23sub list {
24 my ( $self, $c ) = @_;
8c113188 25 my $re = Text::SimpleTable->new( [ 36, 'Regex' ], [ 37, 'Private' ] );
a9cbd748 26 for my $regex ( @{ $self->{compiled} } ) {
694d15f1 27 my $action = $regex->{action};
dcc61a75 28 $re->row( $regex->{path}, "/$action" );
a9cbd748 29 }
30 $c->log->debug( "Loaded Regex actions:\n" . $re->draw )
8c113188 31 if ( @{ $self->{compiled} } );
a9cbd748 32}
33
b5ecfcf0 34=head2 $self->match( $c, $path )
2633d7dc 35
36=cut
37
38sub match {
39 my ( $self, $c, $path ) = @_;
40
41 return if $self->SUPER::match( $c, $path );
42
43 # Check path against plain text first
44
45 foreach my $compiled ( @{ $self->{compiled} || [] } ) {
b96f127f 46 if ( my @snippets = ( $path =~ $compiled->{re} ) ) {
4082e678 47 next unless $compiled->{action}->match($c);
2633d7dc 48 $c->req->action( $compiled->{path} );
b96f127f 49 $c->req->match($path);
2633d7dc 50 $c->req->snippets( \@snippets );
51 $c->action( $compiled->{action} );
11bd4e3e 52 $c->namespace( $compiled->{action}->namespace );
b96f127f 53 return 1;
54 }
55 }
56
57 return 0;
58}
59
b5ecfcf0 60=head2 $self->register( $c, $action )
2633d7dc 61
62=cut
63
64sub register {
b96f127f 65 my ( $self, $c, $action ) = @_;
66 my $attrs = $action->attributes;
27708fc5 67 my @register = @{ $attrs->{'Regex'} || [] };
081def36 68
b96f127f 69 foreach my $r (@register) {
081def36 70 $self->register_path( $c, $r, $action );
71 $self->register_regex( $c, $r, $action );
b96f127f 72 }
27708fc5 73
694d15f1 74 return 1 if @register;
75 return 0;
b96f127f 76}
77
b5ecfcf0 78=head2 $self->register_regex($c, $re, $action)
081def36 79
80=cut
81
82sub register_regex {
83 my ( $self, $c, $re, $action ) = @_;
84 push(
85 @{ $self->{compiled} }, # and compiled regex for us
86 {
87 re => qr#$re#,
88 action => $action,
89 path => $re,
90 }
91 );
92}
93
2633d7dc 94=head1 AUTHOR
95
96Matt S Trout
97Sebastian Riedel, C<sri@cpan.org>
98
99=head1 COPYRIGHT
100
101This program is free software, you can redistribute it and/or modify it under
102the same terms as Perl itself.
103
104=cut
105
b96f127f 1061;