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