From: Sebastian Riedel Date: Tue, 11 Oct 2005 23:15:02 +0000 (+0000) Subject: Added multiple paths support X-Git-Tag: 5.7099_04~1223 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?p=catagits%2FCatalyst-Runtime.git;a=commitdiff_plain;h=749472d6839708f3c04912c0ed39ce6141d4d500 Added multiple paths support --- diff --git a/Changes b/Changes index d614bbd..5b25601 100644 --- a/Changes +++ b/Changes @@ -1,6 +1,7 @@ Tis file documents the revision history for Perl extension Catalyst. 5.50 + - Added multiple paths support to dispatcher - Fixed bug in req->path where changing the path added a trailing slash. - Removed req->handle and res->handle diff --git a/lib/Catalyst/Dispatcher.pm b/lib/Catalyst/Dispatcher.pm index eb4662c..94e5290 100644 --- a/lib/Catalyst/Dispatcher.pm +++ b/lib/Catalyst/Dispatcher.pm @@ -354,10 +354,12 @@ sub set_action { for my $attr ( @{$attrs} ) { if ( $attr =~ /^(Local|Relative)$/ ) { $flags{local}++ } elsif ( $attr =~ /^(Global|Absolute)$/ ) { $flags{global}++ } - elsif ( $attr =~ /^Path\(\s*(.+)\s*\)$/i ) { $flags{path} = $1 } - elsif ( $attr =~ /^Private$/i ) { $flags{private}++ } + elsif ( $attr =~ /^Path\(\s*(.+)\s*\)$/i ) { + push @{ $flags{path} }, $1; + } + elsif ( $attr =~ /^Private$/i ) { $flags{private}++ } elsif ( $attr =~ /^(Regex|Regexp)\(\s*(.+)\s*\)$/i ) { - $flags{regex} = $2; + push @{ $flags{regex} }, $2; } } @@ -388,8 +390,6 @@ sub set_action { $parent = $child; } - my $forward = $prefix ? "$prefix/$method" : $method; - my $reverse = $prefix ? "$prefix/$method" : $method; my $action = Catalyst::Action->new( @@ -403,35 +403,40 @@ sub set_action { my $uid = $parent->getUID; $self->actions->{private}->{$uid}->{$method} = $action; - if ( $flags{path} ) { - $flags{path} =~ s/^\w+//; - $flags{path} =~ s/\w+$//; - if ( $flags{path} =~ /^\s*'(.*)'\s*$/ ) { $flags{path} = $1 } - if ( $flags{path} =~ /^\s*"(.*)"\s*$/ ) { $flags{path} = $1 } + my @path; + for my $path ( @{ $flags{path} } ) { + $path =~ s/^\w+//; + $path =~ s/\w+$//; + if ( $path =~ /^\s*'(.*)'\s*$/ ) { $path = $1 } + if ( $path =~ /^\s*"(.*)"\s*$/ ) { $path = $1 } + push @path, $path; } - - if ( $flags{regex} ) { - $flags{regex} =~ s/^\w+//; - $flags{regex} =~ s/\w+$//; - if ( $flags{regex} =~ /^\s*'(.*)'\s*$/ ) { $flags{regex} = $1 } - if ( $flags{regex} =~ /^\s*"(.*)"\s*$/ ) { $flags{regex} = $1 } + $flags{path} = \@path; + + my @regex; + for my $regex ( @{ $flags{regex} } ) { + $regex =~ s/^\w+//; + $regex =~ s/\w+$//; + if ( $regex =~ /^\s*'(.*)'\s*$/ ) { $regex = $1 } + if ( $regex =~ /^\s*"(.*)"\s*$/ ) { $regex = $1 } + push @regex, $regex; } + $flags{regex} = \@regex; - if ( $flags{local} || $flags{global} || $flags{path} ) { - my $path = $flags{path} || $method; - my $absolute = 0; + if ( $flags{local} || $flags{global} ) { + push( @{ $flags{path} }, $prefix ? "/$prefix/$method" : "/$method" ) + if $flags{local}; - if ( $path =~ /^\/(.+)/ ) { - $path = $1; - $absolute = 1; - } + push( @{ $flags{path} }, "/$method" ) if $flags{global}; + } - $absolute = 1 if $flags{global}; - my $name = $absolute ? $path : $prefix ? "$prefix/$path" : $path; - $self->actions->{plain}->{$name} = $action; + for my $path ( @{ $flags{path} } ) { + if ( $path =~ /^\// ) { $path =~ s/^\/// } + else { $path = $prefix ? "$prefix/$path" : $path } + $self->actions->{plain}->{$path} = $action; } - if ( my $regex = $flags{regex} ) { + for my $regex ( @{ $flags{regex} } ) { push @{ $self->actions->{compiled} }, [ $regex, qr#$regex# ]; $self->actions->{regex}->{$regex} = $action; } diff --git a/t/live/component/controller/action/multipath.t b/t/live/component/controller/action/multipath.t new file mode 100644 index 0000000..2d77968 --- /dev/null +++ b/t/live/component/controller/action/multipath.t @@ -0,0 +1,49 @@ +#!perl + +use strict; +use warnings; + +use FindBin; +use lib "$FindBin::Bin/../../../lib"; + +use Test::More tests => 16; +use Catalyst::Test 'TestApp'; + +my $content = q/foo +bar +baz +/; + +# Local +{ + ok( my $response = request('http://localhost/action/multipath/multipath'), + 'Request' ); + ok( $response->is_success, 'Response Successful 2xx' ); + is( $response->content_type, 'text/plain', 'Response Content-Type' ); + is( $response->content, $content, 'Content is a stream' ); +} + +# Global +{ + ok( my $response = request('http://localhost/multipath'), 'Request' ); + ok( $response->is_success, 'Response Successful 2xx' ); + is( $response->content_type, 'text/plain', 'Response Content-Type' ); + is( $response->content, $content, 'Content is a stream' ); +} + +# Path('/multipath1') +{ + ok( my $response = request('http://localhost/multipath1'), 'Request' ); + ok( $response->is_success, 'Response Successful 2xx' ); + is( $response->content_type, 'text/plain', 'Response Content-Type' ); + is( $response->content, $content, 'Content is a stream' ); +} + +# Path('multipath2') +{ + ok( my $response = request('http://localhost/action/multipath/multipath2'), + 'Request' ); + ok( $response->is_success, 'Response Successful 2xx' ); + is( $response->content_type, 'text/plain', 'Response Content-Type' ); + is( $response->content, $content, 'Content is a stream' ); +} diff --git a/t/live/lib/TestApp/Controller/Action/Multipath.pm b/t/live/lib/TestApp/Controller/Action/Multipath.pm new file mode 100644 index 0000000..c2cb95f --- /dev/null +++ b/t/live/lib/TestApp/Controller/Action/Multipath.pm @@ -0,0 +1,17 @@ +package TestApp::Controller::Action::Multipath; + +use strict; +use base 'TestApp::Controller::Action'; + +sub multipath : Local : Global : Path('/multipath1') : Path('multipath2') { + my ( $self, $c ) = @_; + for my $line ( split "\n", <<'EOF' ) { +foo +bar +baz +EOF + $c->res->write("$line\n"); + } +} + +1;