factor dispatch parser out
[catagits/Web-Simple.git] / lib / Web / Simple / Application.pm
index be34500..4021170 100644 (file)
@@ -72,7 +72,10 @@ sub BUILDALL {
   my @targ;
   while ($targ->isa(__PACKAGE__) and $targ ne __PACKAGE__) {
     push(@targ, "${targ}::BUILD")
-      if do { no strict 'refs'; defined *{"${targ}::BUILD"}{CODE} };
+      if do {
+           no strict 'refs'; no warnings 'once';
+           defined *{"${targ}::BUILD"}{CODE}
+         };
     my @targ_isa = do { no strict 'refs'; @{"${targ}::ISA"} };
     die "${targ} uses Multiple Inheritance: ISA is: ".join ', ', @targ_isa
       if @targ_isa > 1;
@@ -127,8 +130,8 @@ sub _construct_redispatch {
 }
 
 sub _build_dispatch_parser {
-  require Web::Simple::DispatchParser;
-  return Web::Simple::DispatchParser->new;
+  require Web::Dispatch::Parser;
+  return Web::Dispatch::Parser->new;
 }
 
 sub _cannot_call_twice {
@@ -167,14 +170,14 @@ sub _construct_subdispatch {
     my $chain = $class->_build_dispatch_chain(@res);
     return $class->_build_dispatcher({
       call => sub {
-        my ($d, $self, $env) = (shift, shift, shift);
+        my ($d, $self, $env) = (shift, shift, shift); pop; # lose trailing $env
         return $chain->dispatch($env, $self, @_);
       }
     });
   });
   return $class->_build_dispatcher({
     call => sub {
-      my ($d, $self, $env) = (shift, shift, shift);
+      my ($d, $self, $env) = (shift, shift, shift); pop; # lose trailing $env
       my @sub = $disp->dispatch($env, $self, @_);
       return @sub if @sub;
       return unless (my $next = $d->next);
@@ -190,7 +193,7 @@ sub _build_dispatcher_from_spec {
   my $parser = $class->_build_dispatch_parser;
   my $matcher = (
     defined($proto) && length($proto)
-      ? $parser->parse_dispatch_specification($proto)
+      ? $parser->parse($proto)
       : sub { ({}, $_[1]) }
   );
   return $class->_build_dispatcher({
@@ -260,8 +263,8 @@ sub _run_fcgi {
 }
 
 sub as_psgi_app {
-  my $self = shift;
-  ref($self) ? sub { $self->_dispatch(@_) } : sub { $self->new->_dispatch(@_) }
+  my $self = ref($_[0]) ? $_[0] : $_[0]->new;
+  sub { $self->_dispatch(@_) };
 }
 
 sub run {
@@ -271,7 +274,11 @@ sub run {
   } elsif ($ENV{GATEWAY_INTERFACE}) {
     return $self->_run_cgi;
   }
-  my $path = shift(@ARGV) or die "No path passed - use $0 / for root";
+  unless (@ARGV && $ARGV[0] =~ m{^/}) {
+    return $self->_run_cli(@ARGV);
+  }
+
+  my $path = shift @ARGV;
 
   require HTTP::Request::Common;
   require Plack::Test;
@@ -283,4 +290,16 @@ sub run {
   print $response->as_string;
 }
 
+sub _run_cli {
+  my $self = shift;
+  die $self->_cli_usage;
+}
+
+sub _cli_usage {
+  "To run this script in CGI test mode, pass a URL path beginning with /:\n".
+  "\n".
+  "  $0 /some/path\n".
+  "  $0 /\n"
+}
+
 1;