-
Notifications
You must be signed in to change notification settings - Fork 277
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Mikko Johannes Koivunalho <[email protected]>
- Loading branch information
Showing
8 changed files
with
191 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
#!perl | ||
|
||
use Dancer2; | ||
use App1; | ||
|
||
start; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
app: | ||
config: ok | ||
extended: | ||
one: ${ENV:DANCER_FILE_EXTENDED_ONE} | ||
two: Begin ${ENV:DANCER_FILE_EXTENDED_TWO} End |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package App1; | ||
use strict; | ||
use warnings; | ||
use Dancer2; | ||
|
||
1; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
use strict; | ||
use warnings; | ||
|
||
use Test::More; | ||
use File::Spec; | ||
|
||
BEGIN { | ||
# undefine ENV vars used as defaults for app environment in these tests | ||
local $ENV{DANCER_ENVIRONMENT}; | ||
local $ENV{PLACK_ENV}; | ||
$ENV{DANCER_CONFIG_READERS} = 'Dancer2::ConfigReader::File::Extended'; | ||
$ENV{DANCER_FILE_EXTENDED_ONE} = 'Extended String'; | ||
$ENV{DANCER_FILE_EXTENDED_TWO} = 'ExtendedToo'; | ||
} | ||
use lib '.'; | ||
use lib './t/lib'; | ||
|
||
use t::app::t_config_file_extended::lib::App1; | ||
|
||
my $app = Dancer2->runner->apps->[0]; | ||
|
||
is_deeply $app->config_files, | ||
[ File::Spec->rel2abs(File::Spec->catfile( 't', 'app', | ||
't_config_file_extended', 'config.yml' )) ], | ||
$app->name . ": config files found"; | ||
|
||
is $app->config->{app}->{config}, 'ok', | ||
$app->name . ": config loaded properly"; | ||
is $app->config->{extended}->{one}, 'Extended String', | ||
$app->name . ": extended config (extended:one) loaded properly"; | ||
is $app->config->{extended}->{two}, 'Begin ExtendedToo End', | ||
$app->name . ": extended config (extended:two) loaded properly"; | ||
|
||
done_testing; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
use strict; | ||
use warnings; | ||
|
||
use Test::More; | ||
use File::Spec; | ||
|
||
BEGIN { | ||
# undefine ENV vars used as defaults for app environment in these tests | ||
local $ENV{DANCER_ENVIRONMENT}; | ||
local $ENV{PLACK_ENV}; | ||
$ENV{DANCER_CONFIG_READERS} | ||
= 'Dancer2::ConfigReader::File::Simple Dancer2::ConfigReader::TestDummy'; | ||
} | ||
use lib '.'; | ||
use lib './t/lib'; | ||
|
||
use t::app::t1::lib::App1; | ||
|
||
my $app = Dancer2->runner->apps->[0]; | ||
|
||
is_deeply $app->config_files, | ||
[ File::Spec->rel2abs(File::Spec->catfile( 't', 'app', 't1', 'config.yml' )) ], | ||
$app->name . ": config files found"; | ||
|
||
is $app->config->{app}->{config}, 'ok', | ||
$app->name . ": config loaded properly"; | ||
is $app->config->{dummy}->{dummy_subitem}, 2, | ||
$app->name . ": dummy config loaded properly"; | ||
|
||
done_testing; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package Dancer2::ConfigReader::File::Extended; | ||
|
||
use Moo; | ||
use Dancer2::Core::Types; | ||
|
||
use Carp 'croak'; | ||
|
||
extends 'Dancer2::ConfigReader::File::Simple'; | ||
|
||
has name => ( | ||
is => 'ro', | ||
isa => Str, | ||
lazy => 0, | ||
default => sub {'File::Extended'}, | ||
); | ||
|
||
around read_config => sub { | ||
my ($orig, $self) = @_; | ||
my $config = $orig->($self, @_); | ||
$self->_replace_env_vars($config); | ||
return $config; | ||
}; | ||
|
||
# Attn. We are traversing along the original data structure all the time, | ||
# using references, and changing values on the spot, not returning anything. | ||
sub _replace_env_vars { | ||
my ( $self, $entry ) = @_; | ||
if( ref $entry ne 'HASH' && ref $entry ne 'ARRAY' ) { | ||
croak 'Param entry is not HASH or ARRAY'; | ||
} | ||
if( ref $entry eq 'HASH' ) { | ||
foreach my $value (values %{ $entry }) { | ||
if( (ref $value) =~ m/(HASH|ARRAY)/msx ) { | ||
$self->_replace_env_vars( $value ); | ||
} elsif( (ref $value) =~ m/(CODE|REF|GLOB)/msx ) { | ||
# Pretty much anything else except SCALAR. Do nothing | ||
1; | ||
} else { | ||
if( $value ) { | ||
while( my ($k, $v) = each %ENV) { | ||
$value =~ s/ \$ [{] ENV:$k [}] /$v/gmsx; | ||
} | ||
} | ||
} | ||
} | ||
} else { | ||
# ref $entry is 'ARRAY' | ||
foreach my $value (@{ $entry }) { | ||
if( (ref $value) =~ m/(HASH|ARRAY)/msx ) { | ||
$self->_replace_env_vars( $value ); | ||
} elsif( (ref $value) =~ m/(CODE|REF|GLOB)/msx ) { | ||
# Pretty much anything else except SCALAR. Do nothing | ||
1; | ||
} else { | ||
if( $value ) { | ||
while( my ($k, $v) = each %ENV) { | ||
$value =~ s/ \$ [{] ENV:$k [}] /$v/gmsx; | ||
} | ||
} | ||
} | ||
} | ||
} | ||
return; | ||
} | ||
|
||
1; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package Dancer2::ConfigReader::TestDummy; | ||
use Moo; | ||
use Dancer2::Core::Factory; | ||
use Dancer2::Core; | ||
use Dancer2::Core::Types; | ||
use Dancer2::FileUtils 'path'; | ||
|
||
with 'Dancer2::Core::Role::ConfigReader'; | ||
|
||
has name => ( | ||
is => 'ro', | ||
isa => Str, | ||
lazy => 0, | ||
default => sub {'TestDummy'}, | ||
); | ||
|
||
has config_files => ( | ||
is => 'ro', | ||
lazy => 1, | ||
isa => ArrayRef, | ||
default => sub { | ||
my ($self) = @_; | ||
return []; | ||
}, | ||
); | ||
|
||
sub read_config { | ||
my %config = ( | ||
dummy => { | ||
dummy_subitem => 2, | ||
} | ||
); | ||
return \%config; | ||
} | ||
|
||
1; |