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