Create branch register_actions.
[catagits/Catalyst-Runtime.git] / lib / Catalyst / DispatchType / Regex.pm
CommitLineData
b96f127f 1package Catalyst::DispatchType::Regex;
2
ae29b412 3use Moose;
4extends 'Catalyst::DispatchType::Path';
5
8c113188 6use Text::SimpleTable;
e73e3bad 7use Catalyst::Utils;
ea0e58d9 8use Text::Balanced ();
b96f127f 9
ae29b412 10has _compiled => (
11 is => 'rw',
12 isa => 'ArrayRef',
13 required => 1,
14 default => sub{ [] },
15 );
16
17no Moose;
18
2633d7dc 19=head1 NAME
b96f127f 20
2633d7dc 21Catalyst::DispatchType::Regex - Regex DispatchType
b96f127f 22
2633d7dc 23=head1 SYNOPSIS
24
25See L<Catalyst>.
26
27=head1 DESCRIPTION
28
29=head1 METHODS
30
b5ecfcf0 31=head2 $self->list($c)
a9cbd748 32
4ab87e27 33Output a table of all regex actions, and their private equivalent.
34
a9cbd748 35=cut
36
37sub list {
38 my ( $self, $c ) = @_;
e73e3bad 39 my $column_width = Catalyst::Utils::term_width() - 35 - 9;
40 my $re = Text::SimpleTable->new( [ 35, 'Regex' ], [ $column_width, 'Private' ] );
ae29b412 41 for my $regex ( @{ $self->_compiled } ) {
694d15f1 42 my $action = $regex->{action};
dcc61a75 43 $re->row( $regex->{path}, "/$action" );
a9cbd748 44 }
1cf0345b 45 $c->log->debug( "Loaded Regex actions:\n" . $re->draw . "\n" )
ae29b412 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
58sub match {
59 my ( $self, $c, $path ) = @_;
60
61 return if $self->SUPER::match( $c, $path );
62
63 # Check path against plain text first
64
ae29b412 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;
78}
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
ae29b412 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(
ae29b412 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
0bf7ab71 154=head1 AUTHORS
2633d7dc 155
0bf7ab71 156Catalyst Contributors, see Catalyst.pm
2633d7dc 157
158=head1 COPYRIGHT
159
160This program is free software, you can redistribute it and/or modify it under
161the same terms as Perl itself.
162
163=cut
164
ae29b412 165__PACKAGE__->meta->make_immutable;
166
b96f127f 1671;