Debian Codegraph @

t/lib/Dancer2/ConfigReader/File/Extended.pm

raw
1 package Dancer2::ConfigReader::File::Extended;
2
3 use Moo;
4 use Dancer2::Core::Types;
5
6 use Carp 'croak';
7
8 extends 'Dancer2::ConfigReader::Config::Any';
9
10 has name => (
11 is => 'ro',
12 isa => Str,
13 lazy => 0,
14 default => sub {'File::Extended'},
15 );
16
17 around read_config => sub {
18 my ($orig, $self) = @_;
19 my $config = $orig->($self, @_);
20 $self->_replace_env_vars($config);
21 return $config;
22 };
23
24 # Attn. We are traversing along the original data structure all the time,
25 # using references, and changing values on the spot, not returning anything.
26 sub _replace_env_vars {
27 my ( $self, $entry ) = @_;
28 if( ref $entry ne 'HASH' && ref $entry ne 'ARRAY' ) {
29 croak 'Param entry is not HASH or ARRAY';
30 }
31 if( ref $entry eq 'HASH' ) {
32 foreach my $value (values %{ $entry }) {
33 if( (ref $value) =~ m/(HASH|ARRAY)/msx ) {
34 $self->_replace_env_vars( $value );
35 } elsif( (ref $value) =~ m/(CODE|REF|GLOB)/msx ) {
36 # Pretty much anything else except SCALAR. Do nothing
37 1;
38 } else {
39 if( $value ) {
40 while( my ($k, $v) = each %ENV) {
41 $value =~ s/ \$ [{] ENV:$k [}] /$v/gmsx;
42 }
43 }
44 }
45 }
46 } else {
47 # ref $entry is 'ARRAY'
48 foreach my $value (@{ $entry }) {
49 if( (ref $value) =~ m/(HASH|ARRAY)/msx ) {
50 $self->_replace_env_vars( $value );
51 } elsif( (ref $value) =~ m/(CODE|REF|GLOB)/msx ) {
52 # Pretty much anything else except SCALAR. Do nothing
53 1;
54 } else {
55 if( $value ) {
56 while( my ($k, $v) = each %ENV) {
57 $value =~ s/ \$ [{] ENV:$k [}] /$v/gmsx;
58 }
59 }
60 }
61 }
62 }
63 return;
64 }
65
66 1;