1 package Catalyst::DispatchType::Regex;
5 extends 'Catalyst::DispatchType::Path';
21 Catalyst::DispatchType::Regex - Regex DispatchType
31 =head2 $self->list($c)
33 Output a table of all regex actions, and their private equivalent.
38 my ( $self, $c ) = @_;
39 my $re = Text::SimpleTable->new( [ 35, 'Regex' ], [ 36, 'Private' ] );
40 for my $regex ( @{ $self->_compiled } ) {
41 my $action = $regex->{action};
42 $re->row( $regex->{path}, "/$action" );
44 $c->log->debug( "Loaded Regex actions:\n" . $re->draw . "\n" )
45 if ( @{ $self->_compiled } );
48 =head2 $self->match( $c, $path )
50 Checks path against every compiled regex, and offers the action for any regex
51 which matches a chance to match the request. If it succeeds, sets action,
52 match and captures on $c->req and returns 1. If not, returns 0 without
58 my ( $self, $c, $path ) = @_;
60 return if $self->SUPER::match( $c, $path );
62 # Check path against plain text first
64 foreach my $compiled ( @{ $self->_compiled } ) {
65 if ( my @captures = ( $path =~ $compiled->{re} ) ) {
66 next unless $compiled->{action}->match($c);
67 $c->req->action( $compiled->{path} );
68 $c->req->match($path);
69 $c->req->captures( \@captures );
70 $c->action( $compiled->{action} );
71 $c->namespace( $compiled->{action}->namespace );
79 =head2 $self->register( $c, $action )
81 Registers one or more regex actions for an action object.
82 Also registers them as literal paths.
84 Returns 1 if any regexps were registered.
89 my ( $self, $c, $action ) = @_;
90 my $attrs = $action->attributes;
91 my @register = @{ $attrs->{'Regex'} || [] };
93 foreach my $r (@register) {
94 $self->register_path( $c, $r, $action );
95 $self->register_regex( $c, $r, $action );
98 return 1 if @register;
102 =head2 $self->register_regex($c, $re, $action)
104 Register an individual regex on the action. Usually called from the
110 my ( $self, $c, $re, $action ) = @_;
112 @{ $self->_compiled }, # and compiled regex for us
121 =head2 $self->uri_for_action($action, $captures)
123 returns a URI for this action if it can find a regex attributes that contains
124 the correct number of () captures. Note that this may function incorrectly
125 in the case of nested captures - if your regex does (...(..))..(..) you'll
126 need to pass the first and third captures only.
131 my ( $self, $action, $captures ) = @_;
133 if (my $regexes = $action->attributes->{Regex}) {
134 REGEX: foreach my $orig (@$regexes) {
139 my @captures = @$captures;
140 while (my ($front, $rest) = split(/\(/, $re, 2)) {
142 Text::Balanced::extract_bracketed("(${rest}", '(');
143 next REGEX unless @captures;
144 $final .= $front.shift(@captures);
146 next REGEX if @captures;
156 Sebastian Riedel, C<sri@cpan.org>
160 This program is free software, you can redistribute it and/or modify it under
161 the same terms as Perl itself.
165 __PACKAGE__->meta->make_immutable;