Middleware.pm

filter: Statementstmt 96.9% 159 / 164 Branchbran 92.3% 24 / 26 Conditioncond 100% 6 / 6 MC/DCmcdc 100% 4 / 4 Subroutinesub 92.1% 35 / 38 Podpod 0.0% 0 / 5 totaltotal 93.8% 228 / 243 SCAR 30.9
CC
22
Cov
96%
__ANON__
10.0
_build_args
4.0
import
3.0
CRAP 22.0
« Middleware/Encode.pm ↑ Summary Middleware/Status.pm »
1 package HTTP::Engine::Middleware;
2 20 ▶use 5.00800;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 20 ▶use Any::Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 use Any::Moose (
5 20 '::Util' => [qw/apply_all_roles/],
6 20 ▶);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 20 our $VERSION = '0.11_01';
8  
9 20 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 has 'middlewares' => (
12 is => 'ro',
13 isa => 'ArrayRef',
14 59 ▶ default => sub { +[] },
Subroutine __ANON__: called CC=1 SCAR=0.0
15 20 );
16  
17 has '_instance_of' => (
18 is => 'rw',
19 isa => 'HashRef',
20 59 ▶ default => sub { +{} },
Subroutine __ANON__: called CC=1 SCAR=0.0
21 20 );
22  
23 has '_instance_ary_ex' => (
24 is => 'rw',
25 isa => 'ArrayRef',
26 59 ▶ default => sub { +[] },
Subroutine __ANON__: called CC=1 SCAR=0.0
27 20 );
28  
29 20 has 'method_class' => (
30 is => 'ro',
31 isa => 'Str',
32 );
33  
34 20 has 'diecatch' => (
35 is => 'rw',
36 isa => 'Bool',
37 );
38  
39 sub init_class {
40 20 ▶ my $klass = shift;
Subroutine init_class: called CC=2 SCAR=7.0
Pod undocumented
41 20 my $meta = any_moose('::Meta::Class')->initialize($klass);
42 20 ▶ $meta->superclasses(any_moose('::Object'))
Branch: 1 branch
BranchTrueFalseTotal
unless $meta->superclasses 20 0 20
43 unless $meta->superclasses;
44  
45 20 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
46 20 ▶ no warnings 'redefine';
Subroutine BEGIN: called CC=1 SCAR=0.0
47 20 ▶ *{ $klass . '::meta' } = sub { $meta };
Subroutine __ANON__: not called CC=1 SCAR=0.4
48 }
49  
50 sub import {
51 40 ▶ my($class, ) = @_;
Subroutine import: called CC=3 SCAR=11.0
52 40 my $caller = caller;
53  
54 40 ▶ return unless $caller =~ /(?:\:)?Middleware\:\:.+/;
Branch: 1 branch
BranchTrueFalseTotal
unless $caller =~ /(?:\:)?Middleware\:\:.+/ 20 20 40
55  
56 20 strict->import;
57 20 warnings->import;
58  
59 20 init_class($caller);
60  
61 20 ▶ if (Any::Moose::is_moose_loaded()) {
Branch: 1 branch
BranchTrueFalseTotal
if (Any::Moose::is_moose_loaded()) { } 1 19 20
62 1 Moose->import({ into_level => 1 });
63 } else {
64 19 Mouse->export_to_level( 1 );
65 }
66  
67 20 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
68 20 *{"$caller\::__MIDDLEWARE__"} = sub {
69 20 ▶ use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
70 20 ▶ my $caller = caller(0);
Subroutine __ANON__: called CC=1 SCAR=0.0
71 20 __MIDDLEWARE__($caller);
72 20 };
73  
74 20 ▶ *{"$caller\::before_handle"} = sub (&) { goto \&before_handle };
Subroutine __ANON__: called CC=1 SCAR=0.0
75 20 ▶ *{"$caller\::after_handle"} = sub (&) { goto \&after_handle };
Subroutine __ANON__: called CC=1 SCAR=0.0
76 20 ▶ *{"$caller\::middleware_method"} = sub { goto \&middleware_method };
Subroutine __ANON__: called CC=1 SCAR=0.0
77 }
78  
79 sub __MIDDLEWARE__ {
80 20 ▶ my ( $caller, ) = @_;
Subroutine __MIDDLEWARE__: called CC=1 SCAR=0.0
81  
82 20 Any::Moose::unimport;
83 20 apply_all_roles( $caller, 'HTTP::Engine::Middleware::Role' );
84  
85 20 $caller->meta->make_immutable( inline_destructor => 1 );
86 20 "MIDDLEWARE";
87 }
88  
89 BEGIN {
90 20 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
91 20 ▶ for my $meth (
Subroutine BEGIN: called CC=2 SCAR=7.2
92 qw(before_handle after_handle middleware_method)
93 )
94 {
95 60 *{__PACKAGE__ . "::$meth"} = sub {
96 0 ▶ Carp::croak("Can't call ${meth} function outside Middleware's load phase");
Subroutine __ANON__: not called CC=1 SCAR=6.9
97 60 };
98 }
99 };
100  
101 # this method's return value is indefinite.
102 sub install {
103 59 ▶ my($self, @middlewares) = @_;
Subroutine install: called CC=1 SCAR=0.0
Pod undocumented
104  
105 59 my $args = $self->_build_args(@middlewares);
106 59 $self->_create_middleware_instance($args);
107 }
108  
109 # this module accepts
110 # $mw->install(qw/HTTP::Engine::Middleware::Foo/);
111 # and
112 # $mw->install('HTTP::Engine::Middleware::Foo' => { arg1 => 'foo'});
113 sub _build_args {
114 59 ▶ my $self = shift;
Subroutine _build_args: called CC=4 SCAR=13.9
115  
116 # basis of Data::OptList
117 59 my @middlewares;
118 59 my $max = scalar(@_);
119 59 ▶ for (my $i = 0; $i < $max ; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $max) 62 59 121
120 62 ▶ if ($i + 1 < $max && ref($_[$i + 1])) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i + 1 < $max and ref $_[$i + 1]) { } 50 12 62
Condition100%3/3
$i + 1 < $max and ref $_[$i + 1]
!ll&&!rl&&r
9350
MC/DC100%2/2
$i + 1 < $max and ref $_[$i + 1]
$i + 1 < $max ref $_[$i + 1]
Truth table100%3/3
A and B
A= $i + 1 < $maxB= ref $_[$i + 1]
ABresult
0X0
100
111
121 50 push @middlewares, [ $_[$i++] => $_[$i] ];
122 } else {
123 12 push @middlewares, [ $_[$i] => {} ];
124 }
125 }
126  
127 59 return \@middlewares;
128 }
129  
130 # load & create middleware instance
131 20 my %IS_INITIALIZED;
132 sub _create_middleware_instance {
133 59 ▶ my ($self, $args) = @_;
Subroutine _create_middleware_instance: called CC=3 SCAR=11.0
134  
135 59 my %instances;
136 59 for my $stuff (@$args) {
137 62 my $klass = $stuff->[0];
138 62 my $config = $stuff->[1];
139  
140 62 ▶ unless ($IS_INITIALIZED{$klass}++) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($IS_INITIALIZED{$klass}++) 20 42 62
141 20 $self->_init_middleware_class($klass);
142 }
143  
144 62 my $instance = $klass->new(
145 %$config,
146 before_handles => [$klass->_before_handles()],
147 after_handles => [$klass->_after_handles() ],
148 );
149  
150 62 push @{ $self->_instance_ary_ex }, $instance;
151 62 push @{ $self->middlewares }, $klass;
152 62 push @{ $self->_instance_of->{$klass} }, $instance;
153 }
154 }
155  
156 # load one middleware 'class'
157 sub _init_middleware_class {
158 20 ▶ my ($self, $klass,) = @_;
Subroutine _init_middleware_class: called CC=1 SCAR=0.0
159  
160 20 my @before_handles;
161 my @after_handles;
162  
163 20 ▶ no warnings 'redefine';
Subroutine BEGIN: called CC=1 SCAR=0.0
164  
165 20 ▶ local *before_handle = sub { push @before_handles, @_ };
Subroutine __ANON__: called CC=1 SCAR=0.0
166 20 ▶ local *after_handle = sub { push @after_handles, @_ };
Subroutine __ANON__: called CC=1 SCAR=0.0
167 local *middleware_method = sub {
168 7 ▶ my($method, $code) = @_;
Subroutine __ANON__: called CC=3 SCAR=11.0
169 7 my $method_class = $self->method_class;
170 7 ▶ if ($method =~ /^(.+)\:\:([^\:]+)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($method =~ /^(.+)\:\:([^\:]+)$/) 3 4 7
171 3 ($method_class, $method) = ($1, $2);
172 }
173 7 ▶ return unless $method_class;
Branch: 1 branch
BranchTrueFalseTotal
unless $method_class 1 6 7
174  
175 20 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
176 6 *{"$klass\::$method"} = $code;
177 6 *{"$method_class\::$method"} = $code;
178 20 };
179  
180 20 Any::Moose::load_class($klass);
181  
182 20 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
183 20 ▶ *{"${klass}::_before_handles"} = sub () { @before_handles };
Subroutine __ANON__: called CC=1 SCAR=0.0
184 20 ▶ *{"${klass}::_after_handles"} = sub () { @after_handles };
Subroutine __ANON__: called CC=1 SCAR=0.0
185 }
186  
187 sub is_class_loaded {
188 0 ▶ my $class = shift;
Subroutine is_class_loaded: not called CC=1 SCAR=6.9
Pod undocumented
189 0 return Any::Moose::is_class_loaded($class);
190 }
191  
192 sub instance_of {
193 1 ▶ my($self, $name) = @_;
Subroutine instance_of: called CC=2 SCAR=7.6
Pod undocumented
194 1 my $stuff = $self->_instance_of->{$name};
195 1 ▶ return wantarray ? @{$stuff} : $stuff->[0];
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 1 1
196 }
197  
198 sub handler {
199 58 ▶ my($self, $handle) = @_;
Subroutine handler: called CC=1 SCAR=0.0
Pod undocumented
200  
201 sub {
202 64 ▶ my $req = shift;
Subroutine __ANON__: called CC=10 SCAR=23.0
203  
204 64 my $res;
205 my @run_middlewares;
206 LOOP:
207 64 for my $instance (@{ $self->_instance_ary_ex }) {
208 65 for my $code (@{ $instance->before_handles }) {
209 58 my $ret = $code->($self, $instance, $req);
210 45 ▶ if ($ret->isa('HTTP::Engine::Response')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ret->isa("HTTP::Engine::Response")) 19 26 45
211 19 $res = $ret;
212 19 last LOOP;
213 }
214 26 $req = $ret;
215 }
216 33 push @run_middlewares, $instance;
217 }
218 51 my $msg;
219 51 ▶ unless ($res) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($res) 32 19 51
220 32 $self->diecatch(0);
221 32 local $@;
222 32 eval { $res = $handle->($req) };
223 32 ▶ $msg = $@ if !$self->diecatch && $@;
Branch: 1 branch
BranchTrueFalseTotal
if not $self->diecatch and $@ 1 31 32
Condition100%3/3
not $self->diecatch and $@
!ll&&!rl&&r
1301
MC/DC100%2/2
not $self->diecatch and $@
not $self->diecatch $@
Truth table100%3/3
not(A) and B
A= not $self->diecatchB= $@
ABresult
0X0
100
111
224 }
225 51 ▶ die $msg if $msg;
Branch: 1 branch
BranchTrueFalseTotal
if $msg 1 50 51
226 50 for my $instance (reverse @run_middlewares) {
227 33 for my $code (reverse @{ $instance->after_handles }) {
228 14 $res = $code->($self, $instance, $req, $res);
229 }
230 }
231  
232 50 $res;
233 58 };
234 }
235  
236 20 1;
237 __END__
238  
239 =for stopwords Daisuke Maki dann hidek marcus nyarla API middlewares
240  
241 =encoding utf8
242  
243 =head1 NAME
244  
245 HTTP::Engine::Middleware - middlewares distribution
246  
247 =head1 WARNING! WARNING!
248  
249 THIS MODULE IS IN ITS ALPHA QUALITY. THE API MAY CHANGE IN THE FUTURE
250  
251 =head1 SYNOPSIS
252  
253 simply
254  
255 my $mw = HTTP::Engine::Middleware->new;
256 $mw->install(qw/ HTTP::Engine::Middleware::DebugScreen HTTP::Engine::Middleware::ReverseProxy /);
257 HTTP::Engine->new(
258 interface => {
259 module => 'YourFavoriteInterfaceHere',
260 request_handler => $mw->handler( \&handler ),
261 }
262 )->run();
263  
264 method injection middleware
265  
266 my $mw = HTTP::Engine::Middleware->new({ method_class => 'HTTP::Engine::Request' });
267 $mw->install(qw/ HTTP::Engine::Middleware::DebugScreen HTTP::Engine::Middleware::ReverseProxy /);
268 HTTP::Engine->new(
269 interface => {
270 module => 'YourFavoriteInterfaceHere',
271 request_handler => $mw->handler(sub {
272 my $req = shift;
273 HTTP::Engine::Response->new( body => $req->mobile_attribute );
274 })
275 }
276 )->run();
277  
278 =head1 DESCRIPTION
279  
280 HTTP::Engine::Middleware is official middlewares distribution of HTTP::Engine.
281  
282 =head1 WISHLIST
283  
284 Authentication
285  
286 OpenID
287  
288 mod_rewrite ( someone write :p )
289  
290 and more ideas
291  
292 =head1 AUTHOR
293  
294 Kazuhiro Osawa E<lt>ko@yappo.ne.jpE<gt>
295  
296 Daisuke Maki
297  
298 Tokuhiro Matsuno E<lt>tokuhirom@gmail.comE<gt>
299  
300 nyarla
301  
302 marcus
303  
304 hidek
305  
306 walf443
307  
308 Takatoshi Kitano E<lt>techmemo@gmail.com<gt>
309  
310 =head1 SEE ALSO
311  
312 L<HTTP::Engine>
313  
314 =head1 REPOSITORY
315  
316 svn co http://svn.coderepos.org/share/lang/perl/HTTP-Engine-Middleware/trunk HTTP-Engine-Middleware
317  
318 HTTP::Engine::Middleware's Subversion repository is hosted at L<http://coderepos.org/share/>.
319 patches and collaborators are welcome.
320  
321 =head1 LICENSE
322  
323 This library is free software; you can redistribute it and/or modify
324 it under the same terms as Perl itself.
325  
326 =cut
« Middleware/Encode.pm ↑ Summary Middleware/Status.pm »