allow passing either psgi app, or application object to Web::Dispatch
[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
e5250d96 14has dispatch_app => (
15 is => 'lazy', builder => sub { shift->dispatch_object->to_app }
16);
17has dispatch_object => (is => 'ro', required => 0);
4ed4fb42 18has parser_class => (
19 is => 'ro', default => quote_sub q{ 'Web::Dispatch::Parser' }
20);
21has node_class => (
22 is => 'ro', default => quote_sub q{ 'Web::Dispatch::Node' }
23);
24has node_args => (is => 'ro', default => quote_sub q{ {} });
25has _parser => (is => 'lazy');
26
e5250d96 27after BUILDARGS => sub {
28 my ( $self, %args ) = @_;
29 die "Either dispatch_app or dispatch_object need to be supplied."
30 if !$args{dispatch_app} and !$args{dispatch_object}
31};
32
4ed4fb42 33sub _build__parser {
34 my ($self) = @_;
35 $self->parser_class->new;
36}
37
38sub call {
39 my ($self, $env) = @_;
e5250d96 40 my $res = $self->_dispatch($env, $self->dispatch_app);
1a0ea82a 41 return $res->[0] if ref($res) eq 'ARRAY' and @{$res} == 1 and ref($res->[0]) eq 'CODE';
42 return $res;
4ed4fb42 43}
44
45sub _dispatch {
46 my ($self, $env, @match) = @_;
59ccc1e8 47 while (defined(my $try = shift @match)) {
75ad66d6 48
49 return $try if ref($try) eq 'ARRAY';
4ed4fb42 50 if (ref($try) eq 'HASH') {
d96756e8 51 $env = { 'Web::Dispatch.original_env' => $env, %$env, %$try };
4ed4fb42 52 next;
4ed4fb42 53 }
75ad66d6 54
69aaa28a 55 my @result = $self->_to_try($try, \@match)->($env, @match);
4ed4fb42 56 next unless @result and defined($result[0]);
75ad66d6 57
58 my $first = $result[0];
e4c7f3b4 59
6cf1d73a 60 if (my $res = $self->_have_result($first, \@result, \@match, $env)) {
e4c7f3b4 61
62 return $res;
63 }
75ad66d6 64
65 # make a copy so we don't screw with it assigning further up
66 my $env = $env;
67 unshift @match, sub { $self->_dispatch($env, @result) };
4ed4fb42 68 }
75ad66d6 69
4ed4fb42 70 return;
71}
72
e4c7f3b4 73sub _have_result {
6cf1d73a 74 my ($self, $first, $result, $match, $env) = @_;
6bd99619 75
6cf1d73a 76 if (ref($first) eq 'ARRAY') {
1a0ea82a 77 return $first;
6bd99619 78 }
6cf1d73a 79 elsif (blessed($first) && $first->isa('Plack::Middleware')) {
80 return $self->_uplevel_middleware($first, $result);
6bd99619 81 }
6cf1d73a 82 elsif (ref($first) eq 'HASH' and $first->{+MAGIC_MIDDLEWARE_KEY}) {
83 return $self->_redispatch_with_middleware($first, $match, $env);
6bd99619 84 }
481da1e2 85 elsif (
86 blessed($first) &&
87 not($first->can('to_app')) &&
88 not($first->isa('Web::Dispatch::Matcher'))
89 ) {
6bd99619 90 return $first;
91 }
6bd99619 92 return;
93}
94
e4c7f3b4 95sub _uplevel_middleware {
6cf1d73a 96 my ($self, $match, $results) = @_;
75ad66d6 97 die "Multiple results but first one is a middleware ($match)"
98 if @{$results} > 1;
99 # middleware needs to uplevel exactly once to wrap the rest of the
100 # level it was created for - next elsif unwraps it
101 return { MAGIC_MIDDLEWARE_KEY, $match };
102}
103
e4c7f3b4 104sub _redispatch_with_middleware {
6cf1d73a 105 my ($self, $first, $match, $env) = @_;
75ad66d6 106
107 my $mw = $first->{+MAGIC_MIDDLEWARE_KEY};
108
109 $mw->app(sub { $self->_dispatch($_[0], @{$match}) });
110
111 return $mw->to_app->($env);
112}
113
4ed4fb42 114sub _to_try {
69aaa28a 115 my ($self, $try, $more) = @_;
c398ce1a 116
117 # sub (<spec>) {} becomes a dispatcher
118 # sub {} is a PSGI app and can be returned as is
119 # '<spec>' => sub {} becomes a dispatcher
456dc2bb 120 # $obj isa WD:Predicates::Matcher => sub { ... } - become a dispatcher
c398ce1a 121 # $obj w/to_app method is a Plack::App-like thing - call it to get a PSGI app
481da1e2 122 #
c398ce1a 123
4ed4fb42 124 if (ref($try) eq 'CODE') {
125 if (defined(my $proto = prototype($try))) {
e251dfeb 126 $self->_construct_node(match => $proto, run => $try)->to_app;
4ed4fb42 127 } else {
128 $try
129 }
69aaa28a 130 } elsif (!ref($try) and ref($more->[0]) eq 'CODE') {
e251dfeb 131 $self->_construct_node(match => $try, run => shift(@$more))->to_app;
481da1e2 132 } elsif (
133 (blessed($try) && $try->isa('Web::Dispatch::Matcher'))
134 and (ref($more->[0]) eq 'CODE')
135 ) {
136 $self->node_class->new({
137 %{$self->node_args},
138 match => $try,
139 run => shift(@$more)
140 })->to_app;
4ed4fb42 141 } elsif (blessed($try) && $try->can('to_app')) {
142 $try->to_app;
143 } else {
144 die "No idea how we got here with $try";
145 }
146}
147
148sub _construct_node {
149 my ($self, %args) = @_;
e251dfeb 150 $args{match} = $self->_parser->parse($args{match});
69aaa28a 151 $self->node_class->new({ %{$self->node_args}, %args });
4ed4fb42 152}
153
1541;