1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264
|
package Catmandu::Env;
use Catmandu::Sane;
our $VERSION = '1.2024';
use Catmandu::Util qw(require_package use_lib read_yaml read_json :is :check);
use Catmandu::Fix;
use Config::Onion;
use File::Spec;
use Moo;
require Catmandu;
use namespace::clean;
with 'Catmandu::Logger';
sub _search_up {
my $dir = $_[0];
my @dirs = grep length, File::Spec->splitdir(Catmandu->default_load_path);
for (; @dirs; pop @dirs) {
my $path = File::Spec->catdir(File::Spec->rootdir, @dirs);
opendir my $dh, $path or last;
return $path
if grep {-r File::Spec->catfile($path, $_)}
grep /^catmandu.+(?:yaml|yml|json|pl)$/, readdir $dh;
}
Catmandu->default_load_path;
}
has load_paths => (
is => 'ro',
default => sub {[]},
coerce => sub {
[
map {File::Spec->canonpath($_)}
map {$_ eq ':up' ? _search_up($_) : $_} split /,/,
join ',',
ref $_[0] ? @{$_[0]} : $_[0]
];
},
);
has config => (is => 'rwp', default => sub {+{}});
has stores => (is => 'ro', default => sub {+{}});
has validators => (is => 'ro', default => sub {+{}});
has fixers => (is => 'ro', default => sub {+{}});
has default_store => (is => 'ro', default => sub {'default'});
has default_importer => (is => 'ro', default => sub {'default'});
has default_exporter => (is => 'ro', default => sub {'default'});
has default_validator => (is => 'ro', default => sub {'default'});
has default_fixer => (is => 'ro', default => sub {'default'});
has default_store_package => (is => 'ro');
has default_importer_package => (is => 'ro', default => sub {'JSON'});
has default_exporter_package => (is => 'ro', default => sub {'JSON'});
has default_validator_package => (is => 'ro');
has store_namespace => (is => 'ro', default => sub {'Catmandu::Store'});
has importer_namespace => (is => 'ro', default => sub {'Catmandu::Importer'});
has exporter_namespace => (is => 'ro', default => sub {'Catmandu::Exporter'});
has validator_namespace =>
(is => 'ro', default => sub {'Catmandu::Validator'});
sub BUILD {
my ($self) = @_;
my @config_dirs = @{$self->load_paths};
my @lib_dirs;
for my $dir (@config_dirs) {
if (!-d $dir) {
Catmandu::Error->throw("load path $dir doesn't exist");
}
my $lib_dir = File::Spec->catdir($dir, 'lib');
if (-d $lib_dir && -r $lib_dir) {
push @lib_dirs, $lib_dir;
}
}
if (@config_dirs) {
my @globs = map {
my $dir = $_;
map {File::Spec->catfile($dir, "catmandu*.$_")}
qw(yaml yml json pl)
} reverse @config_dirs;
my $config = Config::Onion->new(prefix_key => '_prefix');
$config->load_glob(@globs);
if ($self->log->is_debug) {
use Data::Dumper;
$self->log->debug(Dumper($config->get));
}
$self->_set_config($config->get);
}
if (@lib_dirs) {
lib->import(@lib_dirs);
}
}
sub load_path {
$_[0]->load_paths->[0];
}
sub roots {
goto &load_paths;
}
sub root {
goto &load_path;
}
sub fixer {
my $self = shift;
# it's already a fixer
if (is_instance($_[0], 'Catmandu::Fix')) {
return $_[0];
}
# an array ref of fixes
if (is_array_ref($_[0])) {
return Catmandu::Fix->new(fixes => $_[0]);
}
# a single fix
if (is_able($_[0], 'fix')) {
return Catmandu::Fix->new(fixes => [$_[0]]);
}
# try to load from config
my $key = $_[0] || $self->default_fixer;
my $fixers = $self->fixers;
$fixers->{$key} || do {
if (my $fixes = $self->config->{fixer}{$key}) {
return $fixers->{$key} = Catmandu::Fix->new(fixes => $fixes);
}
return Catmandu::Fix->new(fixes => [@_]);
};
}
sub store {
my $self = shift;
$self->_named_package('store', $self->store_namespace,
$self->default_store, $self->default_store_package,
$self->stores, @_);
}
sub importer {
my $self = shift;
$self->_named_package('importer', $self->importer_namespace,
$self->default_importer, $self->default_importer_package,
undef, @_);
}
sub exporter {
my $self = shift;
$self->_named_package('exporter', $self->exporter_namespace,
$self->default_exporter, $self->default_exporter_package,
undef, @_);
}
sub validator {
my $self = shift;
$self->_named_package(
'validator', $self->validator_namespace,
$self->default_validator, $self->default_validator_package,
$self->validators, @_
);
}
sub _named_package {
my $self = shift;
my $type = shift;
my $ns = shift;
my $default_name = shift;
my $default_package = shift;
my $cache = shift;
my $name = shift;
my $key = $name || $default_name;
return $name if is_instance($name) && index(ref($name), $ns) == 0;
# return cached instance if no arguments are given
if ($cache && !@_ and my $instance = $cache->{$key}) {
return $instance;
}
if (exists $self->config->{$type}) {
if (my $c = $self->config->{$type}{$key}) {
check_hash_ref($c);
check_string(my $package = $c->{package} || $default_package);
my $opts = check_hash_ref($c->{options} || {});
if (@_ > 1) {
$opts = {%$opts, @_};
}
elsif (@_ == 1) {
$opts = {%$opts, %{$_[0]}};
}
my $instance = require_package($package, $ns)->new($opts);
# cache this instance if no arguments are given
if ($cache && !@_) {
$cache->{$key} = $instance;
}
return $instance;
}
}
check_string(my $package = $name || $default_package);
require_package($package, $ns)->new(@_);
}
1;
__END__
=pod
=head1 NAME
Catmandu::Env - A catmandu configuration file loader
=head1 SYNOPSIS
use Catmandu::Env;
my $env = Catmandu::Env->new(load_paths => [ '/etc/catmandu '] );
my $env = Catmandu::Env->new(load_paths => [ ':up'] );
my $store = $env->store('mongodb');
my $importer = $env->importer('loc');
my $exporter = $env->exporter('europeana');
my $fixer = $env->fixer('my_fixes');
my $conf = $env->config;
=head1 DESCRIPTION
This class loads the catmandu.*.pl, catmandu.*.json, catmandu.*.yml and catmandu.*.yaml file from
all provided load_paths. Programmers are advised I<not> to use this class directly
but use the equivalent functionality provided in the Catmandu package:
Catmandu->load('/etc/catmandu');
Catmandu->load(':up');
my $store = Catmandu->store('mongodb');
my $importer = Catmandu->importer('loc');
my $exporter = Catmandu->exporter('europeana');
my $fixer = Catmandu->fixer('my_fixes');
my $conf = Catmandu->config;
=head1 SEE ALSO
L<Catmandu>
=cut
|