first draft of http methods, with a test case
[catagits/Web-Simple.git] / lib / Web / Dispatch.pm
CommitLineData
4ed4fb42 1package Web::Dispatch;
2
3use Sub::Quote;
4use Scalar::Util qw(blessed);
1f4dd6f9 5
6sub MAGIC_MIDDLEWARE_KEY { __PACKAGE__.'.middleware' }
7
4ed4fb42 8use Moo;
9use Web::Dispatch::Parser;
10use Web::Dispatch::Node;
11
12with 'Web::Dispatch::ToApp';
13
14has app => (is => 'ro', required => 1);
15has parser_class => (
16 is => 'ro', default => quote_sub q{ 'Web::Dispatch::Parser' }
17);
18has node_class => (
19 is => 'ro', default => quote_sub q{ 'Web::Dispatch::Node' }
20);
21has node_args => (is => 'ro', default => quote_sub q{ {} });
22has _parser => (is => 'lazy');
23
24sub _build__parser {
25 my ($self) = @_;
26 $self->parser_class->new;
27}
28
29sub call {
30 my ($self, $env) = @_;
1a0ea82a 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';
33 return $res;
4ed4fb42 34}
35
36sub _dispatch {
37 my ($self, $env, @match) = @_;
59ccc1e8 38 while (defined(my $try = shift @match)) {
75ad66d6 39
40 return $try if ref($try) eq 'ARRAY';
4ed4fb42 41 if (ref($try) eq 'HASH') {
d96756e8 42 $env = { 'Web::Dispatch.original_env' => $env, %$env, %$try };
4ed4fb42 43 next;
4ed4fb42 44 }
75ad66d6 45
69aaa28a 46 my @result = $self->_to_try($try, \@match)->($env, @match);
4ed4fb42 47 next unless @result and defined($result[0]);
75ad66d6 48
49 my $first = $result[0];
e4c7f3b4 50
6cf1d73a 51 if (my $res = $self->_have_result($first, \@result, \@match, $env)) {
e4c7f3b4 52
53 return $res;
54 }
75ad66d6 55
56 # make a copy so we don't screw with it assigning further up
57 my $env = $env;
58 unshift @match, sub { $self->_dispatch($env, @result) };
4ed4fb42 59 }
75ad66d6 60
4ed4fb42 61 return;
62}
63
e4c7f3b4 64sub _have_result {
6cf1d73a 65 my ($self, $first, $result, $match, $env) = @_;
6bd99619 66
6cf1d73a 67 if (ref($first) eq 'ARRAY') {
1a0ea82a 68 return $first;
6bd99619 69 }
6cf1d73a 70 elsif (blessed($first) && $first->isa('Plack::Middleware')) {
71 return $self->_uplevel_middleware($first, $result);
6bd99619 72 }
6cf1d73a 73 elsif (ref($first) eq 'HASH' and $first->{+MAGIC_MIDDLEWARE_KEY}) {
74 return $self->_redispatch_with_middleware($first, $match, $env);
6bd99619 75 }
481da1e2 76 elsif (
77 blessed($first) &&
78 not($first->can('to_app')) &&
79 not($first->isa('Web::Dispatch::Matcher'))
80 ) {
6bd99619 81 return $first;
82 }
6bd99619 83 return;
84}
85
e4c7f3b4 86sub _uplevel_middleware {
6cf1d73a 87 my ($self, $match, $results) = @_;
75ad66d6 88 die "Multiple results but first one is a middleware ($match)"
89 if @{$results} > 1;
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 };
93}
94
e4c7f3b4 95sub _redispatch_with_middleware {
6cf1d73a 96 my ($self, $first, $match, $env) = @_;
75ad66d6 97
98 my $mw = $first->{+MAGIC_MIDDLEWARE_KEY};
99
100 $mw->app(sub { $self->_dispatch($_[0], @{$match}) });
101
102 return $mw->to_app->($env);
103}
104
4ed4fb42 105sub _to_try {
69aaa28a 106 my ($self, $try, $more) = @_;
c398ce1a 107
108 # sub (<spec>) {} becomes a dispatcher
109 # sub {} is a PSGI app and can be returned as is
110 # '<spec>' => sub {} becomes a dispatcher
456dc2bb 111 # $obj isa WD:Predicates::Matcher => sub { ... } - become a dispatcher
c398ce1a 112 # $obj w/to_app method is a Plack::App-like thing - call it to get a PSGI app
481da1e2 113 #
c398ce1a 114
4ed4fb42 115 if (ref($try) eq 'CODE') {
116 if (defined(my $proto = prototype($try))) {
e251dfeb 117 $self->_construct_node(match => $proto, run => $try)->to_app;
4ed4fb42 118 } else {
119 $try
120 }
69aaa28a 121 } elsif (!ref($try) and ref($more->[0]) eq 'CODE') {
e251dfeb 122 $self->_construct_node(match => $try, run => shift(@$more))->to_app;
481da1e2 123 } elsif (
124 (blessed($try) && $try->isa('Web::Dispatch::Matcher'))
125 and (ref($more->[0]) eq 'CODE')
126 ) {
127 $self->node_class->new({
128 %{$self->node_args},
129 match => $try,
130 run => shift(@$more)
131 })->to_app;
4ed4fb42 132 } elsif (blessed($try) && $try->can('to_app')) {
133 $try->to_app;
134 } else {
135 die "No idea how we got here with $try";
136 }
137}
138
139sub _construct_node {
140 my ($self, %args) = @_;
e251dfeb 141 $args{match} = $self->_parser->parse($args{match});
69aaa28a 142 $self->node_class->new({ %{$self->node_args}, %args });
4ed4fb42 143}
144
1451;