| 1 | # $Id: /mirror/gungho/lib/Gungho/Provider/Inline.pm 31310 2007-11-29T13:19:42.807767Z lestrrat $ | ||
| 2 | # | ||
| 3 | # Copyright (c) 2007 Daisuke Maki <daisuke@endeworks.jp> | ||
| 4 | # Copyright (c) 2007 Kazuho Oku | ||
| 5 | # All rights reserved. | ||
| 6 | |||
| 7 | package Gungho::Provider::Inline; | ||
| 8 | use strict; | ||
| 9 | use base qw(Gungho::Provider); | ||
| 10 | use Gungho::Request; | ||
| 11 | |||
| 12 | __PACKAGE__->mk_accessors($_) for qw(requests callback); | ||
| 13 | |||
| 14 | sub new { | ||
| 15 | my $class = shift; | ||
| 16 | my $self = $class->next::method(@_); | ||
| 17 | $self->has_requests(1); | ||
| 18 | $self->requests([]); | ||
| 19 | $self; | ||
| 20 | } | ||
| 21 | |||
| 22 | sub setup { | ||
| 23 | my $self = shift; | ||
| 24 | my $callback = $self->config->{callback}; | ||
| 25 | die "``callback'' not supplied\n" unless ref $callback eq 'CODE'; | ||
| 26 | $self->callback($callback); | ||
| 27 | $self->next::method(@_); | ||
| 28 | } | ||
| 29 | |||
| 30 | sub add_request { | ||
| 31 | my ($self, $req) = @_; | ||
| 32 | push @{$self->requests}, $req; | ||
| 33 | $self->has_requests(1); | ||
| 34 | } | ||
| 35 | |||
| 36 | sub pushback_request { | ||
| 37 | my ($self, $c, $req) = @_; | ||
| 38 | $c->log->debug( "[PROVIDER]: Pushback request " . $req->uri ); | ||
| 39 | $self->add_request($req); | ||
| 40 | } | ||
| 41 | |||
| 42 | sub dispatch { | ||
| 43 | my ($self, $c) = @_; | ||
| 44 | |||
| 45 | if ($self->callback) { | ||
| 46 | my @args = ( | ||
| 47 | Class::Inspector->loaded('Gungho::Inline') && | ||
| 48 | &Gungho::Inline::OLD_PARAMETER_LIST ? | ||
| 49 | ($c, $self) : | ||
| 50 | ($self, $c) | ||
| 51 | ); | ||
| 52 | unless ($self->callback->(@args)) { | ||
| 53 | $self->callback(undef); | ||
| 54 | } | ||
| 55 | } | ||
| 56 | |||
| 57 | if (! $self->callback && @{$self->requests} == 0) { | ||
| 58 | $self->has_requests(0); | ||
| 59 | $c->is_running(0); | ||
| 60 | } | ||
| 61 | } | ||
| 62 | |||
| 63 | 1; | ||
| 64 | |||
| 65 | __END__ |