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