1 package Catalyst::DispatchType::Regex;
4 extends 'Catalyst::DispatchType::Path';
20 Catalyst::DispatchType::Regex - Regex DispatchType
30 =head2 $self->list($c)
32 Output a table of all regex actions, and their private equivalent.
37 my ( $self, $c ) = @_;
38 my $re = Text::SimpleTable->new( [ 35, 'Regex' ], [ 36, 'Private' ] );
39 for my $regex ( @{ $self->_compiled } ) {
40 my $action = $regex->{action};
41 $re->row( $regex->{path}, "/$action" );
43 $c->log->debug( "Loaded Regex actions:\n" . $re->draw . "\n" )
44 if ( @{ $self->_compiled } );
47 =head2 $self->match( $c, $path )
49 Checks path against every compiled regex, and offers the action for any regex
50 which matches a chance to match the request. If it succeeds, sets action,
51 match and captures on $c->req and returns 1. If not, returns 0 without
57 my ( $self, $c, $path ) = @_;
59 return if $self->SUPER::match( $c, $path );
61 # Check path against plain text first
63 foreach my $compiled ( @{ $self->_compiled } ) {
64 if ( my @captures = ( $path =~ $compiled->{re} ) ) {
65 next unless $compiled->{action}->match($c);
66 $c->req->action( $compiled->{path} );
67 $c->req->match($path);
68 $c->req->captures( \@captures );
69 $c->action( $compiled->{action} );
70 $c->namespace( $compiled->{action}->namespace );
78 =head2 $self->register( $c, $action )
80 Registers one or more regex actions for an action object.
81 Also registers them as literal paths.
83 Returns 1 if any regexps were registered.
88 my ( $self, $c, $action ) = @_;
89 my $attrs = $action->attributes;
90 my @register = @{ $attrs->{'Regex'} || [] };
92 foreach my $r (@register) {
93 $self->register_path( $c, $r, $action );
94 $self->register_regex( $c, $r, $action );
97 return 1 if @register;
101 =head2 $self->register_regex($c, $re, $action)
103 Register an individual regex on the action. Usually called from the
109 my ( $self, $c, $re, $action ) = @_;
111 @{ $self->_compiled }, # and compiled regex for us
120 =head2 $self->uri_for_action($action, $captures)
122 returns a URI for this action if it can find a regex attributes that contains
123 the correct number of () captures. Note that this may function incorrectly
124 in the case of nested captures - if your regex does (...(..))..(..) you'll
125 need to pass the first and third captures only.
130 my ( $self, $action, $captures ) = @_;
132 if (my $regexes = $action->attributes->{Regex}) {
133 REGEX: foreach my $orig (@$regexes) {
138 my @captures = @$captures;
139 while (my ($front, $rest) = split(/\(/, $re, 2)) {
141 Text::Balanced::extract_bracketed("(${rest}", '(');
142 next REGEX unless @captures;
143 $final .= $front.shift(@captures);
145 next REGEX if @captures;
154 Catalyst Contributors, see Catalyst.pm
158 This program is free software, you can redistribute it and/or modify it under
159 the same terms as Perl itself.
163 __PACKAGE__->meta->make_immutable;