| 1 | package Catalyst::Plugin::ConfigLoader::Etc; | ||||||||||||||||||||||||||||
| 2 | 3 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | 3 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 3 | ▶ | use base qw/Catalyst::Plugin::ConfigLoader/; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 3 | ▶ | use Config::Any; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 3 | ▶ | use Catalyst::Utils; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 3 | ▶ | use DirHandle; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 3 | ▶ | use NEXT; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | 3 | our $VERSION = 0.02; | |||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | =head1 NAME | ||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | Catalyst::Plugin::ConfigLoader::Etc - Load etc config files | ||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | package MyApp; | ||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | use Catalyst qw/ConfigLoader::Etc/; | ||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | __PACKAGE__->config( | ||||||||||||||||||||||||||||
| 23 | 'Plugin::ConfigLoader::Etc' => { | ||||||||||||||||||||||||||||
| 24 | files => [ | ||||||||||||||||||||||||||||
| 25 | "$FindBin::Bin/etc/conf/test1.yml", | ||||||||||||||||||||||||||||
| 26 | ] | ||||||||||||||||||||||||||||
| 27 | } | ||||||||||||||||||||||||||||
| 28 | ); | ||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | In the file, assuming it's in YAML format: | ||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | foo: bar | ||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | Accessible through the context object, or the class itself | ||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | $c->config->{foo} # bar | ||||||||||||||||||||||||||||
| 37 | MyApp->config->{foo} # bar | ||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | config via env, for instance, $ENV{APPNAME_CONFIG_ETC} = "$FindBin::Bin/etc/conf/local.yml"; | ||||||||||||||||||||||||||||
| 42 | config('Plugin::ConfigLoader::Etc' => { files => [ '/path/to/config_file', '/path/to/xx_config' ] } | ||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | =head1 METHODS | ||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | =head2 find_files | ||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | This method determines the potential file paths to be used for config loading | ||||||||||||||||||||||||||||
| 50 | It returns an array of paths (up to the filename less the extension), | ||||||||||||||||||||||||||||
| 51 | It is then passed to L<Catalyst::Plugin::ConfigLoader> for actual configuration loading and processing. | ||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | =cut | ||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | 2 | ▶ | sub find_files { | ||||||||||||||||||||||||||
|
Subroutine
find_files: called CC=5 SCAR=17.5
Pod
documented
| |||||||||||||||||||||||||||||
| 56 | 2 | my $c = shift; | |||||||||||||||||||||||||||
| 57 | 2 | my ( $path, $extension ) = $c->get_config_path; | |||||||||||||||||||||||||||
| 58 | 2 | my $suffix = $c->get_config_local_suffix; | |||||||||||||||||||||||||||
| 59 | 2 | my @extensions = @{ Config::Any->extensions }; | |||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | 2 | ▶ | my $prefix = Catalyst::Utils::appprefix( ref $c || $c ); | ||||||||||||||||||||||||||
|
Condition33%1/3
ref $c || $c
MC/DC0%0/2
ref $c || $c
ref $c
$c
Truth table33%1/3
A || B
A= ref $cB= $c
| |||||||||||||||||||||||||||||
| 62 | 2 | my $config_key_name = uc($prefix) . '_CONFIG_ETC'; | |||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | 2 | my @files; | |||||||||||||||||||||||||||
| 65 | 2 | ▶ | if ( $extension ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 66 | die "Unable to handle files with the extension '${extension}'" | ||||||||||||||||||||||||||||
| 67 | 0 | ▶ | unless grep { $_ eq $extension } @extensions; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 68 | 0 | ( my $local = $path ) =~ s{\.$extension}{_$suffix.$extension}; | |||||||||||||||||||||||||||
| 69 | 0 | push @files, $path, $local; | |||||||||||||||||||||||||||
| 70 | } | ||||||||||||||||||||||||||||
| 71 | else { | ||||||||||||||||||||||||||||
| 72 | # do not append lcoal suffix | ||||||||||||||||||||||||||||
| 73 | 2 | ▶ | if ($ENV{ $config_key_name } ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 74 | 1 | @files = map { ( "$path.$_" ) } @extensions; | |||||||||||||||||||||||||||
| 75 | } | ||||||||||||||||||||||||||||
| 76 | else { | ||||||||||||||||||||||||||||
| 77 | 1 | @files = map { ( "$path.$_", "${path}_${suffix}.$_" ) } @extensions; | |||||||||||||||||||||||||||
| 78 | } | ||||||||||||||||||||||||||||
| 79 | } | ||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | 2 | my @etc_files = $c->_find_etc_files( $config_key_name ); | |||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | 2 | return @etc_files, @files; | |||||||||||||||||||||||||||
| 84 | } | ||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | =head2 _find_etc_files | ||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | Loading custom config files | ||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | =cut | ||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | 2 | ▶ | sub _find_etc_files { | ||||||||||||||||||||||||||
|
Subroutine
_find_etc_files: called CC=5 SCAR=16.4
| |||||||||||||||||||||||||||||
| 93 | 2 | my $c = shift; | |||||||||||||||||||||||||||
| 94 | 2 | my $cfg_key = shift; | |||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | 2 | my @etc_files; | |||||||||||||||||||||||||||
| 97 | 2 | ▶ | push @etc_files , $ENV{$cfg_key} if $ENV{$cfg_key}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | 2 | my $config = $c->config->{'Plugin::ConfigLoader::Etc'}; | |||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | return @etc_files unless ref $config eq 'HASH' and | ||||||||||||||||||||||||||||
| 102 | 2 | ▶ | ref $config->{files} eq 'ARRAY'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
ref $config eq "HASH" and ref $config->{"files"} eq "ARRAY"
MC/DC0%0/2
ref $config eq "HASH" and ref $config->{"files"} eq "ARRAY"
ref $config eq "HASH"
ref $config->{"files"} eq "ARRAY"
Truth table33%1/3
A and B
A= ref $config eq "HASH"B= ref $config->{"files"} eq "ARRAY"
| |||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | 2 | push @etc_files, $_ for @{ $config->{files} }; | |||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | 2 | return @etc_files; | |||||||||||||||||||||||||||
| 107 | } | ||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | zdk (Warachet Samtalee), C<< <zdk at cpan.org> >> | ||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | =head1 COPYRIGHT & LICENSE | ||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | Copyright 2012 zdk (Warachet Samtalee), all rights reserved. | ||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | This program is free software; you can redistribute it and/or modify it | ||||||||||||||||||||||||||||
| 118 | under the same terms as Perl itself. | ||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | If you'd like to use it under a different license, that's probably OK. | ||||||||||||||||||||||||||||
| 121 | Please contact the author. | ||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | =cut | ||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | 3 | 1; #End of Plugin::ConfigLoader::Etc | |||||||||||||||||||||||||||