File Coverage

blib/lib/Dancer2/ConfigReader/Config/Any.pm
Criterion Covered Total %
statement 63 68 92.6
branch 12 22 54.5
condition 2 3 66.6
subroutine 13 13 100.0
pod 1 1 100.0
total 91 107 85.0


line stmt bran cond sub pod time code
1             # ABSTRACT: Config reader for files
2             package Dancer2::ConfigReader::Config::Any;
3             $Dancer2::ConfigReader::Config::Any::VERSION = '2.0.1';
4 145     145   477147 use Moo;
  145         7875  
  145         1216  
5              
6 145     145   68911 use File::Spec;
  145         399  
  145         5120  
7 145     145   3218 use Config::Any;
  145         11114  
  145         4801  
8 145     145   1243 use Hash::Merge::Simple;
  145         735  
  145         10303  
9 145     145   999 use Carp 'croak';
  145         326  
  145         9858  
10 145     145   1044 use Module::Runtime 'require_module';
  145         356  
  145         1404  
11              
12 145     145   9667 use Dancer2::Core::Factory;
  145         385  
  145         4747  
13 145     145   808 use Dancer2::Core;
  145         390  
  145         3876  
14 145     145   1153 use Dancer2::Core::Types;
  145         328  
  145         1499  
15 145     145   2301143 use Dancer2::FileUtils 'path';
  145         414  
  145         142000  
16              
17             with 'Dancer2::Core::Role::ConfigReader';
18              
19             has name => (
20             is => 'ro',
21             isa => Str,
22             lazy => 0,
23             default => sub {'Config::Any'},
24             );
25              
26             has config_files => (
27             is => 'ro',
28             lazy => 1,
29             isa => ArrayRef,
30             builder => '_build_config_files',
31             );
32              
33             sub read_config {
34 256     256 1 2757 my ($self) = @_;
35              
36             my $config = Hash::Merge::Simple->merge(
37             map {
38 150 50       5798 warn "Merging config file $_\n" if $ENV{DANCER_CONFIG_VERBOSE};
39 150         840 $self->_load_config_file($_)
40 256         635 } @{ $self->config_files }
  256         6531  
41             );
42              
43 255         8601 return $config;
44             }
45              
46             sub _build_config_files {
47 256     256   3265 my ($self) = @_;
48              
49 256         5487 my $location = $self->config_location;
50 256 50       18003 warn "Searching config files in location: $location\n" if $ENV{DANCER_CONFIG_VERBOSE};
51             # an undef location means no config files for the caller
52 256 50       1026 return [] unless defined $location;
53              
54 256         1116 my $running_env = $self->environment;
55 256         2521 my @available_exts = Config::Any->extensions;
56 256         2523961 my @files;
57              
58 256         1125 my @exts = @available_exts;
59 256 50       1691 if (my $ext = $ENV{DANCER_CONFIG_EXT}) {
60 0 0       0 if (grep { $ext eq $_ } @available_exts) {
  0         0  
61 0         0 @exts = $ext;
62             warn "Only looking for configs ending in '$ext'\n"
63 0 0       0 if $ENV{DANCER_CONFIG_VERBOSE};
64             } else {
65 0         0 warn "DANCER_CONFIG_EXT environment variable set to '$ext' which\n" .
66             "is not recognized by Config::Any. Looking for config file\n" .
67             "using default list of extensions:\n" .
68             "\t@available_exts\n";
69             }
70             }
71              
72 256         10553 foreach my $file ( [ $location, "config" ],
73             [ $self->environments_location, $running_env ] )
74             {
75 512         20149 foreach my $ext (@exts) {
76 5120         19429 my $path = path( $file->[0], $file->[1] . ".$ext" );
77 5120 100       89020 next if !-r $path;
78              
79             # Look for *_local.ext files
80 148         1067 my $local = path( $file->[0], $file->[1] . "_local.$ext" );
81 148 100       2665 push @files, $path, ( -r $local ? $local : () );
82             }
83             }
84              
85 256 50       1749 warn "Found following config files: @files\n" if $ENV{DANCER_CONFIG_VERBOSE};
86 256         20607 return \@files;
87             }
88              
89             sub _load_config_file {
90 150     150   547 my ( $self, $file ) = @_;
91 150         344 my $config;
92              
93 150         435 eval {
94 150         522 my @files = ($file);
95 150         1912 my $tmpconfig =
96             Config::Any->load_files( { files => \@files, use_ext => 1 } )->[0];
97 149 50       1362639 ( $file, $config ) = %{$tmpconfig} if defined $tmpconfig;
  149         969  
98             };
99 150 100 66     7876 if ( my $err = $@ || ( !$config ) ) {
100 1         175 croak "Unable to parse the configuration file: $file: $@";
101             }
102              
103             # TODO handle mergeable entries
104 149         2270 return $config;
105             }
106              
107             1;
108              
109             __END__