elsif christmas tree is back, but hidden away
[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) = @_;
31 $self->_dispatch($env, $self->app);
32}
33
34sub _dispatch {
35 my ($self, $env, @match) = @_;
36 while (my $try = shift @match) {
75ad66d6 37
38 return $try if ref($try) eq 'ARRAY';
4ed4fb42 39 if (ref($try) eq 'HASH') {
40 $env = { %$env, %$try };
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];
6bd99619 48 my $dispatch = $self->_valid_dispatch( $first, \@result, \@match, $env );
49 return $dispatch if $dispatch;
75ad66d6 50
51 # make a copy so we don't screw with it assigning further up
52 my $env = $env;
53 unshift @match, sub { $self->_dispatch($env, @result) };
4ed4fb42 54 }
75ad66d6 55
4ed4fb42 56 return;
57}
58
6bd99619 59sub _valid_dispatch {
60 my ( $self, $first, $result, $match, $env ) = @_;
61
62 if ( ref($first) eq 'ARRAY' ) {
63 return $self->_unpack_array_match( $first );
64 }
65 elsif ( blessed($first) && $first->isa('Plack::Middleware') ) {
66 return $self->_prepare_middleware( $first, $result );
67 }
68 elsif ( ref($first) eq 'HASH' and $first->{+MAGIC_MIDDLEWARE_KEY} ) {
69 return $self->_unwrap_middleware( $first, $match, $env );
70 }
71 elsif ( blessed($first) && !$first->can('to_app') ) {
72 return $first;
73 }
74
75 return;
76}
77
75ad66d6 78sub _unpack_array_match {
79 my ( $self, $match ) = @_;
80 return $match->[0] if @{$match} == 1 and ref($match->[0]) eq 'CODE';
81 return $match;
82}
83
84sub _prepare_middleware {
85 my ( $self, $match, $results ) = @_;
86 die "Multiple results but first one is a middleware ($match)"
87 if @{$results} > 1;
88 # middleware needs to uplevel exactly once to wrap the rest of the
89 # level it was created for - next elsif unwraps it
90 return { MAGIC_MIDDLEWARE_KEY, $match };
91}
92
93sub _unwrap_middleware {
94 my ( $self, $first, $match, $env ) = @_;
95
96 my $mw = $first->{+MAGIC_MIDDLEWARE_KEY};
97
98 $mw->app(sub { $self->_dispatch($_[0], @{$match}) });
99
100 return $mw->to_app->($env);
101}
102
4ed4fb42 103sub _to_try {
69aaa28a 104 my ($self, $try, $more) = @_;
4ed4fb42 105 if (ref($try) eq 'CODE') {
106 if (defined(my $proto = prototype($try))) {
107 $self->_construct_node(
108 match => $self->_parser->parse($proto), run => $try
109 )->to_app;
110 } else {
111 $try
112 }
69aaa28a 113 } elsif (!ref($try) and ref($more->[0]) eq 'CODE') {
114 $self->_construct_node(
115 match => $self->_parser->parse($try), run => shift(@$more)
116 )->to_app;
4ed4fb42 117 } elsif (blessed($try) && $try->can('to_app')) {
118 $try->to_app;
119 } else {
120 die "No idea how we got here with $try";
121 }
122}
123
124sub _construct_node {
125 my ($self, %args) = @_;
69aaa28a 126 $self->node_class->new({ %{$self->node_args}, %args });
4ed4fb42 127}
128
1291;