Mojolicious-Plugin-MultiConfig
view release on metacpan or search on metacpan
lib/Mojolicious/Plugin/MultiConfig.pm view on Meta::CPAN
* %a.%m.%u.%e
The config files are loaded in order, with later files in the chain overwriting option(s) in the previous config file.
The filenames are configurable, see OPTIONS section below.
Missing config files are ignored without error. (TODO: config option to be added in the near future ensure an error).
=head1 OPTIONS
L<Mojolicious::Plugin::MultiConfig> supports some options. They can be passed as named args. e.g.:
$self->plugin('multi_config', option1 => 'value1', foo => 'bar');
=head2 moniker
moniker => 'appconfig'
Will use this value in place of %a (moniker) in filenames. Defaults to C<$app-E<gt>moniker> if not specified.
=head2 mode
mode => 'test'
Will use this value in place of %m (mode) in filenames. Defaults to C<$app-E<gt>mode> if not specified.
=head2 dir
dir => 'my_config_dir'
Will load config files from this directory. Defaults to C<$app-E<gt>home('conf')> if not specified.
=head2 files
files => ['file1.conf', 'file2.%u.conf']
Will load these config files instead of the default. You can use template codes as per DESCRIPTION section.
Defaults to C<['%a.%e', '%a.%m.%e', '%a.%m.%u.%e']> if not specified.
=head2 ext
ext => 'yml'
Will use this value in place of %e (file extension) in filenames. Defaults to C<conf> if not specified.
=head1 METHODS
=head2 register
Register as a plugin and load config files. Called automatically by C<$app-E<gt>plugin>
=cut
sub register
{
my $self = shift;
my $app = shift;
my $arg = shift;
my $config = {};
my $username = (getpwuid($<))[0]; # TODO: Not on Windows!
my @files;
# Default args if not set
$arg->{moniker} //= $app->moniker;
$arg->{mode} //= $app->mode;
$arg->{dir} //= catfile($app->home, 'conf');
$arg->{files} //= ['%a.%e', '%a.%m.%e', '%a.%m.%u.%e'];
$arg->{ext} //= 'conf';
for (my $i = 0; $i < @{$arg->{files}}; $i++) {
# Prefix dir
$arg->{files}->[$i] = catfile($arg->{dir}, $arg->{files}->[$i]);
# Search/replace for codes used in files
$arg->{files}->[$i] =~ s/\%a/$arg->{moniker}/g;
$arg->{files}->[$i] =~ s/\%m/$arg->{mode}/g;
$arg->{files}->[$i] =~ s/\%u/$username/g;
$arg->{files}->[$i] =~ s/\%e/$arg->{ext}/g;
if (-e $arg->{files}->[$i]) {
$app->log->debug(__PACKAGE__ . ': found ' . $arg->{files}->[$i]);
}
else {
$app->log->debug(__PACKAGE__ . ': cannot find ' . $arg->{files}->[$i]);
splice(@{$arg->{files}}, $i--, 1);
}
}
# Load the config file(s)
my $config_tmp = Config::Any->load_files({
files => $arg->{files},
use_ext => 1,
flatten_to_hash => 1,
driver_args => {
General => {-UTF8 => 1},
},
});
# Merge
$config = {%$config, %{$config_tmp->{$_}}} for (@{$arg->{files}});
$app->config($config);
$app->log->debug($app->dumper($config));
return $config;
}
=head1 AUTHOR
Ben Vinnerd, C<< <ben at vinnerd.com> >>
=head1 LICENCE AND COPYRIGHT
Copyright 2013 Ben Vinnerd.
This program is free software; you can redistribute it and/or modify it
under the terms of the the Artistic License (2.0). You may obtain a
copy of the full license at:
L<http://www.perlfoundation.org/artistic_license_2_0>
( run in 1.890 second using v1.01-cache-2.11-cpan-39bf76dae61 )