blib/lib/WebDAO/Engine.pm

filter: Statementstmt 32.4% 61 / 188 Branchbran 9.0% 8 / 88 Conditioncond 12.5% 2 / 16 MC/DCmcdc 0.0% 0 / 10 Subroutinesub 53.8% 14 / 26 Podpod 40.0% 2 / 5 totaltotal 26.1% 87 / 333 CC 55 SCAR 53.8
CC
55
Cov
24%
execute2
650.0
__handle_out__
72.0
__events__
30.0
CRAP 216.6
↑ Summary blib/lib/WebDAO/Container.pm »
1 package WebDAO::Engine;
2  
3 #$Id$
4  
5 =head1 NAME
6  
7 =head1 DESCRIPTION
8  
9 WebDAO::Engine - Class for root object of application model
10  
11 =cut
12  
13 5 our $VERSION = '0.01';
14  
15 5 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 5 ▶use WebDAO::Container;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 5 ▶use WebDAO::Lib::MethodByPath;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 5 ▶use WebDAO::Lib::RawHTML;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 5 ▶use base qw(WebDAO::Container);
Subroutine BEGIN: called CC=1 SCAR=0.0
20 5 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 5 __PACKAGE__->mk_attr( _session=>undef, __obj=>undef, __events=>undef);
24  
25 sub new {
26 1 ▶ my $class = shift;
Subroutine new: called CC=2 SCAR=7.0
Pod documented
27 1 my $self = {};
28 1 my $stat;
29 1 bless( $self, $class );
30 1 ▶ return ( $stat = $self->_init(@_) ) ? $self : $stat;
Branch: 1 branch
BranchTrueFalseTotal
($stat = $self->_init(@_)) ? : 1 0 1
31 }
32  
33 sub _sysinit {
34 1 ▶ my ( $self, $ref ) = @_;
Subroutine _sysinit: called CC=2 SCAR=6.9
35 1 my %hash = @$ref;
36  
37 # Setup $init_hash;
38 1 ▶ my $my_name = $hash{id} || '';
Condition50%1/2
$hash{'id'} || ''
l!l
01
MC/DC0%0/1
$hash{'id'} || ''
$hash{'id'}
Truth table50%1/2
A
A= $hash{'id'}
Aresult
11
00
39 unshift(
40 1 @{$ref},
41 {
42 ref_engine => $self, #! Setup _engine refernce for childs!
43 name_obj => "$my_name"
44 }
45 ); #! Setup _my_name
46 #Save session
47 1 _session $self $hash{session};
48  
49 # name_obj=>"applic"}); #! Setup _my_name
50 1 $self->SUPER::_sysinit($ref);
51  
52 #!init _runtime variables;
53 1 $self->_set_parent($self);
54  
55 #hash "function" -"package"
56 1 $self->__obj( {} );
57  
58 #init hash of evens names -> @Array of pointers of sub in objects
59 1 $self->__events( {} );
60  
61 }
62  
63 sub init {
64 1 ▶ my ( $self, %opt ) = @_;
Subroutine init: called CC=5 SCAR=26.6
Pod undocumented
65  
66 #register default clasess
67 1 $self->register_class(
68 'WebDAO::Lib::RawHTML' => '_rawhtml_element',
69 'WebDAO::Lib::MethodByPath' => '_method_call'
70 );
71  
72 #Register by init classes
73 1 ▶ if ( ref( my $classes = $opt{register} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref(my $classes = $opt{"register"})) 0 1 1
74 0 $self->register_class(%$classes);
75 }
76 1 ▶ if ( my $lexer = $opt{lexer} ) {
Branch: 2 branches
BranchTrueFalseTotal
if (my $lexer = $opt{'lexer'}) { } 0 1 1
elsif (my $lex = $opt{'lex'}) { } 0 1 1
77 0 map { $_->value($self) } @{ $lexer->auto };
78 0 my @objs = map { $_->value($self) } @{ $lexer->tree };
79 0 $self->_add_childs_(@objs);
80 }
81 elsif ( my $lex = $opt{lex} ) {
82 0 ▶ my ( $pre, $fetch, $post ) = @{ $lex->value($self) || [] };
Branch: 1 branch
BranchTrueFalseTotal
$lex->value($self) or [] 0 0 0
83 0 $self->__add_childs__( 0, @$pre );
84 0 $self->_add_childs_( @$fetch );
85 0 $self->__add_childs__( 2, @$post );
86 }
87  
88 }
89  
90 sub response {
91 0 ▶ my $self = shift;
Subroutine response: not called CC=1 SCAR=6.9
Pod documented
92 0 return $self->_session->response_obj;
93 }
94  
95  
96 =head2 __handle_out__ ($sess, @output)
97  
98 Process output by fetch methods
99  
100 =cut
101  
102 sub __handle_out__ {
103 0 ▶ my $self = shift;
Subroutine __handle_out__: not called CC=8 SCAR=42.8
104 0 my $sess = shift;
105 0 for (@_) {
106 0 ▶ if ( UNIVERSAL::isa( $_, 'WebDAO::Element' ) ) {
Branch: 3 branches
BranchTrueFalseTotal
if (UNIVERSAL::isa($_, 'WebDAO::Element')) { } 0 0 0
elsif (ref $_ eq 'CODE') { } 0 0 0
elsif (UNIVERSAL::isa($_, 'WebDAO::Response')) { } 0 0 0
107 0 ▶ $self->__handle_out__( $sess, $_->pre_fetch($sess) )
Branch: 1 branch
BranchTrueFalseTotal
if UNIVERSAL::can($_, "pre_fetch") 0 0 0
108 if UNIVERSAL::can( $_, 'pre_fetch' );
109  
110 0 $self->__handle_out__( $sess, $_->fetch($sess) );
111 0 ▶ $self->__handle_out__( $sess, $_->post_fetch($sess) )
Branch: 1 branch
BranchTrueFalseTotal
if UNIVERSAL::can($_, "post_fetch") 0 0 0
112 if UNIVERSAL::can( $_, 'post_fetch' );
113  
114 }
115 elsif ( ref($_) eq 'CODE' ) {
116 0 return $self->__handle_out__( $sess, $_->($sess) );
117 }
118 elsif ( UNIVERSAL::isa( $_, 'WebDAO::Response' ) ) {
119 0 $_->_is_headers_printed(1);
120 0 ▶ $_->_print_dep_on_context($sess) unless $_->_is_file_send;
Branch: 1 branch
BranchTrueFalseTotal
unless $_->_is_file_send 0 0 0
121 0 $_->flush;
122 0 $_->_destroy;
123  
124 }
125 else {
126 0 $sess->print($_);
127 }
128 }
129 }
130  
131 sub __events__ {
132 0 ▶ my $self = shift;
Subroutine __events__: not called CC=5 SCAR=34.0
133 0 my $root = shift;
134 0 my $inject_fetch = shift;
135 0 my $path = $root->__path2me;
136 0 my @childs = ();
137  
138 #make inject event for objects
139 0 ▶ if ( my $res = $inject_fetch->{$path} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $res = $inject_fetch->{$path}) { } 0 0 0
140 0 @childs = (
141 {
142 fetch => $root->__path2me,
143 pme => $path,
144 ,
145 event => 'inject',
146 obj => $root,
147 res => $res
148 }
149 );
150  
151 }
152 else {
153  
154 0 ▶ if ( UNIVERSAL::isa( $root, 'WebDAO::Container' ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (UNIVERSAL::isa($root, 'WebDAO::Container')) { } 0 0 0
155  
156 #skip modal
157 0 for ( @{ $root->__childs() } ) {
158 0 ▶ push @childs, $self->__events__( $_, $inject_fetch )
Branch: 1 branch
BranchTrueFalseTotal
unless UNIVERSAL::isa($_, "WebDAO::Modal") 0 0 0
159 unless UNIVERSAL::isa( $_, 'WebDAO::Modal' );
160 }
161 }
162 else {
163 0 @childs = (
164 {
165 fetch => $root->__path2me,
166 pme => $path,
167 ,
168 event => 'fetch',
169 obj => $root
170 }
171 );
172 }
173 }
174 0 my @res = (
175 {
176 st_ev => $root->__path2me,
177 pme => $path,
178 event => 'start',
179 obj => $root
180 },
181 @childs,
182 {
183 end_ev => $root->__path2me,
184 pme => $path,
185 event => 'end',
186 obj => $root
187 }
188 );
189 }
190  
191 sub _execute {
192 0 ▶ my $self =shift;
Subroutine _execute: not called CC=1 SCAR=6.9
193 0 return $self->execute2(@_)
194 }
195  
196 sub execute2 {
197 0 ▶ my $self = shift;
Subroutine execute2: not called CC=25 SCAR=64.8
Pod undocumented
198 0 my $sess = shift;
199 0 my $url = shift;
200 0 my @path = @{ $sess->call_path($url) };
201 0 my ( $src, $res ) = $self->_traverse_( $sess, @path );
202 0 my $response = $self->response;
203 #now analyze answers
204 # undef -> not Found
205 0 ▶ unless ( defined($res) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $res) 0 0 0
206 0 $response->error404( "Url not found:" . join "/", @path );
207 0 $response->flush;
208 0 $response->_destroy;
209 0 return; #end
210 }
211  
212 #convert string and ref(scalar) to resonse with html
213 #special handle strings
214 0 ▶ if ( !ref($res) or ( ref($res) eq 'SCALAR' ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not ref $res or ref $res eq "SCALAR") 0 0 0
Condition0%0/3
not ref $res or ref $res eq "SCALAR"
l!l&&r!l&&!r
000
MC/DC0%0/2
not ref $res or ref $res eq "SCALAR"
not ref $res ref $res eq "SCALAR"
Truth table0%0/3
not(A) or B
A= not ref $resB= ref $res eq "SCALAR"
ABresult
1X1
011
000
215 0 ▶ $res = $response->set_html( ref($res) ? $$res : $res );
Branch: 1 branch
BranchTrueFalseTotal
ref $res ? : 0 0 0
216 }
217 #special handle HASH refs ( interpret as json)
218 0 ▶ if ( ( ref($res) eq 'HASH' ) and $response->wantformat('json') ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $res eq "HASH" and $response->wantformat("json")) 0 0 0
Condition0%0/3
ref $res eq "HASH" and $response->wantformat("json")
!ll&&!rl&&r
000
MC/DC0%0/2
ref $res eq "HASH" and $response->wantformat("json")
ref $res eq "HASH" $response->wantformat("json")
Truth table0%0/3
A and B
A= ref $res eq "HASH"B= $response->wantformat("json")
ABresult
0X0
100
111
219 0 $res = $response->set_json( $res );
220 }
221 #check if response modal
222 0 ▶ if ( UNIVERSAL::isa( $res, 'WebDAO::Response' ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (UNIVERSAL::isa($res, "WebDAO::Response")) 0 0 0
223 #check empty response( $r->set_empty)
224 0 ▶ return if $res->is_empty;
Branch: 1 branch
BranchTrueFalseTotal
if $res->is_empty 0 0 0
225 0 ▶ if ( $res->_is_modal() ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res->_is_modal) 0 0 0
226  
227 #handle response
228 0 ▶ $res->_print_dep_on_context($sess, $res) unless $res->_is_file_send;
Branch: 1 branch
BranchTrueFalseTotal
unless $res->_is_file_send 0 0 0
229 0 $res->flush;
230 0 $res->_destroy;
231 0 return;
232 }
233 }
234  
235 #extract all objects to evenets
236 0 my $root = $self;
237  
238 #if object modal ?
239 0 ▶ if ( UNIVERSAL::isa( $src, 'WebDAO::Modal' ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (UNIVERSAL::isa($src, "WebDAO::Modal")) 0 0 0
240  
241 #set him as root of putput
242 0 $root = $src;
243 }
244 0 my $need_inject_result = 1;
245  
246 #special handle strings
247 0 ▶ if ( !ref($res) or ( ref($res) eq 'SCALAR' ) ) {
Branch: 2 branches
BranchTrueFalseTotal
if (not ref $res or ref $res eq 'SCALAR') { } 0 0 0
elsif ($res == $src) { } 0 0 0
Condition0%0/3
not ref $res or ref $res eq "SCALAR"
l!l&&r!l&&!r
000
MC/DC0%0/2
not ref $res or ref $res eq "SCALAR"
not ref $res ref $res eq "SCALAR"
Truth table0%0/3
not(A) or B
A= not ref $resB= ref $res eq "SCALAR"
ABresult
1X1
011
000
248  
249 #now walk
250 }
251 elsif
252  
253 #if result ref to object and it eq $src run flow
254 ( $res == $src ) {
255 0 $need_inject_result = 0;
256 }
257 0 ▶ if ( UNIVERSAL::isa( $res, 'WebDAO::Element' ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (UNIVERSAL::isa($res, "WebDAO::Element")) 0 0 0
258  
259 #nothing to do
260 }
261 0 my %injects = ();
262  
263 #if need inject check flow by path
264 0 ▶ if ($need_inject_result) {
Branch: 1 branch
BranchTrueFalseTotal
if ($need_inject_result) 0 0 0
265 0 $injects{ $src->__path2me } = $res;
266 }
267 #start out
268 0 $response->print_header;
269  
270 0 my @ev_flow = $self->__events__( $root, \%injects );
271 0 foreach my $ev (@ev_flow) {
272 0 my $obj = $ev->{obj};
273  
274 #_log1 $self "DO " . $ev->{event}. " for $obj";
275 0 ▶ if ( $ev->{event} eq 'start' ) {
Branch: 4 branches
BranchTrueFalseTotal
if ($ev->{'event'} eq 'start') { } 0 0 0
elsif ($ev->{'event'} eq 'inject') { } 0 0 0
elsif ($ev->{'event'} eq 'fetch') { } 0 0 0
elsif ($ev->{'event'} eq 'end') { } 0 0 0
276 0 ▶ $self->__handle_out__( $sess, $obj->pre_fetch($sess) )
Branch: 1 branch
BranchTrueFalseTotal
if UNIVERSAL::can($obj, "pre_fetch") 0 0 0
277 if UNIVERSAL::can( $obj, 'pre_fetch' );
278 }
279 elsif ( $ev->{event} eq 'inject' ) {
280 $self->__handle_out__( $sess, $ev->{res} )
281  
282 0 }
283 elsif ( $ev->{event} eq 'fetch' ) {
284  
285 #skip fetch method for container
286  
287 0 ▶ $self->__handle_out__( $sess, $obj->fetch($sess) )
Branch: 1 branch
BranchTrueFalseTotal
if UNIVERSAL::can($obj, "fetch") 0 0 0
288 if UNIVERSAL::can( $obj, 'fetch' );
289  
290 }
291 elsif ( $ev->{event} eq 'end' ) {
292  
293 0 ▶ $self->__handle_out__( $sess, $obj->post_fetch($sess) )
Branch: 1 branch
BranchTrueFalseTotal
if UNIVERSAL::can($obj, "post_fetch") 0 0 0
294 if UNIVERSAL::can( $obj, 'post_fetch' );
295 }
296  
297 }
298 0 $response->flush;
299 0 $response->_destroy;
300 }
301  
302  
303 #fill $self->__events hash event - method
304 sub __register_event__ {
305 0 ▶ my ( $self, $ref_obj, $event_name, $ref_sub ) = @_;
Subroutine __register_event__: not called CC=2 SCAR=17.9
306 0 my $ev_hash = $self->__events;
307 0 ▶ $ev_hash->{$event_name}->{ scalar($ref_obj) } = {
Branch: 1 branch
BranchTrueFalseTotal
if ref $ref_sub 0 0 0
308 ref_obj => $ref_obj,
309 ref_sub => $ref_sub
310 }
311 if ( ref($ref_sub) );
312 0 return 1;
313 }
314  
315 sub __send_event__ {
316 0 ▶ my ( $self, $event_name, @Par ) = @_;
Subroutine __send_event__: not called CC=3 SCAR=24.8
317 0 my $ev_hash = $self->__events;
318 0 ▶ unless ( exists( $ev_hash->{$event_name} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $ev_hash->{$event_name}) 0 0 0
319 0 _log2 $self "WARN: Event $event_name not exists.";
320 0 return 0;
321 }
322 0 foreach my $ref_rec ( keys %{ $ev_hash->{$event_name} } ) {
323 0 my $ref_sub = $ev_hash->{$event_name}->{$ref_rec}->{ref_sub};
324 0 my $ref_obj = $ev_hash->{$event_name}->{$ref_rec}->{ref_obj};
325 0 $ref_obj->$ref_sub( $event_name, @Par );
326 }
327 }
328  
329 =head3 _create_(<name>,<class or alias>,@parameters)
330  
331 create object by <class or alias>.
332  
333 =cut
334  
335 sub _create_ {
336 0 ▶ my ( $self, $name_obj, $name_func, @par ) = @_;
Subroutine _create_: not called CC=5 SCAR=34.0
337 0 ▶ my $pack = $self->_pack4name($name_func) || $name_func;
Condition0%0/3
$self->_pack4name($name_func) || $name_func
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->_pack4name($name_func) || $name_func
$self->_pack4name($name_func) $name_func
Truth table0%0/3
A || B
A= $self->_pack4name($name_func)B= $name_func
ABresult
1X1
011
000
338 0 my $ref_init_hash = {
339 ref_engine => $self->_root_, #! Setup _engine refernce for childs!
340 name_obj => $name_obj
341 }; #! Setup _my_name
342 0 ▶ my $obj_ref =
Branch: 1 branch
BranchTrueFalseTotal
$pack->isa('WebDAO::Element') ? : 0 0 0
343 $pack->isa('WebDAO::Element')
344 ? eval "'$pack'\-\>new(\@par)"
345 : eval "'$pack'\-\>new(\@par)";
346 # ? eval "'$pack'\-\>new(\$ref_init_hash,\@par)"
347 # : eval "'$pack'\-\>new(\@par)";
348 0 ▶ if ($pack->isa('WebDAO::Element') ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pack->isa("WebDAO::Element")) 0 0 0
349 0 $obj_ref->{_engine} = $self->_root_ ;
350 0 $obj_ref->{__my_name} = $name_obj ;
351 0 $obj_ref->_init($ref_init_hash,@par)
352 }
353 0 ▶ $self->_log1("Error in eval: _create_ $@") if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
354 0 return $obj_ref;
355 }
356  
357 sub _createObj {
358 0 ▶ my $self = shift;
Subroutine _createObj: not called CC=1 SCAR=6.9
359  
360 # _deprecated $self "_create_";
361 0 return $self->_create_(@_);
362 }
363  
364 #Get package for functions name
365 sub _pack4name {
366 0 ▶ my ( $self, $name ) = @_;
Subroutine _pack4name: not called CC=2 SCAR=17.9
367 0 my $ref = $self->__obj;
368 0 ▶ return $$ref{$name} if ( exists $$ref{$name} );
Branch: 1 branch
BranchTrueFalseTotal
if exists $ref->{$name} 0 0 0
369 }
370  
371 sub register_class {
372 1 ▶ my ( $self, %register ) = @_;
Subroutine register_class: called CC=6 SCAR=20.3
Pod undocumented
373 1 my $_obj = $self->__obj;
374 1 ▶ while ( my ( $class, $alias ) = each %register ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %register) 2 1 3
375  
376 #check non loaded mods
377 2 my ( $main, $module ) = $class =~ m/(.*\:\:)?(\S+)$/;
378 2 ▶ $main ||= 'main::';
Condition50%1/2
$main ||= "main::"
l!l
20
MC/DC0%0/1
$main ||= "main::"
$main
Truth table50%1/2
A
A= $main
Aresult
11
00
379 2 $module .= '::';
380 5 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
381 2 ▶ unless ( exists $$main{$module} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $main->{$module}) 0 2 2
382 0 _log1 $self "Try use $class";
383 0 eval "use $class";
384 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
385 0 _log1 $self "Error register class :$class with $@ ";
386 0 return "Error register class :$class with $@ ";
387 0 next;
388 }
389 }
390 5 ▶ use strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
391  
392 #check if register_class used for eval ( see Lobject )
393 2 ▶ $$_obj{$alias} = $class if defined $alias;
Branch: 1 branch
BranchTrueFalseTotal
if defined $alias 2 0 2
394 }
395 1 return;
396 }
397  
398 =head3 _commit
399  
400 Method witch called after HTTP request
401  
402 =cut
403  
404 ▶sub _commit {
Subroutine _commit: not called CC=1 SCAR=0.0
405 #nothing by default
406 }
407  
408 sub _destroy {
409 0 ▶ my $self = shift;
Subroutine _destroy: not called CC=1 SCAR=6.9
410 0 $self->SUPER::_destroy;
411 0 $self->_session(undef);
412 0 $self->__obj(undef);
413 0 $self->__events(undef);
414 }
415 5 1;
416 __DATA__
417  
418 =head1 SEE ALSO
419  
420 http://webdao.sourceforge.net
421  
422 =head1 AUTHOR
423  
424 Zahatski Aliaksandr, E<lt>zag@cpan.orgE<gt>
425  
426 =head1 COPYRIGHT AND LICENSE
427  
428 Copyright 2002-2015 by Zahatski Aliaksandr
429  
430 This library is free software; you can redistribute it and/or modify
431 it under the same terms as Perl itself.
432  
433 =cut
434  
↑ Summary blib/lib/WebDAO/Container.pm »