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