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, weak_ref => 1);
19 is => 'ro', default => quote_sub q{ 'Web::Dispatch::Parser' }
22 is => 'ro', default => quote_sub q{ 'Web::Dispatch::Node' }
24 has _parser => (is => 'lazy');
26 after BUILDARGS => sub {
27 my ( $self, %args ) = @_;
28 die "Either dispatch_app or dispatch_object need to be supplied."
29 if !$args{dispatch_app} and !$args{dispatch_object}
34 $self->parser_class->new;
38 my ($self, $env) = @_;
39 my $res = $self->_dispatch($env, $self->dispatch_app);
40 return $res->[0] if ref($res) eq 'ARRAY' and @{$res} == 1 and ref($res->[0]) eq 'CODE';
45 my ($self, $env, @match) = @_;
46 while (defined(my $try = shift @match)) {
48 return $try if ref($try) eq 'ARRAY';
49 if (ref($try) eq 'HASH') {
50 $env = { 'Web::Dispatch.original_env' => $env, %$env, %$try };
54 my @result = $self->_to_try($try, \@match)->($env, @match);
55 next unless @result and defined($result[0]);
57 my $first = $result[0];
59 if (my $res = $self->_have_result($first, \@result, \@match, $env)) {
64 # make a copy so we don't screw with it assigning further up
66 unshift @match, sub { $self->_dispatch($env, @result) };
73 my ($self, $first, $result, $match, $env) = @_;
75 if (ref($first) eq 'ARRAY') {
78 elsif (blessed($first) && $first->isa('Plack::Middleware')) {
79 return $self->_uplevel_middleware($first, $result);
81 elsif (ref($first) eq 'HASH' and $first->{+MAGIC_MIDDLEWARE_KEY}) {
82 return $self->_redispatch_with_middleware($first, $match, $env);
86 not($first->can('to_app')) &&
87 not($first->isa('Web::Dispatch::Matcher'))
94 sub _uplevel_middleware {
95 my ($self, $match, $results) = @_;
96 die "Multiple results but first one is a middleware ($match)"
98 # middleware needs to uplevel exactly once to wrap the rest of the
99 # level it was created for - next elsif unwraps it
100 return { MAGIC_MIDDLEWARE_KEY, $match };
103 sub _redispatch_with_middleware {
104 my ($self, $first, $match, $env) = @_;
106 my $mw = $first->{+MAGIC_MIDDLEWARE_KEY};
108 $mw->app(sub { $self->_dispatch($_[0], @{$match}) });
110 return $mw->to_app->($env);
114 my ($self, $try, $more) = @_;
116 # sub (<spec>) {} becomes a dispatcher
117 # sub {} is a PSGI app and can be returned as is
118 # '<spec>' => sub {} becomes a dispatcher
119 # $obj isa WD:Predicates::Matcher => sub { ... } - become a dispatcher
120 # $obj w/to_app method is a Plack::App-like thing - call it to get a PSGI app
123 if (ref($try) eq 'CODE') {
124 if (defined(my $proto = prototype($try))) {
125 $self->_construct_node(match => $proto, run => $try);
130 and (ref($more->[0]) eq 'CODE'
131 or (!ref($more->[0]) and $self->dispatch_object
132 and $self->dispatch_object->can($more->[0])))
134 $self->_construct_node(match => $try, run => shift(@$more));
136 (blessed($try) && $try->isa('Web::Dispatch::Matcher'))
137 and (ref($more->[0]) eq 'CODE')
139 $self->_construct_node(match => $try, run => shift(@$more));
140 } elsif (blessed($try) && $try->can('to_app')) {
143 die "No idea how we got here with $try";
147 sub _construct_node {
148 my ($self, %args) = @_;
149 $args{match} = $self->_parser->parse($args{match}) if !ref $args{match};
150 if ( my $obj = $self->dispatch_object) {
151 # if possible, call dispatchers as methods of the app object
152 my $dispatch_sub = $args{run};
153 $args{run} = sub { $obj->$dispatch_sub(@_) };
155 $self->node_class->new(\%args)->to_app;