81 lines
2.0 KiB
Perl
81 lines
2.0 KiB
Perl
package Mojolicious::Plugin::GitConfig;
|
|
|
|
# ABSTRACT: Mojolicious Plugin for using Config::GitLike as the main configuration provider
|
|
use Mojo::Base 'Mojolicious::Plugin';
|
|
use Mojolicious::Plugin::GitConfig::Config;
|
|
use Config::GitLike;
|
|
use Try::Tiny;
|
|
|
|
=head1 DESCRIPTION
|
|
|
|
This modules uses the Config::GitLike Module to implement the Mojolicious App configuration.
|
|
|
|
# uses the default git repository configuration files
|
|
$self->plugin('GitConfig' => {git=>1});
|
|
|
|
# uses a given configuration file
|
|
$self->plugin('GitConfig' => {file=>"myconfig.conf"});
|
|
|
|
# uses the default Mojolicious configuration files
|
|
$self->plugin('GitConfig');
|
|
|
|
$self->gitconfig() returns the Config::GitLike class
|
|
|
|
=cut
|
|
|
|
=attr configdata
|
|
|
|
attribute holding the configuration data after loading the configuration file/files
|
|
|
|
=cut
|
|
has 'configdata' => undef;
|
|
|
|
|
|
=method register
|
|
|
|
method called by Mojolicous while loading this plugin
|
|
|
|
@param #1 - the class itself
|
|
@param #2 - the mojolicious app context
|
|
@param #3 - the configuration provided by loading the plugin
|
|
|
|
=cut
|
|
sub register {
|
|
my $self = shift;
|
|
my $app = shift;
|
|
my $conf = shift;
|
|
|
|
# select config file
|
|
my $file = $conf->{file} || $ENV{MOJO_CONFIG} || "config";
|
|
|
|
# if we use the git configuration files we have to do something a bit different
|
|
if ($conf->{git})
|
|
{
|
|
$self->configdata(Mojolicious::Plugin::GitConfig::Config->new(confname=>"config",compatible => 1, cascade => 1));
|
|
$self->configdata()->load();
|
|
$app->log->info("git configuration files loaded");
|
|
}
|
|
else
|
|
{
|
|
try {
|
|
$self->configdata(Config::GitLike->load_file($file));
|
|
} catch {
|
|
$app->log->fatal("could not load configuration file " . $file);
|
|
die("could not load configuration file " . $file);
|
|
};
|
|
}
|
|
|
|
$app->log->debug(__PACKAGE__ . ": register helper gitconfig");
|
|
|
|
$app->helper(gitconfig => sub {
|
|
shift;
|
|
my $params = shift;
|
|
$self->configdata();
|
|
}
|
|
);
|
|
|
|
|
|
};
|
|
|
|
1;
|