1 package Catalyst::DispatchType::Regex;
4 extends 'Catalyst::DispatchType::Path';
8 has _compiled => (is => 'rw', isa => 'ArrayRef', required => 1, default => sub{[]});
12 Catalyst::DispatchType::Regex - Regex DispatchType
22 =head2 $self->list($c)
24 Output a table of all regex actions, and their private equivalent.
29 my ( $self, $c ) = @_;
30 my @regexes = @{ $self->_compiled };
31 return unless @regexes;
32 my $re = Text::SimpleTable->new( [ 35, 'Regex' ], [ 36, 'Private' ] );
33 for my $regex ( @regexes ) {
34 my $action = $regex->{action};
35 $re->row( $regex->{path}, "/$action" );
37 $c->log->debug( "Loaded Regex actions:\n" . $re->draw . "\n" );
40 =head2 $self->match( $c, $path )
42 Checks path against every compiled regex, and offers the action for any regex
43 which matches a chance to match the request. If it succeeds, sets action,
44 match and captures on $c->req and returns 1. If not, returns 0 without
49 override match => sub {
50 my ( $self, $c, $path ) = @_;
54 # Check path against plain text first
56 foreach my $compiled ( @{ $self->_compiled } ) {
57 if ( my @captures = ( $path =~ $compiled->{re} ) ) {
58 next unless $compiled->{action}->match($c);
59 $c->req->action( $compiled->{path} );
60 $c->req->match($path);
61 $c->req->captures( \@captures );
62 $c->action( $compiled->{action} );
63 $c->namespace( $compiled->{action}->namespace );
71 =head2 $self->register( $c, $action )
73 Registers one or more regex actions for an action object.
74 Also registers them as literal paths.
76 Returns 1 if any regexps were registered.
81 my ( $self, $c, $action ) = @_;
82 my $attrs = $action->attributes;
83 my @register = @{ $attrs->{'Regex'} || [] };
85 foreach my $r (@register) {
86 $self->register_path( $c, $r, $action );
87 $self->register_regex( $c, $r, $action );
90 return 1 if @register;
94 =head2 $self->register_regex($c, $re, $action)
96 Register an individual regex on the action. Usually called from the
102 my ( $self, $c, $re, $action ) = @_;
104 @{ $self->_compiled }, # and compiled regex for us
113 =head2 $self->uri_for_action($action, $captures)
115 returns a URI for this action if it can find a regex attributes that contains
116 the correct number of () captures. Note that this may function incorrectly
117 in the case of nested captures - if your regex does (...(..))..(..) you'll
118 need to pass the first and third captures only.
123 my ( $self, $action, $captures ) = @_;
125 if (my $regexes = $action->attributes->{Regex}) {
126 REGEX: foreach my $orig (@$regexes) {
131 my @captures = @$captures;
132 while (my ($front, $rest) = split(/\(/, $re, 2)) {
134 Text::Balanced::extract_bracketed("(${rest}", '(');
135 next REGEX unless @captures;
136 $final .= $front.shift(@captures);
138 next REGEX if @captures;
148 Sebastian Riedel, C<sri@cpan.org>
152 This program is free software, you can redistribute it and/or modify it under
153 the same terms as Perl itself.