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';
15 is => 'lazy', builder => sub { shift->dispatch_object->to_app }
17 has dispatch_object => (is => 'ro', required => 0);
19 is => 'ro', default => quote_sub q{ 'Web::Dispatch::Parser' }
22 is => 'ro', default => quote_sub q{ 'Web::Dispatch::Node' }
24 has node_args => (is => 'ro', default => quote_sub q{ {} });
25 has _parser => (is => 'lazy');
27 after BUILDARGS => sub {
28 my ( $self, %args ) = @_;
29 die "Either dispatch_app or dispatch_object need to be supplied."
30 if !$args{dispatch_app} and !$args{dispatch_object}
35 $self->parser_class->new;
39 my ($self, $env) = @_;
40 my $res = $self->_dispatch($env, $self->dispatch_app);
41 return $res->[0] if ref($res) eq 'ARRAY' and @{$res} == 1 and ref($res->[0]) eq 'CODE';
46 my ($self, $env, @match) = @_;
47 while (defined(my $try = shift @match)) {
49 return $try if ref($try) eq 'ARRAY';
50 if (ref($try) eq 'HASH') {
51 $env = { 'Web::Dispatch.original_env' => $env, %$env, %$try };
55 my @result = $self->_to_try($try, \@match)->($env, @match);
56 next unless @result and defined($result[0]);
58 my $first = $result[0];
60 if (my $res = $self->_have_result($first, \@result, \@match, $env)) {
65 # make a copy so we don't screw with it assigning further up
67 unshift @match, sub { $self->_dispatch($env, @result) };
74 my ($self, $first, $result, $match, $env) = @_;
76 if (ref($first) eq 'ARRAY') {
79 elsif (blessed($first) && $first->isa('Plack::Middleware')) {
80 return $self->_uplevel_middleware($first, $result);
82 elsif (ref($first) eq 'HASH' and $first->{+MAGIC_MIDDLEWARE_KEY}) {
83 return $self->_redispatch_with_middleware($first, $match, $env);
87 not($first->can('to_app')) &&
88 not($first->isa('Web::Dispatch::Matcher'))
95 sub _uplevel_middleware {
96 my ($self, $match, $results) = @_;
97 die "Multiple results but first one is a middleware ($match)"
99 # middleware needs to uplevel exactly once to wrap the rest of the
100 # level it was created for - next elsif unwraps it
101 return { MAGIC_MIDDLEWARE_KEY, $match };
104 sub _redispatch_with_middleware {
105 my ($self, $first, $match, $env) = @_;
107 my $mw = $first->{+MAGIC_MIDDLEWARE_KEY};
109 $mw->app(sub { $self->_dispatch($_[0], @{$match}) });
111 return $mw->to_app->($env);
115 my ($self, $try, $more) = @_;
117 # sub (<spec>) {} becomes a dispatcher
118 # sub {} is a PSGI app and can be returned as is
119 # '<spec>' => sub {} becomes a dispatcher
120 # $obj isa WD:Predicates::Matcher => sub { ... } - become a dispatcher
121 # $obj w/to_app method is a Plack::App-like thing - call it to get a PSGI app
124 if (ref($try) eq 'CODE') {
125 if (defined(my $proto = prototype($try))) {
126 $self->_construct_node(match => $proto, run => $try)->to_app;
130 } elsif (!ref($try) and ref($more->[0]) eq 'CODE') {
131 $self->_construct_node(match => $try, run => shift(@$more))->to_app;
133 (blessed($try) && $try->isa('Web::Dispatch::Matcher'))
134 and (ref($more->[0]) eq 'CODE')
136 $self->node_class->new({
141 } elsif (blessed($try) && $try->can('to_app')) {
144 die "No idea how we got here with $try";
148 sub _construct_node {
149 my ($self, %args) = @_;
150 $args{match} = $self->_parser->parse($args{match});
151 $self->node_class->new({ %{$self->node_args}, %args });