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 | |
21 | =cut |
22 | |
23 | sub list { |
24 | my ( $self, $c ) = @_; |
8c113188 |
25 | my $re = Text::SimpleTable->new( [ 36, 'Regex' ], [ 37, 'Private' ] ); |
a9cbd748 |
26 | for my $regex ( @{ $self->{compiled} } ) { |
694d15f1 |
27 | my $action = $regex->{action}; |
dcc61a75 |
28 | $re->row( $regex->{path}, "/$action" ); |
a9cbd748 |
29 | } |
30 | $c->log->debug( "Loaded Regex actions:\n" . $re->draw ) |
8c113188 |
31 | if ( @{ $self->{compiled} } ); |
a9cbd748 |
32 | } |
33 | |
b5ecfcf0 |
34 | =head2 $self->match( $c, $path ) |
2633d7dc |
35 | |
36 | =cut |
37 | |
38 | sub match { |
39 | my ( $self, $c, $path ) = @_; |
40 | |
41 | return if $self->SUPER::match( $c, $path ); |
42 | |
43 | # Check path against plain text first |
44 | |
45 | foreach my $compiled ( @{ $self->{compiled} || [] } ) { |
b96f127f |
46 | if ( my @snippets = ( $path =~ $compiled->{re} ) ) { |
4082e678 |
47 | next unless $compiled->{action}->match($c); |
2633d7dc |
48 | $c->req->action( $compiled->{path} ); |
b96f127f |
49 | $c->req->match($path); |
2633d7dc |
50 | $c->req->snippets( \@snippets ); |
51 | $c->action( $compiled->{action} ); |
11bd4e3e |
52 | $c->namespace( $compiled->{action}->namespace ); |
b96f127f |
53 | return 1; |
54 | } |
55 | } |
56 | |
57 | return 0; |
58 | } |
59 | |
b5ecfcf0 |
60 | =head2 $self->register( $c, $action ) |
2633d7dc |
61 | |
62 | =cut |
63 | |
64 | sub register { |
b96f127f |
65 | my ( $self, $c, $action ) = @_; |
66 | my $attrs = $action->attributes; |
2633d7dc |
67 | my @register = map { @{ $_ || [] } } @{$attrs}{ 'Regex', 'Regexp' }; |
081def36 |
68 | foreach |
69 | my $r ( map { @{ $_ || [] } } @{$attrs}{ 'LocalRegex', 'LocalRegexp' } ) |
70 | { |
71 | unless ( $r =~ s/^\^// ) { $r = "(?:.*?)$r"; } |
72 | push( @register, '^' . $action->namespace . '/' . $r ); |
73 | } |
74 | |
b96f127f |
75 | foreach my $r (@register) { |
081def36 |
76 | $self->register_path( $c, $r, $action ); |
77 | $self->register_regex( $c, $r, $action ); |
b96f127f |
78 | } |
694d15f1 |
79 | return 1 if @register; |
80 | return 0; |
b96f127f |
81 | } |
82 | |
b5ecfcf0 |
83 | =head2 $self->register_regex($c, $re, $action) |
081def36 |
84 | |
85 | =cut |
86 | |
87 | sub register_regex { |
88 | my ( $self, $c, $re, $action ) = @_; |
89 | push( |
90 | @{ $self->{compiled} }, # and compiled regex for us |
91 | { |
92 | re => qr#$re#, |
93 | action => $action, |
94 | path => $re, |
95 | } |
96 | ); |
97 | } |
98 | |
2633d7dc |
99 | =head1 AUTHOR |
100 | |
101 | Matt S Trout |
102 | Sebastian Riedel, C<sri@cpan.org> |
103 | |
104 | =head1 COPYRIGHT |
105 | |
106 | This program is free software, you can redistribute it and/or modify it under |
107 | the same terms as Perl itself. |
108 | |
109 | =cut |
110 | |
b96f127f |
111 | 1; |