File Coverage

blib/lib/Plack/Loader.pm
Criterion Covered Total %
statement 31 45 68.8
branch 4 20 20.0
condition 3 17 17.6
subroutine 12 16 75.0
pod 0 8 0.0
total 50 106 47.1


line stmt bran cond sub pod time code
1             package Plack::Loader;
2 59     59   297037 use strict;
  59         169  
  59         2304  
3 59     59   363 use Carp ();
  59         127  
  59         1353  
4 59     59   31482 use Plack::Util;
  59         195  
  59         2167  
5 59     59   30280 use Try::Tiny;
  59         141995  
  59         46979  
6              
7             sub new {
8 39     39 0 561704 my $class = shift;
9 39         1157 bless {}, $class;
10             }
11              
12       0 0   sub watch {
13             # do nothing. Override in subclass
14             }
15              
16             sub auto {
17 1     1 0 15 my($class, @args) = @_;
18              
19 1 50       5 my $backend = $class->guess
20             or Carp::croak("Couldn't auto-guess server server implementation. Set it with PLACK_SERVER");
21              
22             my $server = try {
23 1     1   68 $class->load($backend, @args);
24             } catch {
25 1 50 50 1   27 if (($ENV{PLACK_ENV}||'') eq 'development' or !/^Can't locate /) {
      33        
26             warn "Autoloading '$backend' backend failed. Falling back to the Standalone. ",
27             "(You might need to install Plack::Handler::$backend from CPAN. Caught error was: $_)\n"
28 0 0 0     0 if $ENV{PLACK_ENV} && $ENV{PLACK_ENV} eq 'development';
29             }
30 1         5 $class->load('Standalone' => @args);
31 1         16 };
32              
33 1         12 return $server;
34             }
35              
36             sub load {
37 39     39 0 3263 my($class, $server, @args) = @_;
38              
39 39         78 my($server_class, $error);
40             try {
41 39     39   11889 $server_class = Plack::Util::load_class($server, 'Plack::Handler');
42             } catch {
43 1   33 1   29 $error ||= $_;
44 39         4128 };
45              
46 39 100       1962 if ($server_class) {
47 38         378 $server_class->new(@args);
48             } else {
49 1         7 die $error;
50             }
51             }
52              
53             sub preload_app {
54 2     2 0 24 my($self, $builder) = @_;
55 2         6 $self->{app} = $builder->();
56             }
57              
58             sub guess {
59 0     0 0   my $class = shift;
60              
61 0           my $env = $class->env;
62              
63 0 0         return $env->{PLACK_SERVER} if $env->{PLACK_SERVER};
64              
65 0 0 0       if ($env->{PHP_FCGI_CHILDREN} || $env->{FCGI_ROLE} || $env->{FCGI_SOCKET_PATH}) {
    0 0        
    0          
    0          
    0          
66 0           return "FCGI";
67             } elsif ($env->{GATEWAY_INTERFACE}) {
68 0           return "CGI";
69             } elsif (exists $INC{"Coro.pm"}) {
70 0           return "Corona";
71             } elsif (exists $INC{"AnyEvent.pm"}) {
72 0           return "Twiggy";
73             } elsif (exists $INC{"POE.pm"}) {
74 0           return "POE";
75             } else {
76 0           return "Standalone";
77             }
78             }
79              
80 0     0 0   sub env { \%ENV }
81              
82             sub run {
83 0     0 0   my($self, $server) = @_;
84 0           $server->run($self->{app});
85             }
86              
87             1;
88              
89             __END__