File Coverage

blib/lib/Mojolicious/Plugin/ConfigHashMerge.pm
Criterion Covered Total %
statement 24 25 96.0
branch 9 18 50.0
condition 7 17 41.1
subroutine 4 4 100.0
pod 1 1 100.0
total 45 65 69.2


line stmt bran cond sub pod time code
1             package Mojolicious::Plugin::ConfigHashMerge;
2 2     2   50242 use Mojo::Base 'Mojolicious::Plugin::Config';
  2         5  
  2         12  
3 2     2   3218 use Hash::Merge::Simple qw( merge );
  2         720  
  2         113  
4 2     2   21 use File::Spec::Functions 'file_name_is_absolute';
  2         5  
  2         608  
5              
6             our $VERSION = '0.03';
7              
8             sub register {
9 3     3 1 19040 my ($self, $app, $conf) = @_;
10              
11             # Override
12 3 100       23 return $app->config if $app->config->{config_override};
13              
14             # Config file
15 2   66     38 my $file = $conf->{file} || $ENV{MOJO_CONFIG};
16 2   50     15 $file ||= $app->moniker . '.' . ($conf->{ext} || 'conf');
      66        
17              
18             # Mode specific config file
19 2 50       78 my $mode = $file =~ /^(.*)\.([^.]+)$/ ? join('.', $1, $app->mode, $2) : '';
20              
21 2         36 my $home = $app->home;
22 2 50       27 $file = $home->rel_file($file) unless file_name_is_absolute $file;
23 2 50 33     118 $mode = $home->rel_file($mode) if $mode && !file_name_is_absolute $mode;
24 2 50 33     49 $mode = undef unless $mode && -e $mode;
25              
26             # Read config file
27 2         67 my $config = {};
28 2 50 0     6 if (-e $file) { $config = $self->load($file, $conf, $app) }
  2 0       46  
29              
30             # Check for default and mode specific config file
31             elsif (!$conf->{default} && !$mode) {
32 0         0 die qq{Configuration file "$file" missing, maybe you need to create it?\n};
33             }
34              
35             # Merge everything
36 2 50       1261 $config = merge($config, $self->load($mode, $conf, $app)) if $mode;
37 2 50       12 $config = merge($conf->{default}, $config) if $conf->{default};
38 2         144 return $app->defaults(config => $app->config)->config($config)->config;
39             }
40             1;
41             __END__