File Coverage

blib/lib/Siffra/Bootstrap.pm
Criterion Covered Total %
statement 68 135 50.3
branch 2 24 8.3
condition 2 12 16.6
subroutine 19 24 79.1
pod 5 5 100.0
total 96 200 48.0


line stmt bran cond sub pod time code
1             package Siffra::Bootstrap;
2              
3 1     1   68525 use 5.014;
  1         4  
4 1     1   5 use strict;
  1         3  
  1         19  
5 1     1   5 use warnings;
  1         2  
  1         21  
6 1     1   626 use utf8;
  1         14  
  1         5  
7 1     1   666 use Data::Dumper;
  1         6771  
  1         59  
8 1     1   442 use DDP;
  1         41044  
  1         11  
9 1     1   551 use Log::Any qw($log);
  1         10397  
  1         6  
10 1     1   2171 use Scalar::Util qw(blessed);
  1         2  
  1         53  
11             $Carp::Verbose = 1;
12              
13 1     1   498 use Config::Any;
  1         8518  
  1         34  
14 1     1   935 use IO::Prompter;
  1         34453  
  1         7  
15              
16             $| = 1; #autoflush
17              
18             use constant {
19             FALSE => 0,
20             TRUE => 1,
21 1   50     119 DEBUG => $ENV{ DEBUG } // 0,
22 1     1   91 };
  1         3  
23              
24             BEGIN
25             {
26 1     1   36 binmode( STDOUT, ":encoding(UTF-8)" );
  1     1   7  
  1         2  
  1         7  
27 1         17624 binmode( STDERR, ":encoding(UTF-8)" );
28              
29 1         578 require Siffra::Tools;
30 1     1   6 use Exporter ();
  1         2  
  1         22  
31 1     1   5 use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
  1         20  
  1         100  
32 1         6931 $VERSION = '0.08';
33 1         21 @ISA = qw(Siffra::Tools Exporter);
34              
35             #Give a hoot don't pollute, do not export more than needed by default
36 1         5 @EXPORT = qw();
37 1         2 @EXPORT_OK = qw();
38 1         1488 %EXPORT_TAGS = ();
39             } ## end BEGIN
40              
41             #################### subroutine header begin ####################
42              
43             =head2 sample_function
44              
45             Usage : How to use this function/method
46             Purpose : What it does
47             Returns : What it returns
48             Argument : What it wants to know
49             Throws : Exceptions and other anomolies
50             Comment : This is a sample subroutine header.
51             : It is polite to include more pod and fewer comments.
52              
53             See Also :
54              
55             =cut
56              
57             #################### subroutine header end ####################
58              
59             =head2 C<new()>
60              
61             Usage : $self->block_new_method() within text_pm_file()
62             Purpose : Build 'new()' method as part of a pm file
63             Returns : String holding sub new.
64             Argument : $module: pointer to the module being built
65             (as there can be more than one module built by EU::MM);
66             for the primary module it is a pointer to $self
67             Throws : n/a
68             Comment : This method is a likely candidate for alteration in a subclass,
69             e.g., pass a single hash-ref to new() instead of a list of
70             parameters.
71              
72             =cut
73              
74             sub new
75             {
76 1     1 1 92 $log->debug( "new", { progname => $0, pid => $$, perl_version => $], package => __PACKAGE__ } );
77 1         7 my ( $class, %parameters ) = @_;
78 1         12 my $self = $class->SUPER::new( %parameters );
79              
80             $self->{ beachmarck } = {
81 1         48 created => $^T,
82             started => undef,
83             finished => undef
84             };
85              
86 1         4 $self->{ configurations } = \%parameters;
87              
88 1         4 $self->_initialize( %parameters );
89 1         3684 return $self;
90             } ## end sub new
91              
92             sub _initialize()
93             {
94 1     1   5 $log->debug( "_initialize", { package => __PACKAGE__ } );
95 1         5 my ( $self, %parameters ) = @_;
96 1         6 $self->SUPER::_initialize( %parameters );
97             }
98              
99             sub _finalize()
100             {
101 0     0   0 $log->debug( "_finalize", { package => __PACKAGE__ } );
102 0         0 my ( $self, %parameters ) = @_;
103 0         0 $self->SUPER::_finalize( %parameters );
104             }
105              
106             =head2 C<loadApplication()>
107              
108             Usage : $self->block_new_method() within text_pm_file()
109             Purpose : Build 'new()' method as part of a pm file
110             Returns : String holding sub new.
111             Argument : $module: pointer to the module being built
112             (as there can be more than one module built by EU::MM);
113             for the primary module it is a pointer to $self
114             Throws : n/a
115             Comment : This method is a likely candidate for alteration in a subclass,
116             e.g., pass a single hash-ref to new() instead of a list of
117             parameters.
118              
119             =cut
120              
121             sub loadApplication()
122             {
123 0     0 1 0 my ( $self, %parameters ) = @_;
124 0         0 $log->debug( "loadApplication", { package => __PACKAGE__ } );
125 0         0 my $configurationFile = $self->{ configurations }->{ configurationFile };
126              
127 0 0       0 if ( !-e $configurationFile )
128             {
129 0         0 ( $configurationFile = $0 ) =~ s/\.pl/\-config\.json/;
130 0         0 $log->error( "Não existe o arquivo de configuração...", { package => __PACKAGE__ } );
131              
132 0 0       0 if ( prompt 'Não existe o arquivo de configuração, deseja criar agora ?', -yn1, -default => 'y' )
133             {
134 0         0 my $config;
135              
136 0         0 $config->{ $configurationFile }->{ applicationName } = prompt( 'Application Name :', -v, -echostyle => 'bold white' );
137 0         0 $config->{ $configurationFile }->{ applicationModule } = prompt( 'Application Module :', -v, -echostyle => 'bold white' );
138 0         0 $config->{ $configurationFile }->{ environment } = prompt( 'Environment [desenv] :', -v, -echostyle => 'bold white', -default => 'desenv' );
139              
140             # Mail Config
141             $config->{ $configurationFile }->{ mail } = {
142 0         0 server => prompt( 'E-mail server [mail] :', -v, -echostyle => 'bold white', -default => 'mail' ),
143             port => prompt( 'E-mail port [25] :', -v, -echostyle => 'bold white', -default => '25' ),
144             debug => prompt( 'E-mail debug [0] :', -v, -echostyle => 'bold white', -default => '0' ),
145             from => prompt( 'E-mail from :', -v, -echostyle => 'bold white' ),
146             };
147              
148 0         0 my $json_text = $self->{ json }->pretty( 1 )->canonical( 1 )->encode( $config->{ $configurationFile } );
149              
150 0         0 open FH, ">", $configurationFile;
151 0         0 print FH $json_text;
152 0         0 close FH;
153             } ## end if ( prompt 'Não existe o arquivo de configuração, deseja criar agora ?'...)
154             else
155             {
156 0         0 return FALSE;
157             }
158             } ## end if ( !-e $configurationFile...)
159              
160 0         0 my @configFiles = ( $configurationFile );
161 0         0 my $configAny = Config::Any->load_files( { files => \@configFiles, flatten_to_hash => 1, use_ext => 1 } );
162              
163 0         0 foreach my $configFile ( keys %$configAny )
164             {
165 0         0 $self->{ configurations }->{ $_ } = $configAny->{ $configFile }->{ $_ } foreach ( keys %{ $configAny->{ $configFile } } );
  0         0  
166             }
167              
168 0         0 $log->info( "Configurações lidas com sucesso na aplicação [ $self->{ configurations }->{ application }->{ name } ]..." );
169              
170 0 0       0 $log->error( 'Falta passar nome do package' ) if ( $self->{ configurations }->{ application }->{ package } !~ /^\D/ );
171              
172 0         0 eval "use $self->{ configurations }->{ application }->{ fileName };"; ## Usando o módulo.
173              
174 0 0       0 if ( $@ )
175             {
176 0         0 $log->error( "Problemas ao usar o arquivo [ $self->{ configurations }->{ application }->{ fileName } ]..." );
177 0         0 return FALSE;
178             }
179             else
180             {
181 0         0 $self->{ application }->{ instance } = eval { $self->{ configurations }->{ application }->{ package }->new( bootstrap => $self ); };
  0         0  
182 0 0       0 if ( !$self->{ application }->{ instance } )
183             {
184 0         0 $log->error( "Erro ao iniciar o módulo [ $self->{ configurations }->{ application }->{ package } ]..." );
185 0         0 return FALSE;
186             }
187             } ## end else [ if ( $@ ) ]
188              
189 0         0 my $emailDispatcher = $log->{ adapter }->{ dispatcher }->{ outputs }->{ Email };
190              
191 0 0       0 if ( $emailDispatcher )
192             {
193 0         0 my $mailConf = $self->{ configurations }->{ mail };
194              
195 0 0       0 if ( $mailConf )
196             {
197 0         0 eval { require DateTime; };
  0         0  
198 0         0 $emailDispatcher->{ to } = [ $mailConf->{ to } ];
199 0         0 $emailDispatcher->{ from } = $mailConf->{ from };
200 0         0 $emailDispatcher->{ host } = $mailConf->{ host };
201 0         0 $emailDispatcher->{ port } = $mailConf->{ port };
202 0         0 $emailDispatcher->{ subject } = "[ " . DateTime->now()->datetime( ' ' ) . " ] " . $self->{ configurations }->{ application }->{ name };
203             } ## end if ( $mailConf )
204              
205             } ## end if ( $emailDispatcher ...)
206              
207 0         0 return TRUE;
208             } ## end sub loadApplication
209              
210             =head2 C<run()>
211             =cut
212              
213             sub run
214             {
215 0     0 1 0 my ( $self, %parameters ) = @_;
216 0         0 my $retorno = {};
217              
218 0 0       0 if ( ref $self->{ application }->{ instance } eq $self->{ configurations }->{ application }->{ package } )
219             {
220 0         0 $self->{ beachmarck }->{ started } = time();
221 0         0 $retorno = eval { $self->{ application }->{ instance }->start(); };
  0         0  
222 0         0 $self->{ beachmarck }->{ finished } = time();
223 0 0       0 $log->error( "Problemas durante execucao de start: $@" ) if ( $@ );
224             } ## end if ( ref $self->{ application...})
225             else
226             {
227 0         0 $log->error( "Impossivel de executar $self->{ configurations }->{ application }->{ package }::start\nSub nao existe" );
228             }
229              
230 0         0 return $retorno;
231             } ## end sub run
232              
233             =head2 C<getExecutionTime()>
234             =cut
235              
236             sub getExecutionTime()
237             {
238 0     0 1 0 my ( $self, %parameters ) = @_;
239 0   0     0 return ( ( $self->{ beachmarck }->{ finished } // 0 ) - ( $self->{ beachmarck }->{ started } // 0 ) );
      0        
240             }
241              
242             =head2 C<getExecutionInfo()>
243             =cut
244              
245             sub getExecutionInfo()
246             {
247 0     0 1 0 my ( $self, %parameters ) = @_;
248              
249 0         0 $log->info( "Quantidade de erro(s): 0" );
250 0         0 $log->info( "Tempo de execucao: " . $self->getExecutionTime() . " segundo(s)" );
251 0         0 $log->info( "Fim da aplicacao." );
252             } ## end sub getExecutionInfo
253              
254             sub END
255             {
256 1     1   185 $log->debug( "END", { package => __PACKAGE__ } );
257 1         6 my ( $self, %parameters ) = @_;
258 1         2 eval { $log->{ adapter }->{ dispatcher }->{ outputs }->{ Email }->flush; };
  1         60  
259             }
260              
261             sub DESTROY
262             {
263 1     1   594 my ( $self, %parameters ) = @_;
264 1 50       6 if ( ${^GLOBAL_PHASE} eq 'DESTRUCT' )
265             {
266 0 0 0     0 $self->getExecutionInfo() if ( blessed( $self ) && $self->isa( __PACKAGE__ ) );
267 0         0 return;
268             }
269              
270 1 50 33     11 if ( blessed( $self ) && $self->isa( __PACKAGE__ ) )
271             {
272 1         9 $log->debug( "DESTROY", { package => __PACKAGE__, GLOBAL_PHASE => ${^GLOBAL_PHASE}, blessed => TRUE } );
273             }
274             else
275             {
276 0         0 $log->debug( 'DESTROY', { package => __PACKAGE__, GLOBAL_PHASE => ${^GLOBAL_PHASE}, blessed => FALSE } );
277             }
278              
279 1         14 Siffra::Bootstrap->SUPER::DESTROY;
280             } ## end sub DESTROY
281              
282             #################### main pod documentation begin ###################
283             ## Below is the stub of documentation for your module.
284             ## You better edit it!
285              
286             =encoding UTF-8
287              
288              
289             =head1 NAME
290              
291             Siffra::Bootstrap - Module abstract (<= 44 characters) goes here
292              
293             =head1 SYNOPSIS
294              
295             use Siffra::Bootstrap;
296             blah blah blah
297              
298              
299             =head1 DESCRIPTION
300              
301             Stub documentation for this module was created by ExtUtils::ModuleMaker.
302             It looks like the author of the extension was negligent enough
303             to leave the stub unedited.
304              
305             Blah blah blah.
306              
307              
308             =head1 USAGE
309              
310              
311              
312             =head1 BUGS
313              
314              
315              
316             =head1 SUPPORT
317              
318              
319              
320             =head1 AUTHOR
321              
322             Luiz Benevenuto
323             CPAN ID: LUIZBENE
324             Siffra TI
325             luiz@siffra.com.br
326             https://siffra.com.br
327              
328             =head1 COPYRIGHT
329              
330             This program is free software; you can redistribute
331             it and/or modify it under the same terms as Perl itself.
332              
333             The full text of the license can be found in the
334             LICENSE file included with this module.
335              
336              
337             =head1 SEE ALSO
338              
339             perl(1).
340              
341             =cut
342              
343             #################### main pod documentation end ###################
344              
345             1;
346              
347             # The preceding line will help the module return a true value
348              
349             __DATA__