Skip to content

Commit 1746c5f

Browse files
committed
Change how configs are read
Signed-off-by: Mikko Johannes Koivunalho <[email protected]>
1 parent d54db2e commit 1746c5f

File tree

12 files changed

+705
-263
lines changed

12 files changed

+705
-263
lines changed
Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
# ABSTRACT: Ye Olde Config Reader
2+
package Dancer2::ConfigReader::FileSimple;
3+
4+
use Moo;
5+
6+
use File::Spec;
7+
use Config::Any;
8+
use Hash::Merge::Simple;
9+
use Carp 'croak';
10+
use Module::Runtime 'require_module';
11+
12+
use Dancer2::Core::Factory;
13+
use Dancer2::Core;
14+
use Dancer2::Core::Types;
15+
use Dancer2::FileUtils 'path';
16+
17+
with 'Dancer2::Core::Role::ConfigReader';
18+
19+
has name => (
20+
is => 'ro',
21+
isa => Str,
22+
lazy => 0,
23+
default => sub {'FileSimple'},
24+
);
25+
26+
has config_files => (
27+
is => 'ro',
28+
lazy => 1,
29+
isa => ArrayRef,
30+
builder => '_build_config_files',
31+
);
32+
33+
sub read_config {
34+
my ($self) = @_;
35+
36+
my $config = Hash::Merge::Simple->merge(
37+
map {
38+
warn "Merging config file $_\n" if $ENV{DANCER_CONFIG_VERBOSE};
39+
$self->_load_config_file($_)
40+
} @{ $self->config_files }
41+
);
42+
43+
return $config;
44+
}
45+
46+
sub _build_config_files {
47+
my ($self) = @_;
48+
49+
my $location = $self->config_location;
50+
# an undef location means no config files for the caller
51+
return [] unless defined $location;
52+
53+
my $running_env = $self->environment;
54+
my @available_exts = Config::Any->extensions;
55+
my @files;
56+
57+
my @exts = @available_exts;
58+
if (my $ext = $ENV{DANCER_CONFIG_EXT}) {
59+
if (grep { $ext eq $_ } @available_exts) {
60+
@exts = $ext;
61+
warn "Only looking for configs ending in '$ext'\n"
62+
if $ENV{DANCER_CONFIG_VERBOSE};
63+
} else {
64+
warn "DANCER_CONFIG_EXT environment variable set to '$ext' which\n" .
65+
"is not recognized by Config::Any. Looking for config file\n" .
66+
"using default list of extensions:\n" .
67+
"\t@available_exts\n";
68+
}
69+
}
70+
71+
foreach my $file ( [ $location, "config" ],
72+
[ $self->environments_location, $running_env ] )
73+
{
74+
foreach my $ext (@exts) {
75+
my $path = path( $file->[0], $file->[1] . ".$ext" );
76+
next if !-r $path;
77+
78+
# Look for *_local.ext files
79+
my $local = path( $file->[0], $file->[1] . "_local.$ext" );
80+
push @files, $path, ( -r $local ? $local : () );
81+
}
82+
}
83+
84+
return \@files;
85+
}
86+
87+
sub _load_config_file {
88+
my ( $self, $file ) = @_;
89+
my $config;
90+
91+
eval {
92+
my @files = ($file);
93+
my $tmpconfig =
94+
Config::Any->load_files( { files => \@files, use_ext => 1 } )->[0];
95+
( $file, $config ) = %{$tmpconfig} if defined $tmpconfig;
96+
};
97+
if ( my $err = $@ || ( !$config ) ) {
98+
croak "Unable to parse the configuration file: $file: $@";
99+
}
100+
101+
# TODO handle mergeable entries
102+
return $config;
103+
}
104+
105+
1;
106+
107+
__END__
108+
109+
=head1 DESCRIPTION
110+
111+
This class provides the same features to read configuration files
112+
which was earlier done by C<Dancer2::Core::Role::Config>.
113+
114+
=head1 ATTRIBUTES
115+
116+
=attr name
117+
118+
The name of the class.
119+
120+
=attr location
121+
122+
Absolute path to the directory where the server started.
123+
124+
=attr config_location
125+
126+
Gets the location from the configuration. Same as C<< $object->location >>.
127+
128+
=attr environments_location
129+
130+
Gets the directory where the environment files are stored.
131+
132+
=attr environment
133+
134+
Returns the name of the environment.
135+
136+
=attr config_files
137+
138+
List of all the configuration files.
139+
140+
=head1 METHODS
141+
142+
=head2 read_config
143+
144+
Load the configuration files.

lib/Dancer2/Core/App.pm

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ our $EVAL_SHIM; $EVAL_SHIM ||= sub {
3939
# we have hooks here
4040
with qw<
4141
Dancer2::Core::Role::Hookable
42-
Dancer2::Core::Role::ConfigReader
42+
Dancer2::Core::Role::Config
4343
>;
4444

4545
sub supported_engines { [ qw<logger serializer session template> ] }

0 commit comments

Comments
 (0)