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   14262 use Mojo::Base 'Mojolicious::Plugin::Config';
  2         6  
  2         16  
3 2     2   3183 use Hash::Merge::Simple qw( merge );
  2         794  
  2         116  
4 2     2   17 use File::Spec::Functions 'file_name_is_absolute';
  2         6  
  2         610  
5              
6             our $VERSION = '0.04';
7              
8             sub register {
9 3     3 1 23677 my ($self, $app, $conf) = @_;
10              
11             # Override
12 3 100       21 return $app->config if $app->config->{config_override};
13              
14             # Config file
15 2   66     37 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       117 my $mode = $file =~ /^(.*)\.([^.]+)$/ ? join('.', $1, $app->mode, $2) : '';
20              
21 2         40 my $home = $app->home;
22 2 50       22 $file = $home->rel_file($file) unless file_name_is_absolute $file;
23 2 50 33     124 $mode = $home->rel_file($mode) if $mode && !file_name_is_absolute $mode;
24 2 50 33     50 $mode = undef unless $mode && -e $mode;
25              
26             # Read config file
27 2         69 my $config = {};
28 2 50 0     8 if (-e $file) { $config = $self->load($file, $conf, $app) }
  2 0       44  
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       1145 $config = merge($config, $self->load($mode, $conf, $app)) if $mode;
37 2 50       11 $config = merge($conf->{default}, $config) if $conf->{default};
38 2         142 return $app->defaults(config => $app->config)->config($config)->config;
39             }
40             1;
41             __END__