line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Config::JFDI::Source::Loader; |
2
|
|
|
|
|
|
|
|
3
|
15
|
|
|
15
|
|
75162
|
use Any::Moose; |
|
15
|
|
|
|
|
33459
|
|
|
15
|
|
|
|
|
93
|
|
4
|
|
|
|
|
|
|
|
5
|
15
|
|
|
15
|
|
21618
|
use Config::Any; |
|
15
|
|
|
|
|
167639
|
|
|
15
|
|
|
|
|
523
|
|
6
|
15
|
|
|
15
|
|
160
|
use Carp; |
|
15
|
|
|
|
|
30
|
|
|
15
|
|
|
|
|
1150
|
|
7
|
15
|
|
|
15
|
|
14780
|
use List::MoreUtils qw/ any /; |
|
15
|
|
|
|
|
19028
|
|
|
15
|
|
|
|
|
22526
|
|
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
has name => qw/ is ro required 0 isa Str|ScalarRef /; |
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
has path => qw/ is ro default . /; |
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
has driver => qw/ is ro lazy_build 1 /; |
14
|
|
|
|
|
|
|
sub _build_driver { |
15
|
0
|
|
|
0
|
|
0
|
return {}; |
16
|
|
|
|
|
|
|
} |
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
has local_suffix => qw/ is ro required 1 lazy 1 default local /; |
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
has no_env => qw/ is ro required 1 /, default => 0; |
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
has no_local => qw/ is ro required 1 /, default => 0; |
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
has env_lookup => qw/ is ro /, default => sub { [] }; |
25
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
has path_is_file => qw/ is ro default 0 /; |
27
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
has _found => qw/ is rw isa ArrayRef /; |
29
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
sub _env (@) { |
31
|
0
|
|
|
0
|
|
0
|
my $key = uc join "_", @_; |
32
|
0
|
|
|
|
|
0
|
$key =~ s/::/_/g; |
33
|
0
|
|
|
|
|
0
|
$key =~ s/\W/_/g; |
34
|
0
|
|
|
|
|
0
|
return $ENV{$key}; |
35
|
|
|
|
|
|
|
} |
36
|
|
|
|
|
|
|
|
37
|
|
|
|
|
|
|
sub BUILD { |
38
|
0
|
|
|
0
|
1
|
0
|
my $self = shift; |
39
|
0
|
|
|
|
|
0
|
my $given = shift; |
40
|
|
|
|
|
|
|
|
41
|
0
|
0
|
|
|
|
0
|
if (defined( my $name = $self->name )) { |
42
|
0
|
0
|
|
|
|
0
|
if (ref $name eq "SCALAR") { |
43
|
0
|
|
|
|
|
0
|
$name = $$name; |
44
|
|
|
|
|
|
|
} |
45
|
|
|
|
|
|
|
else { |
46
|
0
|
|
|
|
|
0
|
$name =~ s/::/_/g; |
47
|
0
|
|
|
|
|
0
|
$name = lc $name; |
48
|
|
|
|
|
|
|
} |
49
|
0
|
|
|
|
|
0
|
$self->{name} = $name; |
50
|
|
|
|
|
|
|
} |
51
|
|
|
|
|
|
|
|
52
|
0
|
0
|
|
|
|
0
|
if (defined $self->env_lookup) { |
53
|
0
|
0
|
|
|
|
0
|
$self->{env_lookup} = [ $self->env_lookup ] unless ref $self->env_lookup eq "ARRAY"; |
54
|
|
|
|
|
|
|
} |
55
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
} |
57
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
sub read { |
59
|
0
|
|
|
0
|
0
|
0
|
my $self = shift; |
60
|
|
|
|
|
|
|
|
61
|
0
|
|
|
|
|
0
|
my @files = $self->_find_files; |
62
|
0
|
|
|
|
|
0
|
my $cfg_files = $self->_load_files(\@files); |
63
|
0
|
|
|
|
|
0
|
my %cfg_files = map { (%$_)[0] => $_ } reverse @$cfg_files; |
|
0
|
|
|
|
|
0
|
|
64
|
0
|
|
|
|
|
0
|
$self->_found( [ map { (%$_)[0] } @$cfg_files ] ); |
|
0
|
|
|
|
|
0
|
|
65
|
|
|
|
|
|
|
|
66
|
0
|
|
|
|
|
0
|
my (@cfg, @local_cfg); |
67
|
|
|
|
|
|
|
{ |
68
|
|
|
|
|
|
|
# Anything that is local takes precedence |
69
|
0
|
|
|
|
|
0
|
my $local_suffix = $self->_get_local_suffix; |
|
0
|
|
|
|
|
0
|
|
70
|
0
|
|
|
|
|
0
|
for (sort keys %cfg_files) { |
71
|
|
|
|
|
|
|
|
72
|
0
|
|
|
|
|
0
|
my $cfg = $cfg_files{$_}; |
73
|
|
|
|
|
|
|
|
74
|
0
|
0
|
|
|
|
0
|
if (m{$local_suffix\.}ms) { |
75
|
0
|
|
|
|
|
0
|
push @local_cfg, $cfg; |
76
|
|
|
|
|
|
|
} |
77
|
|
|
|
|
|
|
else { |
78
|
0
|
|
|
|
|
0
|
push @cfg, $cfg; |
79
|
|
|
|
|
|
|
} |
80
|
|
|
|
|
|
|
} |
81
|
|
|
|
|
|
|
} |
82
|
|
|
|
|
|
|
|
83
|
0
|
0
|
|
|
|
0
|
return $self->no_local ? @cfg : (@cfg, @local_cfg); |
84
|
|
|
|
|
|
|
} |
85
|
|
|
|
|
|
|
|
86
|
|
|
|
|
|
|
sub found { |
87
|
|
|
|
|
|
|
my $self = shift; |
88
|
|
|
|
|
|
|
die if @_; |
89
|
|
|
|
|
|
|
return @{ $self->_found }; |
90
|
|
|
|
|
|
|
} |
91
|
|
|
|
|
|
|
|
92
|
|
|
|
|
|
|
around found => sub { |
93
|
|
|
|
|
|
|
my $inner = shift; |
94
|
|
|
|
|
|
|
my $self = shift; |
95
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
$self->read unless $self->{_found}; |
97
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
return $inner->( $self, @_ ); |
99
|
|
|
|
|
|
|
}; |
100
|
|
|
|
|
|
|
|
101
|
|
|
|
|
|
|
sub _load_files { |
102
|
0
|
|
|
0
|
|
0
|
my $self = shift; |
103
|
0
|
|
|
|
|
0
|
my $files = shift; |
104
|
0
|
|
|
|
|
0
|
return Config::Any->load_files({ |
105
|
|
|
|
|
|
|
files => $files, |
106
|
|
|
|
|
|
|
use_ext => 1, |
107
|
|
|
|
|
|
|
driver_args => $self->driver, |
108
|
|
|
|
|
|
|
}); |
109
|
|
|
|
|
|
|
} |
110
|
|
|
|
|
|
|
|
111
|
|
|
|
|
|
|
sub _find_files { # Doesn't really find files...hurm... |
112
|
0
|
|
|
0
|
|
0
|
my $self = shift; |
113
|
|
|
|
|
|
|
|
114
|
0
|
0
|
|
|
|
0
|
if ($self->path_is_file) { |
115
|
0
|
|
|
|
|
0
|
my $path; |
116
|
0
|
0
|
|
|
|
0
|
$path = $self->_env_lookup('CONFIG') unless $self->no_env; |
117
|
0
|
|
0
|
|
|
0
|
$path ||= $self->path; |
118
|
0
|
|
|
|
|
0
|
return ($path); |
119
|
|
|
|
|
|
|
} |
120
|
|
|
|
|
|
|
else { |
121
|
0
|
|
|
|
|
0
|
my ($path, $extension) = $self->_get_path; |
122
|
0
|
|
|
|
|
0
|
my $local_suffix = $self->_get_local_suffix; |
123
|
0
|
|
|
|
|
0
|
my @extensions = $self->_get_extensions; |
124
|
0
|
|
|
|
|
0
|
my $no_local = $self->no_local; |
125
|
|
|
|
|
|
|
|
126
|
0
|
|
|
|
|
0
|
my @files; |
127
|
0
|
0
|
|
|
|
0
|
if ($extension) { |
128
|
0
|
0
|
|
0
|
|
0
|
croak "Can't handle file extension $extension" unless any { $_ eq $extension } @extensions; |
|
0
|
|
|
|
|
0
|
|
129
|
0
|
|
|
|
|
0
|
push @files, $path; |
130
|
0
|
0
|
|
|
|
0
|
unless ($no_local) { |
131
|
0
|
|
|
|
|
0
|
(my $local_path = $path) =~ s{\.$extension$}{_$local_suffix.$extension}; |
132
|
0
|
|
|
|
|
0
|
push @files, $local_path; |
133
|
|
|
|
|
|
|
} |
134
|
|
|
|
|
|
|
} |
135
|
|
|
|
|
|
|
else { |
136
|
0
|
|
|
|
|
0
|
push @files, map { "$path.$_" } @extensions; |
|
0
|
|
|
|
|
0
|
|
137
|
0
|
0
|
|
|
|
0
|
push @files, map { "${path}_${local_suffix}.$_" } @extensions unless $no_local; |
|
0
|
|
|
|
|
0
|
|
138
|
|
|
|
|
|
|
} |
139
|
|
|
|
|
|
|
|
140
|
0
|
|
|
|
|
0
|
return @files; |
141
|
|
|
|
|
|
|
} |
142
|
|
|
|
|
|
|
} |
143
|
|
|
|
|
|
|
|
144
|
|
|
|
|
|
|
sub _env_lookup { |
145
|
0
|
|
|
0
|
|
0
|
my $self = shift; |
146
|
0
|
|
|
|
|
0
|
my @suffix = @_; |
147
|
|
|
|
|
|
|
|
148
|
0
|
|
|
|
|
0
|
my $name = $self->name; |
149
|
0
|
|
|
|
|
0
|
my $env_lookup = $self->env_lookup; |
150
|
0
|
|
|
|
|
0
|
my @lookup; |
151
|
0
|
0
|
|
|
|
0
|
push @lookup, $name if $name; |
152
|
0
|
|
|
|
|
0
|
push @lookup, @$env_lookup; |
153
|
|
|
|
|
|
|
|
154
|
0
|
|
|
|
|
0
|
for my $prefix (@lookup) { |
155
|
0
|
|
|
|
|
0
|
my $value = _env($prefix, @suffix); |
156
|
0
|
0
|
|
|
|
0
|
return $value if defined $value; |
157
|
|
|
|
|
|
|
} |
158
|
|
|
|
|
|
|
|
159
|
0
|
|
|
|
|
0
|
return; |
160
|
|
|
|
|
|
|
} |
161
|
|
|
|
|
|
|
|
162
|
|
|
|
|
|
|
sub _get_local_suffix { |
163
|
0
|
|
|
0
|
|
0
|
my $self = shift; |
164
|
|
|
|
|
|
|
|
165
|
0
|
|
|
|
|
0
|
my $name = $self->name; |
166
|
0
|
|
|
|
|
0
|
my $suffix; |
167
|
0
|
0
|
|
|
|
0
|
$suffix = $self->_env_lookup('CONFIG_LOCAL_SUFFIX') unless $self->no_env; |
168
|
|
|
|
|
|
|
# $suffix = _env($self->name, 'CONFIG_LOCAL_SUFFIX') if $name && ! $self->no_env; |
169
|
0
|
|
0
|
|
|
0
|
$suffix ||= $self->local_suffix; |
170
|
|
|
|
|
|
|
|
171
|
0
|
|
|
|
|
0
|
return $suffix; |
172
|
|
|
|
|
|
|
} |
173
|
|
|
|
|
|
|
|
174
|
|
|
|
|
|
|
sub _get_extensions { |
175
|
0
|
|
|
0
|
|
0
|
return @{ Config::Any->extensions } |
|
0
|
|
|
|
|
0
|
|
176
|
|
|
|
|
|
|
} |
177
|
|
|
|
|
|
|
|
178
|
|
|
|
|
|
|
sub file_extension ($) { |
179
|
6
|
|
|
6
|
0
|
20
|
my $path = shift; |
180
|
6
|
100
|
|
|
|
117
|
return if -d $path; |
181
|
3
|
|
|
|
|
17
|
my ($extension) = $path =~ m{\.([^/\.]{1,4})$}; |
182
|
3
|
|
|
|
|
16
|
return $extension; |
183
|
|
|
|
|
|
|
} |
184
|
|
|
|
|
|
|
|
185
|
|
|
|
|
|
|
sub _get_path { |
186
|
0
|
|
|
0
|
|
|
my $self = shift; |
187
|
|
|
|
|
|
|
|
188
|
0
|
|
|
|
|
|
my $name = $self->name; |
189
|
0
|
|
|
|
|
|
my $path; |
190
|
|
|
|
|
|
|
# $path = _env($name, 'CONFIG') if $name && ! $self->no_env; |
191
|
0
|
0
|
|
|
|
|
$path = $self->_env_lookup('CONFIG') unless $self->no_env; |
192
|
0
|
|
0
|
|
|
|
$path ||= $self->path; |
193
|
|
|
|
|
|
|
|
194
|
0
|
|
|
|
|
|
my $extension = file_extension $path; |
195
|
|
|
|
|
|
|
|
196
|
0
|
0
|
|
|
|
|
if (-d $path) { |
197
|
0
|
|
|
|
|
|
$path =~ s{[\/\\]$}{}; # Remove any trailing slash, e.g. apple/ or apple\ => apple |
198
|
0
|
|
|
|
|
|
$path .= "/$name"; # Look for a file in path with $self->name, e.g. apple => apple/name |
199
|
|
|
|
|
|
|
} |
200
|
|
|
|
|
|
|
|
201
|
0
|
|
|
|
|
|
return ($path, $extension); |
202
|
|
|
|
|
|
|
} |
203
|
|
|
|
|
|
|
|
204
|
|
|
|
|
|
|
|
205
|
|
|
|
|
|
|
1; |