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