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