GX/Application.pm

filter: Statementstmt 16.1% 81 / 501 Branchbran 1.0% 2 / 184 Conditioncond 0.0% 0 / 71 MC/DCmcdc 0.0% 0 / 44 Subroutinesub 20.9% 17 / 81 Podpod 100% 19 / 19 totaltotal 13.2% 119 / 900 CC 133 SCAR 38.2
CC
133
Cov
11%
_setup_config
182.0
_setup_components
132.0
remove_hook
110.0
CRAP 45.5
« GX/Database/Pg.pm ↑ Summary GX/Database.pm »
1 # ----------------------------------------------------------------------------------------------------------------------
2 # GX Framework (c) 2009-2011 Jörg A. Uzarek <uzarek@runlevelnull.de>
3 # File: GX/Application.pm
4 # ----------------------------------------------------------------------------------------------------------------------
5  
6 package GX::Application;
7  
8 34 ▶use GX::Callback::Hook;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 34 ▶use GX::Callback::Hook::Queue;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 34 ▶use GX::Callback::Method;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 34 ▶use GX::Exception;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 34 ▶use GX::Meta::Constants qw( REGEX_CLASS_NAME REGEX_MODULE_NAME );
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 34 ▶use Encode ();
Subroutine BEGIN: called CC=1 SCAR=0.0
15 34 ▶use File::Spec ();
Subroutine BEGIN: called CC=1 SCAR=0.0
16 34 ▶use Scalar::Util qw( blessed weaken );
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18  
19 # ----------------------------------------------------------------------------------------------------------------------
20 # Constants
21 # ----------------------------------------------------------------------------------------------------------------------
22  
23 34 ▶use constant {
Subroutine BEGIN: called CC=1 SCAR=0.0
24 34 STATE_RELOADING => -1,
25 STATE_INITIALIZED => 0,
26 STATE_SETUP => 1,
27 STATE_RUNNING => 2
28 34 };
29  
30 34 ▶use constant {
Subroutine BEGIN: called CC=1 SCAR=0.0
31 34 MODE_DEVELOPMENT => 'development',
32 MODE_PRODUCTION => 'production'
33 34 };
34  
35 34 ▶use constant DEFAULT_HOOKS => qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
36 Initialize
37 ProcessConnection
38 ProcessRequest
39 ProcessRequestQuery
40 ProcessRequestHeaders
41 ProcessRequestCookies
42 ProcessRequestBody
43 ProcessSessions
44 ResolveActions
45 DispatchActions
46 FinalizeSessions
47 FinalizeResponse
48 FinalizeResponseBody
49 FinalizeResponseCookies
50 FinalizeResponseHeaders
51 FinalizeResponseStatus
52 SendResponse
53 Cleanup
54 34 );
55  
56  
57 # ----------------------------------------------------------------------------------------------------------------------
58 # Class setup
59 # ----------------------------------------------------------------------------------------------------------------------
60  
61 34 ▶use GX::Class::Singleton ( code_attributes => [ 'Handler' ] );
Subroutine BEGIN: called CC=1 SCAR=0.0
62  
63 34 has 'component_regex' => (
64 isa => 'Scalar',
65 initialize => 1,
66 initializer => '_initialize_component_regex',
67 accessors => {
68 '_get_component_regex' => { type => 'get' }
69 }
70 );
71  
72 34 has 'component_registry' => (
73 isa => 'Hash',
74 initialize => 1,
75 initializer => '_initialize_component_registry',
76 accessors => {
77 '_get_component_registry' => { type => 'get_reference' }
78 }
79 );
80  
81 34 has 'components' => (
82 isa => 'Hash::Ordered',
83 initialize => 1,
84 accessors => {
85 '_get_components' => { type => 'get_reference' },
86 }
87 );
88  
89 34 has 'config' => (
90 isa => 'Hash',
91 initialize => 1,
92 accessors => {
93 '_get_config' => { type => 'get_reference' }
94 }
95 );
96  
97 34 has 'default_components' => (
98 isa => 'Hash',
99 initialize => 1,
100 accessors => {
101 '_get_default_components' => { type => 'get_reference' }
102 }
103 );
104  
105 34 has 'default_encoding' => (
106 isa => 'String',
107 default => 'utf-8-strict',
108 initialize => 1,
109 accessors => {
110 '_get_default_encoding' => { type => 'get' },
111 '_set_default_encoding' => { type => 'set' }
112 }
113 );
114  
115 34 has 'handler_queue' => (
116 isa => 'Scalar',
117 initialize => 1,
118 accessors => {
119 '_get_handler_queue' => { type => 'get' },
120 '_set_handler_queue' => { type => 'set' }
121 }
122 );
123  
124 34 has 'hook_registry' => (
125 isa => 'Hash::Ordered',
126 initialize => 1,
127 accessors => {
128 '_get_hook' => { type => 'get_value' },
129 '_set_hook' => { type => 'set_value' },
130 '_get_hook_names' => { type => 'get_keys' },
131 '_get_hooks' => { type => 'get_values' },
132 '_get_hook_registry' => { type => 'get_reference' }
133 }
134 );
135  
136 34 has 'mode' => (
137 isa => 'String',
138 default => MODE_PRODUCTION,
139 initialize => 1,
140 accessors => {
141 '_get_mode' => { type => 'get' },
142 '_set_mode' => { type => 'set' }
143 }
144 );
145  
146 34 has 'paths' => (
147 isa => 'Hash',
148 initialize => 1,
149 initializer => '_initialize_paths',
150 accessors => {
151 '_get_path' => { type => 'get_value' },
152 '_set_path' => { type => 'set_value' },
153 '_get_paths' => { type => 'get_reference' }
154 }
155 );
156  
157 34 has 'state' => (
158 isa => 'Scalar',
159 initialize => 1,
160 default => STATE_INITIALIZED,
161 accessors => {
162 '_get_state' => { type => 'get' },
163 '_set_state' => { type => 'set' }
164 }
165 );
166  
167 34 has 'watcher' => (
168 isa => 'Scalar',
169 initialize => 1,
170 accessors => {
171 '_get_watcher' => { type => 'get' },
172 '_set_watcher' => { type => 'set' }
173 }
174 );
175  
176 34 build;
177  
178  
179 # ----------------------------------------------------------------------------------------------------------------------
180 # Export mechanism
181 # ----------------------------------------------------------------------------------------------------------------------
182  
183 32 ▶sub import {
Subroutine import: called CC=3 SCAR=11.2
184  
185 32 my $package = shift;
186  
187 32 ▶ return if $package ne __PACKAGE__;
Branch: 1 branch
BranchTrueFalseTotal
if $package ne __PACKAGE__ 0 32 32
188  
189 32 my $class = caller();
190  
191 32 ▶ return if $class eq 'main';
Branch: 1 branch
BranchTrueFalseTotal
if $class eq "main" 0 32 32
192  
193 32 my $meta = GX::Meta::Class->new( $class );
194 32 $meta->inherit_from( $package );
195  
196 32 return;
197  
198 }
199  
200  
201 # ----------------------------------------------------------------------------------------------------------------------
202 # Overloading
203 # ----------------------------------------------------------------------------------------------------------------------
204  
205 use overload
206 0 ▶ 'bool' => sub { 1 },
Subroutine BEGIN: called CC=1 SCAR=0.4
Subroutine __ANON__: not called CC=1 SCAR=6.9
207 0 ▶ '0+' => sub { $_[0] },
Subroutine __ANON__: not called CC=1 SCAR=6.9
208 56 ▶ '""' => sub { ref $_[0] },
Subroutine __ANON__: called CC=1 SCAR=0.0
209 34 'fallback' => 1;
210  
211  
212 # ----------------------------------------------------------------------------------------------------------------------
213 # Public methods
214 # ----------------------------------------------------------------------------------------------------------------------
215  
216 0 ▶sub action {
Subroutine action: not called CC=2 SCAR=17.9
Pod documented
217  
218 0 ▶ return ( $_[0]->controller( $_[1] ) || return undef )->action( $_[2] );
Condition0%0/2
$_[0]->controller($_[1]) || (return undef)
l!l
00
MC/DC0%0/1
$_[0]->controller($_[1]) || (return undef)
$_[0]->controller($_[1])
Truth table0%0/2
A
A= $_[0]->controller($_[1])
Aresult
11
00
219  
220 }
221  
222 0 ▶sub actions {
Subroutine actions: not called CC=1 SCAR=6.9
Pod documented
223  
224 0 return map { $_->actions } $_[0]->controllers;
225  
226 }
227  
228 0 ▶sub components {
Subroutine components: not called CC=1 SCAR=6.9
Pod documented
229  
230 0 return keys %{$_[0]->instance->_get_components};
231  
232 }
233  
234 0 ▶sub default_encoding {
Subroutine default_encoding: not called CC=1 SCAR=6.9
Pod documented
235  
236 0 return $_[0]->instance->_get_default_encoding;
237  
238 }
239  
240 0 ▶sub handlers {
Subroutine handlers: not called CC=1 SCAR=6.9
Pod documented
241  
242 0 return map { $_->all } $_[0]->hooks;
243  
244 }
245  
246 0 ▶sub hook {
Subroutine hook: not called CC=1 SCAR=6.9
Pod documented
247  
248 0 return $_[0]->instance->_get_hook( $_[1] );
249  
250 }
251  
252 0 ▶sub hooks {
Subroutine hooks: not called CC=1 SCAR=6.9
Pod documented
253  
254 0 return $_[0]->instance->_get_hooks;
255  
256 }
257  
258 0 ▶sub log {
Subroutine log: not called CC=6 SCAR=37.4
Pod documented
259  
260 0 my $self = shift->instance;
261  
262 0 ▶ if ( my $logger = $self->logger ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $logger = $self->logger) { } 0 0 0
263  
264 0 $logger->log( @_ );
265  
266 }
267 else {
268  
269 0 my $level;
270  
271 0 ▶ if ( @_ > 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ > 1) { } 0 0 0
272 0 ▶ $level = shift // 'notice';
Condition0%0/2
shift() // "notice"
l!l
00
MC/DC0%0/1
shift() // "notice"
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
273 }
274 else {
275 0 $level = 'notice';
276 }
277  
278 0 for ( @_ ) {
279  
280 0 ▶ defined or next;
Branch: 1 branch
BranchTrueFalseTotal
defined $_ or next 0 0 0
281  
282 0 my $message = $_;
283 0 $message =~ s/\n+$//;
284 0 $message =~ s/\n/\\n/g;
285  
286 0 print STDERR "[$self] [$level] $message\n";
287
288 }
289  
290 }
291  
292 0 return;
293  
294 }
295  
296 30 ▶sub setup {
Subroutine setup: called CC=4 SCAR=29.0
Pod documented
297  
298 30 my $self = shift->instance;
299  
300 0 ▶ if ( @_ % 2 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ % 2) 0 0 0
301 0 complain "Invalid number of arguments";
302 }
303  
304 0 ▶ if ( $self->_is_setup ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_is_setup) 0 0 0
305 0 complain "$self has already been setup";
306 }
307  
308 0 eval {
309  
310 0 $self->_setup_config( @_ );
311 0 $self->_setup_inc;
312 0 $self->_setup_watcher;
313 0 $self->_setup_hooks;
314 0 $self->_setup_handlers;
315 0 $self->_setup_plugins;
316 0 $self->_setup_components;
317 0 $self->_setup_default_components;
318  
319 0 $self->_deploy_plugins;
320 0 $self->_deploy_components;
321  
322 };
323  
324 0 ▶ if ( $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
325 0 GX::Exception->complain(
326 message => "$self setup failed",
327 subexception => $@,
328 verbosity => 1
329 );
330 }
331  
332 0 $self->_is_setup( 1 );
333  
334 0 return;
335  
336 }
337  
338 0 ▶sub mode {
Subroutine mode: not called CC=1 SCAR=6.9
Pod documented
339  
340 0 return $_[0]->instance->_get_mode;
341  
342 }
343  
344 0 ▶sub path {
Subroutine path: not called CC=1 SCAR=6.9
Pod documented
345  
346 0 return $_[0]->instance->_get_path( $_[1] );
347  
348 }
349  
350 0 ▶sub paths {
Subroutine paths: not called CC=1 SCAR=6.9
Pod documented
351  
352 0 return %{$_[0]->instance->_get_paths};
353  
354 }
355  
356 0 ▶sub start {
Subroutine start: not called CC=4 SCAR=30.0
Pod documented
357  
358 0 my $self = shift->instance;
359  
360 0 ▶ if ( $self->_is_running ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_is_running) 0 0 0
361 0 complain "$self is already running";
362 }
363  
364 0 ▶ if ( ! $self->_is_setup ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->_is_setup) 0 0 0
365 0 complain "$self has not been set up properly";
366 }
367  
368 0 eval {
369  
370 0 $self->_start_plugins;
371 0 $self->_start_components;
372  
373 0 $self->_compile_handler_queue;
374  
375 };
376  
377 0 ▶ if ( $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
378 0 GX::Exception->complain(
379 message => "Cannot start $self",
380 subexception => $@,
381 verbosity => 1
382 );
383 }
384  
385 0 $self->_is_running( 1 );
386  
387 0 return;
388  
389 }
390  
391 0 ▶sub watcher {
Subroutine watcher: not called CC=1 SCAR=6.9
Pod documented
392  
393 0 return $_[0]->instance->_get_watcher;
394  
395 }
396  
397 {
398  
399 34 for ( qw( cache controller database logger model session view ) ) {
400  
401 238 my $accessor_name = $_ . 's';
402  
403 0 ▶ my $accessor_code = eval join( "\n",
Subroutine __ANON__: not called CC=1 SCAR=6.0
Subroutine __ANON__: not called CC=1 SCAR=6.0
Subroutine __ANON__: not called CC=1 SCAR=6.0
Subroutine __ANON__: not called CC=1 SCAR=6.0
Subroutine __ANON__: not called CC=1 SCAR=6.0
Subroutine __ANON__: not called CC=1 SCAR=6.0
Subroutine __ANON__: not called CC=1 SCAR=6.0
404 'sub {',
405 ' return values %{$_[0]->instance->_get_component_registry->{' . $_ . '}};',
406 '}'
407 );
408  
409 238 __PACKAGE__->meta->add_method( $accessor_name, $accessor_code );
410  
411 }
412  
413 34 for ( qw( controller model view ) ) {
414  
415 102 my $accessor_name = $_;
416  
417 0 ▶ my $accessor_code = eval join( "\n",
Subroutine __ANON__: not called CC=3 SCAR=24.0
Subroutine __ANON__: not called CC=3 SCAR=24.0
Subroutine __ANON__: not called CC=3 SCAR=24.0
Branch: 3 branches
BranchTrueFalseTotal
unless defined $_[0] 0 0 0
unless defined $_[0] 0 0 0
unless defined $_[0] 0 0 0
Condition0%0/9
$$components{$_[0]} // $$components{ref($self) . '::Controller::' . $_[0]}
l!l&&r!l&&!r
000
$$components{$_[0]} // $$components{ref($self) . '::Model::' . $_[0]}
l!l&&r!l&&!r
000
$$components{$_[0]} // $$components{ref($self) . '::View::' . $_[0]}
l!l&&r!l&&!r
000
MC/DC0%0/2
$$components{$_[0]} // $$components{ref($self) . '::Controller::' . $_[0]}
$$components{$_[0]} $$components{ref($self) . '::Controller::' . $_[0]}
MC/DC0%0/2
$$components{$_[0]} // $$components{ref($self) . '::Model::' . $_[0]}
$$components{$_[0]} $$components{ref($self) . '::Model::' . $_[0]}
MC/DC0%0/2
$$components{$_[0]} // $$components{ref($self) . '::View::' . $_[0]}
$$components{$_[0]} $$components{ref($self) . '::View::' . $_[0]}
Truth table0%0/3
A // B
A= $$components{$_[0]}B= $$components{ref($self) . '::Controller::' . $_[0]}
ABresult
1X1
011
000
Truth table0%0/3
A // B
A= $$components{$_[0]}B= $$components{ref($self) . '::Model::' . $_[0]}
ABresult
1X1
011
000
Truth table0%0/3
A // B
A= $$components{$_[0]}B= $$components{ref($self) . '::View::' . $_[0]}
ABresult
1X1
011
000
418 'sub {',
419 ' my $self = shift->instance;',
420 ' return undef unless defined $_[0];',
421 ' my $components = $self->_get_component_registry->{' . $_ . '};',
422 ' return $components->{$_[0]} // $components->{ ref( $self ) . \'::' . ucfirst( $_ ) . '::\' . $_[0] };',
423 '}'
424 );
425  
426 102 __PACKAGE__->meta->add_method( $accessor_name, $accessor_code );
427  
428 }
429  
430 34 for ( qw( cache database logger session ) ) {
431  
432 136 my $accessor_name = $_;
433  
434 0 ▶ my $accessor_code = eval join( "\n",
Subroutine __ANON__: not called CC=4 SCAR=29.5
Subroutine __ANON__: not called CC=4 SCAR=29.5
Subroutine __ANON__: not called CC=4 SCAR=29.5
Subroutine __ANON__: not called CC=4 SCAR=29.5
Branch: 8 branches
BranchTrueFalseTotal
unless @_ 0 0 0
unless defined $_[0] 0 0 0
unless @_ 0 0 0
unless defined $_[0] 0 0 0
unless @_ 0 0 0
unless defined $_[0] 0 0 0
unless @_ 0 0 0
unless defined $_[0] 0 0 0
Condition0%0/12
$$components{$_[0]} // $$components{ref($self) . '::Cache::' . $_[0]}
l!l&&r!l&&!r
000
$$components{$_[0]} // $$components{ref($self) . '::Database::' . $_[0]}
l!l&&r!l&&!r
000
$$components{$_[0]} // $$components{ref($self) . '::Logger::' . $_[0]}
l!l&&r!l&&!r
000
$$components{$_[0]} // $$components{ref($self) . '::Session::' . $_[0]}
l!l&&r!l&&!r
000
MC/DC0%0/2
$$components{$_[0]} // $$components{ref($self) . '::Cache::' . $_[0]}
$$components{$_[0]} $$components{ref($self) . '::Cache::' . $_[0]}
MC/DC0%0/2
$$components{$_[0]} // $$components{ref($self) . '::Database::' . $_[0]}
$$components{$_[0]} $$components{ref($self) . '::Database::' . $_[0]}
MC/DC0%0/2
$$components{$_[0]} // $$components{ref($self) . '::Logger::' . $_[0]}
$$components{$_[0]} $$components{ref($self) . '::Logger::' . $_[0]}
MC/DC0%0/2
$$components{$_[0]} // $$components{ref($self) . '::Session::' . $_[0]}
$$components{$_[0]} $$components{ref($self) . '::Session::' . $_[0]}
Truth table0%0/3
A // B
A= $$components{$_[0]}B= $$components{ref($self) . '::Cache::' . $_[0]}
ABresult
1X1
011
000
Truth table0%0/3
A // B
A= $$components{$_[0]}B= $$components{ref($self) . '::Database::' . $_[0]}
ABresult
1X1
011
000
Truth table0%0/3
A // B
A= $$components{$_[0]}B= $$components{ref($self) . '::Logger::' . $_[0]}
ABresult
1X1
011
000
Truth table0%0/3
A // B
A= $$components{$_[0]}B= $$components{ref($self) . '::Session::' . $_[0]}
ABresult
1X1
011
000
435 'sub {',
436 ' my $self = shift->instance;',
437 ' return $self->_get_default_components->{' . $_ . '} unless @_;',
438 ' return undef unless defined $_[0];',
439 ' my $components = $self->_get_component_registry->{' . $_ . '};',
440 ' return $components->{$_[0]} // $components->{ ref( $self ) . \'::' . ucfirst( $_ ) . '::\' . $_[0] };',
441 '}'
442 );
443  
444 136 __PACKAGE__->meta->add_method( $accessor_name, $accessor_code );
445  
446 }
447  
448 34 for my $component_type ( qw( dispatcher engine router ) ) {
449  
450 102 my $accessor_name = $component_type;
451  
452 0 ▶ my $accessor_code = eval join( "\n",
Subroutine __ANON__: not called CC=1 SCAR=3.5
Subroutine __ANON__: not called CC=1 SCAR=3.5
Subroutine __ANON__: not called CC=1 SCAR=3.5
453 'sub {',
454 ' return $_[0]->instance->_get_component_registry->{\'' . $component_type . '\'};',
455 '}'
456 );
457  
458 102 __PACKAGE__->meta->add_method( $accessor_name, $accessor_code );
459  
460 }
461  
462 }
463  
464  
465 # ----------------------------------------------------------------------------------------------------------------------
466 # Internal methods
467 # ----------------------------------------------------------------------------------------------------------------------
468  
469 0 ▶sub add_hook {
Subroutine add_hook: not called CC=5 SCAR=34.0
Pod documented
470  
471 0 my $self = shift;
472 0 my $hook = shift;
473  
474 0 ▶ if ( ! blessed $hook || ! $hook->isa( 'GX::Callback::Hook' ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (builtin::blessed($hook) and $hook->isa("GX::Callback::Hook")) 0 0 0
Condition0%0/3
builtin::blessed($hook) and $hook->isa("GX::Callback::Hook")
!ll&&!rl&&r
000
MC/DC0%0/2
builtin::blessed($hook) and $hook->isa("GX::Callback::Hook")
builtin::blessed($hook) $hook->isa("GX::Callback::Hook")
Truth table0%0/3
A and B
A= builtin::blessed($hook)B= $hook->isa("GX::Callback::Hook")
ABresult
0X0
100
111
475 0 complain "Missing or invalid argument";
476 }
477  
478 0 my $hook_name = $hook->name;
479  
480 0 ▶ if ( ! defined $hook_name ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $hook_name) 0 0 0
481 0 complain "Undefined hook name";
482 }
483  
484 0 my $hook_registry = $self->_get_hook_registry;
485  
486 0 ▶ if ( $hook_registry->{$hook_name} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($hook_registry->{$hook_name}) 0 0 0
487 0 complain "Duplicate hook name \"$hook_name\"";
488 }
489  
490 0 $hook_registry->{$hook_name} = $hook;
491  
492 0 return;
493  
494 }
495  
496 0 ▶sub handle_error {
Subroutine handle_error: not called CC=2 SCAR=17.9
Pod documented
497  
498 0 my $self = shift;
499 0 my $context = shift;
500 0 my $error = shift;
501  
502 0 $self->log( error => $error );
503  
504 0 $context->error( $error );
505  
506 0 ▶ $context->send_response( status => 500, render_hint => 'Error' ) or $context->bail_out;
Branch: 1 branch
BranchTrueFalseTotal
$context->send_response("status", 500, "render_hint", "Error") or $context->bail_out 0 0 0
507  
508 0 return;
509  
510 }
511  
512 0 ▶sub process {
Subroutine process: not called CC=3 SCAR=24.8
Pod documented
513  
514 0 my $self = shift;
515 0 my $context = shift;
516  
517 0 ▶ my $handler_queue = $self->_clone_handler_queue || $self->_create_handler_queue;
Condition0%0/3
$self->_clone_handler_queue || $self->_create_handler_queue
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->_clone_handler_queue || $self->_create_handler_queue
$self->_clone_handler_queue $self->_create_handler_queue
Truth table0%0/3
A || B
A= $self->_clone_handler_queueB= $self->_create_handler_queue
ABresult
1X1
011
000
518  
519 0 $context->handler_queue( $handler_queue );
520  
521 0 ▶ while ( my $handler = $handler_queue->next ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $handler = $handler_queue->next) 0 0 0
522 0 $handler->call( $context );
523 }
524  
525 0 $context->handler_queue( undef );
526  
527 0 return;
528  
529 }
530  
531 0 ▶sub reload {
Subroutine reload: not called CC=5 SCAR=34.0
Pod documented
532  
533 0 my $self = shift->instance;
534 0 my $force = shift;
535  
536 0 ▶ if ( $force || ( $self->_get_watcher || return )->find_changes ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($force or ($self->_get_watcher || (return))->find_changes) 0 0 0
Condition0%0/5
$force or ($self->_get_watcher || (return))->find_changes
l!l&&r!l&&!r
000
$self->_get_watcher || (return)
l!l
00
MC/DC0%0/2
$force or ($self->_get_watcher || (return))->find_changes
$force ($self->_get_watcher || (return))->find_changes
MC/DC0%0/1
$self->_get_watcher || (return)
$self->_get_watcher
Truth table0%0/3
A or B
A= $forceB= ($self->_get_watcher || (return))->find_changes
ABresult
1X1
011
000
Truth table0%0/2
A
A= $self->_get_watcher
Aresult
11
00
537  
538 0 $self->log( 'Reloading' );
539  
540 0 $self->_is_reloading( 1 );
541  
542 0 my $class = ref $self;
543  
544 0 undef $self;
545  
546 0 $class->_unload;
547  
548 0 ▶ eval "require $class" or throw $@;
Branch: 1 branch
BranchTrueFalseTotal
eval "require $class" or &throw($@) 0 0 0
549  
550 0 return 1;
551  
552 }
553  
554 0 return;
555  
556 }
557  
558 0 ▶sub remove_hook {
Subroutine remove_hook: not called CC=10 SCAR=47.0
Pod documented
559  
560 0 my $self = shift;
561  
562 0 ▶ if ( ! @_ ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@_) 0 0 0
563 0 complain "Missing argument";
564 }
565  
566 0 my $hook_registry = $self->_get_hook_registry;
567  
568 0 ▶ if ( ref $_[0] ) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $_[0]) { } 0 0 0
elsif (defined $_[0]) { } 0 0 0
569  
570 0 ▶ if ( ! blessed $_[0] || ! $_[0]->isa( 'GX::Callback::Hook' ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (builtin::blessed($_[0]) and $_[0]->isa("GX::Callback::Hook")) 0 0 0
Condition0%0/3
builtin::blessed($_[0]) and $_[0]->isa("GX::Callback::Hook")
!ll&&!rl&&r
000
MC/DC0%0/2
builtin::blessed($_[0]) and $_[0]->isa("GX::Callback::Hook")
builtin::blessed($_[0]) $_[0]->isa("GX::Callback::Hook")
Truth table0%0/3
A and B
A= builtin::blessed($_[0])B= $_[0]->isa("GX::Callback::Hook")
ABresult
0X0
100
111
571 0 complain "Invalid argument";
572 }
573  
574 0 my $hook_name = $_[0]->name;
575  
576 0 ▶ if ( ! defined $hook_name ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $hook_name) 0 0 0
577 0 complain "Undefined hook name";
578 }
579  
580 0 my $hook = $hook_registry->{$hook_name};
581  
582 0 ▶ if ( $hook && $hook == $_[0] ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($hook and $hook == $_[0]) 0 0 0
Condition0%0/3
$hook and $hook == $_[0]
!ll&&!rl&&r
000
MC/DC0%0/2
$hook and $hook == $_[0]
$hook $hook == $_[0]
Truth table0%0/3
A and B
A= $hookB= $hook == $_[0]
ABresult
0X0
100
111
583 0 delete $hook_registry->{$hook_name};
584 0 return 1;
585 }
586  
587 }
588 elsif ( defined $_[0] ) {
589  
590 0 ▶ if ( delete $hook_registry->{$_[0]} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (delete $$hook_registry{$_[0]}) 0 0 0
591 0 return 1;
592 }
593  
594 }
595 else {
596 0 complain "Invalid argument";
597 }
598  
599 0 return;
600  
601 }
602  
603  
604 # ----------------------------------------------------------------------------------------------------------------------
605 # Private methods
606 # ----------------------------------------------------------------------------------------------------------------------
607  
608 0 ▶sub _bootstrap_component {
Subroutine _bootstrap_component: not called CC=8 SCAR=42.8
609  
610 0 my $self = shift;
611 0 my $component = shift;
612 0 my $base_class = shift;
613  
614 0 ▶ if ( ! defined $component || ! defined $base_class ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $component and defined $base_class) 0 0 0
Condition0%0/3
defined $component and defined $base_class
!ll&&!rl&&r
000
MC/DC0%0/2
defined $component and defined $base_class
defined $component defined $base_class
Truth table0%0/3
A and B
A= defined $componentB= defined $base_class
ABresult
0X0
100
111
615 0 throw "Missing or invalid argument";
616 }
617  
618 0 ▶ if ( $component !~ REGEX_CLASS_NAME ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($component =~ /^[a-zA-Z_]\w*(?:::[a-zA-Z_]\w*)*$/) 0 0 0
619 0 throw "Cannot bootstrap component (invalid component class name)";
620 }
621  
622 0 ▶ if ( $base_class !~ REGEX_CLASS_NAME ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($base_class =~ /^[a-zA-Z_]\w*(?:::[a-zA-Z_]\w*)*$/) 0 0 0
623 0 throw "Cannot bootstrap $component (invalid base class name)";
624 }
625  
626 0 my $inc_key = GX::Meta::Util::module_inc_key( $component );
627  
628 0 ▶ if ( exists $INC{$inc_key} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $INC{$inc_key}) 0 0 0
629 0 throw "Cannot bootstrap $component (module is already loaded)";
630 }
631  
632 0 ▶ if ( GX::Class::Util::class_exists( $component ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (GX::Class::Util::class_exists($component)) 0 0 0
633 0 throw "Cannot bootstrap $component (namespace is not empty)";
634 }
635  
636 0 eval join( "\n",
637 "package $component;",
638 "use $base_class;",
639 "1;"
640 );
641  
642 0 ▶ if ( $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
643 0 GX::Exception->throw(
644 message => "Cannot bootstrap $component",
645 subexception => $@
646 );
647 }
648  
649 0 $INC{$inc_key} = \__PACKAGE__;
650  
651 0 return;
652  
653 }
654  
655 0 ▶sub _clone_handler_queue {
Subroutine _clone_handler_queue: not called CC=2 SCAR=17.9
656  
657 0 ▶ return ( $_[0]->_get_handler_queue || return undef )->clone;
Condition0%0/2
$_[0]->_get_handler_queue || (return undef)
l!l
00
MC/DC0%0/1
$_[0]->_get_handler_queue || (return undef)
$_[0]->_get_handler_queue
Truth table0%0/2
A
A= $_[0]->_get_handler_queue
Aresult
11
00
658  
659 }
660  
661 0 ▶sub _compile_handler_queue {
Subroutine _compile_handler_queue: not called CC=1 SCAR=6.9
662  
663 0 my $self = shift;
664  
665 0 $self->_set_handler_queue( $self->_create_handler_queue );
666  
667 0 return;
668  
669 }
670  
671 0 ▶sub _create_handler {
Subroutine _create_handler: not called CC=1 SCAR=6.9
672  
673 0 my $self = shift;
674 0 my $method = shift;
675  
676 0 return GX::Callback::Method->new( invocant => $self, method => $method );
677  
678 }
679  
680 0 ▶sub _create_handler_queue {
Subroutine _create_handler_queue: not called CC=1 SCAR=6.9
681  
682 0 my $self = shift;
683  
684 0 my $queue = GX::Callback::Hook::Queue->new;
685  
686 0 $queue->add( $self->_get_hooks );
687  
688 0 return $queue;
689  
690 }
691  
692 0 ▶sub _create_hook {
Subroutine _create_hook: not called CC=1 SCAR=6.9
693  
694 0 my $self = shift;
695 0 my $name = shift;
696  
697 0 return GX::Callback::Hook->new( name => $name );
698  
699 }
700  
701 0 ▶sub _deploy_components {
Subroutine _deploy_components: not called CC=2 SCAR=17.9
702  
703 0 my $self = shift;
704  
705 0 for my $component ( $self->components ) {
706 0 $component->__deploy;
707 }
708  
709 0 return;
710  
711 }
712  
713 0 ▶sub _deploy_plugins {
Subroutine _deploy_plugins: not called CC=1 SCAR=0.0
714  
715 # Reserved method name
716  
717 }
718  
719 56 ▶sub _initialize_component_regex {
Subroutine _initialize_component_regex: called CC=1 SCAR=0.0
720  
721 56 my $self = shift;
722  
723 56 return qr/
724 ^
725 ($self)
726 ::
727 (
728 ?|
729 (Context|Dispatcher|Engine|Request|Response|Router)
730 () # workaround for branch reset bug under perl v5.10.0
731 |
732 (Cache|Controller|Database|Logger|Model|Session|View)::(.*)
733 )
734 $
735 /x;
736  
737 }
738  
739 0 ▶sub _initialize_component_registry {
Subroutine _initialize_component_registry: not called CC=1 SCAR=6.9
740  
741 return {
742  
743 0 'dispatcher' => undef,
744 'engine' => undef,
745 'router' => undef,
746  
747 'context' => undef,
748 'request' => undef,
749 'response' => undef,
750  
751 'cache' => {},
752 'controller' => {},
753 'database' => {},
754 'logger' => {},
755 'model' => {},
756 'session' => {},
757 'view' => {}
758  
759 };
760  
761 }
762  
763 0 ▶sub _initialize_paths {
Subroutine _initialize_paths: not called CC=5 SCAR=34.0
764  
765 0 my $self = shift;
766  
767 0 my %paths;
768  
769 0 ▶ if ( my $file = GX::Meta::Util::module_inc_file( ref $self ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $file = GX::Meta::Util::module_inc_file(ref $self)) 0 0 0
770  
771 0 my $base_path = do {
772 0 my ( $volume, $dirs, undef ) = File::Spec->splitpath( $file );
773 0 my @dirs = File::Spec->splitdir( $dirs );
774 0 pop @dirs for split( /::/, ref $self );
775 0 pop @dirs;
776 0 File::Spec->catpath( $volume, File::Spec->catdir( @dirs ), '' );
777 };
778  
779 %paths = (
780 'base' => $base_path,
781 (
782 map {
783 0 $_ => File::Spec->rel2abs( $_, $base_path )
784 } qw(
785 cache
786 lib
787 log
788 public
789 script
790 templates
791 tmp
792 )
793 )
794 );
795  
796 }
797  
798 0 ▶ if ( ! defined $paths{'tmp'} || ! -d $paths{'tmp'} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $paths{"tmp"} and -d $paths{"tmp"}) 0 0 0
Condition0%0/3
defined $paths{"tmp"} and -d $paths{"tmp"}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $paths{"tmp"} and -d $paths{"tmp"}
defined $paths{"tmp"} -d $paths{"tmp"}
Truth table0%0/3
A and B
A= defined $paths{"tmp"}B= -d $paths{"tmp"}
ABresult
0X0
100
111
799 0 $paths{'tmp'} = File::Spec->tmpdir;
800 }
801  
802 0 return \%paths;
803  
804 }
805  
806 0 ▶sub _is_reloading {
Subroutine _is_reloading: not called CC=2 SCAR=17.9
807  
808 0 my $self = shift;
809  
810 0 ▶ if ( $_[0] ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_[0]) 0 0 0
811 0 $self->_set_state( STATE_RELOADING );
812 0 return;
813 }
814  
815 0 return $self->_get_state == STATE_RELOADING;
816  
817 }
818  
819 0 ▶sub _is_running {
Subroutine _is_running: not called CC=2 SCAR=17.9
820  
821 0 my $self = shift;
822  
823 0 ▶ if ( $_[0] ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_[0]) 0 0 0
824 0 $self->_set_state( STATE_RUNNING );
825 0 return;
826 }
827  
828 0 return $self->_get_state >= STATE_RUNNING;
829  
830 }
831  
832 0 ▶sub _is_setup {
Subroutine _is_setup: not called CC=2 SCAR=17.9
833  
834 0 my $self = shift;
835  
836 0 ▶ if ( $_[0] ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_[0]) 0 0 0
837 0 $self->_set_state( STATE_SETUP );
838 0 return;
839 }
840  
841 0 return $self->_get_state >= STATE_SETUP;
842  
843 }
844  
845 0 ▶sub _load_module {
Subroutine _load_module: not called CC=10 SCAR=47.0
846  
847 0 my $self = shift;
848 0 my $module = shift;
849  
850 0 my $inc_key = GX::Meta::Util::module_inc_key( $module );
851  
852 0 ▶ if ( ! exists $INC{$inc_key} ) {
Branch: 2 branches
BranchTrueFalseTotal
if (not exists $INC{$inc_key}) { } 0 0 0
elsif (not $INC{$inc_key}) { } 0 0 0
853  
854 0 my $lib_path = $self->_get_path( 'lib' );
855  
856 0 ▶ if ( ! -d $lib_path ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-d $lib_path) 0 0 0
857 0 ▶ return if defined wantarray;
Branch: 1 branch
BranchTrueFalseTotal
if defined wantarray 0 0 0
858 0 throw "Cannot load $module (undefined application \"lib\" path)";
859 }
860  
861 0 my $module_file = File::Spec->rel2abs(
862 GX::Meta::Util::module_to_file_name( $module ),
863 $lib_path
864 );
865  
866 0 ▶ if ( ! -f $module_file ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-f $module_file) 0 0 0
867 0 ▶ return if defined wantarray;
Branch: 1 branch
BranchTrueFalseTotal
if defined wantarray 0 0 0
868 0 throw "Cannot load $module (module was not found in \"$lib_path\")";
869 }
870  
871 0 my $result = do $module_file;
872  
873 0 delete $INC{$module_file};
874  
875 0 ▶ if ( ! $result ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($result) 0 0 0
876  
877 0 ▶ if ( $@ ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($@) { } 0 0 0
elsif ($!) { } 0 0 0
878  
879 0 $INC{$inc_key} = undef;
880  
881 0 GX::Exception->throw(
882 message => "Cannot compile $module",
883 subexception => $@
884 );
885  
886 }
887 elsif ( $! ) {
888  
889 0 delete $INC{$inc_key};
890  
891 0 GX::Exception->throw(
892 message => "Cannot load $module",
893 subexception => $!
894 );
895  
896 }
897 else {
898  
899 0 delete $INC{$inc_key};
900  
901 0 throw "Cannot run $module (module did not return a true value)";
902  
903 }
904  
905 }
906  
907 0 $INC{$inc_key} = $module_file;
908  
909 }
910 elsif ( ! $INC{$inc_key} ) {
911 0 throw "Compilation of $module failed";
912 }
913
914 0 return 1;
915  
916 }
917  
918 0 ▶sub _register_component {
Subroutine _register_component: not called CC=8 SCAR=42.8
919  
920 0 my $self = shift;
921 0 my $component = shift;
922  
923 0 ▶ if ( ! defined $component ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $component) 0 0 0
924 0 throw "Missing argument";
925 }
926  
927 0 ▶ if ( $component !~ $self->_get_component_regex ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($component =~ $self->_get_component_regex) 0 0 0
928 0 throw "Cannot register component (\"$component\" is not a valid component class name)";
929 }
930  
931 0 my $component_type = lc $2;
932 0 my $component_base_class = "GX::$2";
933  
934 0 ▶ if ( ! $component->isa( $component_base_class ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($component->isa($component_base_class)) 0 0 0
935 0 throw "Cannot register $component (not a $component_base_class subclass)";
936 }
937  
938 0 my $components = $self->_get_components;
939 0 my $component_registry = $self->_get_component_registry;
940  
941 0 ▶ if ( $components->{$component} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($components->{$component}) 0 0 0
942 0 throw "Cannot register $component (already registered)";
943 }
944  
945 0 ▶ if ( ! exists $component_registry->{$component_type} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $component_registry->{$component_type}) 0 0 0
946 0 throw "Cannot register $component (unsupported component type)";
947 }
948  
949 0 my $component_interface = $component->__register( $self );
950  
951 0 ▶ if ( ! $component_interface ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($component_interface) 0 0 0
952 0 throw "Cannot register $component (undefined component interface)";
953 }
954  
955 0 $components->{$component} = $component_interface;
956  
957 0 ▶ if ( ref $component_registry->{$component_type} eq 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $component_registry->{$component_type} eq 'HASH') { } 0 0 0
958 0 $component_registry->{$component_type}{$component} = $component_interface;
959 }
960 else {
961 0 $component_registry->{$component_type} = $component_interface;
962 }
963  
964 0 return;
965  
966 }
967  
968 0 ▶sub _setup_components {
Subroutine _setup_components: not called CC=11 SCAR=48.8
969  
970 0 my $self = shift;
971  
972 0 my $class = ref $self;
973 0 my $config = $self->_get_config;
974  
975 0 for my $type ( qw( dispatcher engine router ) ) {
976  
977 0 my $component = $class . '::' . ucfirst( $type );
978  
979 0 ▶ if ( ! $self->_load_module( $component ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->_load_module($component)) 0 0 0
980  
981 0 my $base_class;
982  
983 0 ▶ if ( defined $config->{$type} ) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $config->{$type}) { } 0 0 0
elsif (defined $$config{"${type}_base_class"}) { } 0 0 0
984 0 $base_class = 'GX::' . ucfirst( $type ) . '::' . $config->{$type};
985 }
986 elsif ( defined $config->{"${type}_base_class"} ) {
987 0 $base_class = $config->{"${type}_base_class"};
988 }
989 else {
990 0 $base_class = 'GX::' . ucfirst( $type );
991 }
992  
993 0 $self->_bootstrap_component( $component, $base_class );
994  
995 }
996  
997 0 $self->_register_component( $component );
998  
999 }
1000  
1001 0 for my $type ( qw( context request response ) ) {
1002  
1003 0 my $component = $class . '::' . ucfirst( $type );
1004  
1005 0 ▶ if ( ! $self->_load_module( $component ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->_load_module($component)) 0 0 0
1006 0 $self->_bootstrap_component( $component, 'GX::' . ucfirst( $type ) );
1007 }
1008  
1009 0 $self->_register_component( $component );
1010  
1011 }
1012  
1013 0 ▶ if ( my $lib_path = $self->_get_path( 'lib' ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $lib_path = $self->_get_path("lib")) 0 0 0
1014
1015 0 for my $type ( qw( logger cache database model session view controller ) ) {
1016  
1017 0 my $search_path = File::Spec->rel2abs(
1018 File::Spec->catdir( split( /::/, $class ), ucfirst( $type ) ),
1019 $lib_path
1020 );
1021  
1022 0 ▶ next unless -d $search_path;
Branch: 1 branch
BranchTrueFalseTotal
unless -d $search_path 0 0 0
1023  
1024 0 for my $component ( GX::Meta::Util::find_modules( $search_path, $lib_path ) ) {
1025 0 $self->_load_module( $component );
1026 0 $self->_register_component( $component );
1027 }
1028  
1029 }
1030  
1031 }
1032  
1033 0 return;
1034  
1035 }
1036  
1037 0 ▶sub _setup_config {
Subroutine _setup_config: not called CC=13 SCAR=52.0
1038  
1039 0 my $self = shift;
1040 0 my %args = @_;
1041  
1042 0 for ( qw(
1043 default_cache
1044 default_database
1045 default_logger
1046 default_session
1047 dispatcher
1048 dispatcher_base_class
1049 engine
1050 engine_base_class
1051 router
1052 router_base_class
1053 ) ) {
1054  
1055 0 ▶ next unless exists $args{$_};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $args{$_} 0 0 0
1056  
1057 0 ▶ if ( ! defined $args{$_} || $args{$_} !~ REGEX_CLASS_NAME ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $args{$_} and $args{$_} =~ /^[a-zA-Z_]\w*(?:::[a-zA-Z_]\w*)*$/) 0 0 0
Condition0%0/3
defined $args{$_} and $args{$_} =~ /^[a-zA-Z_]\w*(?:::[a-zA-Z_]\w*)*$/
!ll&&!rl&&r
000
MC/DC0%0/2
defined $args{$_} and $args{$_} =~ /^[a-zA-Z_]\w*(?:::[a-zA-Z_]\w*)*$/
defined $args{$_} $args{$_} =~ /^[a-zA-Z_]\w*(?:::[a-zA-Z_]\w*)*$/
Truth table0%0/3
A and B
A= defined $args{$_}B= $args{$_} =~ /^[a-zA-Z_]\w*(?:::[a-zA-Z_]\w*)*$/
ABresult
0X0
100
111
1058 0 throw "Invalid option (\"$_\")";
1059 }
1060  
1061 0 $self->{'config'}{$_} = delete $args{$_};
1062  
1063 }
1064  
1065 0 ▶ if ( exists $args{'default_encoding'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $args{"default_encoding"}) 0 0 0
1066  
1067 0 my $encoding = delete $args{'default_encoding'};
1068  
1069 0 ▶ if ( ! defined $encoding ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $encoding) 0 0 0
1070 0 throw "Invalid option (\"default_encoding\")";
1071 }
1072  
1073 0 my $encode_object = Encode::find_encoding( $encoding );
1074  
1075 0 ▶ if ( ! $encode_object ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($encode_object) 0 0 0
1076 0 throw "Invalid option (\"default_encoding\" must be an encoding supported by Encode)";
1077 }
1078  
1079 0 $self->_set_default_encoding( $encode_object->name );
1080  
1081 }
1082  
1083 0 ▶ if ( exists $args{'mode'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $args{"mode"}) 0 0 0
1084  
1085 0 my $mode = delete $args{'mode'};
1086  
1087 0 ▶ if ( ! defined $mode || ( $mode ne MODE_DEVELOPMENT && $mode ne MODE_PRODUCTION ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $mode or $mode ne "development" and $mode ne "production") 0 0 0
Condition0%0/6
not defined $mode or $mode ne "development" and $mode ne "production"
l!l&&r!l&&!r
000
$mode ne "development" and $mode ne "production"
!ll&&!rl&&r
000
MC/DC0%0/3
not defined $mode or $mode ne "development" and $mode ne "production"
not defined $mode $mode ne "development" $mode ne "production"
Truth table0%0/4
not(A) or B and C
A= not defined $modeB= $mode ne "development"C= $mode ne "production"
ABCresult
1XX1
0111
00X0
0100
1088 0 throw "Invalid option (\"mode\" must be \"" . MODE_DEVELOPMENT . "\" or \"" . MODE_PRODUCTION . "\")";
1089 }
1090  
1091 0 $self->_set_mode( $mode );
1092  
1093 }
1094  
1095 0 ▶ if ( %args ) {
Branch: 1 branch
BranchTrueFalseTotal
if (%args) 0 0 0
1096 0 throw sprintf( "Unrecognized setup option (\"%s\")", ( sort keys %args )[0] );
1097 }
1098  
1099 0 return;
1100  
1101 }
1102  
1103 0 ▶sub _setup_default_components {
Subroutine _setup_default_components: not called CC=6 SCAR=37.4
1104  
1105 0 my $self = shift;
1106  
1107 0 my $config = $self->_get_config;
1108 0 my $default_components = $self->_get_default_components;
1109 0 my $component_registry = $self->_get_component_registry;
1110  
1111 0 for my $component_type ( qw( cache database logger session ) ) {
1112  
1113 0 my $config_key = "default_$component_type";
1114  
1115 0 ▶ if ( exists $config->{$config_key} ) {
Branch: 2 branches
BranchTrueFalseTotal
if (exists $config->{$config_key}) { } 0 0 0
elsif (keys %{$$component_registry{$component_type};} == 1) { } 0 0 0
1116  
1117 0 ▶ if ( defined $config->{$config_key} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $config->{$config_key}) { } 0 0 0
1118  
1119 0 my $component_interface = $self->$component_type( $config->{$config_key} );
1120  
1121 0 ▶ if ( ! $component_interface ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($component_interface) 0 0 0
1122 0 throw "Invalid option (unkown \"$config_key\")";
1123 }
1124  
1125 0 $default_components->{$component_type} = $component_interface;
1126  
1127 }
1128 else {
1129 0 $default_components->{$component_type} = undef;
1130 }
1131  
1132 }
1133 0 elsif ( keys %{$component_registry->{$component_type}} == 1 ) {
1134 0 $default_components->{$component_type} = ( values %{$component_registry->{$component_type}} )[0];
1135 }
1136 else {
1137 0 next;
1138 }
1139  
1140 }
1141  
1142 0 return;
1143  
1144 }
1145  
1146 0 ▶sub _setup_handlers {
Subroutine _setup_handlers: not called CC=5 SCAR=34.0
1147  
1148 0 my $self = shift;
1149  
1150 0 for my $method ( $self->meta->all_methods ) {
1151  
1152 0 for my $attribute ( $method->code_attributes ) {
1153  
1154 0 ▶ if ( $attribute =~ /^Handler\(\s?(\w+?)\s?\)$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($attribute =~ /^Handler\(\s?(\w+?)\s?\)$/) 0 0 0
1155  
1156 0 ▶ if ( my $hook = $self->_get_hook( $1 ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $hook = $self->_get_hook($1)) 0 0 0
1157 0 $hook->add( $self->_create_handler( $method->name ) );
1158 }
1159  
1160 }
1161  
1162 }
1163  
1164 }
1165  
1166 0 return;
1167  
1168 }
1169  
1170 0 ▶sub _setup_hooks {
Subroutine _setup_hooks: not called CC=2 SCAR=17.9
1171  
1172 0 my $self = shift;
1173  
1174 0 my @hook_names = DEFAULT_HOOKS;
1175  
1176 0 for my $hook_name ( @hook_names ) {
1177 0 $self->add_hook( $self->_create_hook( $hook_name ) );
1178 }
1179  
1180 0 return;
1181  
1182 }
1183  
1184 0 ▶sub _setup_inc {
Subroutine _setup_inc: not called CC=4 SCAR=30.0
1185  
1186 0 my $self = shift;
1187  
1188 0 my $path = $self->_get_path( 'lib' );
1189  
1190 0 ▶ if ( defined $path && -d $path && ! grep { $_ eq $path } @INC ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $path and -d $path and not grep({$_ eq $path;} @INC)) 0 0 0
Condition0%0/6
defined $path and -d $path and not grep({$_ eq $path;} @INC)
!ll&&!rl&&r
000
defined $path and -d $path
!ll&&!rl&&r
000
MC/DC0%0/3
defined $path and -d $path and not grep({$_ eq $path;} @INC)
defined $path -d $path not grep({$_ eq $path;} @INC)
Truth table0%0/4
A and B and not(C)
A= defined $pathB= -d $pathC= not grep({$_ eq $path;} @INC)
ABCresult
0XX0
10X0
1100
1111
1191 0 unshift @INC, $path;
1192 }
1193  
1194 0 return;
1195  
1196 }
1197  
1198 0 ▶sub _setup_plugins {
Subroutine _setup_plugins: not called CC=1 SCAR=0.0
1199  
1200 # Reserved method name
1201  
1202 }
1203  
1204 0 ▶sub _setup_watcher {
Subroutine _setup_watcher: not called CC=4 SCAR=30.0
1205  
1206 0 my $self = shift;
1207  
1208 0 ▶ if ( $self->_get_mode eq MODE_DEVELOPMENT ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_get_mode eq "development") 0 0 0
1209  
1210 0 my $watcher = $self->_get_watcher;
1211  
1212 0 ▶ if ( ! $watcher ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($watcher) 0 0 0
1213 0 require GX::File::Watcher;
1214 0 $watcher = GX::File::Watcher->new;
1215 0 $self->_set_watcher( $watcher );
1216 }
1217  
1218 $watcher->watch(
1219 0 ▶ grep { defined && -d } map { $self->_get_path( $_ ) } qw( lib templates )
Condition0%0/3
defined $_ && -d $_
!ll&&!rl&&r
000
MC/DC0%0/2
defined $_ && -d $_
defined $_ -d $_
Truth table0%0/3
A && B
A= defined $_B= -d $_
ABresult
0X0
100
111
1220 );
1221  
1222 }
1223  
1224 0 return;
1225  
1226 }
1227  
1228 0 ▶sub _start_components {
Subroutine _start_components: not called CC=2 SCAR=17.9
1229  
1230 0 my $self = shift;
1231  
1232 0 for my $component ( $self->components ) {
1233 0 $component->__start;
1234 }
1235  
1236 0 return;
1237  
1238 }
1239  
1240 0 ▶sub _start_plugins {
Subroutine _start_plugins: not called CC=1 SCAR=0.0
1241  
1242 # Reserved method name
1243  
1244 }
1245  
1246 0 ▶sub _unload {
Subroutine _unload: not called CC=4 SCAR=30.0
1247  
1248 0 my $self = shift->instance;
1249  
1250 0 my @components = reverse $self->components;
1251  
1252 0 for my $component ( @components ) {
1253 0 $self->_unregister_component( $component );
1254 }
1255  
1256 0 for my $component ( @components ) {
1257 0 $component->__unload;
1258 0 $component->meta->destroy;
1259 0 GX::Meta::Util::unload_module( $component );
1260 }
1261  
1262 0 my $class = ref $self;
1263  
1264 0 $self->destroy;
1265  
1266 0 weaken $self;
1267  
1268 0 ▶ if ( $self ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self) 0 0 0
1269 0 warn "$class was not destroyed as expected (possible memory leak)";
1270 }
1271  
1272 0 $class->meta->destroy;
1273  
1274 0 GX::Meta::Util::unload_module( $class );
1275  
1276 0 return;
1277  
1278 }
1279  
1280 0 ▶sub _unregister_component {
Subroutine _unregister_component: not called CC=7 SCAR=40.3
1281  
1282 0 my $self = shift;
1283 0 my $component = shift;
1284  
1285 0 ▶ if ( ! defined $component ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $component) 0 0 0
1286 0 throw "Missing argument";
1287 }
1288  
1289 0 ▶ if ( $component !~ $self->_get_component_regex ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($component =~ $self->_get_component_regex) 0 0 0
1290 0 throw "Cannot unregister component (\"$component\" is not a valid component class name)";
1291 }
1292  
1293 0 my $component_type = lc $2;
1294  
1295 0 my $components = $self->_get_components;
1296  
1297 0 ▶ if ( ! $components->{$component} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($components->{$component}) 0 0 0
1298 0 return;
1299 }
1300  
1301 0 $component->__unregister;
1302  
1303 0 delete $components->{$component};
1304  
1305 0 my $component_registry = $self->_get_component_registry;
1306  
1307 0 ▶ if ( ref $component_registry->{$component_type} eq 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $component_registry->{$component_type} eq 'HASH') { } 0 0 0
1308 0 delete $component_registry->{$component_type}{$component};
1309 }
1310 else {
1311 0 $component_registry->{$component_type} = undef;
1312 }
1313  
1314 0 my $default_components = $self->_get_default_components;
1315  
1316 0 ▶ if ( $default_components->{$component_type} && $default_components->{$component_type} eq $component ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($default_components->{$component_type} and $default_components->{$component_type} eq $component) 0 0 0
Condition0%0/3
$default_components->{$component_type} and $default_components->{$component_type} eq $component
!ll&&!rl&&r
000
MC/DC0%0/2
$default_components->{$component_type} and $default_components->{$component_type} eq $component
$default_components->{$component_type} $default_components->{$component_type} eq $component
Truth table0%0/3
A and B
A= $default_components->{$component_type}B= $default_components->{$component_type} eq $component
ABresult
0X0
100
111
1317 0 delete $default_components->{$component_type};
1318 }
1319  
1320 0 return 1;
1321  
1322 }
1323  
1324  
1325 34 1;
1326  
1327 __END__
« GX/Database/Pg.pm ↑ Summary GX/Database.pm »