avoid undef errors blowing out XML::Tags
[catagits/Web-Simple.git] / lib / Web / Dispatch.pm
CommitLineData
4ed4fb42 1package Web::Dispatch;
2
3use Sub::Quote;
4use Scalar::Util qw(blessed);
5use Moo;
6use Web::Dispatch::Parser;
7use Web::Dispatch::Node;
8
9with 'Web::Dispatch::ToApp';
10
11has app => (is => 'ro', required => 1);
12has parser_class => (
13 is => 'ro', default => quote_sub q{ 'Web::Dispatch::Parser' }
14);
15has node_class => (
16 is => 'ro', default => quote_sub q{ 'Web::Dispatch::Node' }
17);
18has node_args => (is => 'ro', default => quote_sub q{ {} });
19has _parser => (is => 'lazy');
20
21sub _build__parser {
22 my ($self) = @_;
23 $self->parser_class->new;
24}
25
26sub call {
27 my ($self, $env) = @_;
28 $self->_dispatch($env, $self->app);
29}
30
31sub _dispatch {
32 my ($self, $env, @match) = @_;
33 while (my $try = shift @match) {
34 if (ref($try) eq 'HASH') {
35 $env = { %$env, %$try };
36 next;
37 } elsif (ref($try) eq 'ARRAY') {
38 return $try;
39 }
69aaa28a 40 my @result = $self->_to_try($try, \@match)->($env, @match);
4ed4fb42 41 next unless @result and defined($result[0]);
42 if (ref($result[0]) eq 'ARRAY') {
d9ae38d9 43 if (@{$result[0]} == 1 and ref($result[0][0]) eq 'CODE') {
44 return $result[0][0];
45 }
4ed4fb42 46 return $result[0];
5ba2eb68 47 } elsif (blessed($result[0]) && $result[0]->isa('Plack::Middleware')) {
48 die "Multiple results but first one is a middleware ($result[0])"
49 if @result > 1;
50 my $mw = $result[0];
51 $mw->app(sub { $self->_dispatch($_[0], @match) });
52 return $mw->to_app->($env);
4ed4fb42 53 } elsif (blessed($result[0]) && !$result[0]->can('to_app')) {
54 return $result[0];
55 } else {
56 # make a copy so we don't screw with it assigning further up
57 my $env = $env;
58 # try not to end up quite so bloody deep in the call stack
59 if (@match) {
60 unshift @match, sub { $self->_dispatch($env, @result) };
61 } else {
62 @match = @result;
63 }
64 }
65 }
66 return;
67}
68
69sub _to_try {
69aaa28a 70 my ($self, $try, $more) = @_;
4ed4fb42 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
75 )->to_app;
76 } else {
77 $try
78 }
69aaa28a 79 } elsif (!ref($try) and ref($more->[0]) eq 'CODE') {
80 $self->_construct_node(
81 match => $self->_parser->parse($try), run => shift(@$more)
82 )->to_app;
4ed4fb42 83 } elsif (blessed($try) && $try->can('to_app')) {
84 $try->to_app;
85 } else {
86 die "No idea how we got here with $try";
87 }
88}
89
90sub _construct_node {
91 my ($self, %args) = @_;
69aaa28a 92 $self->node_class->new({ %{$self->node_args}, %args });
4ed4fb42 93}
94
951;