File Coverage

blib/lib/KelpX/Symbiosis/Util.pm
Criterion Covered Total %
statement 25 26 96.1
branch 1 2 50.0
condition 3 5 60.0
subroutine 5 6 83.3
pod 3 3 100.0
total 37 42 88.1


line stmt bran cond sub pod time code
1             package KelpX::Symbiosis::Util;
2             $KelpX::Symbiosis::Util::VERSION = '2.11';
3 13     13   88 use Kelp::Base -strict;
  13         24  
  13         97  
4 13     13   2220 use Plack::Util;
  13         26  
  13         320  
5 13     13   53 use Kelp::Util;
  13         23  
  13         5280  
6              
7             sub wrap
8             {
9 68     68 1 172 my ($self, $app) = @_;
10              
11 68         110 for (@{$self->middleware}) {
  68         230  
12 6         58 my ($class, $args) = @$_;
13              
14             # Same middleware loading procedure as Kelp
15 6 50 66     46 next if $self->{_loaded_middleware}{$class}++ && !$ENV{KELP_TESTING};
16              
17 6         32 my $mw = Plack::Util::load_class($class, "Plack::Middleware");
18 6   50     5257 $app = $mw->wrap($app, %{$args // {}});
  6         96  
19             }
20              
21 68         718 return $app;
22             }
23              
24             sub load_middleware
25             {
26 27     27 1 116 my ($self, %args) = @_;
27              
28 27         111 my $middleware = $self->middleware;
29 27         50 foreach my $mw (@{$args{middleware}}) {
  27         116  
30 5         14 my $config = $args{middleware_init}{$mw};
31 5         20 push @$middleware, [$mw, $config];
32             }
33              
34 27         90 return;
35             }
36              
37             sub plack_to_kelp
38             {
39 0     0 1   goto \&Kelp::Util::adapt_psgi;
40             }
41              
42             1;
43              
44             __END__
45              
46             =head1 NAME
47              
48             KelpX::Symbiosis::Util - Reusable tools for Symbiosis
49              
50             =head1 SYNOPSIS
51              
52             use KelpX::Symbiosis::Util;
53              
54             my $app = SomePlackApp->new;
55             $kelp->routes->add(
56             '/newapp/>',
57             KelpX::Symbiosis::Util::plack_to_kelp($app->to_app),
58             );
59              
60             =head1 DESCRIPTION
61              
62             These are some utilities helpful when Plack and Kelp are wrestling each other.
63              
64             =head1 USAGE
65              
66             =head2 Functions
67              
68             None of the functions are exported, use them together with package name.
69              
70             =head3 wrap
71              
72             my $wrapped = KelpX::Symbiosis::Util::wrap($obj, $app);
73              
74             Wraps C<$app> inside all the middleware declared in C<$obj>, which should
75             contain attribute C<middleware> with contents loaded using L</load_middleware>.
76              
77             =head3 load_middleware
78              
79             KelpX::Symbiosis::Util::load_middleware($obj, %config);
80              
81             Loads standard Kelp middleware definitions into C<middleware> attribute of
82             C<$obj> (adds to it, does not clear it). Can be used later with L</wrap>.
83              
84             =head3 plack_to_kelp
85              
86             Moved to core Kelp in version I<2.10>. This function will not be removed for
87             backward compatilibity, but is now just an alias for L<Kelp::Util/adapt_psgi>.
88