line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Mojolicious::Plugin::CascadingConfig; |
2
|
6
|
|
|
6
|
|
393101
|
use Mojo::Base 'Mojolicious::Plugin::Config'; |
|
6
|
|
|
|
|
15
|
|
|
6
|
|
|
|
|
30
|
|
3
|
|
|
|
|
|
|
|
4
|
|
|
|
|
|
|
our $VERSION = '0.02'; |
5
|
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
sub register { |
7
|
20
|
|
|
20
|
1
|
54473
|
my ($self, $app, $plugin_conf) = @_; |
8
|
|
|
|
|
|
|
die 'Modes must be a non-empty array reference if provided' |
9
|
20
|
100
|
100
|
|
|
147
|
if defined $plugin_conf->{modes} and (not ref $plugin_conf->{modes} eq 'ARRAY' or not @{$plugin_conf->{modes}}); |
|
|
|
100
|
|
|
|
|
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
# Override |
12
|
14
|
|
|
|
|
77
|
$app->defaults(config => $app->config); |
13
|
14
|
100
|
|
|
|
395
|
return $app->config if $app->config->{config_override}; |
14
|
|
|
|
|
|
|
|
15
|
12
|
100
|
|
|
|
93
|
my @modes = @{$plugin_conf->{modes} || ['production', 'development']}; |
|
12
|
|
|
|
|
62
|
|
16
|
12
|
|
|
|
|
34
|
my $moniker = $app->moniker; |
17
|
12
|
|
|
|
|
60
|
my $app_mode = $app->mode; |
18
|
12
|
|
|
|
|
50
|
my $config = {}; |
19
|
12
|
|
|
|
|
30
|
for my $mode (@modes) { |
20
|
21
|
|
|
|
|
60
|
$config = $self->_load_and_merge_mode_config($app, $mode, $moniker, $plugin_conf, $config, 1); |
21
|
|
|
|
|
|
|
|
22
|
18
|
100
|
|
|
|
67
|
if ($mode eq $app_mode) { |
23
|
7
|
|
|
|
|
27
|
return $app->config($config)->config; |
24
|
|
|
|
|
|
|
} |
25
|
|
|
|
|
|
|
} |
26
|
|
|
|
|
|
|
|
27
|
2
|
|
|
|
|
9
|
$config = $self->_load_and_merge_mode_config($app, $app->mode, $moniker, $plugin_conf, $config, undef); |
28
|
2
|
|
|
|
|
10
|
return $app->config($config)->config; |
29
|
|
|
|
|
|
|
} |
30
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
sub _load_and_merge_mode_config { |
32
|
23
|
|
|
23
|
|
59
|
my ($self, $app, $mode, $moniker, $plugin_conf, $config, $require) = @_; |
33
|
|
|
|
|
|
|
|
34
|
23
|
100
|
|
|
|
99
|
my $filename = $mode eq 'production' ? "$moniker.conf" : "$moniker.$mode.conf"; |
35
|
23
|
|
|
|
|
67
|
my $file = $app->home->child($filename); |
36
|
|
|
|
|
|
|
|
37
|
23
|
100
|
|
|
|
568
|
if (not -e $file) { |
38
|
4
|
100
|
|
|
|
96
|
if ($require) { |
39
|
3
|
|
|
|
|
10
|
die qq{Configuration file "$file" missing, maybe you need to create it?}; |
40
|
|
|
|
|
|
|
} else { |
41
|
1
|
|
|
|
|
5
|
return $config; |
42
|
|
|
|
|
|
|
} |
43
|
|
|
|
|
|
|
} |
44
|
|
|
|
|
|
|
|
45
|
19
|
|
|
|
|
469
|
my $mode_config = $self->load($file, $plugin_conf, $app); |
46
|
19
|
|
|
|
|
7987
|
return {%$config, %$mode_config}; |
47
|
|
|
|
|
|
|
} |
48
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
1; |
50
|
|
|
|
|
|
|
__END__ |