Reverted restarter change, until some fixes are made
[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/;
a9cbd748 5use Text::ASCIITable;
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
19=over 4
20
a9cbd748 21=item $self->list($c)
22
23=cut
24
25sub list {
26 my ( $self, $c ) = @_;
27 my $re = Text::ASCIITable->new;
28 $re->setCols( 'Regex', 'Private' );
29 $re->setColWidth( 'Regex', 36, 1 );
30 $re->setColWidth( 'Private', 37, 1 );
31 for my $regex ( @{ $self->{compiled} } ) {
32 my $compiled = $regex->{re};
33 my $action = $regex->{action};
34 $re->addRow( $compiled, "/$action" );
35 }
36 $c->log->debug( "Loaded Regex actions:\n" . $re->draw )
37 if ( @{ $re->{tbl_rows} } );
38}
39
2633d7dc 40=item $self->match( $c, $path )
41
42=cut
43
44sub match {
45 my ( $self, $c, $path ) = @_;
46
47 return if $self->SUPER::match( $c, $path );
48
49 # Check path against plain text first
50
51 foreach my $compiled ( @{ $self->{compiled} || [] } ) {
b96f127f 52 if ( my @snippets = ( $path =~ $compiled->{re} ) ) {
2633d7dc 53 $c->req->action( $compiled->{path} );
b96f127f 54 $c->req->match($path);
2633d7dc 55 $c->req->snippets( \@snippets );
56 $c->action( $compiled->{action} );
57 $c->namespace( $compiled->{action}->prefix );
b96f127f 58 return 1;
59 }
60 }
61
62 return 0;
63}
64
2633d7dc 65=item $self->register( $c, $action )
66
67=cut
68
69sub register {
b96f127f 70 my ( $self, $c, $action ) = @_;
71 my $attrs = $action->attributes;
2633d7dc 72 my @register = map { @{ $_ || [] } } @{$attrs}{ 'Regex', 'Regexp' };
b96f127f 73 foreach my $r (@register) {
2633d7dc 74 $self->{paths}{$r} = $action; # Register path for superclass
75 push(
76 @{ $self->{compiled} }, # and compiled regex for us
b96f127f 77 {
2633d7dc 78 re => qr#$r#,
b96f127f 79 action => $action,
2633d7dc 80 path => $r,
81 }
82 );
b96f127f 83 }
84}
85
2633d7dc 86=back
87
88=head1 AUTHOR
89
90Matt S Trout
91Sebastian Riedel, C<sri@cpan.org>
92
93=head1 COPYRIGHT
94
95This program is free software, you can redistribute it and/or modify it under
96the same terms as Perl itself.
97
98=cut
99
b96f127f 1001;