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   263436 use strict;
  59         82  
  59         1653  
3 59     59   226 use Carp ();
  59         74  
  59         795  
4 59     59   19939 use Plack::Util;
  59         118  
  59         1469  
5 59     59   22272 use Try::Tiny;
  59         91928  
  59         28943  
6              
7             sub new {
8 39     39 0 363101 my $class = shift;
9 39         523 bless {}, $class;
10             }
11              
12       0 0   sub watch {
13             # do nothing. Override in subclass
14             }
15              
16             sub auto {
17 1     1 0 10 my($class, @args) = @_;
18              
19 1 50       3 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   43 $class->load($backend, @args);
24             } catch {
25 1 50 50 1   17 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         3 $class->load('Standalone' => @args);
31 1         18 };
32              
33 1         12 return $server;
34             }
35              
36             sub load {
37 39     39 0 3260 my($class, $server, @args) = @_;
38              
39 39         595 my($server_class, $error);
40             try {
41 39     39   11899 $server_class = Plack::Util::load_class($server, 'Plack::Handler');
42             } catch {
43 1   33 1   20 $error ||= $_;
44 39         4594 };
45              
46 39 100       1511 if ($server_class) {
47 38         377 $server_class->new(@args);
48             } else {
49 1         4 die $error;
50             }
51             }
52              
53             sub preload_app {
54 2     2 0 11 my($self, $builder) = @_;
55 2         13 $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__