| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 63 | # an undef location means no config files for the caller | ||||||||||||||||||||||||||||
| 64 | 318 | ▶ | return [] unless defined $location; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 75 | 0 | ▶ | if (grep { $ext eq $_ } @available_exts) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 76 | 0 | @exts = $ext; | |||||||||||||||||||||||||||
| 77 | warn "Only looking for configs ending in '$ext'\n" | ||||||||||||||||||||||||||||
| 78 | 0 | ▶ | if $ENV{DANCER_CONFIG_VERBOSE}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 97 | } | ||||||||||||||||||||||||||||
| 98 | } | ||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | 318 | ▶ | warn "Found following config files: @files\n" if $ENV{DANCER_CONFIG_VERBOSE}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 113 | }; | ||||||||||||||||||||||||||||
| 114 | 325 | ▶ | if ( my $err = $@ || ( !$config ) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$@ || !$config
MC/DC50%1/2
$@ || !$config
$@
!$config
Truth table66%2/3
A || not(B)
A= $@B= !$config
| |||||||||||||||||||||||||||||
| 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__ | ||||||||||||||||||||||||||||