| 1 | package Mojolicious::Plugin::Config; | ||||||||||||||||||||||||||||
| 2 | 4 | ▶ | use Mojo::Base 'Mojolicious::Plugin'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||
| 4 | 4 | ▶ | use Mojo::File qw(path); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 3 | ▶ | use Mojo::Util qw(decode); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 6 | ▶ | sub load { $_[0]->parse(decode('UTF-8', path($_[1])->slurp), @_[1, 2, 3]) } | ||||||||||||||||||||||||||
|
Subroutine
load: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 6 | ▶ | sub parse { | ||||||||||||||||||||||||||
|
Subroutine
parse: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 10 | 6 | my ($self, $content, $file, $conf, $app) = @_; | |||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | # Run Perl code in sandbox | ||||||||||||||||||||||||||||
| 13 | 3 | ▶ | my $config = eval 'package Mojolicious::Plugin::Config::Sandbox; no warnings;' | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | . "sub app; local *app = sub { \$app }; use Mojo::Base -strict; $content"; | ||||||||||||||||||||||||||||
| 15 | 6 | ▶ | die qq{Can't load configuration from file "$file": $@} if $@; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 16 | 6 | ▶ | die qq{Configuration file "$file" did not return a hash reference} unless ref $config eq 'HASH'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | 6 | return $config; | |||||||||||||||||||||||||||
| 19 | } | ||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | 10 | ▶ | sub register { | ||||||||||||||||||||||||||
|
Subroutine
register: called CC=16 SCAR=28.8
Pod
documented
| |||||||||||||||||||||||||||||
| 22 | 10 | my ($self, $app, $conf) = @_; | |||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | # Override | ||||||||||||||||||||||||||||
| 25 | 10 | ▶ | return _plugins($app, $app->config) if $app->config->{config_override}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | # Config file | ||||||||||||||||||||||||||||
| 28 | 7 | ▶ | my $file = $conf->{file} || $ENV{MOJO_CONFIG}; | ||||||||||||||||||||||||||
|
Condition33%1/3
$conf->{'file'} || $ENV{'MOJO_CONFIG'}
MC/DC0%0/2
$conf->{'file'} || $ENV{'MOJO_CONFIG'}
$conf->{'file'}
$ENV{'MOJO_CONFIG'}
Truth table33%1/3
A || B
A= $conf->{'file'}B= $ENV{'MOJO_CONFIG'}
| |||||||||||||||||||||||||||||
| 29 | 7 | ▶ | $file ||= $app->moniker . '.' . ($conf->{ext} || 'conf'); | ||||||||||||||||||||||||||
|
Condition50%2/4
$file ||= $app->moniker . '.' . ($conf->{"ext"} || "conf")
$conf->{'ext'} || 'conf'
MC/DC0%0/1
$file ||= $app->moniker . '.' . ($conf->{"ext"} || "conf")
$file
MC/DC0%0/1
$conf->{'ext'} || 'conf'
$conf->{'ext'}
Truth table50%1/2
A
A= $file
Truth table50%1/2
A
A= $conf->{'ext'}
| |||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | # Mode specific config file | ||||||||||||||||||||||||||||
| 32 | 7 | ▶ | my $mode = $file =~ /^(.*)\.([^.]+)$/ ? join('.', $1, $app->mode, $2) : ''; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | 7 | my $home = $app->home; | |||||||||||||||||||||||||||
| 35 | 7 | ▶ | $file = $home->child($file) unless path($file)->is_abs; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 36 | 7 | ▶ | $mode = $home->child($mode) if $mode && !path($mode)->is_abs; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$mode and not path($mode)->is_abs
MC/DC0%0/2
$mode and not path($mode)->is_abs
$mode
not path($mode)->is_abs
Truth table33%1/3
A and not(B)
A= $modeB= not path($mode)->is_abs
| |||||||||||||||||||||||||||||
| 37 | 7 | ▶ | $mode = undef unless $mode && -e $mode; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$mode and -e $mode
MC/DC50%1/2
$mode and -e $mode
$mode
-e $mode
Truth table66%2/3
A and B
A= $modeB= -e $mode
| |||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | # Read config file | ||||||||||||||||||||||||||||
| 40 | 7 | my $config = {}; | |||||||||||||||||||||||||||
| 41 | 7 | ▶ | if (-e $file) { $config = $self->load($file, $conf, $app) } | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | # Check for default and mode specific config file | ||||||||||||||||||||||||||||
| 44 | 1 | ▶ | elsif (!$conf->{default} && !$mode) { die qq{Configuration file "$file" missing, maybe you need to create it?\n} } | ||||||||||||||||||||||||||
|
Condition100%3/3
$conf->{'default'} || $mode
MC/DC100%2/2
$conf->{'default'} || $mode
$conf->{'default'}
$mode
Truth table100%3/3
A || B
A= $conf->{'default'}B= $mode
| |||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | # Merge everything | ||||||||||||||||||||||||||||
| 47 | 6 | ▶ | $config = {%$config, %{$self->load($mode, $conf, $app)}} if $mode; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 48 | 6 | ▶ | $config = {%{$conf->{default}}, %$config} if $conf->{default}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 49 | 6 | return _plugins($app, $app->config($config)->config); | |||||||||||||||||||||||||||
| 50 | } | ||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | 9 | ▶ | sub _plugins { | ||||||||||||||||||||||||||
|
Subroutine
_plugins: called CC=5 SCAR=16.1
| |||||||||||||||||||||||||||||
| 53 | 9 | my ($app, $config) = @_; | |||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | 9 | ▶ | if (my $plugins = $config->{plugins}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 56 | 3 | ▶ | die qq{Configuration value "plugins" is not an array reference} unless ref $plugins eq 'ARRAY'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 57 | 2 | for my $plugin (@$plugins) { | |||||||||||||||||||||||||||
| 58 | 2 | ▶ | die qq{Configuration value "plugins" contains an entry that is not a hash reference} unless ref $plugin eq 'HASH'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 59 | 1 | $app->plugin((keys %$plugin)[0], (values %$plugin)[0]); | |||||||||||||||||||||||||||
| 60 | } | ||||||||||||||||||||||||||||
| 61 | } | ||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | 7 | return $app->config; | |||||||||||||||||||||||||||
| 64 | } | ||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | 3 | 1; | |||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | =encoding utf8 | ||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | =head1 NAME | ||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | Mojolicious::Plugin::Config - Perl-ish configuration plugin | ||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | # myapp.conf (it's just Perl returning a hash) | ||||||||||||||||||||||||||||
| 77 | { | ||||||||||||||||||||||||||||
| 78 | # Just a value | ||||||||||||||||||||||||||||
| 79 | foo => "bar", | ||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | # Nested data structures are fine too | ||||||||||||||||||||||||||||
| 82 | baz => ['♥'], | ||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | # You have full access to the application | ||||||||||||||||||||||||||||
| 85 | music_dir => app->home->child('music') | ||||||||||||||||||||||||||||
| 86 | }; | ||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | # Mojolicious | ||||||||||||||||||||||||||||
| 89 | my $config = $app->plugin('Config'); | ||||||||||||||||||||||||||||
| 90 | say $config->{foo}; | ||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | # Mojolicious::Lite | ||||||||||||||||||||||||||||
| 93 | my $config = plugin 'Config'; | ||||||||||||||||||||||||||||
| 94 | say $config->{foo}; | ||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | # foo.html.ep | ||||||||||||||||||||||||||||
| 97 | %= config->{foo} | ||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | # The configuration is available application-wide | ||||||||||||||||||||||||||||
| 100 | my $config = app->config; | ||||||||||||||||||||||||||||
| 101 | say $config->{foo}; | ||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | # Everything can be customized with options | ||||||||||||||||||||||||||||
| 104 | my $config = plugin Config => {file => '/etc/myapp.stuff'}; | ||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | L<Mojolicious::Plugin::Config> is a Perl-ish configuration plugin. | ||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | The application object can be accessed via C<$app> or the C<app> function, L<strict>, L<warnings>, L<utf8> and Perl | ||||||||||||||||||||||||||||
| 111 | 5.16 L<features|feature> are automatically enabled. A default configuration filename in the application home directory | ||||||||||||||||||||||||||||
| 112 | will be generated from the value of L<Mojolicious/"moniker"> (C<$moniker.conf>). You can extend the normal | ||||||||||||||||||||||||||||
| 113 | configuration file C<$moniker.conf> with C<mode> specific ones like C<$moniker.$mode.conf>, which will be detected | ||||||||||||||||||||||||||||
| 114 | automatically. | ||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | These configuration values are currently reserved: | ||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | =over 2 | ||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | =item C<config_override> | ||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | If this configuration value has been set in L<Mojolicious/"config"> when this plugin is loaded, it will not do anything | ||||||||||||||||||||||||||||
| 123 | besides loading deployment specific plugins. | ||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||
| 125 | =item C<plugins> | ||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||
| 127 | plugins => [{SetUserGroup => {user => 'sri', group => 'staff'}}] | ||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||
| 129 | One or more deployment specific plugins that should be loaded right after this plugin has been loaded. | ||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||
| 131 | =back | ||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||
| 133 | The code of this plugin is a good example for learning to build new plugins, you're welcome to fork it. | ||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||
| 135 | See L<Mojolicious::Plugins/"PLUGINS"> for a list of plugins that are available by default. | ||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||
| 137 | =head1 OPTIONS | ||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||
| 139 | L<Mojolicious::Plugin::Config> supports the following options. | ||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||
| 141 | =head2 default | ||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||
| 143 | # Mojolicious::Lite | ||||||||||||||||||||||||||||
| 144 | plugin Config => {default => {foo => 'bar'}}; | ||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||
| 146 | Default configuration, making configuration files optional. | ||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||
| 148 | =head2 ext | ||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||
| 150 | # Mojolicious::Lite | ||||||||||||||||||||||||||||
| 151 | plugin Config => {ext => 'stuff'}; | ||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||
| 153 | File extension for generated configuration filenames, defaults to C<conf>. | ||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||
| 155 | =head2 file | ||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||
| 157 | # Mojolicious::Lite | ||||||||||||||||||||||||||||
| 158 | plugin Config => {file => 'myapp.conf'}; | ||||||||||||||||||||||||||||
| 159 | plugin Config => {file => '/etc/foo.stuff'}; | ||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||
| 161 | Path to configuration file, absolute or relative to the application home directory, defaults to the value of the | ||||||||||||||||||||||||||||
| 162 | C<MOJO_CONFIG> environment variable or C<$moniker.conf> in the application home directory. | ||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||
| 164 | =head1 METHODS | ||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||
| 166 | L<Mojolicious::Plugin::Config> inherits all methods from L<Mojolicious::Plugin> and implements the following new ones. | ||||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||
| 168 | =head2 load | ||||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||
| 170 | $plugin->load($file, $conf, $app); | ||||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||
| 172 | Loads configuration file and passes the content to L</"parse">. | ||||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||
| 174 | sub load ($self, $file, $conf, $app) { | ||||||||||||||||||||||||||||
| 175 | ... | ||||||||||||||||||||||||||||
| 176 | return $self->parse($content, $file, $conf, $app); | ||||||||||||||||||||||||||||
| 177 | } | ||||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||
| 179 | =head2 parse | ||||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||
| 181 | $plugin->parse($content, $file, $conf, $app); | ||||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||
| 183 | Parse configuration file. | ||||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||
| 185 | sub parse ($self, $content, $file, $conf, $app) { | ||||||||||||||||||||||||||||
| 186 | ... | ||||||||||||||||||||||||||||
| 187 | return $hash; | ||||||||||||||||||||||||||||
| 188 | } | ||||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||
| 190 | =head2 register | ||||||||||||||||||||||||||||
| 191 | |||||||||||||||||||||||||||||
| 192 | my $config = $plugin->register(Mojolicious->new); | ||||||||||||||||||||||||||||
| 193 | my $config = $plugin->register(Mojolicious->new, {file => '/etc/app.conf'}); | ||||||||||||||||||||||||||||
| 194 | |||||||||||||||||||||||||||||
| 195 | Register plugin in L<Mojolicious> application and merge configuration. | ||||||||||||||||||||||||||||
| 196 | |||||||||||||||||||||||||||||
| 197 | =head1 SEE ALSO | ||||||||||||||||||||||||||||
| 198 | |||||||||||||||||||||||||||||
| 199 | L<Mojolicious>, L<Mojolicious::Guides>, L<https://mojolicious.org>. | ||||||||||||||||||||||||||||
| 200 | |||||||||||||||||||||||||||||
| 201 | =cut | ||||||||||||||||||||||||||||