line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Catalyst::Plugin::ConfigLoader::Etc; |
2
|
1
|
|
|
1
|
|
23104
|
use strict; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
36
|
|
3
|
1
|
|
|
1
|
|
5
|
use warnings; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
30
|
|
4
|
1
|
|
|
1
|
|
5
|
use base qw/Catalyst::Plugin::ConfigLoader/; |
|
1
|
|
|
|
|
5
|
|
|
1
|
|
|
|
|
645
|
|
5
|
1
|
|
|
1
|
|
934
|
use Config::Any; |
|
1
|
|
|
|
|
19203
|
|
|
1
|
|
|
|
|
34
|
|
6
|
1
|
|
|
1
|
|
647
|
use Catalyst::Utils; |
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
use DirHandle; |
8
|
|
|
|
|
|
|
use NEXT; |
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
sub find_files { |
56
|
|
|
|
|
|
|
my $c = shift; |
57
|
|
|
|
|
|
|
my ( $path, $extension ) = $c->get_config_path; |
58
|
|
|
|
|
|
|
my $suffix = $c->get_config_local_suffix; |
59
|
|
|
|
|
|
|
my @extensions = @{ Config::Any->extensions }; |
60
|
|
|
|
|
|
|
|
61
|
|
|
|
|
|
|
my $prefix = Catalyst::Utils::appprefix( ref $c || $c ); |
62
|
|
|
|
|
|
|
my $config_key_name = uc($prefix) . '_CONFIG_ETC'; |
63
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
my @files; |
65
|
|
|
|
|
|
|
if ( $extension ) { |
66
|
|
|
|
|
|
|
die "Unable to handle files with the extension '${extension}'" |
67
|
|
|
|
|
|
|
unless grep { $_ eq $extension } @extensions; |
68
|
|
|
|
|
|
|
( my $local = $path ) =~ s{\.$extension}{_$suffix.$extension}; |
69
|
|
|
|
|
|
|
push @files, $path, $local; |
70
|
|
|
|
|
|
|
} |
71
|
|
|
|
|
|
|
else { |
72
|
|
|
|
|
|
|
# do not append lcoal suffix |
73
|
|
|
|
|
|
|
if ($ENV{ $config_key_name } ) { |
74
|
|
|
|
|
|
|
@files = map { ( "$path.$_" ) } @extensions; |
75
|
|
|
|
|
|
|
} |
76
|
|
|
|
|
|
|
else { |
77
|
|
|
|
|
|
|
@files = map { ( "$path.$_", "${path}_${suffix}.$_" ) } @extensions; |
78
|
|
|
|
|
|
|
} |
79
|
|
|
|
|
|
|
} |
80
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
my @etc_files = $c->_find_etc_files( $config_key_name ); |
82
|
|
|
|
|
|
|
|
83
|
|
|
|
|
|
|
return @etc_files, @files; |
84
|
|
|
|
|
|
|
} |
85
|
|
|
|
|
|
|
|
86
|
|
|
|
|
|
|
=head2 _find_etc_files |
87
|
|
|
|
|
|
|
|
88
|
|
|
|
|
|
|
Loading custom config files |
89
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
=cut |
91
|
|
|
|
|
|
|
|
92
|
|
|
|
|
|
|
sub _find_etc_files { |
93
|
|
|
|
|
|
|
my $c = shift; |
94
|
|
|
|
|
|
|
my $cfg_key = shift; |
95
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
my @etc_files; |
97
|
|
|
|
|
|
|
push @etc_files , $ENV{$cfg_key} if $ENV{$cfg_key}; |
98
|
|
|
|
|
|
|
|
99
|
|
|
|
|
|
|
my $config = $c->config->{'Plugin::ConfigLoader::Etc'}; |
100
|
|
|
|
|
|
|
|
101
|
|
|
|
|
|
|
return @etc_files unless ref $config eq 'HASH' and |
102
|
|
|
|
|
|
|
ref $config->{files} eq 'ARRAY'; |
103
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
push @etc_files, $_ for @{ $config->{files} }; |
105
|
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
1; #End of Plugin::ConfigLoader::Etc |