sub new {
my ($class, $data) = @_;
my $config = { $class->_default_config, %{($data||{})->{config}||{}} };
- bless({ config => $config }, $class);
+ my $new = bless({ config => $config }, $class);
+ $new->BUILDALL($data);
+ $new;
+}
+
+sub BUILDALL {
+ my ($self, $data) = @_;
+ my $targ = ref($self);
+ my @targ;
+ while ($targ->isa(__PACKAGE__) and $targ ne __PACKAGE__) {
+ push(@targ, "${targ}::BUILD")
+ if do { no strict 'refs'; defined *{"${targ}::BUILD"}{CODE} };
+ my @targ_isa = do { no strict 'refs'; @{"${targ}::ISA"} };
+ die "${targ} uses Multiple Inheritance: ISA is: ".join ', ', @targ_isa
+ if @targ_isa > 1;
+ $targ = $targ_isa[0];
+ }
+ $self->$_($data) for reverse @targ;
+ return;
}
sub _setup_default_config {
my $proto = prototype $spec;
my $parser = $class->_build_dispatch_parser;
my $matcher = (
- defined($proto)
+ defined($proto) && length($proto)
? $parser->parse_dispatch_specification($proto)
: undef
);
}
sub run_if_script {
- return 1 if caller(1); # 1 so we can be the last thing in the file
+ # ->as_psgi_app is true for require() but also works for plackup
+ return $_[0]->as_psgi_app if caller(1);
my $class = shift;
my $self = $class->new;
$self->run(@_);
sub _run_cgi {
my $self = shift;
require Web::Simple::HackedPlack;
- Plack::Server::CGI->run(sub { $self->_dispatch(@_) });
+ Plack::Server::CGI->run($self->as_psgi_app);
+}
+
+sub as_psgi_app {
+ my $self = shift;
+ ref($self) ? sub { $self->_dispatch(@_) } : sub { $self->new->_dispatch(@_) }
}
sub run {