lib/Dancer2/ConfigReader/Config/Any.pm

filter: Statementstmt 93.5% 72 / 77 Branchbran 54.5% 12 / 22 Conditioncond 66.6% 2 / 3 MC/DCmcdc 50.0% 1 / 2 Subroutinesub 100% 14 / 14 Podpod 100% 1 / 1 totaltotal 85.7% 102 / 119 CC 15 SCAR 19.6
CC
15
Cov
84%
_build_config_files_path
15.2
_load_config_file
4.0
read_config
2.0
CRAP 7.1
« lib/Dancer2/Core/App.pm ↑ Summary lib/Dancer2/Core/Error.pm »
1 # ABSTRACT: Config reader for files
2 package Dancer2::ConfigReader::Config::Any;
3 168 $Dancer2::ConfigReader::Config::Any::VERSION = '2.3.0';
4 168 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 168 ▶use Config::Any;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 168 ▶use Hash::Merge::Simple;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 168 ▶use Carp 'croak';
Subroutine BEGIN: called CC=1 SCAR=0.0
9 168 ▶use Module::Runtime 'require_module';
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 168 ▶use Dancer2::Core::Factory;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 168 ▶use Dancer2::Core;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 168 ▶use Dancer2::Core::Types;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 168 ▶use Path::Tiny qw< path >;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 168 with 'Dancer2::Core::Role::ConfigReader';
17  
18 has name => (
19 is => 'ro',
20 isa => Str,
21 lazy => 0,
22 318 ▶ default => sub {'Config::Any'},
Subroutine __ANON__: called CC=1 SCAR=0.0
23 168 );
24  
25 168 has config_files => (
26 is => 'ro',
27 lazy => 1,
28 isa => ArrayRef,
29 builder => '_build_config_files',
30 );
31  
32 168 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 313 ▶sub read_config {
Subroutine read_config: called CC=2 SCAR=7.0
Pod documented
41 313 my ($self) = @_;
42  
43 my $config = Hash::Merge::Simple->merge(
44 map {
45 325 ▶ warn "Merging config file $_\n" if $ENV{DANCER_CONFIG_VERBOSE};
Branch: 1 branch
BranchTrueFalseTotal
if $ENV{"DANCER_CONFIG_VERBOSE"} 0 325 325
46 325 $self->_load_config_file($_)
47 313 } @{ $self->config_files }
48 );
49  
50 311 return $config;
51 }
52  
53 318 ▶sub _build_config_files {
Subroutine _build_config_files: called CC=1 SCAR=0.0
54 318 my ($self) = @_;
55 318 return [ map $_->stringify, @{ $self->_config_files_path } ];
56 }
57  
58 318 ▶sub _build_config_files_path {
Subroutine _build_config_files_path: called CC=11 SCAR=27.2
59 318 my ($self) = @_;
60  
61 318 my $location = $self->config_location;
62 318 ▶ warn "Searching config files in location: $location\n" if $ENV{DANCER_CONFIG_VERBOSE};
Branch: 1 branch
BranchTrueFalseTotal
if $ENV{"DANCER_CONFIG_VERBOSE"} 0 318 318
63 # an undef location means no config files for the caller
64 318 ▶ return [] unless defined $location;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $location 0 318 318
65  
66 318 my $config_location = Path::Tiny::path($location);
67 318 my $environments_location = Path::Tiny::path( $self->environments_location );
68  
69 318 my $running_env = $self->environment;
70 318 my @available_exts = Config::Any->extensions;
71 318 my @files;
72  
73 318 my @exts = @available_exts;
74 318 ▶ if (my $ext = $ENV{DANCER_CONFIG_EXT}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $ext = $ENV{"DANCER_CONFIG_EXT"}) 0 318 318
75 0 ▶ if (grep { $ext eq $_ } @available_exts) {
Branch: 1 branch
BranchTrueFalseTotal
if (grep {$ext eq $_;} @available_exts) { } 0 0 0
76 0 @exts = $ext;
77 warn "Only looking for configs ending in '$ext'\n"
78 0 ▶ if $ENV{DANCER_CONFIG_VERBOSE};
Branch: 1 branch
BranchTrueFalseTotal
if $ENV{"DANCER_CONFIG_VERBOSE"} 0 0 0
79 } else {
80 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 318 foreach my $file ( [ $config_location, "config" ],
88 [ $environments_location, $running_env ] )
89 {
90 636 foreach my $ext (@exts) {
91 6360 my $path = Path::Tiny::path( $file->[0], $file->[1] . ".$ext" );
92 6360 ▶ next unless -r $path;
Branch: 1 branch
BranchTrueFalseTotal
unless -r $path 6033 327 6360
93  
94 # Look for *_local.ext files
95 327 my $local = Path::Tiny::path( $file->[0], $file->[1] . "_local.$ext" );
96 327 ▶ push @files, $path, ( -r $local ? $local : () );
Branch: 1 branch
BranchTrueFalseTotal
-r $local ? : 9 318 327
97 }
98 }
99  
100 318 ▶ warn "Found following config files: @files\n" if $ENV{DANCER_CONFIG_VERBOSE};
Branch: 1 branch
BranchTrueFalseTotal
if $ENV{"DANCER_CONFIG_VERBOSE"} 0 318 318
101 318 return \@files;
102 }
103  
104 325 ▶sub _load_config_file {
Subroutine _load_config_file: called CC=4 SCAR=13.9
105 325 my ( $self, $file ) = @_;
106 325 my $config;
107  
108 325 eval {
109 325 my @files = ($file);
110 325 my $tmpconfig =
111 Config::Any->load_files( { files => \@files, use_ext => 1 } )->[0];
112 323 ▶ ( $file, $config ) = %{$tmpconfig} if defined $tmpconfig;
Branch: 1 branch
BranchTrueFalseTotal
if defined $tmpconfig 323 0 323
113 };
114 325 ▶ if ( my $err = $@ || ( !$config ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $err = $@ || !$config) 2 323 325
Condition66%2/3
$@ || !$config
l!l&&r!l&&!r
20323
MC/DC50%1/2
$@ || !$config
$@ !$config
Truth table66%2/3
A || not(B)
A= $@B= !$config
ABresult
1X1
011
000
115 2 croak "Unable to parse the configuration file: $file: $@";
116 }
117  
118 # TODO handle mergeable entries
119 323 return $config;
120 }
121  
122 168 1;
123  
124 __END__
« lib/Dancer2/Core/App.pm ↑ Summary lib/Dancer2/Core/Error.pm »