1 package Catalyst::DispatchType::Regex;
4 use base qw/Catalyst::DispatchType::Path/;
11 Catalyst::DispatchType::Regex - Regex DispatchType
21 =head2 $self->list($c)
23 Output a table of all regex actions, and their private equivalent.
28 my ( $self, $c ) = @_;
29 my $column_width = Catalyst::Utils::term_width() - 35 - 9;
30 my $re = Text::SimpleTable->new( [ 35, 'Regex' ], [ $column_width, 'Private' ] );
31 for my $regex ( @{ $self->{compiled} } ) {
32 my $action = $regex->{action};
33 $re->row( $regex->{path}, "/$action" );
35 $c->log->debug( "Loaded Regex actions:\n" . $re->draw . "\n" )
36 if ( @{ $self->{compiled} } );
39 =head2 $self->match( $c, $path )
41 Checks path against every compiled regex, and offers the action for any regex
42 which matches a chance to match the request. If it succeeds, sets action,
43 match and captures on $c->req and returns 1. If not, returns 0 without
49 my ( $self, $c, $path ) = @_;
51 return if $self->SUPER::match( $c, $path );
53 # Check path against plain text first
55 foreach my $compiled ( @{ $self->{compiled} || [] } ) {
56 if ( my @captures = ( $path =~ $compiled->{re} ) ) {
57 next unless $compiled->{action}->match($c);
58 $c->req->action( $compiled->{path} );
59 $c->req->match($path);
60 $c->req->captures( \@captures );
61 $c->action( $compiled->{action} );
62 $c->namespace( $compiled->{action}->namespace );
70 =head2 $self->register( $c, $action )
72 Registers one or more regex actions for an action object.
73 Also registers them as literal paths.
75 Returns 1 if any regexps were registered.
80 my ( $self, $c, $action ) = @_;
81 my $attrs = $action->attributes;
82 my @register = @{ $attrs->{'Regex'} || [] };
84 foreach my $r (@register) {
85 $self->register_path( $c, $r, $action );
86 $self->register_regex( $c, $r, $action );
89 return 1 if @register;
93 =head2 $self->register_regex($c, $re, $action)
95 Register an individual regex on the action. Usually called from the
101 my ( $self, $c, $re, $action ) = @_;
103 @{ $self->{compiled} }, # and compiled regex for us
112 =head2 $self->uri_for_action($action, $captures)
114 returns a URI for this action if it can find a regex attributes that contains
115 the correct number of () captures. Note that this may function incorrectly
116 in the case of nested captures - if your regex does (...(..))..(..) you'll
117 need to pass the first and third captures only.
122 my ( $self, $action, $captures ) = @_;
124 if (my $regexes = $action->attributes->{Regex}) {
125 REGEX: foreach my $orig (@$regexes) {
130 my @captures = @$captures;
131 while (my ($front, $rest) = split(/\(/, $re, 2)) {
133 Text::Balanced::extract_bracketed("(${rest}", '(');
134 next REGEX unless @captures;
135 $final .= $front.shift(@captures);
137 next REGEX if @captures;
146 Catalyst Contributors, see Catalyst.pm
150 This program is free software, you can redistribute it and/or modify it under
151 the same terms as Perl itself.