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