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