make debug output prettier with large widths
[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
8c113188 6use Text::SimpleTable;
39fc2ce1 7use Catalyst::Utils;
ea0e58d9 8use Text::Balanced ();
b96f127f 9
e8b9f2a9 10has _compiled => (
11 is => 'rw',
12 isa => 'ArrayRef',
13 required => 1,
14 default => sub{ [] },
15 );
3c0186f2 16
0fc2d522 17no Moose;
18
2633d7dc 19=head1 NAME
b96f127f 20
2633d7dc 21Catalyst::DispatchType::Regex - Regex DispatchType
b96f127f 22
2633d7dc 23=head1 SYNOPSIS
24
26dc649a 25See L<Catalyst::DispatchType>.
2633d7dc 26
27=head1 DESCRIPTION
28
26dc649a 29Dispatch type managing path-matching behaviour using regexes. For
30more information on dispatch types, see:
31
32=over 4
33
b9b89145 34=item * L<Catalyst::Manual::Intro> for how they affect application authors
26dc649a 35
36=item * L<Catalyst::DispatchType> for implementation information.
37
38=back
39
2633d7dc 40=head1 METHODS
41
b5ecfcf0 42=head2 $self->list($c)
a9cbd748 43
4ab87e27 44Output a table of all regex actions, and their private equivalent.
45
a9cbd748 46=cut
47
48sub list {
49 my ( $self, $c ) = @_;
48d435ba 50 my $avail_width = Catalyst::Utils::term_width() - 9;
51 my $col1_width = ($avail_width * .50) < 35 ? 35 : int($avail_width * .50);
52 my $col2_width = $avail_width - $col1_width;
53 my $re = Text::SimpleTable->new(
54 [ $col1_width, 'Regex' ], [ $col2_width, 'Private' ]
55 );
e8b9f2a9 56 for my $regex ( @{ $self->_compiled } ) {
694d15f1 57 my $action = $regex->{action};
dcc61a75 58 $re->row( $regex->{path}, "/$action" );
a9cbd748 59 }
e8b9f2a9 60 $c->log->debug( "Loaded Regex actions:\n" . $re->draw . "\n" )
61 if ( @{ $self->_compiled } );
a9cbd748 62}
63
b5ecfcf0 64=head2 $self->match( $c, $path )
2633d7dc 65
b2b90ec2 66Checks path against every compiled regex, and offers the action for any regex
67which matches a chance to match the request. If it succeeds, sets action,
68match and captures on $c->req and returns 1. If not, returns 0 without
69altering $c.
4ab87e27 70
2633d7dc 71=cut
72
e8b9f2a9 73sub match {
2633d7dc 74 my ( $self, $c, $path ) = @_;
75
e8b9f2a9 76 return if $self->SUPER::match( $c, $path );
2633d7dc 77
78 # Check path against plain text first
79
3c0186f2 80 foreach my $compiled ( @{ $self->_compiled } ) {
2982e768 81 if ( my @captures = ( $path =~ $compiled->{re} ) ) {
4082e678 82 next unless $compiled->{action}->match($c);
2633d7dc 83 $c->req->action( $compiled->{path} );
b96f127f 84 $c->req->match($path);
2982e768 85 $c->req->captures( \@captures );
2633d7dc 86 $c->action( $compiled->{action} );
11bd4e3e 87 $c->namespace( $compiled->{action}->namespace );
b96f127f 88 return 1;
89 }
90 }
91
92 return 0;
e8b9f2a9 93}
b96f127f 94
b5ecfcf0 95=head2 $self->register( $c, $action )
2633d7dc 96
b2b90ec2 97Registers one or more regex actions for an action object.
4ab87e27 98Also registers them as literal paths.
99
b2b90ec2 100Returns 1 if any regexps were registered.
4ab87e27 101
2633d7dc 102=cut
103
104sub register {
b96f127f 105 my ( $self, $c, $action ) = @_;
34d28dfd 106 my $attrs = $action->attributes;
27708fc5 107 my @register = @{ $attrs->{'Regex'} || [] };
081def36 108
b96f127f 109 foreach my $r (@register) {
081def36 110 $self->register_path( $c, $r, $action );
111 $self->register_regex( $c, $r, $action );
b96f127f 112 }
27708fc5 113
694d15f1 114 return 1 if @register;
115 return 0;
b96f127f 116}
117
b5ecfcf0 118=head2 $self->register_regex($c, $re, $action)
081def36 119
3c0186f2 120Register an individual regex on the action. Usually called from the
b2b90ec2 121register method.
4ab87e27 122
081def36 123=cut
124
125sub register_regex {
126 my ( $self, $c, $re, $action ) = @_;
127 push(
3c0186f2 128 @{ $self->_compiled }, # and compiled regex for us
081def36 129 {
130 re => qr#$re#,
131 action => $action,
132 path => $re,
133 }
134 );
135}
136
ea0e58d9 137=head2 $self->uri_for_action($action, $captures)
138
139returns a URI for this action if it can find a regex attributes that contains
140the correct number of () captures. Note that this may function incorrectly
141in the case of nested captures - if your regex does (...(..))..(..) you'll
142need to pass the first and third captures only.
143
144=cut
145
146sub uri_for_action {
147 my ( $self, $action, $captures ) = @_;
148
149 if (my $regexes = $action->attributes->{Regex}) {
150 REGEX: foreach my $orig (@$regexes) {
151 my $re = "$orig";
152 $re =~ s/^\^//;
153 $re =~ s/\$$//;
154 my $final = '/';
f9155483 155 my @captures = @$captures;
ea0e58d9 156 while (my ($front, $rest) = split(/\(/, $re, 2)) {
873b98ee 157 last unless defined $rest;
ea0e58d9 158 ($rest, $re) =
159 Text::Balanced::extract_bracketed("(${rest}", '(');
160 next REGEX unless @captures;
161 $final .= $front.shift(@captures);
162 }
873b98ee 163 $final .= $re;
ea0e58d9 164 next REGEX if @captures;
165 return $final;
166 }
167 }
168 return undef;
169}
170
2f381252 171=head1 AUTHORS
2633d7dc 172
2f381252 173Catalyst Contributors, see Catalyst.pm
2633d7dc 174
175=head1 COPYRIGHT
176
536bee89 177This library is free software. You can redistribute it and/or modify it under
2633d7dc 178the same terms as Perl itself.
179
180=cut
181
e5ecd5bc 182__PACKAGE__->meta->make_immutable;
183
b96f127f 1841;