blib/lib/Magpie/Event.pm

filter: Statementstmt 92.6% 228 / 246 Branchbran 78.7% 63 / 80 Conditioncond 57.1% 24 / 42 MC/DCmcdc 28.0% 7 / 25 Subroutinesub 89.1% 41 / 46 Podpod 0.0% 0 / 21 totaltotal 78.9% 363 / 460 CC 62 SCAR 16.3
CC
62
Cov
86%
run_handler
10.5
handle_symbol
9.9
remove_from_queue
9.0
CRAP 5.1
« blib/lib/Magpie/Component.pm ↑ Summary blib/lib/Magpie/Dispatcher/Env.pm »
1 package Magpie::Event;
2 49 $Magpie::Event::VERSION = '1.141660';
3 # ABSTRACT: Core Event Role Shared By All Magpie Classes
4 49 ▶use Moose::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 49 with qw( Magpie::Event::Symbol Magpie::Types );
7  
8 49 ▶use Magpie::Constants;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 49 ▶use Magpie::SymbolTable;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 49 ▶use Magpie::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 49 ▶use Class::Load;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 49 ▶use Plack::Request;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 49 ▶use Plack::Response;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 49 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 49 ▶BEGIN { $SIG{__DIE__} = sub { Carp::confess(@_) } }
Subroutine BEGIN: called CC=1 SCAR=1.2
Subroutine __ANON__: not called CC=1 SCAR=1.2
17  
18  
19 has plack_request => (
20 is => 'rw',
21 isa => 'Plack::Request',
22 5 ▶ default => sub { Plack::Request->new({}); },
Subroutine __ANON__: called CC=1 SCAR=0.0
23 49 reader => 'request',
24 );
25  
26 has plack_response => (
27 is => 'rw',
28 isa => 'Plack::Response',
29 120 ▶ default => sub { Plack::Response->new(200); },
Subroutine __ANON__: called CC=1 SCAR=0.0
30 reader => 'response',
31 predicate => 'has_response',
32 765 ▶ trigger => sub { shift->response_changed(1) },
Subroutine __ANON__: called CC=1 SCAR=0.0
33 49 );
34  
35 49 has response_changed => (
36 is => 'rw',
37 isa => 'Bool',
38 default => 0,
39 );
40  
41 49 has parent_handler => (
42 is => 'rw',
43 predicate => 'has_parent_handler',
44 weak_ref => 1,
45 );
46  
47 49 has error => (
48 is => 'rw',
49 isa => 'SmartHTTPError',
50 coerce => 1,
51 predicate => 'has_error',
52 writer => 'set_error',
53 clearer => 'clear_error',
54 );
55  
56 has handlers => (
57 traits => ['Array'],
58 is => 'rw',
59 isa => 'ArrayRef[ArrayRef]',
60 497 ▶ default => sub { [] },
Subroutine __ANON__: called CC=1 SCAR=0.0
61 49 handles => {
62 push_handlers => 'push',
63 pop_handlers => 'pop',
64 shift_handlers => 'shift',
65 unshift_handlers => 'unshift',
66 clear_handlers => 'clear',
67 },
68  
69 );
70  
71 has event_queue => (
72 traits => ['Array'],
73 is => 'rw',
74 isa => 'ArrayRef[Str]',
75 497 ▶ default => sub { [] },
Subroutine __ANON__: called CC=1 SCAR=0.0
76 49 handles => {
77 push_queue => 'push',
78 pop_queue => 'pop',
79 shift_queue => 'shift',
80 unshift_queue => 'unshift',
81 free_queue => 'clear',
82 },
83 );
84  
85 has registered_handlers => (
86 traits => ['Hash'],
87 is => 'rw',
88 isa => 'HashRef[Object]',
89 497 ▶ default => sub { {} },
Subroutine __ANON__: called CC=1 SCAR=0.0
90 49 handles => {
91 register_handler => 'set',
92 fetch_handler => 'get',
93 unregister_handler => 'delete',
94 },
95 );
96  
97 49 has current_handler => (
98 is => 'rw',
99 predicate => 'has_current_handler',
100 );
101  
102 49 has current_handler_args => (
103 is => 'rw',
104 predicate => 'has_current_handler_args',
105 );
106  
107 sub BUILD{
108 497 ▶ my $self = shift;
Subroutine BUILD: called CC=2 SCAR=6.9
Pod undocumented
109 497 $self->init_common_symbols();
110 497 ▶ $self->init_symbols() if $self->can('init_symbols');
Branch: 1 branch
BranchTrueFalseTotal
if $self->can("init_symbols") 496 1 497
111 };
112  
113 # Class methods
114 49 our %registered_events = ();
115  
116 49 __PACKAGE__->register_events(qw( next_in_pipe load_handler run_handler ) );
117  
118 sub register_events {
119 221 ▶ my $pkg = shift;
Subroutine register_events: called CC=3 SCAR=11.2
Pod undocumented
120 221 ▶ if ( scalar @_ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @_) 221 0 221
121 221 ▶ $registered_events{$pkg} ||= [];
Condition50%1/2
$Magpie::Event::registered_events{$pkg} ||= []
l!l
0221
MC/DC0%0/1
$Magpie::Event::registered_events{$pkg} ||= []
$Magpie::Event::registered_events{$pkg}
Truth table50%1/2
A
A= $Magpie::Event::registered_events{$pkg}
Aresult
11
00
122 221 push @{ $registered_events{$pkg} }, @_;
123 }
124 221 return $pkg->registered_events;
125 }
126  
127 sub registered_events {
128 1443 ▶ my $thing = shift;
Subroutine registered_events: called CC=3 SCAR=11.0
Pod undocumented
129 1443 ▶ my $pkg = blessed( $thing ) ? $thing->meta->name : $thing;
Branch: 1 branch
BranchTrueFalseTotal
builtin::blessed($thing) ? : 0 1443 1443
130 1443 ▶ my $ref = $registered_events{$pkg} || [];
Condition100%2/2
$Magpie::Event::registered_events{$pkg} || []
l!l
599844
MC/DC100%1/1
$Magpie::Event::registered_events{$pkg} || []
$Magpie::Event::registered_events{$pkg}
Truth table100%2/2
A
A= $Magpie::Event::registered_events{$pkg}
Aresult
11
00
131 1443 return @{ $ref };
132 }
133  
134 #-------------------------------------------------------------------------------
135 # stop_application() stops everything dead in its tracks, potentially
136 # calling itself for all parent handlers as well.
137 # DO NOT confuse this with the harmless end_application() which only adds
138 # a hook for doing clean-up, etc.
139 #-------------------------------------------------------------------------------
140 sub stop_application {
141 4 ▶ my $self = shift;
Subroutine stop_application: called CC=6 SCAR=18.4
Pod undocumented
142 4 my $ctxt = shift;
143  
144 4 $self->free_queue();
145 4 $self->clear_handlers();
146 4 ▶ if ($self->has_parent_handler) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_parent_handler) 2 2 4
147 2 ▶ if ($self->isa('Magpie::Resource')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->isa('Magpie::Resource')) { } 1 1 2
148 1 ▶ $self->parent_handler->resource($self) if $self->parent_handler->can('resource');
Branch: 1 branch
BranchTrueFalseTotal
if $self->parent_handler->can("resource") 1 0 1
149 }
150 else {
151 1 ▶ $self->parent_handler->resource($self->resource) if $self->can('resource') && $self->parent_handler->can('resource');
Branch: 1 branch
BranchTrueFalseTotal
if $self->can("resource") and $self->parent_handler->can("resource") 0 1 1
Condition33%1/3
$self->can("resource") and $self->parent_handler->can("resource")
!ll&&!rl&&r
100
MC/DC0%0/2
$self->can("resource") and $self->parent_handler->can("resource")
$self->can("resource") $self->parent_handler->can("resource")
Truth table33%1/3
A and B
A= $self->can("resource")B= $self->parent_handler->can("resource")
ABresult
0X0
100
111
152 }
153 2 $self->parent_handler->stop_application;
154 }
155 }
156  
157 #-------------------------------------------------------------------------------
158 # next_in_pipe
159 # Event queue method for transitioning from one handler to the next.
160 #-------------------------------------------------------------------------------
161 sub next_in_pipe {
162 887 ▶ my $self = shift;
Subroutine next_in_pipe: called CC=2 SCAR=6.9
Pod undocumented
163 887 my $ctxt = shift;
164  
165 887 my $tuple = $self->shift_handlers;
166 887 ▶ if ( defined $tuple ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $tuple) 386 501 887
167 386 $self->current_handler( $tuple->[0] );
168 386 $self->current_handler_args( $tuple->[1] );
169 386 $self->add_to_queue( 'load_handler' );
170 }
171  
172 887 return OK;
173 }
174  
175 #-------------------------------------------------------------------------------
176 # load_handler($context)
177 # Event queue method that checks to see if the currently selected handler
178 # class has been instantiated and registered in the loaded_handler table. If
179 # not, it loads that class, calls its constructor, sets the current Machine
180 # as the parent handler, and registers the handler in loaded_handler.
181 # It then adds the 'run_handler' method to the event queue to keep the pipline
182 # "moving" forward.
183 #-------------------------------------------------------------------------------
184 sub load_handler {
185 386 ▶ my $self = shift;
Subroutine load_handler: called CC=8 SCAR=21.8
Pod undocumented
186 386 my $ctxt = shift;
187  
188 386 my $handler = $self->current_handler;
189 386 ▶ my $handler_args = $self->current_handler_args || {};
Condition50%1/2
$self->current_handler_args || {}
l!l
3860
MC/DC0%0/1
$self->current_handler_args || {}
$self->current_handler_args
Truth table50%1/2
A
A= $self->current_handler_args
Aresult
11
00
190 #warn "load: current handler: $handler " . $self->has_error ." \n";
191  
192 386 ▶ unless ( defined $self->fetch_handler( $handler ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->fetch_handler($handler)) 377 9 386
193 # we only make it here if the app class was passed
194 # to the pipeline as the *name* of a class, rather
195 # than a blessed instance
196 377 my $new_handler;
197 377 my $handler_error = undef;
198  
199 try {
200 377 ▶ Class::Load::load_class( $handler );
Subroutine __ANON__: called CC=1 SCAR=0.0
201 }
202 catch {
203 0 ▶ $handler_error = "Fatal error loading handler class '$handler': $_ \n";
Subroutine __ANON__: not called CC=1 SCAR=6.9
204 #$self->set_error({ status_code => 500, reason => $error });
205  
206 377 };
207  
208 377 ▶ if ( $handler_error ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($handler_error) 0 377 377
209 0 $self->set_error({ status_code => 500, reason => $handler_error });
210 0 return HANDLER_ERROR;
211 }
212  
213 377 ▶ if ( $handler->isa('Plack::Middleware') ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($handler->isa("Plack::Middleware")) 2 375 377
214 2 Class::Load::load_class( 'Magpie::Transformer::Middleware' );
215 2 my $munged_args = {
216 middleware_args => $handler_args,
217 middleware_class => $handler,
218 };
219 2 $handler = 'Magpie::Transformer::Middleware';
220 2 $handler_args = $munged_args;
221 2 $self->current_handler($handler);
222 2 $self->current_handler_args($handler_args);
223 }
224  
225 377 ▶ my $constructor = defined($handler_args->{traits}) ? 'new_with_traits' : 'new';
Branch: 1 branch
BranchTrueFalseTotal
defined $handler_args->{'traits'} ? : 13 364 377
226  
227 try {
228 $new_handler = $handler->$constructor(
229 377 ▶ %{ $handler_args },
Subroutine __ANON__: called CC=3 SCAR=12.0
Branch: 1 branch
BranchTrueFalseTotal
$self->has_parent_handler ? : 0 377 377
Condition50%1/2
$handler->$constructor(%{$handler_args;}, 'parent_handler', $self, 'plack_request', $self->plack_request, 'plack_response', $self->plack_response, 'breadboard', $self->has_parent_handler ? $self->parent_handler->breadboard : $self->breadboard, 'resource', $self->resource) || die("Error loading handler $!")
l!l
3770
MC/DC0%0/1
$handler->$constructor(%{$handler_args;}, 'parent_handler', $self, 'plack_request', $self->plack_request, 'plack_response', $self->plack_response, 'breadboard', $self->has_parent_handler ? $self->parent_handler->breadboard : $self->breadboard, 'resource', $self->resource) || die("Error loading handler $!")
$handler->$constructor(%{$handler_args;}, 'parent_handler', $self, 'plack_request', $self->plack_request, 'plack_response', $self->plack_response, 'breadboard', $self->has_parent_handler ? $self->parent_handler->breadboard : $self->breadboard, 'resource', $self->resource)
Truth table50%1/2
A
A= $handler->$constructor(%{$handler_args;}, 'parent_handler', $self, 'plack_request', $self->plack_request, 'plack_response', $self->plack_response, 'breadboard', $self->has_parent_handler ? $self->parent_handler->breadboard : $self->breadboard, 'resource', $self->resource)
Aresult
11
00
230 parent_handler => $self,
231 plack_request => $self->plack_request,
232 plack_response => $self->plack_response,
233 breadboard => ($self->has_parent_handler ? $self->parent_handler->breadboard : $self->breadboard),
234 resource => $self->resource,
235 ) || die "Error loading handler $!";
236  
237 }
238 catch {
239 0 ▶ $handler_error = "Fatal error during build for class '$handler': $_\n";
Subroutine __ANON__: not called CC=1 SCAR=6.9
240 #$self->set_error({ status_code => 500, reason => $error });
241 377 };
242  
243 377 ▶ if ( $handler_error ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($handler_error) 0 377 377
244 0 $self->set_error({ status_code => 500, reason => $handler_error });
245 0 return HANDLER_ERROR;
246 }
247 #return HANDLER_ERROR if $self->has_error;
248  
249 #$new_handler->parent_handler( $self );
250 377 $self->register_handler( $handler => $new_handler );
251 }
252  
253 386 ▶ if ($self->fetch_handler( $handler )) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->fetch_handler($handler)) 386 0 386
254 386 $self->add_to_queue( "run_handler" );
255 }
256  
257 386 return OK;
258 }
259  
260 #-------------------------------------------------------------------------------
261 # run_handler($context)
262 # Run the instance of the currently selected handler class, passing in the
263 # application's context member. This method is called by the parent classes'
264 # event queue (see init_queue() in this class)
265 #-------------------------------------------------------------------------------
266 sub run_handler {
267 386 ▶ my $self = shift;
Subroutine run_handler: called CC=10 SCAR=23.5
Pod undocumented
268 386 my $ctxt = shift;
269 386 my $handler = $self->current_handler;
270 386 ▶ my $handler_args = $self->current_handler_args || {};
Condition50%1/2
$self->current_handler_args || {}
l!l
3860
MC/DC0%0/1
$self->current_handler_args || {}
$self->current_handler_args
Truth table50%1/2
A
A= $self->current_handler_args
Aresult
11
00
271 #warn "run handler: $handler\n";
272  
273 386 ▶ if ( my $h = $self->fetch_handler( $handler ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $h = $self->fetch_handler($handler)) { } 386 0 386
274 # class may be loaded but params may be different
275 386 my @attributes = $h->meta->get_all_attributes;
276 386 foreach my $param (keys( %{$handler_args})) {
277 100 foreach my $attr (@attributes) {
278 1651 ▶ next unless $attr->has_writer || $attr->has_accessor;
Branch: 1 branch
BranchTrueFalseTotal
unless $attr->has_writer or $attr->has_accessor 187 1464 1651
Condition100%3/3
$attr->has_writer or $attr->has_accessor
l!l&&r!l&&!r
1001364187
MC/DC100%2/2
$attr->has_writer or $attr->has_accessor
$attr->has_writer $attr->has_accessor
Truth table100%3/3
A or B
A= $attr->has_writerB= $attr->has_accessor
ABresult
1X1
011
000
279 1464 ▶ my $method = $attr->get_write_method || $attr->accessor;
Condition33%1/3
$attr->get_write_method || $attr->accessor
l!l&&r!l&&!r
146400
MC/DC0%0/2
$attr->get_write_method || $attr->accessor
$attr->get_write_method $attr->accessor
Truth table33%1/3
A || B
A= $attr->get_write_methodB= $attr->accessor
ABresult
1X1
011
000
280 1464 ▶ next unless $param eq $method;
Branch: 1 branch
BranchTrueFalseTotal
unless $param eq $method 1393 71 1464
281 71 $h->$method( $handler_args->{$param} );
282 }
283 }
284 # "Running handler $handler \n";
285 try {
286 386 ▶ $h->run( $ctxt );
Subroutine __ANON__: called CC=1 SCAR=0.0
287 }
288 catch {
289 0 ▶ my $error = "Fatal error running handler '$handler': $_";
Subroutine __ANON__: not called CC=1 SCAR=6.9
290 0 $self->set_error({ status_code => 500, reason => $_ });
291 386 };
292  
293 # propagate errors up the handler stack
294 386 ▶ if ( $h->has_error ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($h->has_error) 5 381 386
295 5 $self->set_error( $h->error );
296 }
297  
298 # remember, nesting.
299 386 $self->plack_response( $h->plack_response );
300 386 $self->breadboard( $h->breadboard );
301 386 $self->add_to_queue( "next_in_pipe" );
302  
303 }
304 else {
305 0 return QUEUE_ERROR;
306 }
307 386 return OK;
308 }
309  
310 #-------------------------------------------------------------------------------
311 # add_handler()
312 # Add a handler into the end of the event queue.
313 #-------------------------------------------------------------------------------
314 sub add_handler {
315 6 ▶ my $self = shift;
Subroutine add_handler: called CC=4 SCAR=15.2
Pod undocumented
316 6 my $handler = shift;
317 6 ▶ my $args = shift || {};
Condition50%1/2
shift() || {}
l!l
06
MC/DC0%0/1
shift() || {}
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
318 6 ▶ if ( defined $handler && length $handler ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $handler and length $handler) 6 0 6
Condition33%1/3
defined $handler and length $handler
!ll&&!rl&&r
006
MC/DC0%0/2
defined $handler and length $handler
defined $handler length $handler
Truth table33%1/3
A and B
A= defined $handlerB= length $handler
ABresult
0X0
100
111
319 6 $self->push_handlers([ $handler, $args ]);
320 }
321 }
322  
323 #-------------------------------------------------------------------------------
324 # add_next_handler()
325 # Add a handler into the front of the event queue.
326 #-------------------------------------------------------------------------------
327 sub add_next_handler {
328 1 ▶ my $self = shift;
Subroutine add_next_handler: called CC=4 SCAR=15.2
Pod undocumented
329 1 my $handler = shift;
330 1 ▶ my $args = shift || {};
Condition50%1/2
shift() || {}
l!l
01
MC/DC0%0/1
shift() || {}
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
331  
332 1 ▶ if ( defined $handler && length $handler ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $handler and length $handler) 1 0 1
Condition33%1/3
defined $handler and length $handler
!ll&&!rl&&r
001
MC/DC0%0/2
defined $handler and length $handler
defined $handler length $handler
Truth table33%1/3
A and B
A= defined $handlerB= length $handler
ABresult
0X0
100
111
333 1 $self->unshift_handlers([$handler, $args]);
334 }
335 }
336  
337 #-------------------------------------------------------------------------------
338 # add_handlers( @list )
339 # Add a list of handlers to the event queue.
340 #-------------------------------------------------------------------------------
341 sub add_handlers {
342 3 ▶ my $self = shift;
Subroutine add_handlers: called CC=1 SCAR=0.0
Pod undocumented
343 3 my @handlers = @_;
344 3 @handlers = Magpie::Util::make_tuples( @handlers );
345 3 $self->push_handlers(@handlers);
346 }
347  
348 #-------------------------------------------------------------------------------
349 # reset_handlers( @list )
350 # Replaces the current list of handlers with @list .
351 #-------------------------------------------------------------------------------
352 sub reset_handlers {
353 1 ▶ my $self = shift;
Subroutine reset_handlers: called CC=1 SCAR=0.0
Pod undocumented
354 1 my @handlers = @_;
355 1 $self->clear_handlers;
356 1 return $self->add_handlers( @handlers );
357 }
358  
359 ▶sub end_application {
Subroutine end_application: called CC=1 SCAR=0.0
Pod undocumented
360 #warn "implement end_application already, will you?\n";
361 }
362  
363 has server_status => (
364 is => 'rw',
365 isa => 'Int',
366 497 ▶ default => sub { 200 },
Subroutine __ANON__: called CC=1 SCAR=0.0
367 49 );
368  
369 sub init_common_symbols {
370 497 ▶ my $self = shift;
Subroutine init_common_symbols: called CC=1 SCAR=0.0
Pod undocumented
371 497 $self->add_symbol_handler( next_in_pipe => \&next_in_pipe );
372 497 $self->add_symbol_handler( load_handler => \&load_handler );
373 497 $self->add_symbol_handler( run_handler => \&run_handler );
374 }
375  
376 #-------------------------------------------------------------------------------
377 # handle_symbol( $context, $symbol_name )
378 # Here's teh beef!
379 # Accepting the current context member and a symbol name as arguments, this
380 # method fetches the list of handler subs associated with $symbol_name and
381 # fires each of them in turn (passing in the $context). The return codes from
382 # each sub is examined (see Magpie::Constants, and the handle_* subs below)
383 # and the handler's program flow is controlled accordingly. If all subs
384 # return OK (200) this method does not intervene-- each sub is fired and
385 # we return OK to the main event loop (which will then move to the next symbol
386 # in the queue).
387 #-------------------------------------------------------------------------------
388 sub handle_symbol {
389 2211 ▶ my $self = shift;
Subroutine handle_symbol: called CC=8 SCAR=22.9
Pod undocumented
390 2211 my $ctxt = shift;
391 2211 my $symbol = shift;
392 2211 my $return_code = undef;
393  
394 # warn "Handling symbol: $symbol \n";
395 # load each handler associated with $symbol, run them,
396 # and manipulate program flow if need be based on their
397 # return values
398 2211 foreach my $h ( $self->get_symbol_handler( $symbol ) ) {
399 try {
400 2211 ▶ $return_code = $h->($self, $ctxt);
Subroutine __ANON__: called CC=1 SCAR=0.0
401 }
402 catch {
403 0 ▶ $self->set_error({ status_code => 500, reason => $_ });
Subroutine __ANON__: not called CC=1 SCAR=6.9
404 #warn "Error running symbol '$symbol': $_";
405 2211 };
406  
407 2211 ▶ if ( (!length $return_code) or ($return_code >= SERVER_ERROR) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not length $return_code or $return_code >= 500) 0 2211 2211
Condition33%1/3
not length $return_code or $return_code >= 500
l!l&&r!l&&!r
002211
MC/DC0%0/2
not length $return_code or $return_code >= 500
not length $return_code $return_code >= 500
Truth table33%1/3
not(A) or B
A= not length $return_codeB= $return_code >= 500
ABresult
1X1
011
000
408 0 ▶ unless ( $self->has_error ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->has_error) 0 0 0
409 0 $self->set_error({
410 status_code => 500,
411 reason => "Internal error or unknown return code from symbol '$symbol'"
412 });
413 }
414 0 $return_code = DONE;
415 }
416  
417 2211 ▶ return $self->control_done() if $return_code == DONE;
Branch: 1 branch
BranchTrueFalseTotal
if $return_code == 299 2 2209 2211
418 2209 ▶ return $self->control_declined() if $return_code == DECLINED;
Branch: 1 branch
BranchTrueFalseTotal
if $return_code == 199 1 2208 2209
419 2208 ▶ return $self->control_output() if $return_code == OUTPUT;
Branch: 1 branch
BranchTrueFalseTotal
if $return_code == 300 1 2207 2208
420 }
421 2207 return OK;
422 }
423  
424 sub init_queue {
425 501 ▶ my $self = shift;
Subroutine init_queue: called CC=3 SCAR=11.0
Pod undocumented
426 501 my $ctxt = shift;
427  
428 # always first
429 501 $self->add_to_queue( 'next_in_pipe' );
430 501 my $pkg = $self->meta->name;
431 501 my @event_names = ();
432  
433 501 ▶ if ( $self->can('load_queue') ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->can("load_queue")) 386 115 501
434 386 @event_names = $self->load_queue($ctxt);
435 }
436  
437 501 foreach my $event_name ( @event_names ) {
438 553 $self->add_to_queue( $event_name );
439 }
440  
441 501 return OK;
442 }
443  
444 #-------------------------------------------------------------------------------
445 # add_to_queue( $symbol, $priority )
446 #-------------------------------------------------------------------------------
447 sub add_to_queue {
448 2227 ▶ my $self = shift;
Subroutine add_to_queue: called CC=6 SCAR=18.2
Pod undocumented
449 2227 my $symbol = shift;
450 2227 my $priority = shift;
451  
452 #warn "add to queue $symbol";
453 2227 $symbol = $self->_qualify_symbol_name( $symbol );
454  
455 2227 ▶ unless ( $self->has_symbol($symbol) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->has_symbol($symbol)) 0 2227 2227
456 0 warn "Warning: '$symbol' could not be added to the queue. Are you sure you registered it via register_events?";
457 #XXX: should we die or set_error here instead?
458 0 return;
459 }
460  
461 # add events with high priority to the beginning of the stack.
462 2227 ▶ if ( defined $priority and $priority == 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $priority and $priority == 1) { } 1 2226 2227
Condition100%3/3
defined $priority and $priority == 1
!ll&&!rl&&r
222511
MC/DC100%2/2
defined $priority and $priority == 1
defined $priority $priority == 1
Truth table100%3/3
A and B
A= defined $priorityB= $priority == 1
ABresult
0X0
100
111
463 1 $self->unshift_queue( $symbol );
464 }
465 else {
466 2226 ▶ $self->free_queue() if defined $priority and $priority == -1;
Branch: 1 branch
BranchTrueFalseTotal
if defined $priority and $priority == -1 1 2225 2226
Condition66%2/3
defined $priority and $priority == -1
!ll&&!rl&&r
222501
MC/DC50%1/2
defined $priority and $priority == -1
defined $priority $priority == -1
Truth table66%2/3
A and B
A= defined $priorityB= $priority == -1
ABresult
0X0
100
111
467 2226 $self->push_queue( $symbol );
468 }
469 }
470  
471 #-------------------------------------------------------------------------------
472 # remove_from_queue( $symbol, $priority )
473 #-------------------------------------------------------------------------------
474 sub remove_from_queue {
475 3 ▶ my $self = shift;
Subroutine remove_from_queue: called CC=9 SCAR=22.0
Pod undocumented
476 3 my $symbol = shift;
477 3 ▶ my $priority = shift || 0;
Condition100%2/2
shift() || 0
l!l
21
MC/DC100%1/1
shift() || 0
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
478  
479 3 $symbol = $self->_qualify_symbol_name( $symbol );
480  
481 3 ▶ unless ( $self->has_symbol($symbol) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->has_symbol($symbol)) 0 3 3
482 0 warn "Warning: Unregistered event '$symbol' could not be removed from the queue because it does not exist. Are you sure you registered it via register_events?";
483 0 return;
484 }
485  
486 3 my $f = 0;
487  
488 3 ▶ if ($priority == 0 ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($priority == 0) { } 1 2 3
elsif ($priority == -1) { } 1 1 2
489 1 my @new = grep { $_ ne $symbol } @{$self->event_queue};
490 1 $self->event_queue(\@new);
491 }
492  
493 # remove the last occourence of $symbol
494 elsif ( $priority == -1 ) {
495 5 my @new = reverse grep { defined $_ } map {
496 $_ ne $symbol ?
497 $_ :
498 $f == 1 ?
499 5 ▶ $_ : do{ $f=1; undef }
Branch: 2 branches
BranchTrueFalseTotal
$_ ne $symbol ? : 3 2 5
$f == 1 ? : 1 1 2
500 1 } reverse( @{$self->event_queue} );
501 1 $self->event_queue(\@new);
502 }
503  
504 # otherwise, drop the first occourence of $symbol
505 else {
506 6 my @new = grep { defined $_ } map {
507 $_ ne $symbol ?
508 $_ :
509 $f == 1 ?
510 6 ▶ $_ : do{ $f=1; undef }
Branch: 2 branches
BranchTrueFalseTotal
$_ ne $symbol ? : 4 2 6
$f == 1 ? : 1 1 2
511 1 } @{$self->event_queue};
512 1 $self->event_queue(\@new);
513 }
514 }
515  
516  
517 ################################################################################
518 # Control handlers.
519 ################################################################################
520 # Event handlers that manage program flow in response to the control
521 # codes returned from the various handler subs.
522 #-------------------------------------------------------------------------------
523  
524 #-------------------------------------------------------------------------------
525 # control_done()
526 # Fires when user returns DONE (299) from their handler sub. Is expected to
527 # stop the application immediately.
528 #-------------------------------------------------------------------------------
529 sub control_done {
530 2 ▶ my $self = shift;
Subroutine control_done: called CC=1 SCAR=0.0
Pod undocumented
531 2 $self->stop_application;
532 2 return DONE;
533 }
534  
535 #-------------------------------------------------------------------------------
536 # control_declined()
537 # Fires when user returns DECLINED (199) from their handler sub. Is expected to
538 # nukes the rest of the events associated with the current handler and move
539 # to the next handler class in the queue.
540 #-------------------------------------------------------------------------------
541 sub control_declined {
542 1 ▶ my $self = shift;
Subroutine control_declined: called CC=1 SCAR=0.0
Pod undocumented
543 1 $self->free_queue;
544 1 return OK;
545 }
546  
547 #-------------------------------------------------------------------------------
548 # control_output()
549 # Fires when user returns DECLINED (300) from their handler sub. Is expected to
550 # cause the queue to jump immediately to the Output handler and its queued subs.
551 #-------------------------------------------------------------------------------
552 sub control_output {
553 1 ▶ my $self = shift;
Subroutine control_output: called CC=2 SCAR=7.0
Pod undocumented
554  
555 1 my $new_handlers = [];
556 1 ▶ if ( defined $self->{parent_handler}{handlers} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"parent_handler"}{"handlers"}) 1 0 1
557  
558 # XXX: this is lame because it assumes that the last
559 # parent handler is the Output class
560 # I'll fix it if it really becomes an issue in Real Life(tm)
561 # -ubu
562  
563 1 push @{$new_handlers}, $self->{parent_handler}{handlers}->[-1];
564 1 $self->free_queue;
565 1 $self->{parent_handler}{handlers} = $new_handlers;
566 }
567 1 return OK;
568 }
569  
570 sub run {
571 501 ▶ my $self = shift;
Subroutine run: called CC=4 SCAR=13.9
Pod undocumented
572 501 my $ctxt = shift;
573 501 my $state = OK;
574  
575 501 ▶ $ctxt ||= {};
Condition50%1/2
$ctxt ||= {}
l!l
5010
MC/DC0%0/1
$ctxt ||= {}
$ctxt
Truth table50%1/2
A
A= $ctxt
Aresult
11
00
576  
577 # reinit per each run required for pipelining
578 501 $self->init_queue($ctxt);
579  
580 501 ▶ while ( my $symbol = $self->shift_queue() ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $symbol = $self->shift_queue) 2210 499 2709
581 2210 $state = $self->handle_symbol( $ctxt, $symbol );
582 # if an error occours here we must stop!
583 # warn "state is $state from $symbol";
584 2210 ▶ last unless $state == OK;
Branch: 1 branch
BranchTrueFalseTotal
unless $state == 100 2 2208 2210
585 }
586 501 $self->end_application( $ctxt );
587 501 return $self->server_status;
588 }
589  
590 # SEEALSO: Magpie
591  
592 49 1;
593  
594 __END__
595 =pod
596  
597 =head1 NAME
598  
599 Magpie::Event - Core Event Role Shared By All Magpie Classes
600  
601 =head1 VERSION
602  
603 version 1.141660
604  
605 =head1 AUTHORS
606  
607 =over 4
608  
609 =item *
610  
611 Kip Hampton <kip.hampton@tamarou.com>
612  
613 =item *
614  
615 Chris Prather <chris.prather@tamarou.com>
616  
617 =back
618  
619 =head1 COPYRIGHT AND LICENSE
620  
621 This software is copyright (c) 2011 by Tamarou, LLC.
622  
623 This is free software; you can redistribute it and/or modify it under
624 the same terms as the Perl 5 programming language system itself.
625  
626 =cut
627  
« blib/lib/Magpie/Component.pm ↑ Summary blib/lib/Magpie/Dispatcher/Env.pm »