| 1 | package Kelp::Module::Config; | ||||||||||
| 2 | |||||||||||
| 3 | 51 | ▶ | use Kelp::Base 'Kelp::Module'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 45 | ▶ | use Carp; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | 43 | ▶ | use Try::Tiny; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | 43 | ▶ | use Path::Tiny; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | 43 | ▶ | use Kelp::Util; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | |||||||||||
| 9 | # Extension to look for | ||||||||||
| 10 | 42 | attr ext => 'pl'; | |||||||||
| 11 | |||||||||||
| 12 | # Default modes to be processed before the app mode | ||||||||||
| 13 | 56 | ▶ | attr default_modes => sub { [qw(config)] }; | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 14 | |||||||||||
| 15 | # Directory where config files are | ||||||||||
| 16 | 56 | ▶ | attr path => sub { | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 17 | 56 | my $self = shift; | |||||||||
| 18 | return [ | ||||||||||
| 19 | $ENV{KELP_CONFIG_DIR}, | ||||||||||
| 20 | 56 | 'conf', | |||||||||
| 21 | $self->app->path, | ||||||||||
| 22 | $self->app->path . '/conf', | ||||||||||
| 23 | $self->app->path . '/../conf' | ||||||||||
| 24 | ]; | ||||||||||
| 25 | 42 | }; | |||||||||
| 26 | |||||||||||
| 27 | 55 | ▶ | attr separator => sub { quotemeta '.' }; | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 28 | |||||||||||
| 29 | # Defaults | ||||||||||
| 30 | 54 | ▶ | attr data => sub { | ||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||
| 31 | 54 | my $self = shift; | |||||||||
| 32 | |||||||||||
| 33 | # Return a big hash with default values | ||||||||||
| 34 | return { | ||||||||||
| 35 | |||||||||||
| 36 | # Default charset is UTF-8 | ||||||||||
| 37 | 54 | ▶ | charset => 'UTF-8', | ||||||||
|
Branch: 1 branch
| |||||||||||
| 38 | request_charset => 'UTF-8', | ||||||||||
| 39 | |||||||||||
| 40 | app_url => 'http://localhost:5000', | ||||||||||
| 41 | |||||||||||
| 42 | # Modules to load | ||||||||||
| 43 | modules => [qw/JSON Template/], | ||||||||||
| 44 | |||||||||||
| 45 | # Encoders | ||||||||||
| 46 | encoders => { | ||||||||||
| 47 | json => { | ||||||||||
| 48 | internal => { | ||||||||||
| 49 | utf8 => 0, | ||||||||||
| 50 | }, | ||||||||||
| 51 | }, | ||||||||||
| 52 | }, | ||||||||||
| 53 | |||||||||||
| 54 | # Module initialization params | ||||||||||
| 55 | modules_init => { | ||||||||||
| 56 | |||||||||||
| 57 | # Routes | ||||||||||
| 58 | Routes => { | ||||||||||
| 59 | base => ($self->app->can('_real_class') ? $self->app->_real_class : ref $self->app), | ||||||||||
| 60 | }, | ||||||||||
| 61 | |||||||||||
| 62 | # Template | ||||||||||
| 63 | Template => { | ||||||||||
| 64 | paths => [ | ||||||||||
| 65 | $self->app->path . '/views', | ||||||||||
| 66 | $self->app->path . '/../views' | ||||||||||
| 67 | ] | ||||||||||
| 68 | }, | ||||||||||
| 69 | |||||||||||
| 70 | # JSON | ||||||||||
| 71 | JSON => { | ||||||||||
| 72 | allow_blessed => 1, | ||||||||||
| 73 | convert_blessed => 1, | ||||||||||
| 74 | utf8 => 1 | ||||||||||
| 75 | }, | ||||||||||
| 76 | }, | ||||||||||
| 77 | |||||||||||
| 78 | # List of the middleware to add | ||||||||||
| 79 | middleware => [], | ||||||||||
| 80 | |||||||||||
| 81 | # Initializations of the middleware | ||||||||||
| 82 | middleware_init => {}, | ||||||||||
| 83 | |||||||||||
| 84 | }; | ||||||||||
| 85 | 42 | }; | |||||||||
| 86 | |||||||||||
| 87 | sub get | ||||||||||
| 88 | 663 | ▶ | { | ||||||||
|
Subroutine
get: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||
| 89 | 663 | my ($self, $path, $default) = @_; | |||||||||
| 90 | 663 | ▶ | return unless $path; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 91 | |||||||||||
| 92 | 661 | my $val = $self->data; | |||||||||
| 93 | 661 | for my $chunk (split($self->separator, $path)) { | |||||||||
| 94 | return $default | ||||||||||
| 95 | 973 | ▶ | unless exists $val->{$chunk}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 96 | |||||||||||
| 97 | 863 | ▶ | croak "Config path $path breaks at '$chunk'" | ||||||||
|
Branch: 1 branch
| |||||||||||
| 98 | unless ref $val eq 'HASH'; | ||||||||||
| 99 | |||||||||||
| 100 | 863 | $val = $val->{$chunk}; | |||||||||
| 101 | } | ||||||||||
| 102 | |||||||||||
| 103 | 551 | return $val; | |||||||||
| 104 | } | ||||||||||
| 105 | |||||||||||
| 106 | # Override this one to use other config formats. | ||||||||||
| 107 | sub load | ||||||||||
| 108 | 41 | ▶ | { | ||||||||
|
Subroutine
load: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||
| 109 | 41 | my ($self, $filename) = @_; | |||||||||
| 110 | |||||||||||
| 111 | # Open and read file | ||||||||||
| 112 | 41 | my $text; | |||||||||
| 113 | 41 | ▶ | try { | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 114 | 41 | $text = path($filename)->slurp({binmode => ':encoding(UTF-8)'}); | |||||||||
| 115 | 41 | }; | |||||||||
| 116 | |||||||||||
| 117 | 41 | ▶ | if (!defined $text) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 118 | 0 | warn "Can not read config file " . $filename; | |||||||||
| 119 | 0 | return {}; | |||||||||
| 120 | } | ||||||||||
| 121 | |||||||||||
| 122 | 41 | my ($hash, $error); | |||||||||
| 123 | { | ||||||||||
| 124 | 41 | local $@; | |||||||||
| 125 | 41 | my $app = $self->app; | |||||||||
| 126 | 41 | my $module = $filename; | |||||||||
| 127 | 41 | $module =~ s/\W/_/g; | |||||||||
| 128 | 26 | ▶ | $hash = | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 129 | eval "package Kelp::Module::Config::Sandbox::$module;" | ||||||||||
| 130 | . "use Kelp::Base -strict;" | ||||||||||
| 131 | . "sub app; local *app = sub { \$app };" | ||||||||||
| 132 | . "sub include(\$); local *include = sub { \$self->load(\@_) };" | ||||||||||
| 133 | . $text; | ||||||||||
| 134 | 41 | $error = $@; | |||||||||
| 135 | } | ||||||||||
| 136 | |||||||||||
| 137 | 41 | ▶ | die "Config file $filename parse error: " . $error if $error; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 138 | 40 | ▶ | die "Config file $filename did not return a HASH - $hash" | ||||||||
|
Branch: 1 branch
| |||||||||||
| 139 | unless ref $hash eq 'HASH'; | ||||||||||
| 140 | |||||||||||
| 141 | 39 | return $hash; | |||||||||
| 142 | } | ||||||||||
| 143 | |||||||||||
| 144 | sub process_mode | ||||||||||
| 145 | 125 | ▶ | { | ||||||||
|
Subroutine
process_mode: called CC=4 SCAR=14.0
Pod
documented
| |||||||||||
| 146 | 125 | my ($self, $mode) = @_; | |||||||||
| 147 | |||||||||||
| 148 | 125 | ▶ | my $filename = sub { | ||||||||
|
Subroutine
__ANON__: called CC=5 SCAR=16.1
| |||||||||||
| 149 | 125 | ▶ | my @paths = ref($self->path) ? @{$self->path} : ($self->path); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 150 | 125 | for my $path (@paths) { | |||||||||
| 151 | 460 | ▶ | next unless defined $path; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 152 | 368 | my $filename = sprintf('%s/%s.%s', $path, $mode, $self->ext); | |||||||||
| 153 | 368 | ▶ | return $filename if -r $filename; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 154 | } | ||||||||||
| 155 | } | ||||||||||
| 156 | 125 | ->(); | |||||||||
| 157 | |||||||||||
| 158 | 125 | ▶ | unless ($filename) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 159 | 81 | ▶ | if ($ENV{KELP_CONFIG_WARN}) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 160 | 0 | ▶ | my $message = | ||||||||
|
Branch: 1 branch
| |||||||||||
| 161 | $mode eq 'config' | ||||||||||
| 162 | ? "Main config file not found or not readable" | ||||||||||
| 163 | : "Config file for mode '$mode' not found or not readable"; | ||||||||||
| 164 | 0 | warn $message; | |||||||||
| 165 | } | ||||||||||
| 166 | 81 | return; | |||||||||
| 167 | } | ||||||||||
| 168 | |||||||||||
| 169 | 44 | my $parsed = {}; | |||||||||
| 170 | 44 | ▶ | try { | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 171 | 44 | $parsed = $self->load($filename); | |||||||||
| 172 | } | ||||||||||
| 173 | 2 | ▶ | catch { | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 174 | 2 | die "Parsing $filename died with error: '${_}'"; | |||||||||
| 175 | 44 | }; | |||||||||
| 176 | 42 | $self->data($self->merge($self->data, $parsed)); | |||||||||
| 177 | } | ||||||||||
| 178 | |||||||||||
| 179 | sub build | ||||||||||
| 180 | 62 | ▶ | { | ||||||||
|
Subroutine
build: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||
| 181 | 62 | my ($self, %args) = @_; | |||||||||
| 182 | |||||||||||
| 183 | # Find, parse and merge 'config' and mode files | ||||||||||
| 184 | 62 | for my $name (@{$self->default_modes}, $self->app->mode) { | |||||||||
| 185 | 122 | $self->process_mode($name); | |||||||||
| 186 | } | ||||||||||
| 187 | |||||||||||
| 188 | # Undocumented! Add 'extra' argument to unlock these special features: | ||||||||||
| 189 | # 1. If the extra argument contains a HASH, it will be merged to the | ||||||||||
| 190 | # configuration upon loading. | ||||||||||
| 191 | # 2. A new attribute '_cfg' will be registered into the app, which has | ||||||||||
| 192 | # three methods: merge, clear and set. Use them to merge a hash into | ||||||||||
| 193 | # the configuration, clear it, or set it to a new value. You can do those | ||||||||||
| 194 | # at any point in the life of the app. | ||||||||||
| 195 | # | ||||||||||
| 196 | 60 | ▶ | if (my $extra = delete $args{extra}) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 197 | 8 | ▶ | $self->data($self->merge($self->data, $extra)) if ref($extra) eq 'HASH'; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 198 | $self->register( | ||||||||||
| 199 | |||||||||||
| 200 | # A tiny object containing only merge, clear and set. Very useful when | ||||||||||
| 201 | # you're writing tests and need to add new config options, set the | ||||||||||
| 202 | # entire config hash to a new value, or clear it completely. | ||||||||||
| 203 | _cfg => Plack::Util::inline_object( | ||||||||||
| 204 | 1 | ▶ | merge => sub { | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 205 | 1 | $self->data($self->merge($self->data, $_[0])); | |||||||||
| 206 | }, | ||||||||||
| 207 | 0 | ▶ | clear => sub { $self->data({}) }, | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 208 | 0 | ▶ | set => sub { $self->data($_[0]) } | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 209 | ) | ||||||||||
| 210 | 8 | ); | |||||||||
| 211 | } | ||||||||||
| 212 | |||||||||||
| 213 | $self->register( | ||||||||||
| 214 | |||||||||||
| 215 | # Return the entire config hash | ||||||||||
| 216 | config_hash => $self->data, | ||||||||||
| 217 | |||||||||||
| 218 | # A wrapper arount the get method | ||||||||||
| 219 | 652 | ▶ | config => sub { | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 220 | 652 | my $app = shift; | |||||||||
| 221 | 652 | return $self->get(@_); | |||||||||
| 222 | } | ||||||||||
| 223 | 60 | ); | |||||||||
| 224 | } | ||||||||||
| 225 | |||||||||||
| 226 | sub merge | ||||||||||
| 227 | 50 | ▶ | { | ||||||||
|
Subroutine
merge: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 228 | 50 | my ($self, @args) = @_; | |||||||||
| 229 | |||||||||||
| 230 | 50 | return _merge(@args); | |||||||||
| 231 | } | ||||||||||
| 232 | |||||||||||
| 233 | # backcompat | ||||||||||
| 234 | sub _merge | ||||||||||
| 235 | 77 | ▶ | { | ||||||||
|
Subroutine
_merge: called CC=1 SCAR=0.0
| |||||||||||
| 236 | 77 | goto \&Kelp::Util::merge; | |||||||||
| 237 | } | ||||||||||
| 238 | |||||||||||
| 239 | 42 | 1; | |||||||||
| 240 | |||||||||||
| 241 | __END__ | ||||||||||