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