s/snippets/captures/
[catagits/Catalyst-Runtime.git] / lib / Catalyst / DispatchType / Regex.pm
1 package Catalyst::DispatchType::Regex;
2
3 use strict;
4 use base qw/Catalyst::DispatchType::Path/;
5 use Text::SimpleTable;
6
7 =head1 NAME
8
9 Catalyst::DispatchType::Regex - Regex DispatchType
10
11 =head1 SYNOPSIS
12
13 See L<Catalyst>.
14
15 =head1 DESCRIPTION
16
17 =head1 METHODS
18
19 =head2 $self->list($c)
20
21 Output a table of all regex actions, and their private equivalent.
22
23 =cut
24
25 sub list {
26     my ( $self, $c ) = @_;
27     my $re = Text::SimpleTable->new( [ 36, 'Regex' ], [ 37, 'Private' ] );
28     for my $regex ( @{ $self->{compiled} } ) {
29         my $action = $regex->{action};
30         $re->row( $regex->{path}, "/$action" );
31     }
32     $c->log->debug( "Loaded Regex actions:\n" . $re->draw )
33       if ( @{ $self->{compiled} } );
34 }
35
36 =head2 $self->match( $c, $path )
37
38 Check path against compiled regexes, and set action to any matching
39 action. Returns 1 on success and 0 on failure.
40
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} || [] } ) {
51         if ( my @captures = ( $path =~ $compiled->{re} ) ) {
52             next unless $compiled->{action}->match($c);
53             $c->req->action( $compiled->{path} );
54             $c->req->match($path);
55             $c->req->captures( \@captures );
56             $c->action( $compiled->{action} );
57             $c->namespace( $compiled->{action}->namespace );
58             return 1;
59         }
60     }
61
62     return 0;
63 }
64
65 =head2 $self->register( $c, $action )
66
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
72 =cut
73
74 sub register {
75     my ( $self, $c, $action ) = @_;
76     my $attrs = $action->attributes;
77     my @register = @{ $attrs->{'Regex'} || [] };
78
79     foreach my $r (@register) {
80         $self->register_path( $c, $r, $action );
81         $self->register_regex( $c, $r, $action );
82     }
83
84     return 1 if @register;
85     return 0;
86 }
87
88 =head2 $self->register_regex($c, $re, $action)
89
90 Register an individual regex on the action. Usually called from the 
91 register action.
92
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
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
119 1;