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