4 use Scalar::Util qw(blessed);
6 use Web::Dispatch::Parser;
7 use Web::Dispatch::Node;
9 with 'Web::Dispatch::ToApp';
11 has app => (is => 'ro', required => 1);
13 is => 'ro', default => quote_sub q{ 'Web::Dispatch::Parser' }
16 is => 'ro', default => quote_sub q{ 'Web::Dispatch::Node' }
18 has node_args => (is => 'ro', default => quote_sub q{ {} });
19 has _parser => (is => 'lazy');
23 $self->parser_class->new;
27 my ($self, $env) = @_;
28 $self->_dispatch($env, $self->app);
32 my ($self, $env, @match) = @_;
33 while (my $try = shift @match) {
34 if (ref($try) eq 'HASH') {
35 $env = { %$env, %$try };
37 } elsif (ref($try) eq 'ARRAY') {
40 my @result = $self->_to_try($try, \@match)->($env, @match);
41 next unless @result and defined($result[0]);
42 if (ref($result[0]) eq 'ARRAY') {
43 if (@{$result[0]} == 1 and ref($result[0][0]) eq 'CODE') {
47 } elsif (blessed($result[0]) && $result[0]->isa('Plack::Middleware')) {
48 die "Multiple results but first one is a middleware ($result[0])"
51 $mw->app(sub { $self->_dispatch($_[0], @match) });
52 return $mw->to_app->($env);
53 } elsif (blessed($result[0]) && !$result[0]->can('to_app')) {
56 # make a copy so we don't screw with it assigning further up
58 # try not to end up quite so bloody deep in the call stack
60 unshift @match, sub { $self->_dispatch($env, @result) };
70 my ($self, $try, $more) = @_;
71 if (ref($try) eq 'CODE') {
72 if (defined(my $proto = prototype($try))) {
73 $self->_construct_node(
74 match => $self->_parser->parse($proto), run => $try
79 } elsif (!ref($try) and ref($more->[0]) eq 'CODE') {
80 $self->_construct_node(
81 match => $self->_parser->parse($try), run => shift(@$more)
83 } elsif (blessed($try) && $try->can('to_app')) {
86 die "No idea how we got here with $try";
91 my ($self, %args) = @_;
92 $self->node_class->new({ %{$self->node_args}, %args });