File Coverage

blib/lib/KelpX/Symbiosis/Adapter.pm
Criterion Covered Total %
statement 46 51 90.2
branch 8 14 57.1
condition 2 3 66.6
subroutine 11 13 84.6
pod 0 4 0.0
total 67 85 78.8


line stmt bran cond sub pod time code
1             package KelpX::Symbiosis::Adapter;
2             $KelpX::Symbiosis::Adapter::VERSION = '2.11';
3 13     13   84 use Kelp::Base;
  13         24  
  13         111  
4 13     13   3115 use Carp;
  13         44  
  13         993  
5 13     13   6026 use Plack::Middleware::Conditional;
  13         28761  
  13         415  
6 13     13   77 use Plack::Util;
  13         27  
  13         276  
7 13     13   6505 use KelpX::Symbiosis::Util;
  13         64  
  13         11782  
8              
9             attr engine => sub { croak 'no engine was choosen' };
10             attr -app => sub { croak 'app is required' };
11             attr -mounted => sub { {} };
12             attr -loaded => sub { {} };
13             attr -middleware => sub { [] };
14             attr reverse_proxy => 0;
15              
16             sub mount
17             {
18 28     28 0 13611 my ($self, $path, $app) = @_;
19              
20 28 100 66     154 if (!ref $app && $app) {
21 4         9 my $loaded = $self->loaded;
22             croak "Symbiosis: cannot mount $app because no such name was loaded"
23 4 50       30 unless $loaded->{$app};
24 4         8 $app = $loaded->{$app};
25             }
26              
27             # mount first, not to pollute mounted hash if the exception is caught and
28             # the program continues
29 28         107 my $mounted = $self->engine->mount($path, $app);
30 28         27511 $self->mounted->{$path} = $app;
31 28         174 return $mounted;
32             }
33              
34             sub _link
35             {
36 14     14   225 my ($self, $name, $app, $mount) = @_;
37 14         54 my $loaded = $self->loaded;
38              
39             carp "Symbiosis: overriding module name $name"
40 14 50       68 if exists $loaded->{$name};
41 14         37 $loaded->{$name} = $app;
42              
43 14 100       43 if ($mount) {
44 3         13 $self->mount($mount, $app);
45             }
46 14         46 return scalar keys %{$loaded};
  14         56  
47             }
48              
49             sub run
50             {
51 56     56 0 428 my $self = shift;
52              
53 56         214 my $app = $self->engine->run(@_);
54              
55 56         16937 my $wrapped = KelpX::Symbiosis::Util::wrap($self, $app);
56 56         221 return $self->_reverse_proxy_wrap($wrapped);
57             }
58              
59             sub _reverse_proxy_wrap
60             {
61 56     56   152 my ($self, $app) = @_;
62 56 50       191 return $app unless $self->reverse_proxy;
63              
64 0         0 my $mw_class = Plack::Util::load_class('ReverseProxy', 'Plack::Middleware');
65             return Plack::Middleware::Conditional->wrap(
66             $app,
67 0 0   0   0 condition => sub { !$_[0]{REMOTE_ADDR} || $_[0]{REMOTE_ADDR} =~ m{127\.0\.0\.1} },
68 0     0   0 builder => sub { $mw_class->wrap($_[0]) },
69 0         0 );
70             }
71              
72             sub build
73             {
74 13     13 0 72 my ($self, %args) = @_;
75              
76 13 50       56 if ($args{reverse_proxy}) {
77 0         0 $self->reverse_proxy(1);
78             }
79              
80 13         41 $self->engine->build(%args);
81 13         53 KelpX::Symbiosis::Util::load_middleware($self, %args);
82             }
83              
84             sub new
85             {
86 13     13 0 185 my ($class, %args) = @_;
87              
88 13         77 my $self = $class->SUPER::new(%args);
89              
90             # turn engine into an object
91 13         101 my $engine_class = Plack::Util::load_class($self->engine, 'KelpX::Symbiosis::Engine');
92 13         146 $self->engine($engine_class->new(adapter => $self));
93              
94 13         213 return $self;
95             }
96              
97             1;
98              
99             # This is not internal, but currently no specific documentation is provided.
100             # All of the interface is documented in Kelp::Module::Symbiosis.
101