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 Check path against compiled regexes, and set action to any matching
40 action. Returns 1 on success and 0 on failure.
45 my ( $self, $c, $path ) = @_;
47 return if $self->SUPER::match( $c, $path );
49 # Check path against plain text first
51 foreach my $compiled ( @{ $self->{compiled} || [] } ) {
52 if ( my @captures = ( $path =~ $compiled->{re} ) ) {
53 next unless $compiled->{action}->match($c);
54 $c->req->action( $compiled->{path} );
55 $c->req->match($path);
56 $c->req->captures( \@captures );
57 $c->action( $compiled->{action} );
58 $c->namespace( $compiled->{action}->namespace );
66 =head2 $self->register( $c, $action )
68 Registers one or more regex actions for an action object.\
69 Also registers them as literal paths.
71 Returns 1 on if any regexps were registered.
76 my ( $self, $c, $action ) = @_;
77 my $attrs = $action->attributes;
78 my @register = @{ $attrs->{'Regex'} || [] };
80 foreach my $r (@register) {
81 $self->register_path( $c, $r, $action );
82 $self->register_regex( $c, $r, $action );
85 return 1 if @register;
89 =head2 $self->register_regex($c, $re, $action)
91 Register an individual regex on the action. Usually called from the
97 my ( $self, $c, $re, $action ) = @_;
99 @{ $self->{compiled} }, # and compiled regex for us
108 =head2 $self->uri_for_action($action, $captures)
110 returns a URI for this action if it can find a regex attributes that contains
111 the correct number of () captures. Note that this may function incorrectly
112 in the case of nested captures - if your regex does (...(..))..(..) you'll
113 need to pass the first and third captures only.
118 my ( $self, $action, $captures ) = @_;
120 if (my $regexes = $action->attributes->{Regex}) {
121 REGEX: foreach my $orig (@$regexes) {
126 my @captures = @$captures;
127 while (my ($front, $rest) = split(/\(/, $re, 2)) {
129 Text::Balanced::extract_bracketed("(${rest}", '(');
130 next REGEX unless @captures;
131 $final .= $front.shift(@captures);
133 next REGEX if @captures;
143 Sebastian Riedel, C<sri@cpan.org>
147 This program is free software, you can redistribute it and/or modify it under
148 the same terms as Perl itself.