- Fixes for rt.cpan #17322 and #17331
[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} ) ) {
2633d7dc 47 $c->req->action( $compiled->{path} );
b96f127f 48 $c->req->match($path);
2633d7dc 49 $c->req->snippets( \@snippets );
50 $c->action( $compiled->{action} );
11bd4e3e 51 $c->namespace( $compiled->{action}->namespace );
b96f127f 52 return 1;
53 }
54 }
55
56 return 0;
57}
58
b5ecfcf0 59=head2 $self->register( $c, $action )
2633d7dc 60
61=cut
62
63sub register {
b96f127f 64 my ( $self, $c, $action ) = @_;
65 my $attrs = $action->attributes;
2633d7dc 66 my @register = map { @{ $_ || [] } } @{$attrs}{ 'Regex', 'Regexp' };
081def36 67 foreach
68 my $r ( map { @{ $_ || [] } } @{$attrs}{ 'LocalRegex', 'LocalRegexp' } )
69 {
70 unless ( $r =~ s/^\^// ) { $r = "(?:.*?)$r"; }
71 push( @register, '^' . $action->namespace . '/' . $r );
72 }
73
b96f127f 74 foreach my $r (@register) {
081def36 75 $self->register_path( $c, $r, $action );
76 $self->register_regex( $c, $r, $action );
b96f127f 77 }
694d15f1 78 return 1 if @register;
79 return 0;
b96f127f 80}
81
b5ecfcf0 82=head2 $self->register_regex($c, $re, $action)
081def36 83
84=cut
85
86sub register_regex {
87 my ( $self, $c, $re, $action ) = @_;
88 push(
89 @{ $self->{compiled} }, # and compiled regex for us
90 {
91 re => qr#$re#,
92 action => $action,
93 path => $re,
94 }
95 );
96}
97
2633d7dc 98=head1 AUTHOR
99
100Matt S Trout
101Sebastian Riedel, C<sri@cpan.org>
102
103=head1 COPYRIGHT
104
105This program is free software, you can redistribute it and/or modify it under
106the same terms as Perl itself.
107
108=cut
109
b96f127f 1101;