Debian Codegraph @

lib/Dancer2/ConfigReader/Config/Any.pm

raw
1 # ABSTRACT: Config reader for files
2 package Dancer2::ConfigReader::Config::Any;
3 $Dancer2::ConfigReader::Config::Any::VERSION = '2.2.2';
4 use Moo;
5
6 use Config::Any;
7 use Hash::Merge::Simple;
8 use Carp 'croak';
9 use Module::Runtime 'require_module';
10
11 use Dancer2::Core::Factory;
12 use Dancer2::Core;
13 use Dancer2::Core::Types;
14 use Path::Tiny qw< path >;
15
16 with 'Dancer2::Core::Role::ConfigReader';
17
18 has name => (
19 is => 'ro',
20 isa => Str,
21 lazy => 0,
22 default => sub {'Config::Any'},
23 );
24
25 has config_files => (
26 is => 'ro',
27 lazy => 1,
28 isa => ArrayRef,
29 builder => '_build_config_files',
30 );
31
32 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 sub read_config {
41 my ($self) = @_;
42
43 my $config = Hash::Merge::Simple->merge(
44 map {
45 warn "Merging config file $_\n" if $ENV{DANCER_CONFIG_VERBOSE};
46 $self->_load_config_file($_)
47 } @{ $self->config_files }
48 );
49
50 return $config;
51 }
52
53 sub _build_config_files {
54 my ($self) = @_;
55 return [ map $_->stringify, @{ $self->_config_files_path } ];
56 }
57
58 sub _build_config_files_path {
59 my ($self) = @_;
60
61 my $location = $self->config_location;
62 warn "Searching config files in location: $location\n" if $ENV{DANCER_CONFIG_VERBOSE};
63 # an undef location means no config files for the caller
64 return [] unless defined $location;
65
66 my $config_location = Path::Tiny::path($location);
67 my $environments_location = Path::Tiny::path( $self->environments_location );
68
69 my $running_env = $self->environment;
70 my @available_exts = Config::Any->extensions;
71 my @files;
72
73 my @exts = @available_exts;
74 if (my $ext = $ENV{DANCER_CONFIG_EXT}) {
75 if (grep { $ext eq $_ } @available_exts) {
76 @exts = $ext;
77 warn "Only looking for configs ending in '$ext'\n"
78 if $ENV{DANCER_CONFIG_VERBOSE};
79 } else {
80 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 foreach my $file ( [ $config_location, "config" ],
88 [ $environments_location, $running_env ] )
89 {
90 foreach my $ext (@exts) {
91 my $path = Path::Tiny::path( $file->[0], $file->[1] . ".$ext" );
92 next unless -r $path;
93
94 # Look for *_local.ext files
95 my $local = Path::Tiny::path( $file->[0], $file->[1] . "_local.$ext" );
96 push @files, $path, ( -r $local ? $local : () );
97 }
98 }
99
100 warn "Found following config files: @files\n" if $ENV{DANCER_CONFIG_VERBOSE};
101 return \@files;
102 }
103
104 sub _load_config_file {
105 my ( $self, $file ) = @_;
106 my $config;
107
108 eval {
109 my @files = ($file);
110 my $tmpconfig =
111 Config::Any->load_files( { files => \@files, use_ext => 1 } )->[0];
112 ( $file, $config ) = %{$tmpconfig} if defined $tmpconfig;
113 };
114 if ( my $err = $@ || ( !$config ) ) {
115 croak "Unable to parse the configuration file: $file: $@";
116 }
117
118 # TODO handle mergeable entries
119 return $config;
120 }
121
122 1;
123
124 __END__
125
126 =pod
127
128 =encoding UTF-8
129
130 =head1 NAME
131
132 Dancer2::ConfigReader::Config::Any - Config reader for files
133
134 =head1 VERSION
135
136 version 2.2.2
137
138 =head1 DESCRIPTION
139
140 This class is an implementation of C<Dancer2::Core::Role::ConfigReader>.
141 It reads the configuration files of C<Dancer2>.
142
143 Please see C<Dancer2::Config> for more information.
144
145 If you need to add additional functionality to the reading
146 mechanism, you can extend this class.
147 An example of this is providing the possibility to replace
148 random parts of the file config with environmental variables:
149
150 package Dancer2::ConfigReader::File::Extended;
151
152 use Moo;
153 use Dancer2::Core::Types;
154
155 use Carp 'croak';
156
157 extends 'Dancer2::ConfigReader::Config::Any';
158
159 has name => (
160 is => 'ro',
161 isa => Str,
162 lazy => 0,
163 default => sub {'File::Extended'},
164 );
165
166 around read_config => sub {
167 my ($orig, $self) = @_;
168 my $config = $orig->($self, @_);
169 $self->_replace_env_vars($config);
170 return $config;
171 };
172
173 # Attn. We are traversing along the original data structure all the time,
174 # using references, and changing values on the spot, not returning anything.
175 sub _replace_env_vars {
176 my ( $self, $entry ) = @_;
177 if( ref $entry ne 'HASH' && ref $entry ne 'ARRAY' ) {
178 croak 'Param entry is not HASH or ARRAY';
179 }
180 if( ref $entry eq 'HASH' ) {
181 foreach my $value (values %{ $entry }) {
182 if( (ref $value) =~ m/(HASH|ARRAY)/msx ) {
183 $self->_replace_env_vars( $value );
184 } elsif( (ref $value) =~ m/(CODE|REF|GLOB)/msx ) {
185 # Pretty much anything else except SCALAR. Do nothing
186 1;
187 } else {
188 if( $value ) {
189 while( my ($k, $v) = each %ENV) {
190 $value =~ s/ \$ [{] ENV:$k [}] /$v/gmsx;
191 }
192 }
193 }
194 }
195 } else {
196 # ref $entry is 'ARRAY'
197 foreach my $value (@{ $entry }) {
198 if( (ref $value) =~ m/(HASH|ARRAY)/msx ) {
199 $self->_replace_env_vars( $value );
200 } elsif( (ref $value) =~ m/(CODE|REF|GLOB)/msx ) {
201 # Pretty much anything else except SCALAR. Do nothing
202 1;
203 } else {
204 if( $value ) {
205 while( my ($k, $v) = each %ENV) {
206 $value =~ s/ \$ [{] ENV:$k [}] /$v/gmsx;
207 }
208 }
209 }
210 }
211 }
212 return;
213 }
214
215 1;
216
217 =head1 ATTRIBUTES
218
219 =head2 name
220
221 The name of the Config Reader class: C<Config::Any>.
222
223 =head2 location
224
225 Absolute path to the directory where the server started.
226
227 =head2 config_location
228
229 Gets the location from the configuration. Same as C<< $object->location >>.
230
231 =head2 environments_location
232
233 Gets the directory where the environment files are stored.
234
235 =head2 environment
236
237 Returns the name of the environment.
238
239 =head2 config_files
240
241 List of all the configuration files.
242
243 =head1 METHODS
244
245 =head2 read_config
246
247 Load the configuration files.
248
249 =head1 AUTHOR
250
251 Dancer Core Developers
252
253 =head1 COPYRIGHT AND LICENSE
254
255 This software is copyright (c) 2026 by Alexis Sukrieh.
256
257 This is free software; you can redistribute it and/or modify it under
258 the same terms as the Perl 5 programming language system itself.
259
260 =cut