1 package Catalyst::DispatchType::Regex;
4 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 $column_width = Catalyst::Utils::term_width() - 35 - 9;
40 my $re = Text::SimpleTable->new( [ 35, 'Regex' ], [ $column_width, 'Private' ] );
41 for my $regex ( @{ $self->_compiled } ) {
42 my $action = $regex->{action};
43 $re->row( $regex->{path}, "/$action" );
45 $c->log->debug( "Loaded Regex actions:\n" . $re->draw . "\n" )
46 if ( @{ $self->_compiled } );
49 =head2 $self->match( $c, $path )
51 Checks path against every compiled regex, and offers the action for any regex
52 which matches a chance to match the request. If it succeeds, sets action,
53 match and captures on $c->req and returns 1. If not, returns 0 without
59 my ( $self, $c, $path ) = @_;
61 return if $self->SUPER::match( $c, $path );
63 # Check path against plain text first
65 foreach my $compiled ( @{ $self->_compiled } ) {
66 if ( my @captures = ( $path =~ $compiled->{re} ) ) {
67 next unless $compiled->{action}->match($c);
68 $c->req->action( $compiled->{path} );
69 $c->req->match($path);
70 $c->req->captures( \@captures );
71 $c->action( $compiled->{action} );
72 $c->namespace( $compiled->{action}->namespace );
80 =head2 $self->register( $c, $action )
82 Registers one or more regex actions for an action object.
83 Also registers them as literal paths.
85 Returns 1 if any regexps were registered.
90 my ( $self, $c, $action ) = @_;
91 my $attrs = $action->attributes;
92 my @register = @{ $attrs->{'Regex'} || [] };
94 foreach my $r (@register) {
95 $self->register_path( $c, $r, $action );
96 $self->register_regex( $c, $r, $action );
99 return 1 if @register;
103 =head2 $self->register_regex($c, $re, $action)
105 Register an individual regex on the action. Usually called from the
111 my ( $self, $c, $re, $action ) = @_;
113 @{ $self->_compiled }, # and compiled regex for us
122 =head2 $self->uri_for_action($action, $captures)
124 returns a URI for this action if it can find a regex attributes that contains
125 the correct number of () captures. Note that this may function incorrectly
126 in the case of nested captures - if your regex does (...(..))..(..) you'll
127 need to pass the first and third captures only.
132 my ( $self, $action, $captures ) = @_;
134 if (my $regexes = $action->attributes->{Regex}) {
135 REGEX: foreach my $orig (@$regexes) {
140 my @captures = @$captures;
141 while (my ($front, $rest) = split(/\(/, $re, 2)) {
143 Text::Balanced::extract_bracketed("(${rest}", '(');
144 next REGEX unless @captures;
145 $final .= $front.shift(@captures);
147 next REGEX if @captures;
156 Catalyst Contributors, see Catalyst.pm
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;