line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Mojolicious::Plugin::CascadingConfig; |
2
|
6
|
|
|
6
|
|
431770
|
use Mojo::Base 'Mojolicious::Plugin::Config'; |
|
6
|
|
|
|
|
18
|
|
|
6
|
|
|
|
|
37
|
|
3
|
|
|
|
|
|
|
|
4
|
|
|
|
|
|
|
our $VERSION = '0.01'; |
5
|
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
sub register { |
7
|
20
|
|
|
20
|
1
|
59309
|
my ($self, $app, $plugin_conf) = @_; |
8
|
|
|
|
|
|
|
die 'Modes must be a non-empty array reference if provided' |
9
|
20
|
100
|
100
|
|
|
179
|
if defined $plugin_conf->{modes} and (not ref $plugin_conf->{modes} eq 'ARRAY' or not @{$plugin_conf->{modes}}); |
|
|
|
100
|
|
|
|
|
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
# Override |
12
|
14
|
|
|
|
|
84
|
$app->defaults(config => $app->config); |
13
|
14
|
100
|
|
|
|
426
|
return $app->config if $app->config->{config_override}; |
14
|
|
|
|
|
|
|
|
15
|
12
|
100
|
|
|
|
100
|
my @modes = @{$plugin_conf->{modes} || ['production', 'development']}; |
|
12
|
|
|
|
|
67
|
|
16
|
12
|
|
|
|
|
39
|
my $moniker = $app->moniker; |
17
|
12
|
|
|
|
|
68
|
my $app_mode = $app->mode; |
18
|
12
|
|
|
|
|
59
|
my $config = {}; |
19
|
12
|
|
|
|
|
30
|
for my $mode (@modes) { |
20
|
21
|
|
|
|
|
58
|
$config = $self->_load_and_merge_mode_config($app, $mode, $moniker, $plugin_conf, $config, 1); |
21
|
|
|
|
|
|
|
|
22
|
18
|
100
|
|
|
|
73
|
if ($mode eq $app_mode) { |
23
|
7
|
|
|
|
|
30
|
return $app->config($config)->config; |
24
|
|
|
|
|
|
|
} |
25
|
|
|
|
|
|
|
} |
26
|
|
|
|
|
|
|
|
27
|
2
|
|
|
|
|
12
|
$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
|
|
67
|
my ($self, $app, $mode, $moniker, $plugin_conf, $config, $require) = @_; |
33
|
|
|
|
|
|
|
|
34
|
23
|
100
|
|
|
|
86
|
my $filename = $mode eq 'production' ? "$moniker.conf" : "$moniker.$mode.conf"; |
35
|
23
|
|
|
|
|
75
|
my $file = $app->home->child($filename); |
36
|
|
|
|
|
|
|
|
37
|
23
|
100
|
|
|
|
621
|
if (not -e $file) { |
38
|
4
|
100
|
|
|
|
102
|
if ($require) { |
39
|
3
|
|
|
|
|
11
|
die qq{Configuration file "$file" missing, maybe you need to create it?}; |
40
|
|
|
|
|
|
|
} else { |
41
|
1
|
|
|
|
|
5
|
return $config; |
42
|
|
|
|
|
|
|
} |
43
|
|
|
|
|
|
|
} |
44
|
|
|
|
|
|
|
|
45
|
19
|
|
|
|
|
548
|
my $mode_config = $self->load($file, $plugin_conf, $app); |
46
|
19
|
|
|
|
|
8761
|
return {%$config, %$mode_config}; |
47
|
|
|
|
|
|
|
} |
48
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
1; |
50
|
|
|
|
|
|
|
__END__ |