mro compat stuff
[catagits/Catalyst-Runtime.git] / lib / Catalyst / DispatchType / Regex.pm
CommitLineData
b96f127f 1package Catalyst::DispatchType::Regex;
2
6f1f968a 3use MRO::Compat;
4use mro 'c3';
3c0186f2 5use Moose;
6extends 'Catalyst::DispatchType::Path';
e8b9f2a9 7
8c113188 8use Text::SimpleTable;
ea0e58d9 9use Text::Balanced ();
b96f127f 10
e8b9f2a9 11has _compiled => (
12 is => 'rw',
13 isa => 'ArrayRef',
14 required => 1,
15 default => sub{ [] },
16 );
3c0186f2 17
0fc2d522 18no Moose;
19
2633d7dc 20=head1 NAME
b96f127f 21
2633d7dc 22Catalyst::DispatchType::Regex - Regex DispatchType
b96f127f 23
2633d7dc 24=head1 SYNOPSIS
25
26See L<Catalyst>.
27
28=head1 DESCRIPTION
29
30=head1 METHODS
31
b5ecfcf0 32=head2 $self->list($c)
a9cbd748 33
4ab87e27 34Output a table of all regex actions, and their private equivalent.
35
a9cbd748 36=cut
37
38sub list {
39 my ( $self, $c ) = @_;
34d28dfd 40 my $re = Text::SimpleTable->new( [ 35, 'Regex' ], [ 36, 'Private' ] );
e8b9f2a9 41 for my $regex ( @{ $self->_compiled } ) {
694d15f1 42 my $action = $regex->{action};
dcc61a75 43 $re->row( $regex->{path}, "/$action" );
a9cbd748 44 }
e8b9f2a9 45 $c->log->debug( "Loaded Regex actions:\n" . $re->draw . "\n" )
46 if ( @{ $self->_compiled } );
a9cbd748 47}
48
b5ecfcf0 49=head2 $self->match( $c, $path )
2633d7dc 50
b2b90ec2 51Checks path against every compiled regex, and offers the action for any regex
52which matches a chance to match the request. If it succeeds, sets action,
53match and captures on $c->req and returns 1. If not, returns 0 without
54altering $c.
4ab87e27 55
2633d7dc 56=cut
57
e8b9f2a9 58sub match {
2633d7dc 59 my ( $self, $c, $path ) = @_;
60
e8b9f2a9 61 return if $self->SUPER::match( $c, $path );
2633d7dc 62
63 # Check path against plain text first
64
3c0186f2 65 foreach my $compiled ( @{ $self->_compiled } ) {
2982e768 66 if ( my @captures = ( $path =~ $compiled->{re} ) ) {
4082e678 67 next unless $compiled->{action}->match($c);
2633d7dc 68 $c->req->action( $compiled->{path} );
b96f127f 69 $c->req->match($path);
2982e768 70 $c->req->captures( \@captures );
2633d7dc 71 $c->action( $compiled->{action} );
11bd4e3e 72 $c->namespace( $compiled->{action}->namespace );
b96f127f 73 return 1;
74 }
75 }
76
77 return 0;
e8b9f2a9 78}
b96f127f 79
b5ecfcf0 80=head2 $self->register( $c, $action )
2633d7dc 81
b2b90ec2 82Registers one or more regex actions for an action object.
4ab87e27 83Also registers them as literal paths.
84
b2b90ec2 85Returns 1 if any regexps were registered.
4ab87e27 86
2633d7dc 87=cut
88
89sub register {
b96f127f 90 my ( $self, $c, $action ) = @_;
34d28dfd 91 my $attrs = $action->attributes;
27708fc5 92 my @register = @{ $attrs->{'Regex'} || [] };
081def36 93
b96f127f 94 foreach my $r (@register) {
081def36 95 $self->register_path( $c, $r, $action );
96 $self->register_regex( $c, $r, $action );
b96f127f 97 }
27708fc5 98
694d15f1 99 return 1 if @register;
100 return 0;
b96f127f 101}
102
b5ecfcf0 103=head2 $self->register_regex($c, $re, $action)
081def36 104
3c0186f2 105Register an individual regex on the action. Usually called from the
b2b90ec2 106register method.
4ab87e27 107
081def36 108=cut
109
110sub register_regex {
111 my ( $self, $c, $re, $action ) = @_;
112 push(
3c0186f2 113 @{ $self->_compiled }, # and compiled regex for us
081def36 114 {
115 re => qr#$re#,
116 action => $action,
117 path => $re,
118 }
119 );
120}
121
ea0e58d9 122=head2 $self->uri_for_action($action, $captures)
123
124returns a URI for this action if it can find a regex attributes that contains
125the correct number of () captures. Note that this may function incorrectly
126in the case of nested captures - if your regex does (...(..))..(..) you'll
127need to pass the first and third captures only.
128
129=cut
130
131sub uri_for_action {
132 my ( $self, $action, $captures ) = @_;
133
134 if (my $regexes = $action->attributes->{Regex}) {
135 REGEX: foreach my $orig (@$regexes) {
136 my $re = "$orig";
137 $re =~ s/^\^//;
138 $re =~ s/\$$//;
139 my $final = '/';
140 my @captures = @$captures;
141 while (my ($front, $rest) = split(/\(/, $re, 2)) {
142 ($rest, $re) =
143 Text::Balanced::extract_bracketed("(${rest}", '(');
144 next REGEX unless @captures;
145 $final .= $front.shift(@captures);
146 }
147 next REGEX if @captures;
148 return $final;
149 }
150 }
151 return undef;
152}
153
2633d7dc 154=head1 AUTHOR
155
156Matt S Trout
157Sebastian Riedel, C<sri@cpan.org>
158
159=head1 COPYRIGHT
160
161This program is free software, you can redistribute it and/or modify it under
162the same terms as Perl itself.
163
164=cut
165
e5ecd5bc 166__PACKAGE__->meta->make_immutable;
167
b96f127f 1681;