File Coverage

blib/lib/Kelp/Middleware.pm
Criterion Covered Total %
statement 21 21 100.0
branch 2 4 50.0
condition 3 5 60.0
subroutine 5 5 100.0
pod 1 1 100.0
total 32 36 88.8


line stmt bran cond sub pod time code
1             package Kelp::Middleware;
2              
3 25     25   18264 use Kelp::Base;
  25         67  
  25         191  
4 25     25   245 use Plack::Util;
  25         97  
  25         862  
5 25     25   183 use Kelp::Util;
  25         62  
  25         831  
6 25     25   128 use Carp;
  25         46  
  25         10622  
7              
8             attr -app => sub { croak 'app is required' };
9              
10             sub wrap
11             {
12 251     251 1 585 my ($self, $psgi) = @_;
13              
14 251 50       842 if (defined(my $middleware = $self->app->config('middleware'))) {
15 251         706 for my $class (@$middleware) {
16              
17             # Make sure the middleware was not already loaded
18             # This does not apply for testing, in which case we want
19             # the middleware to wrap every single time
20 10 50 33     215 next if $self->{_loaded_middleware}->{$class}++ && !$ENV{KELP_TESTING};
21              
22 10         49 my $mw = Plack::Util::load_class($class, 'Plack::Middleware');
23 10   100     16938 my $args = $self->app->config("middleware_init.$class") // {};
24              
25 10         57 Kelp::Util::_DEBUG(modules => "Wrapping app in $mw middleware with args: ", $args);
26              
27 10         102 $psgi = $mw->wrap($psgi, %$args);
28             }
29             }
30              
31 251         2786 return $psgi;
32             }
33              
34             1;
35              
36             __END__
37              
38             =pod
39              
40             =head1 NAME
41              
42             Kelp::Middleware - Kelp app wrapper (PSGI middleware)
43              
44             =head1 SYNOPSIS
45              
46             middleware => [qw(TrailingSlashKiller Static)],
47             middleware_init => {
48             TrailingSlashKiller => {
49             redirect => 1,
50             },
51             Static => {
52             path => qr{^/static},
53             root => '.',
54             },
55             }
56              
57             =head1 DESCRIPTION
58              
59             This is a small helper class which wraps Kelp in PSGI middleware. It is loaded
60             and constructed by Kelp based on the value of L<Kelp/middleware_obj> (class
61             name).
62              
63             This class only handles global middleware declared in configuration. Middleware
64             localized to routes cannot be adjusted by customizing this class.
65              
66             =head1 ATTRIBUTES
67              
68             =head2 app
69              
70             Main application object. Required.
71              
72             =head1 METHODS
73              
74             =head2 wrap
75              
76             $wrapped_psgi = $object->wrap($psgi)
77              
78             Wraps the object in all middlewares according to L</app> configuration.
79