X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FWeb%2FDispatch.pm;h=5da58548650e174993bd79c27d293256b6aa5791;hb=456dc2bbf3016f2597cee32e06de13bd06d5eb0f;hp=210c297a02a243f578070e122eae5464e30d7aba;hpb=6cf1d73a73e01b627a0feb4ccd2b9aa16e1e8901;p=catagits%2FWeb-Simple.git diff --git a/lib/Web/Dispatch.pm b/lib/Web/Dispatch.pm index 210c297..5da5854 100644 --- a/lib/Web/Dispatch.pm +++ b/lib/Web/Dispatch.pm @@ -28,7 +28,9 @@ sub _build__parser { sub call { my ($self, $env) = @_; - $self->_dispatch($env, $self->app); + my $res = $self->_dispatch($env, $self->app); + return $res->[0] if ref($res) eq 'ARRAY' and @{$res} == 1 and ref($res->[0]) eq 'CODE'; + return $res; } sub _dispatch { @@ -37,7 +39,7 @@ sub _dispatch { return $try if ref($try) eq 'ARRAY'; if (ref($try) eq 'HASH') { - $env = { %$env, %$try }; + $env = { 'Web::Dispatch.original_env' => $env, %$env, %$try }; next; } @@ -63,7 +65,7 @@ sub _have_result { my ($self, $first, $result, $match, $env) = @_; if (ref($first) eq 'ARRAY') { - return $self->_unpack_array_match($first); + return $first; } elsif (blessed($first) && $first->isa('Plack::Middleware')) { return $self->_uplevel_middleware($first, $result); @@ -71,19 +73,16 @@ sub _have_result { elsif (ref($first) eq 'HASH' and $first->{+MAGIC_MIDDLEWARE_KEY}) { return $self->_redispatch_with_middleware($first, $match, $env); } - elsif (blessed($first) && !$first->can('to_app')) { + elsif ( + blessed($first) && + not($first->can('to_app')) && + not($first->isa('Web::Dispatch::Matcher')) + ) { return $first; } - return; } -sub _unpack_array_match { - my ($self, $match) = @_; - return $match->[0] if @{$match} == 1 and ref($match->[0]) eq 'CODE'; - return $match; -} - sub _uplevel_middleware { my ($self, $match, $results) = @_; die "Multiple results but first one is a middleware ($match)" @@ -105,6 +104,14 @@ sub _redispatch_with_middleware { sub _to_try { my ($self, $try, $more) = @_; + + # sub () {} becomes a dispatcher + # sub {} is a PSGI app and can be returned as is + # '' => sub {} becomes a dispatcher + # $obj isa WD:Predicates::Matcher => sub { ... } - become a dispatcher + # $obj w/to_app method is a Plack::App-like thing - call it to get a PSGI app + # + if (ref($try) eq 'CODE') { if (defined(my $proto = prototype($try))) { $self->_construct_node(match => $proto, run => $try)->to_app; @@ -113,6 +120,15 @@ sub _to_try { } } elsif (!ref($try) and ref($more->[0]) eq 'CODE') { $self->_construct_node(match => $try, run => shift(@$more))->to_app; + } elsif ( + (blessed($try) && $try->isa('Web::Dispatch::Matcher')) + and (ref($more->[0]) eq 'CODE') + ) { + $self->node_class->new({ + %{$self->node_args}, + match => $try, + run => shift(@$more) + })->to_app; } elsif (blessed($try) && $try->can('to_app')) { $try->to_app; } else {