File Coverage

blib/lib/Dancer2/ConfigReader/Config/Any.pm
Criterion Covered Total %
statement 65 70 92.8
branch 12 22 54.5
condition 2 3 66.6
subroutine 13 13 100.0
pod 1 1 100.0
total 93 109 85.3


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.1.0';
4 149     149   323653 use Moo;
  149         9059  
  149         1267  
5              
6 149     149   74207 use Config::Any;
  149         12321  
  149         6054  
7 149     149   1527 use Hash::Merge::Simple;
  149         864  
  149         11828  
8 149     149   947 use Carp 'croak';
  149         404  
  149         10233  
9 149     149   971 use Module::Runtime 'require_module';
  149         540  
  149         1603  
10              
11 149     149   10000 use Dancer2::Core::Factory;
  149         392  
  149         4818  
12 149     149   876 use Dancer2::Core;
  149         316  
  149         4164  
13 149     149   1388 use Dancer2::Core::Types;
  149         323  
  149         1704  
14 149     149   2295479 use Path::Tiny qw< path >;
  149         12278  
  149         158364  
15              
16             with 'Dancer2::Core::Role::ConfigReader';
17              
18             has name => (
19             is => 'ro',
20             isa => Str,
21             lazy => 0,
22             default => sub {'Config::Any'},
23             );
24              
25             has config_files => (
26             is => 'ro',
27             lazy => 1,
28             isa => ArrayRef,
29             builder => '_build_config_files',
30             );
31              
32             has _config_files_path => (
33             is => 'ro',
34             lazy => 1,
35             isa => ArrayRef[InstanceOf['Path::Tiny']],
36             builder => '_build_config_files_path',
37             init_arg => undef,
38             );
39              
40             sub read_config {
41 266     266 1 917 my ($self) = @_;
42              
43             my $config = Hash::Merge::Simple->merge(
44             map {
45 162 50       16081 warn "Merging config file $_\n" if $ENV{DANCER_CONFIG_VERBOSE};
46 162         834 $self->_load_config_file($_)
47 266         690 } @{ $self->config_files }
  266         7235  
48             );
49              
50 264         15643 return $config;
51             }
52              
53             sub _build_config_files {
54 271     271   8214 my ($self) = @_;
55 271         623 return [ map $_->stringify, @{ $self->_config_files_path } ];
  271         6208  
56             }
57              
58             sub _build_config_files_path {
59 271     271   3433 my ($self) = @_;
60              
61 271         6191 my $location = $self->config_location;
62 271 50       20898 warn "Searching config files in location: $location\n" if $ENV{DANCER_CONFIG_VERBOSE};
63             # an undef location means no config files for the caller
64 271 50       1344 return [] unless defined $location;
65              
66 271         1790 my $config_location = Path::Tiny::path($location);
67 271         22814 my $environments_location = Path::Tiny::path( $self->environments_location );
68              
69 271         27994 my $running_env = $self->environment;
70 271         3444 my @available_exts = Config::Any->extensions;
71 271         2613776 my @files;
72              
73 271         1331 my @exts = @available_exts;
74 271 50       1784 if (my $ext = $ENV{DANCER_CONFIG_EXT}) {
75 0 0       0 if (grep { $ext eq $_ } @available_exts) {
  0         0  
76 0         0 @exts = $ext;
77             warn "Only looking for configs ending in '$ext'\n"
78 0 0       0 if $ENV{DANCER_CONFIG_VERBOSE};
79             } else {
80 0         0 warn "DANCER_CONFIG_EXT environment variable set to '$ext' which\n" .
81             "is not recognized by Config::Any. Looking for config file\n" .
82             "using default list of extensions:\n" .
83             "\t@available_exts\n";
84             }
85             }
86              
87 271         1489 foreach my $file ( [ $config_location, "config" ],
88             [ $environments_location, $running_env ] )
89             {
90 542         5492 foreach my $ext (@exts) {
91 5420         100971 my $path = Path::Tiny::path( $file->[0], $file->[1] . ".$ext" );
92 5420 100       248262 next unless -r $path;
93              
94             # Look for *_local.ext files
95 167         5669 my $local = Path::Tiny::path( $file->[0], $file->[1] . "_local.$ext" );
96 167 100       7799 push @files, $path, ( -r $local ? $local : () );
97             }
98             }
99              
100 271 50       5351 warn "Found following config files: @files\n" if $ENV{DANCER_CONFIG_VERBOSE};
101 271         11531 return \@files;
102             }
103              
104             sub _load_config_file {
105 162     162   607 my ( $self, $file ) = @_;
106 162         434 my $config;
107              
108 162         413 eval {
109 162         538 my @files = ($file);
110 162         2025 my $tmpconfig =
111             Config::Any->load_files( { files => \@files, use_ext => 1 } )->[0];
112 160 50       1266234 ( $file, $config ) = %{$tmpconfig} if defined $tmpconfig;
  160         996  
113             };
114 162 100 66     12759 if ( my $err = $@ || ( !$config ) ) {
115 2         300 croak "Unable to parse the configuration file: $file: $@";
116             }
117              
118             # TODO handle mergeable entries
119 160         2170 return $config;
120             }
121              
122             1;
123              
124             __END__