use Sub::Quote;
use Scalar::Util qw(blessed);
+
+sub MAGIC_MIDDLEWARE_KEY { __PACKAGE__.'.middleware' }
+
use Moo;
use Web::Dispatch::Parser;
use Web::Dispatch::Node;
} elsif (blessed($result[0]) && $result[0]->isa('Plack::Middleware')) {
die "Multiple results but first one is a middleware ($result[0])"
if @result > 1;
+ # middleware needs to uplevel exactly once to wrap the rest of the
+ # level it was created for - next elsif unwraps it
+ return { MAGIC_MIDDLEWARE_KEY, $result[0] };
my $mw = $result[0];
+ } elsif (
+ ref($result[0]) eq 'HASH'
+ and my $mw = $result[0]->{+MAGIC_MIDDLEWARE_KEY}
+ ) {
$mw->app(sub { $self->_dispatch($_[0], @match) });
return $mw->to_app->($env);
} elsif (blessed($result[0]) && !$result[0]->can('to_app')) {
} else {
# make a copy so we don't screw with it assigning further up
my $env = $env;
- # try not to end up quite so bloody deep in the call stack
- if (@match) {
- unshift @match, sub { $self->_dispatch($env, @result) };
- } else {
- @match = @result;
- }
+ unshift @match, sub { $self->_dispatch($env, @result) };
}
}
return;
sub call {
my ($self, $env) = @_;
if (my ($env_delta, @match) = $self->_match->($env)) {
- $self->_curry(@match)->({ %$env, %$env_delta });
+ ($env_delta, $self->_curry(@match));
} else {
()
}
--- /dev/null
+use strictures 1;
+use Test::More;
+
+{
+ package TestApp;
+
+ use Web::Simple;
+
+ sub dispatch_request {
+ sub (/foo/...) {
+ sub (GET) { [ 200, [], [ $_[PSGI_ENV]->{PATH_INFO} ] ] }
+ },
+ sub (POST) { [ 200, [], [ $_[PSGI_ENV]->{PATH_INFO} ] ] }
+ }
+}
+
+my $app = TestApp->new->to_psgi_app;
+
+my $call = sub { $app->({
+ SCRIPT_NAME => '/base', PATH_INFO => '/foo/bar', REQUEST_METHOD => shift
+})->[2]->[0] };
+
+is($call->('GET'), '/bar', 'recursive strip ok');
+is($call->('POST'), '/foo/bar', 'later dispatchers unaffected');
+
+done_testing;