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