4 use Scalar::Util qw(blessed);
6 sub MAGIC_MIDDLEWARE_KEY { __PACKAGE__.'.middleware' }
9 use Web::Dispatch::Parser;
10 use Web::Dispatch::Node;
12 with 'Web::Dispatch::ToApp';
14 has app => (is => 'ro', required => 1);
16 is => 'ro', default => quote_sub q{ 'Web::Dispatch::Parser' }
19 is => 'ro', default => quote_sub q{ 'Web::Dispatch::Node' }
21 has node_args => (is => 'ro', default => quote_sub q{ {} });
22 has _parser => (is => 'lazy');
26 $self->parser_class->new;
30 my ($self, $env) = @_;
31 my $res = $self->_dispatch($env, $self->app);
32 return $res->[0] if ref($res) eq 'ARRAY' and @{$res} == 1 and ref($res->[0]) eq 'CODE';
37 my ($self, $env, @match) = @_;
38 while (defined(my $try = shift @match)) {
40 return $try if ref($try) eq 'ARRAY';
41 if (ref($try) eq 'HASH') {
42 $env = { 'Web::Dispatch.original_env' => $env, %$env, %$try };
46 my @result = $self->_to_try($try, \@match)->($env, @match);
47 next unless @result and defined($result[0]);
49 my $first = $result[0];
51 if (my $res = $self->_have_result($first, \@result, \@match, $env)) {
56 # make a copy so we don't screw with it assigning further up
58 unshift @match, sub { $self->_dispatch($env, @result) };
65 my ($self, $first, $result, $match, $env) = @_;
67 if (ref($first) eq 'ARRAY') {
70 elsif (blessed($first) && $first->isa('Plack::Middleware')) {
71 return $self->_uplevel_middleware($first, $result);
73 elsif (ref($first) eq 'HASH' and $first->{+MAGIC_MIDDLEWARE_KEY}) {
74 return $self->_redispatch_with_middleware($first, $match, $env);
78 not($first->can('to_app')) &&
79 not($first->isa('Web::Dispatch::Matcher'))
86 sub _uplevel_middleware {
87 my ($self, $match, $results) = @_;
88 die "Multiple results but first one is a middleware ($match)"
90 # middleware needs to uplevel exactly once to wrap the rest of the
91 # level it was created for - next elsif unwraps it
92 return { MAGIC_MIDDLEWARE_KEY, $match };
95 sub _redispatch_with_middleware {
96 my ($self, $first, $match, $env) = @_;
98 my $mw = $first->{+MAGIC_MIDDLEWARE_KEY};
100 $mw->app(sub { $self->_dispatch($_[0], @{$match}) });
102 return $mw->to_app->($env);
106 my ($self, $try, $more) = @_;
108 # sub (<spec>) {} becomes a dispatcher
109 # sub {} is a PSGI app and can be returned as is
110 # '<spec>' => sub {} becomes a dispatcher
111 # $obj isa WD:Predicates::Matcher => sub { ... } - become a dispatcher
112 # $obj w/to_app method is a Plack::App-like thing - call it to get a PSGI app
115 if (ref($try) eq 'CODE') {
116 if (defined(my $proto = prototype($try))) {
117 $self->_construct_node(match => $proto, run => $try)->to_app;
121 } elsif (!ref($try) and ref($more->[0]) eq 'CODE') {
122 $self->_construct_node(match => $try, run => shift(@$more))->to_app;
124 (blessed($try) && $try->isa('Web::Dispatch::Matcher'))
125 and (ref($more->[0]) eq 'CODE')
127 $self->node_class->new({
132 } elsif (blessed($try) && $try->can('to_app')) {
135 die "No idea how we got here with $try";
139 sub _construct_node {
140 my ($self, %args) = @_;
141 $args{match} = $self->_parser->parse($args{match});
142 $self->node_class->new({ %{$self->node_args}, %args });