1 package Catalyst::DispatchType::Regex;
4 use base qw/Catalyst::DispatchType::Path/;
10 Catalyst::DispatchType::Regex - Regex DispatchType
20 =head2 $self->list($c)
22 Output a table of all regex actions, and their private equivalent.
27 my ( $self, $c ) = @_;
28 my $re = Text::SimpleTable->new( [ 35, 'Regex' ], [ 36, 'Private' ] );
29 for my $regex ( @{ $self->{compiled} } ) {
30 my $action = $regex->{action};
31 $re->row( $regex->{path}, "/$action" );
33 $c->log->debug( "Loaded Regex actions:\n" . $re->draw )
34 if ( @{ $self->{compiled} } );
37 =head2 $self->match( $c, $path )
39 Checks path against every compiled regex, and offers the action for any regex
40 which matches a chance to match the request. If it succeeds, sets action,
41 match and captures on $c->req and returns 1. If not, returns 0 without
47 my ( $self, $c, $path ) = @_;
49 return if $self->SUPER::match( $c, $path );
51 # Check path against plain text first
53 foreach my $compiled ( @{ $self->{compiled} || [] } ) {
54 if ( my @captures = ( $path =~ $compiled->{re} ) ) {
55 next unless $compiled->{action}->match($c);
56 $c->req->action( $compiled->{path} );
57 $c->req->match($path);
58 $c->req->captures( \@captures );
59 $c->action( $compiled->{action} );
60 $c->namespace( $compiled->{action}->namespace );
68 =head2 $self->register( $c, $action )
70 Registers one or more regex actions for an action object.
71 Also registers them as literal paths.
73 Returns 1 if any regexps were registered.
78 my ( $self, $c, $action ) = @_;
79 my $attrs = $action->attributes;
80 my @register = @{ $attrs->{'Regex'} || [] };
82 foreach my $r (@register) {
83 $self->register_path( $c, $r, $action );
84 $self->register_regex( $c, $r, $action );
87 return 1 if @register;
91 =head2 $self->register_regex($c, $re, $action)
93 Register an individual regex on the action. Usually called from the
99 my ( $self, $c, $re, $action ) = @_;
101 @{ $self->{compiled} }, # and compiled regex for us
110 =head2 $self->uri_for_action($action, $captures)
112 returns a URI for this action if it can find a regex attributes that contains
113 the correct number of () captures. Note that this may function incorrectly
114 in the case of nested captures - if your regex does (...(..))..(..) you'll
115 need to pass the first and third captures only.
120 my ( $self, $action, $captures ) = @_;
122 if (my $regexes = $action->attributes->{Regex}) {
123 REGEX: foreach my $orig (@$regexes) {
128 my @captures = @$captures;
129 while (my ($front, $rest) = split(/\(/, $re, 2)) {
131 Text::Balanced::extract_bracketed("(${rest}", '(');
132 next REGEX unless @captures;
133 $final .= $front.shift(@captures);
135 next REGEX if @captures;
145 Sebastian Riedel, C<sri@cpan.org>
149 This program is free software, you can redistribute it and/or modify it under
150 the same terms as Perl itself.