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; |
b96f127f |
6 | |
2633d7dc |
7 | =head1 NAME |
b96f127f |
8 | |
2633d7dc |
9 | Catalyst::DispatchType::Regex - Regex DispatchType |
b96f127f |
10 | |
2633d7dc |
11 | =head1 SYNOPSIS |
12 | |
13 | See L<Catalyst>. |
14 | |
15 | =head1 DESCRIPTION |
16 | |
17 | =head1 METHODS |
18 | |
b5ecfcf0 |
19 | =head2 $self->list($c) |
a9cbd748 |
20 | |
4ab87e27 |
21 | Output a table of all regex actions, and their private equivalent. |
22 | |
a9cbd748 |
23 | =cut |
24 | |
25 | sub list { |
26 | my ( $self, $c ) = @_; |
34d28dfd |
27 | my $re = Text::SimpleTable->new( [ 35, 'Regex' ], [ 36, 'Private' ] ); |
a9cbd748 |
28 | for my $regex ( @{ $self->{compiled} } ) { |
694d15f1 |
29 | my $action = $regex->{action}; |
dcc61a75 |
30 | $re->row( $regex->{path}, "/$action" ); |
a9cbd748 |
31 | } |
32 | $c->log->debug( "Loaded Regex actions:\n" . $re->draw ) |
8c113188 |
33 | if ( @{ $self->{compiled} } ); |
a9cbd748 |
34 | } |
35 | |
b5ecfcf0 |
36 | =head2 $self->match( $c, $path ) |
2633d7dc |
37 | |
4ab87e27 |
38 | Check path against compiled regexes, and set action to any matching |
39 | action. Returns 1 on success and 0 on failure. |
40 | |
2633d7dc |
41 | =cut |
42 | |
43 | sub match { |
44 | my ( $self, $c, $path ) = @_; |
45 | |
46 | return if $self->SUPER::match( $c, $path ); |
47 | |
48 | # Check path against plain text first |
49 | |
50 | foreach my $compiled ( @{ $self->{compiled} || [] } ) { |
2982e768 |
51 | if ( my @captures = ( $path =~ $compiled->{re} ) ) { |
4082e678 |
52 | next unless $compiled->{action}->match($c); |
2633d7dc |
53 | $c->req->action( $compiled->{path} ); |
b96f127f |
54 | $c->req->match($path); |
2982e768 |
55 | $c->req->captures( \@captures ); |
2633d7dc |
56 | $c->action( $compiled->{action} ); |
11bd4e3e |
57 | $c->namespace( $compiled->{action}->namespace ); |
b96f127f |
58 | return 1; |
59 | } |
60 | } |
61 | |
62 | return 0; |
63 | } |
64 | |
b5ecfcf0 |
65 | =head2 $self->register( $c, $action ) |
2633d7dc |
66 | |
4ab87e27 |
67 | Registers one or more regex actions for an action object.\ |
68 | Also registers them as literal paths. |
69 | |
70 | Returns 1 on if any regexps were registered. |
71 | |
2633d7dc |
72 | =cut |
73 | |
74 | sub register { |
b96f127f |
75 | my ( $self, $c, $action ) = @_; |
34d28dfd |
76 | my $attrs = $action->attributes; |
27708fc5 |
77 | my @register = @{ $attrs->{'Regex'} || [] }; |
081def36 |
78 | |
b96f127f |
79 | foreach my $r (@register) { |
081def36 |
80 | $self->register_path( $c, $r, $action ); |
81 | $self->register_regex( $c, $r, $action ); |
b96f127f |
82 | } |
27708fc5 |
83 | |
694d15f1 |
84 | return 1 if @register; |
85 | return 0; |
b96f127f |
86 | } |
87 | |
b5ecfcf0 |
88 | =head2 $self->register_regex($c, $re, $action) |
081def36 |
89 | |
4ab87e27 |
90 | Register an individual regex on the action. Usually called from the |
91 | register action. |
92 | |
081def36 |
93 | =cut |
94 | |
95 | sub register_regex { |
96 | my ( $self, $c, $re, $action ) = @_; |
97 | push( |
98 | @{ $self->{compiled} }, # and compiled regex for us |
99 | { |
100 | re => qr#$re#, |
101 | action => $action, |
102 | path => $re, |
103 | } |
104 | ); |
105 | } |
106 | |
2633d7dc |
107 | =head1 AUTHOR |
108 | |
109 | Matt S Trout |
110 | Sebastian Riedel, C<sri@cpan.org> |
111 | |
112 | =head1 COPYRIGHT |
113 | |
114 | This program is free software, you can redistribute it and/or modify it under |
115 | the same terms as Perl itself. |
116 | |
117 | =cut |
118 | |
b96f127f |
119 | 1; |