enable matching of empty string specs
[catagits/Web-Simple.git] / lib / Web / Dispatch.pm
index 581cc04..69df554 100644 (file)
@@ -2,6 +2,9 @@ package Web::Dispatch;
 
 use Sub::Quote;
 use Scalar::Util qw(blessed);
+
+sub MAGIC_MIDDLEWARE_KEY { __PACKAGE__.'.middleware' }
+
 use Moo;
 use Web::Dispatch::Parser;
 use Web::Dispatch::Node;
@@ -30,51 +33,86 @@ sub call {
 
 sub _dispatch {
   my ($self, $env, @match) = @_;
-  while (my $try = shift @match) {
+  while (defined(my $try = shift @match)) {
+
+    return $try if ref($try) eq 'ARRAY';
     if (ref($try) eq 'HASH') {
       $env = { %$env, %$try };
       next;
-    } elsif (ref($try) eq 'ARRAY') {
-      return $try;
     }
+
     my @result = $self->_to_try($try, \@match)->($env, @match);
     next unless @result and defined($result[0]);
-    if (ref($result[0]) eq 'ARRAY') {
-      return $result[0];
-    } elsif (blessed($result[0]) && $result[0]->can('wrap')) {
-      return $result[0]->wrap(sub {
-        $self->_dispatch($_[0], @match)
-      })->($env);
-    } elsif (blessed($result[0]) && !$result[0]->can('to_app')) {
-      return $result[0];
-    } 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;
-      }
+
+    my $first = $result[0];
+
+    if (my $res = $self->_have_result( $first, \@result, \@match, $env )) {
+
+      return $res;
     }
+
+    # make a copy so we don't screw with it assigning further up
+    my $env = $env;
+    unshift @match, sub { $self->_dispatch($env, @result) };
+  }
+
+  return;
+}
+
+sub _have_result {
+  my ( $self, $first, $result, $match, $env ) = @_;
+
+  if ( ref($first) eq 'ARRAY' ) {
+    return $self->_unpack_array_match( $first );
+  }
+  elsif ( blessed($first) && $first->isa('Plack::Middleware') ) {
+    return $self->_uplevel_middleware( $first, $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') ) {
+    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)"
+    if @{$results} > 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, $match };
+}
+
+sub _redispatch_with_middleware {
+  my ( $self, $first, $match, $env ) = @_;
+
+  my $mw = $first->{+MAGIC_MIDDLEWARE_KEY};
+
+  $mw->app(sub { $self->_dispatch($_[0], @{$match}) });
+
+  return $mw->to_app->($env);
+}
+
 sub _to_try {
   my ($self, $try, $more) = @_;
   if (ref($try) eq 'CODE') {
     if (defined(my $proto = prototype($try))) {
-      $self->_construct_node(
-        match => $self->_parser->parse($proto), run => $try
-      )->to_app;
+      $self->_construct_node( match=> $proto, run => $try )->to_app;
     } else {
       $try
     }
   } elsif (!ref($try) and ref($more->[0]) eq 'CODE') {
-    $self->_construct_node(
-      match => $self->_parser->parse($try), run => shift(@$more)
-    )->to_app;
+    $self->_construct_node( match => $try, run => shift(@$more) )->to_app;
   } elsif (blessed($try) && $try->can('to_app')) {
     $try->to_app;
   } else {
@@ -84,6 +122,7 @@ sub _to_try {
 
 sub _construct_node {
   my ($self, %args) = @_;
+  $args{match} = $self->_parser->parse( $args{match} );
   $self->node_class->new({ %{$self->node_args}, %args });
 }