| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package Enbld::App::Configuration; | 
| 2 |  |  |  |  |  |  |  | 
| 3 | 3 |  |  | 3 |  | 937 | use strict; | 
|  | 3 |  |  |  |  | 6 |  | 
|  | 3 |  |  |  |  | 119 |  | 
| 4 | 3 |  |  | 3 |  | 18 | use warnings; | 
|  | 3 |  |  |  |  | 4 |  | 
|  | 3 |  |  |  |  | 123 |  | 
| 5 |  |  |  |  |  |  |  | 
| 6 | 3 |  |  | 3 |  | 17 | use File::Spec; | 
|  | 3 |  |  |  |  | 6 |  | 
|  | 3 |  |  |  |  | 68 |  | 
| 7 | 3 |  |  | 3 |  | 1924 | use autodie; | 
|  | 3 |  |  |  |  | 50136 |  | 
|  | 3 |  |  |  |  | 21 |  | 
| 8 | 3 |  |  | 3 |  | 42969 | use Carp; | 
|  | 3 |  |  |  |  | 19 |  | 
|  | 3 |  |  |  |  | 4290 |  | 
| 9 |  |  |  |  |  |  |  | 
| 10 |  |  |  |  |  |  | our $envname = 'myenv'; | 
| 11 |  |  |  |  |  |  |  | 
| 12 |  |  |  |  |  |  | our $config_ref; | 
| 13 |  |  |  |  |  |  | our $rcfile_ref; | 
| 14 |  |  |  |  |  |  |  | 
| 15 |  |  |  |  |  |  | our $CONFIGURATIONFILE; | 
| 16 |  |  |  |  |  |  |  | 
| 17 |  |  |  |  |  |  | our $dirty; | 
| 18 |  |  |  |  |  |  |  | 
| 19 |  |  |  |  |  |  | sub is_dirty { | 
| 20 | 3 |  |  | 3 | 0 | 12 | return $dirty; | 
| 21 |  |  |  |  |  |  | } | 
| 22 |  |  |  |  |  |  |  | 
| 23 |  |  |  |  |  |  | sub config { | 
| 24 | 1 |  |  | 1 | 0 | 3 | return $config_ref; | 
| 25 |  |  |  |  |  |  | } | 
| 26 |  |  |  |  |  |  |  | 
| 27 |  |  |  |  |  |  | sub rcfile { | 
| 28 | 1 |  |  | 1 | 0 | 5 | return $rcfile_ref; | 
| 29 |  |  |  |  |  |  | } | 
| 30 |  |  |  |  |  |  |  | 
| 31 |  |  |  |  |  |  | # method for only test. | 
| 32 |  |  |  |  |  |  | sub destroy { | 
| 33 | 37 |  |  | 37 | 0 | 12719 | undef $CONFIGURATIONFILE; | 
| 34 |  |  |  |  |  |  |  | 
| 35 | 37 |  |  |  |  | 170 | undef $envname; | 
| 36 | 37 |  |  |  |  | 166 | undef $dirty; | 
| 37 |  |  |  |  |  |  |  | 
| 38 | 37 |  |  |  |  | 107 | undef $config_ref; | 
| 39 | 37 |  |  |  |  | 181 | undef $rcfile_ref; | 
| 40 |  |  |  |  |  |  | } | 
| 41 |  |  |  |  |  |  |  | 
| 42 |  |  |  |  |  |  | sub _configuration_file_path { | 
| 43 | 6 |  |  | 6 |  | 41 | require Enbld::Home; | 
| 44 |  |  |  |  |  |  |  | 
| 45 | 6 |  |  |  |  | 52 | return File::Spec->catfile( Enbld::Home->conf, 'enbld.conf' ); | 
| 46 |  |  |  |  |  |  | } | 
| 47 |  |  |  |  |  |  |  | 
| 48 |  |  |  |  |  |  | sub read_file { | 
| 49 | 4 |  |  | 4 | 0 | 33 | my $pkg = shift; | 
| 50 |  |  |  |  |  |  |  | 
| 51 | 4 |  |  |  |  | 15 | my $path = _configuration_file_path(); | 
| 52 |  |  |  |  |  |  |  | 
| 53 | 4 | 100 |  |  |  | 122 | return if ( ! -e $path ); | 
| 54 |  |  |  |  |  |  |  | 
| 55 | 2 |  |  |  |  | 8 | open my $fh, '<', $path; | 
| 56 | 2 |  |  |  |  | 193 | my $str = do { local $/; <$fh> }; | 
|  | 2 |  |  |  |  | 9 |  | 
|  | 2 |  |  |  |  | 51 |  | 
| 57 | 2 |  |  |  |  | 10 | close $fh; | 
| 58 |  |  |  |  |  |  |  | 
| 59 | 2 |  |  |  |  | 316 | eval $str; | 
| 60 |  |  |  |  |  |  |  | 
| 61 | 2 |  |  |  |  | 11 | $envname = $CONFIGURATIONFILE->{envname}; | 
| 62 |  |  |  |  |  |  |  | 
| 63 | 2 |  |  |  |  | 13 | require Enbld::Config; | 
| 64 | 2 |  |  |  |  | 5 | foreach my $name ( keys %{ $CONFIGURATIONFILE->{config} } ) { | 
|  | 2 |  |  |  |  | 9 |  | 
| 65 | 1 |  |  |  |  | 9 | $config_ref->{$name} = | 
| 66 | 1 |  |  |  |  | 2 | Enbld::Config->new( %{ $CONFIGURATIONFILE->{config}{$name} } ); | 
| 67 |  |  |  |  |  |  | } | 
| 68 |  |  |  |  |  |  |  | 
| 69 | 2 |  |  |  |  | 718 | require Enbld::RcFile; | 
| 70 | 2 |  |  |  |  | 7 | foreach my $filename ( keys %{ $CONFIGURATIONFILE->{rcfile} } ) { | 
|  | 2 |  |  |  |  | 169 |  | 
| 71 | 1 |  |  |  |  | 9 | $rcfile_ref->{$filename} = | 
| 72 | 1 |  |  |  |  | 2 | Enbld::RcFile->new( %{ $CONFIGURATIONFILE->{rcfile}{$filename} }); | 
| 73 |  |  |  |  |  |  | } | 
| 74 |  |  |  |  |  |  |  | 
| 75 | 2 |  |  |  |  | 13 | return $envname; | 
| 76 |  |  |  |  |  |  | } | 
| 77 |  |  |  |  |  |  |  | 
| 78 |  |  |  |  |  |  | sub write_file { | 
| 79 |  |  |  |  |  |  |  | 
| 80 | 4 |  |  | 4 | 0 | 43 | require Enbld::Feature; | 
| 81 | 4 | 100 |  |  |  | 22 | return if Enbld::Feature->is_deploy_mode; | 
| 82 |  |  |  |  |  |  |  | 
| 83 | 3 | 100 |  |  |  | 12 | return unless is_dirty(); | 
| 84 |  |  |  |  |  |  |  | 
| 85 | 2 |  |  |  |  | 18 | $CONFIGURATIONFILE = { | 
| 86 |  |  |  |  |  |  | envname     =>  $envname, | 
| 87 |  |  |  |  |  |  | config      =>  {}, | 
| 88 |  |  |  |  |  |  | rcfile      =>  {}, | 
| 89 |  |  |  |  |  |  | format      =>  'v2.0',  # for future...configuration format version. | 
| 90 |  |  |  |  |  |  | }; | 
| 91 |  |  |  |  |  |  |  | 
| 92 | 2 |  |  |  |  | 4 | foreach my $name ( keys %{ $config_ref } ) { | 
|  | 2 |  |  |  |  | 9 |  | 
| 93 | 1 |  |  |  |  | 4 | $CONFIGURATIONFILE->{config}{$name} = $config_ref->{$name}->serialize; | 
| 94 |  |  |  |  |  |  | } | 
| 95 |  |  |  |  |  |  |  | 
| 96 | 2 |  |  |  |  | 7 | foreach my $filename ( keys %{ $rcfile_ref } ) { | 
|  | 2 |  |  |  |  | 7 |  | 
| 97 | 1 |  |  |  |  | 6 | $CONFIGURATIONFILE->{rcfile}{$filename} = | 
| 98 |  |  |  |  |  |  | $rcfile_ref->{$filename}->serialize; | 
| 99 |  |  |  |  |  |  | } | 
| 100 |  |  |  |  |  |  |  | 
| 101 | 2 |  |  |  |  | 13 | my $path = _configuration_file_path(); | 
| 102 |  |  |  |  |  |  |  | 
| 103 | 2 |  |  |  |  | 1162 | require Data::Dumper; | 
| 104 | 2 |  |  |  |  | 8086 | open my $fh, '>', $path; | 
| 105 | 2 |  |  |  |  | 2532 | my $dump = Data::Dumper->new( | 
| 106 |  |  |  |  |  |  | [ $CONFIGURATIONFILE ], | 
| 107 |  |  |  |  |  |  | [ 'Enbld::App::Configuration::CONFIGURATIONFILE' ] | 
| 108 |  |  |  |  |  |  | ); | 
| 109 | 2 |  |  |  |  | 84 | print $fh $dump->Dump; | 
| 110 | 2 |  |  |  |  | 252 | close $fh; | 
| 111 |  |  |  |  |  |  |  | 
| 112 | 2 |  |  |  |  | 1358 | return $envname; | 
| 113 |  |  |  |  |  |  | } | 
| 114 |  |  |  |  |  |  |  | 
| 115 |  |  |  |  |  |  | sub set_config { | 
| 116 | 7 |  |  | 7 | 0 | 111 | my ( $pkg, $config ) = @_; | 
| 117 |  |  |  |  |  |  |  | 
| 118 | 7 |  |  |  |  | 58 | my $name = $config->name; | 
| 119 | 7 |  |  |  |  | 232 | $config_ref->{$name} = $config; | 
| 120 |  |  |  |  |  |  |  | 
| 121 | 7 |  |  |  |  | 70 | $dirty++; | 
| 122 |  |  |  |  |  |  |  | 
| 123 | 7 |  |  |  |  | 25 | return $config->name; | 
| 124 |  |  |  |  |  |  | } | 
| 125 |  |  |  |  |  |  |  | 
| 126 |  |  |  |  |  |  | sub set_rcfile { | 
| 127 | 2 |  |  | 2 | 0 | 32 | my ( $pkg, $rcfile ) = @_; | 
| 128 |  |  |  |  |  |  |  | 
| 129 | 2 |  |  |  |  | 13 | my $filename = $rcfile->filename; | 
| 130 | 2 |  |  |  |  | 10 | $rcfile_ref->{$filename} = $rcfile; | 
| 131 |  |  |  |  |  |  |  | 
| 132 | 2 |  |  |  |  | 7 | $dirty++; | 
| 133 |  |  |  |  |  |  |  | 
| 134 | 2 |  |  |  |  | 9 | return $rcfile->filename; | 
| 135 |  |  |  |  |  |  | } | 
| 136 |  |  |  |  |  |  |  | 
| 137 |  |  |  |  |  |  | sub search_config { | 
| 138 | 9 |  |  | 9 | 0 | 1434 | my ( $pkg, $name ) = @_; | 
| 139 |  |  |  |  |  |  |  | 
| 140 | 9 | 100 |  |  |  | 75 | return $config_ref->{$name} if ( exists $config_ref->{$name} ); | 
| 141 | 3 |  |  |  |  | 9 | return; | 
| 142 |  |  |  |  |  |  | } | 
| 143 |  |  |  |  |  |  |  | 
| 144 |  |  |  |  |  |  | sub search_rcfile { | 
| 145 | 3 |  |  | 3 | 0 | 2958 | my ( $pkg, $filename ) = @_; | 
| 146 |  |  |  |  |  |  |  | 
| 147 | 3 | 100 |  |  |  | 22 | return $rcfile_ref->{$filename} if ( exists $rcfile_ref->{$filename} ); | 
| 148 | 1 |  |  |  |  | 4 | return; | 
| 149 |  |  |  |  |  |  | } | 
| 150 |  |  |  |  |  |  |  | 
| 151 |  |  |  |  |  |  | sub envname { | 
| 152 | 1 |  |  | 1 | 0 | 9 | return $envname; | 
| 153 |  |  |  |  |  |  | } | 
| 154 |  |  |  |  |  |  |  | 
| 155 |  |  |  |  |  |  | sub set_envname { | 
| 156 | 1 |  |  | 1 | 0 | 1140 | my ( $pkg, $name ) = @_; | 
| 157 |  |  |  |  |  |  |  | 
| 158 | 1 |  |  |  |  | 4 | $envname = $name; | 
| 159 |  |  |  |  |  |  |  | 
| 160 | 1 |  |  |  |  | 4 | return $envname; | 
| 161 |  |  |  |  |  |  | } | 
| 162 |  |  |  |  |  |  |  | 
| 163 |  |  |  |  |  |  | 1; | 
| 164 |  |  |  |  |  |  |  |