File Coverage

blib/lib/Dancer/Handler/PSGI.pm
Criterion Covered Total %
statement 56 67 83.5
branch 5 12 41.6
condition n/a
subroutine 15 16 93.7
pod 0 5 0.0
total 76 100 76.0


line stmt bran cond sub pod time code
1             package Dancer::Handler::PSGI;
2             our $AUTHORITY = 'cpan:SUKRIA';
3             #ABSTRACT: a PSGI handler for Dancer applications
4             $Dancer::Handler::PSGI::VERSION = '1.3520';
5 3     3   71846 use strict;
  3         18  
  3         97  
6 3     3   25 use warnings;
  3         10  
  3         75  
7 3     3   16 use Carp;
  3         6  
  3         223  
8 3     3   21 use base 'Dancer::Handler';
  3         15  
  3         793  
9              
10 3     3   26 use Dancer::Deprecation;
  3         8  
  3         79  
11 3     3   17 use Dancer::GetOpt;
  3         7  
  3         89  
12 3     3   21 use Dancer::Config;
  3         6  
  3         126  
13 3     3   19 use Dancer::ModuleLoader;
  3         6  
  3         69  
14 3     3   18 use Dancer::SharedData;
  3         7  
  3         78  
15 3     3   19 use Dancer::Logger;
  3         5  
  3         111  
16 3     3   18 use Dancer::Exception qw(:all);
  3         7  
  3         2201  
17              
18             sub new {
19 5     5 0 1405 my $class = shift;
20              
21 5 50       23 raise core_handler_PSGI => "Plack::Request is needed by the PSGI handler"
22             unless Dancer::ModuleLoader->load('Plack::Request');
23              
24 5         18 my $self = {};
25 5         46 bless $self, $class;
26             }
27              
28             sub start {
29 1     1 0 2 my $self = shift;
30 1         5 my $app = $self->psgi_app();
31              
32 1         3 foreach my $setting (qw/plack_middlewares plack_middlewares_map/) {
33 2 100       54 if (Dancer::Config::setting($setting)) {
34 1         4 my $method = 'apply_'.$setting;
35 1         3 $app = $self->$method($app);
36             }
37             }
38              
39 1         14 return $app;
40             }
41              
42             sub apply_plack_middlewares_map {
43 0     0 0 0 my ($self, $app) = @_;
44              
45 0         0 my $mw_map = Dancer::Config::setting('plack_middlewares_map');
46              
47 0         0 foreach my $req (qw(Plack::App::URLMap Plack::Builder)) {
48 0 0       0 raise core_handler_PSGI => "$req is needed to use apply_plack_middlewares_map"
49             unless Dancer::ModuleLoader->load($req);
50             }
51              
52 0         0 my $urlmap = Plack::App::URLMap->new;
53              
54 0         0 while ( my ( $path, $mw ) = each %$mw_map ) {
55 0         0 my $builder = Plack::Builder->new();
56 0         0 $builder->add_middleware(@$_) for @$mw;
57 0         0 $urlmap->map( $path => $builder->to_app($app) );
58             }
59              
60 0 0       0 $urlmap->map('/' => $app) unless $mw_map->{'/'};
61 0         0 return $urlmap->to_app;
62             }
63              
64             sub apply_plack_middlewares {
65 2     2 0 8 my ($self, $app) = @_;
66              
67 2         5 my $middlewares = Dancer::Config::setting('plack_middlewares');
68              
69 2 50       6 raise core_handler_PSGI => "Plack::Builder is needed for middlewares support"
70             unless Dancer::ModuleLoader->load('Plack::Builder');
71              
72 2 50       8 ref $middlewares eq "ARRAY"
73             or raise core_handler_PSGI => "'plack_middlewares' setting must be an ArrayRef";
74              
75 2         13 my $builder = Plack::Builder->new();
76              
77 2         15 for my $mw (@$middlewares) {
78 2         11 Dancer::Logger::core "add middleware " . $mw->[0];
79 2         5 $builder->add_middleware(@$mw)
80             }
81              
82 2         895 return $builder->to_app($app);
83             }
84              
85             sub init_request_headers {
86 2     2 0 15 my ($self, $env) = @_;
87 2         13 my $plack = Plack::Request->new($env);
88 2         26 Dancer::SharedData->headers($plack->headers);
89             }
90              
91             1;
92              
93             __END__