| 1 | package Kelp::Module::Config; | ||||||||||
| 2 | |||||||||||
| 3 | 43 | ▶ | use Kelp::Base 'Kelp::Module'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 43 | ▶ | use Carp; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | 41 | ▶ | use Try::Tiny; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | 41 | ▶ | use Path::Tiny; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | 41 | ▶ | use Kelp::Util; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | |||||||||||
| 9 | # Extension to look for | ||||||||||
| 10 | 40 | attr ext => 'pl'; | |||||||||
| 11 | |||||||||||
| 12 | # Default modes to be processed before the app mode | ||||||||||
| 13 | 53 | ▶ | attr default_modes => sub { [qw(config)] }; | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 14 | |||||||||||
| 15 | # Directory where config files are | ||||||||||
| 16 | 53 | ▶ | attr path => sub { | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 17 | 53 | my $self = shift; | |||||||||
| 18 | return [ | ||||||||||
| 19 | $ENV{KELP_CONFIG_DIR}, | ||||||||||
| 20 | 53 | 'conf', | |||||||||
| 21 | $self->app->path, | ||||||||||
| 22 | $self->app->path . '/conf', | ||||||||||
| 23 | $self->app->path . '/../conf' | ||||||||||
| 24 | ]; | ||||||||||
| 25 | 40 | }; | |||||||||
| 26 | |||||||||||
| 27 | 52 | ▶ | attr separator => sub { quotemeta '.' }; | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 28 | |||||||||||
| 29 | # Defaults | ||||||||||
| 30 | 51 | ▶ | attr data => sub { | ||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||
| 31 | 51 | my $self = shift; | |||||||||
| 32 | |||||||||||
| 33 | # Return a big hash with default values | ||||||||||
| 34 | return { | ||||||||||
| 35 | |||||||||||
| 36 | # Default charset is UTF-8 | ||||||||||
| 37 | 51 | ▶ | 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 | 40 | }; | |||||||||
| 86 | |||||||||||
| 87 | sub get | ||||||||||
| 88 | 634 | ▶ | { | ||||||||
|
Subroutine
get: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||
| 89 | 634 | my ($self, $path, $default) = @_; | |||||||||
| 90 | 634 | ▶ | return unless $path; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 91 | |||||||||||
| 92 | 632 | my $val = $self->data; | |||||||||
| 93 | 632 | for my $chunk (split($self->separator, $path)) { | |||||||||
| 94 | return $default | ||||||||||
| 95 | 929 | ▶ | unless exists $val->{$chunk}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 96 | |||||||||||
| 97 | 825 | ▶ | croak "Config path $path breaks at '$chunk'" | ||||||||
|
Branch: 1 branch
| |||||||||||
| 98 | unless ref $val eq 'HASH'; | ||||||||||
| 99 | |||||||||||
| 100 | 825 | $val = $val->{$chunk}; | |||||||||
| 101 | } | ||||||||||
| 102 | |||||||||||
| 103 | 528 | return $val; | |||||||||
| 104 | } | ||||||||||
| 105 | |||||||||||
| 106 | # Override this one to use other config formats. | ||||||||||
| 107 | sub load | ||||||||||
| 108 | 38 | ▶ | { | ||||||||
|
Subroutine
load: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||
| 109 | 38 | my ($self, $filename) = @_; | |||||||||
| 110 | |||||||||||
| 111 | # Open and read file | ||||||||||
| 112 | 38 | my $text; | |||||||||
| 113 | 38 | ▶ | try { | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 114 | 38 | $text = path($filename)->slurp({binmode => ':encoding(UTF-8)'}); | |||||||||
| 115 | 38 | }; | |||||||||
| 116 | |||||||||||
| 117 | 38 | ▶ | if (!defined $text) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 118 | 0 | warn "Can not read config file " . $filename; | |||||||||
| 119 | 0 | return {}; | |||||||||
| 120 | } | ||||||||||
| 121 | |||||||||||
| 122 | 38 | my ($hash, $error); | |||||||||
| 123 | { | ||||||||||
| 124 | 38 | local $@; | |||||||||
| 125 | 38 | my $app = $self->app; | |||||||||
| 126 | 38 | my $module = $filename; | |||||||||
| 127 | 38 | $module =~ s/\W/_/g; | |||||||||
| 128 | 24 | ▶ | $hash = | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
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 | 38 | $error = $@; | |||||||||
| 135 | } | ||||||||||
| 136 | |||||||||||
| 137 | 38 | ▶ | die "Config file $filename parse error: " . $error if $error; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 138 | 37 | ▶ | die "Config file $filename did not return a HASH - $hash" | ||||||||
|
Branch: 1 branch
| |||||||||||
| 139 | unless ref $hash eq 'HASH'; | ||||||||||
| 140 | |||||||||||
| 141 | 36 | return $hash; | |||||||||
| 142 | } | ||||||||||
| 143 | |||||||||||
| 144 | sub process_mode | ||||||||||
| 145 | 119 | ▶ | { | ||||||||
|
Subroutine
process_mode: called CC=4 SCAR=14.0
Pod
documented
| |||||||||||
| 146 | 119 | my ($self, $mode) = @_; | |||||||||
| 147 | |||||||||||
| 148 | 119 | ▶ | my $filename = sub { | ||||||||
|
Subroutine
__ANON__: called CC=5 SCAR=16.1
| |||||||||||
| 149 | 119 | ▶ | my @paths = ref($self->path) ? @{$self->path} : ($self->path); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 150 | 119 | for my $path (@paths) { | |||||||||
| 151 | 439 | ▶ | next unless defined $path; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 152 | 353 | my $filename = sprintf('%s/%s.%s', $path, $mode, $self->ext); | |||||||||
| 153 | 353 | ▶ | return $filename if -r $filename; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 154 | } | ||||||||||
| 155 | } | ||||||||||
| 156 | 119 | ->(); | |||||||||
| 157 | |||||||||||
| 158 | 119 | ▶ | unless ($filename) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 159 | 78 | ▶ | 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 | 78 | return; | |||||||||
| 167 | } | ||||||||||
| 168 | |||||||||||
| 169 | 41 | my $parsed = {}; | |||||||||
| 170 | 41 | ▶ | try { | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 171 | 41 | $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 | 41 | }; | |||||||||
| 176 | 39 | $self->data($self->merge($self->data, $parsed)); | |||||||||
| 177 | } | ||||||||||
| 178 | |||||||||||
| 179 | sub build | ||||||||||
| 180 | 59 | ▶ | { | ||||||||
|
Subroutine
build: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||
| 181 | 59 | my ($self, %args) = @_; | |||||||||
| 182 | |||||||||||
| 183 | # Find, parse and merge 'config' and mode files | ||||||||||
| 184 | 59 | for my $name (@{$self->default_modes}, $self->app->mode) { | |||||||||
| 185 | 116 | $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 | 57 | ▶ | 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 | 623 | ▶ | config => sub { | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 220 | 623 | my $app = shift; | |||||||||
| 221 | 623 | return $self->get(@_); | |||||||||
| 222 | } | ||||||||||
| 223 | 57 | ); | |||||||||
| 224 | } | ||||||||||
| 225 | |||||||||||
| 226 | sub merge | ||||||||||
| 227 | 47 | ▶ | { | ||||||||
|
Subroutine
merge: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 228 | 47 | my ($self, @args) = @_; | |||||||||
| 229 | |||||||||||
| 230 | 47 | return _merge(@args); | |||||||||
| 231 | } | ||||||||||
| 232 | |||||||||||
| 233 | # backcompat | ||||||||||
| 234 | sub _merge | ||||||||||
| 235 | 74 | ▶ | { | ||||||||
|
Subroutine
_merge: called CC=1 SCAR=0.0
| |||||||||||
| 236 | 74 | goto \&Kelp::Util::merge; | |||||||||
| 237 | } | ||||||||||
| 238 | |||||||||||
| 239 | 40 | 1; | |||||||||
| 240 | |||||||||||
| 241 | __END__ | ||||||||||
| 242 | |||||||||||
| 243 | =pod | ||||||||||
| 244 | |||||||||||
| 245 | =head1 NAME | ||||||||||
| 246 | |||||||||||
| 247 | Kelp::Module::Config - Configuration for Kelp applications | ||||||||||
| 248 | |||||||||||
| 249 | =head1 DESCRIPTION | ||||||||||
| 250 | |||||||||||
| 251 | This is one of the two modules that are automatically loaded for each and every | ||||||||||
| 252 | Kelp application. The other one is L<Kelp::Module::Routes>. It reads | ||||||||||
| 253 | configuration files containing Perl-style hashes, and merges them depending on | ||||||||||
| 254 | the value of the application's C<mode> attribute. | ||||||||||
| 255 | |||||||||||
| 256 | The main configuration file name is C<config.pl>, and it will be searched in | ||||||||||
| 257 | the C<conf> and C<../conf> directories. You can also set the C<KELP_CONFIG_DIR> | ||||||||||
| 258 | environmental variable with the path to the configuration files. | ||||||||||
| 259 | |||||||||||
| 260 | This module comes with some L<default values|/DEFAULTS>, so if there are no | ||||||||||
| 261 | configuration files found, those values will be used. Any values from | ||||||||||
| 262 | configuration files will add to or override the default values. | ||||||||||
| 263 | |||||||||||
| 264 | =head1 ORDER | ||||||||||
| 265 | |||||||||||
| 266 | First the module will look for C<conf/config.pl>, then for | ||||||||||
| 267 | C<../conf/config.pl>. If found, they will be parsed and merged into the | ||||||||||
| 268 | default values. The same order applies to the I<mode> file too, so if the | ||||||||||
| 269 | application L<mode|Kelp/mode> is I<development>, then C<conf/development.pl> | ||||||||||
| 270 | and C<../conf/development.pl> will be looked for. If found, they will also be | ||||||||||
| 271 | merged to the config hash. | ||||||||||
| 272 | |||||||||||
| 273 | =head1 ACCESSING THE APPLICATION | ||||||||||
| 274 | |||||||||||
| 275 | The application instance can be accessed within the config files via the C<app> | ||||||||||
| 276 | keyword. | ||||||||||
| 277 | |||||||||||
| 278 | { | ||||||||||
| 279 | bin_path => app->path . '/bin' | ||||||||||
| 280 | } | ||||||||||
| 281 | |||||||||||
| 282 | =head1 INCLUDING FILES | ||||||||||
| 283 | |||||||||||
| 284 | To include other config files, one may use the C<include> keyword. | ||||||||||
| 285 | |||||||||||
| 286 | # config.pl | ||||||||||
| 287 | { | ||||||||||
| 288 | modules_init => { | ||||||||||
| 289 | Template => include('conf/my_template.pl') | ||||||||||
| 290 | } | ||||||||||
| 291 | } | ||||||||||
| 292 | |||||||||||
| 293 | # my_template.pl | ||||||||||
| 294 | { | ||||||||||
| 295 | path => 'views/', | ||||||||||
| 296 | utf8 => 1 | ||||||||||
| 297 | } | ||||||||||
| 298 | |||||||||||
| 299 | Any config file may be included as long as it returns a hashref. | ||||||||||
| 300 | |||||||||||
| 301 | =head1 MERGING | ||||||||||
| 302 | |||||||||||
| 303 | The first configuration file this module will look for is C<config.pl>. This is | ||||||||||
| 304 | where you should keep configuration options that apply to all running | ||||||||||
| 305 | environments. The mode-specific configuration file will be merged to this | ||||||||||
| 306 | config, and it will take priority. Merging is done as follows: | ||||||||||
| 307 | |||||||||||
| 308 | =over | ||||||||||
| 309 | |||||||||||
| 310 | =item Scalars will always be overwritten. | ||||||||||
| 311 | |||||||||||
| 312 | =item Hashes will be merged. | ||||||||||
| 313 | |||||||||||
| 314 | =item Arrays will be overwritten, except in case when the name of the array contains a | ||||||||||
| 315 | sigil as follows: | ||||||||||
| 316 | |||||||||||
| 317 | =over | ||||||||||
| 318 | |||||||||||
| 319 | =item | ||||||||||
| 320 | |||||||||||
| 321 | C<+> in front of the name will add the elements to the array: | ||||||||||
| 322 | |||||||||||
| 323 | # in config.pl | ||||||||||
| 324 | { | ||||||||||
| 325 | middleware => [qw/Bar Foo/] | ||||||||||
| 326 | } | ||||||||||
| 327 | |||||||||||
| 328 | # in development.pl | ||||||||||
| 329 | { | ||||||||||
| 330 | '+middleware' => ['Baz'] # Add 'Baz' in development | ||||||||||
| 331 | } | ||||||||||
| 332 | |||||||||||
| 333 | =item | ||||||||||
| 334 | |||||||||||
| 335 | C<-> in front of the name will remove the elements from the array: | ||||||||||
| 336 | |||||||||||
| 337 | # in config.pl | ||||||||||
| 338 | { | ||||||||||
| 339 | modules => [qw/Template JSON Logger/] | ||||||||||
| 340 | } | ||||||||||
| 341 | |||||||||||
| 342 | # in test.pl | ||||||||||
| 343 | { | ||||||||||
| 344 | '-modules' => [qw/Logger/] # Remove the Logger modules in test mode | ||||||||||
| 345 | } | ||||||||||
| 346 | |||||||||||
| 347 | =item | ||||||||||
| 348 | |||||||||||
| 349 | No sigil will cause the array to be completely replaced: | ||||||||||
| 350 | |||||||||||
| 351 | # in config.pl | ||||||||||
| 352 | { | ||||||||||
| 353 | middleware => [qw/Bar Foo/] | ||||||||||
| 354 | } | ||||||||||
| 355 | |||||||||||
| 356 | # in cli.pl | ||||||||||
| 357 | { | ||||||||||
| 358 | middleware => [] # No middleware in CLI | ||||||||||
| 359 | } | ||||||||||
| 360 | |||||||||||
| 361 | =back | ||||||||||
| 362 | |||||||||||
| 363 | Note that the merge sigils only apply to arrays. All other types will keep the | ||||||||||
| 364 | sigil in the key name: | ||||||||||
| 365 | |||||||||||
| 366 | # config.pl | ||||||||||
| 367 | { | ||||||||||
| 368 | modules => ["+MyApp::Fully::Qualified::Name"], | ||||||||||
| 369 | modules_init => { | ||||||||||
| 370 | "+MyApp::Fully::Qualified::Name" => { opt1 => 1, opt2 => 2 } | ||||||||||
| 371 | } | ||||||||||
| 372 | } | ||||||||||
| 373 | |||||||||||
| 374 | # development.pl | ||||||||||
| 375 | { | ||||||||||
| 376 | modules_init => { | ||||||||||
| 377 | "+MyApp::Fully::Qualified::Name" => { opt3 => 3 } | ||||||||||
| 378 | } | ||||||||||
| 379 | } | ||||||||||
| 380 | |||||||||||
| 381 | =back | ||||||||||
| 382 | |||||||||||
| 383 | =head1 REGISTERED METHODS | ||||||||||
| 384 | |||||||||||
| 385 | This module registers the following methods into the underlying app: | ||||||||||
| 386 | |||||||||||
| 387 | =head2 config | ||||||||||
| 388 | |||||||||||
| 389 | A wrapper for the L</get> method. | ||||||||||
| 390 | |||||||||||
| 391 | # Somewhere in the app | ||||||||||
| 392 | my $pos = $self->config('row.col.position'); | ||||||||||
| 393 | |||||||||||
| 394 | # Gets {row}->{col}->{position} from the config hash | ||||||||||
| 395 | |||||||||||
| 396 | my $hello = $self->config('hello', 'world'); | ||||||||||
| 397 | |||||||||||
| 398 | # gets {hello} from the config hash and returns 'world' if not found | ||||||||||
| 399 | |||||||||||
| 400 | =head2 config_hash | ||||||||||
| 401 | |||||||||||
| 402 | A reference to the entire configuration hash. | ||||||||||
| 403 | |||||||||||
| 404 | my $pos = $self->config_hash->{row}->{col}->{position}; | ||||||||||
| 405 | |||||||||||
| 406 | Using this or L</config> is entirely up to the application developer. | ||||||||||
| 407 | |||||||||||
| 408 | =head3 _cfg | ||||||||||
| 409 | |||||||||||
| 410 | A tiny object that contains only three methods - B<merge>, B<clear> and B<set>. | ||||||||||
| 411 | It allows you to merge values to the config hash, clear it completely or | ||||||||||
| 412 | set it to an entirely new value. This method comes handy when writing tests. | ||||||||||
| 413 | |||||||||||
| 414 | # Somewhere in a .t file | ||||||||||
| 415 | my $app = MyApp->new( mode => 'test' ); | ||||||||||
| 416 | |||||||||||
| 417 | my %original_config = %{ $app->config_hash }; | ||||||||||
| 418 | $app->_cfg->merge( { middleware => ['Foo'] } ); | ||||||||||
| 419 | |||||||||||
| 420 | # Now you can test with middleware Foo added to the config | ||||||||||
| 421 | |||||||||||
| 422 | # Revert to the original configuration | ||||||||||
| 423 | $app->_cfg->set( \%original_config ); | ||||||||||
| 424 | |||||||||||
| 425 | =head1 ATTRIBUTES | ||||||||||
| 426 | |||||||||||
| 427 | This module implements some attributes, which can be overridden by subclasses. | ||||||||||
| 428 | |||||||||||
| 429 | =head2 ext | ||||||||||
| 430 | |||||||||||
| 431 | The file extension of the configuration files. Default is C<pl>. | ||||||||||
| 432 | |||||||||||
| 433 | =head2 default_modes | ||||||||||
| 434 | |||||||||||
| 435 | An array reference of modes to be processed before the application's mode. | ||||||||||
| 436 | Default is C<['config']>. | ||||||||||
| 437 | |||||||||||
| 438 | =head2 separator | ||||||||||
| 439 | |||||||||||
| 440 | A regular expression for the value separator used by L</get>. The default is | ||||||||||
| 441 | C<qr/\./>, i.e. a dot. | ||||||||||
| 442 | |||||||||||
| 443 | =head2 path | ||||||||||
| 444 | |||||||||||
| 445 | Specifies a path, or an array of paths where to look for configuration files. | ||||||||||
| 446 | This is particularly useful when writing tests, because you can set a custom | ||||||||||
| 447 | path to a peculiar configuration. | ||||||||||
| 448 | |||||||||||
| 449 | =head2 data | ||||||||||
| 450 | |||||||||||
| 451 | The hashref with data contained in all of the merged configurations. | ||||||||||
| 452 | |||||||||||
| 453 | =head1 METHODS | ||||||||||
| 454 | |||||||||||
| 455 | The module also implements some methods for parsing the config files, which can | ||||||||||
| 456 | be overridden in extending classes. | ||||||||||
| 457 | |||||||||||
| 458 | =head2 get | ||||||||||
| 459 | |||||||||||
| 460 | C<get($string)> | ||||||||||
| 461 | |||||||||||
| 462 | C<get($string, $default)> | ||||||||||
| 463 | |||||||||||
| 464 | Get a value from the config using a separated string. | ||||||||||
| 465 | |||||||||||
| 466 | my $value = $c->get('bar.foo.baz'); | ||||||||||
| 467 | my $same = $c->get('bar')->{foo}->{baz}; | ||||||||||
| 468 | my $again = $c->data->{bar}->{foo}->{baz}; | ||||||||||
| 469 | |||||||||||
| 470 | By default the separator is a dot, but this can be changed via the | ||||||||||
| 471 | L</separator> attribute. | ||||||||||
| 472 | |||||||||||
| 473 | If it doesn't find the requested value, C<$default> will be returned (or undef | ||||||||||
| 474 | if not passed). If along the way it finds a different type that C<HASH> (for | ||||||||||
| 475 | example you requested C<a.b>, but C<a> is a string) then an exception will be | ||||||||||
| 476 | raised. | ||||||||||
| 477 | |||||||||||
| 478 | =head2 load | ||||||||||
| 479 | |||||||||||
| 480 | C<load(filename)> | ||||||||||
| 481 | |||||||||||
| 482 | Loads, and parses the file C<$filename> and returns a hash reference. | ||||||||||
| 483 | |||||||||||
| 484 | =head2 process_mode | ||||||||||
| 485 | |||||||||||
| 486 | C<process_mode($mode)> | ||||||||||
| 487 | |||||||||||
| 488 | Finds the file (if it exists) corresponding to C<$mode>, parses it and merges | ||||||||||
| 489 | it into the data. Useful, when you want to process and extra config file during | ||||||||||
| 490 | the application initialization. | ||||||||||
| 491 | |||||||||||
| 492 | # lib/MyApp.pm | ||||||||||
| 493 | sub build { | ||||||||||
| 494 | $self->loaded_modules->{Config}->process_mode( 'more_config' ); | ||||||||||
| 495 | } | ||||||||||
| 496 | |||||||||||
| 497 | =head1 DEFAULTS | ||||||||||
| 498 | |||||||||||
| 499 | This module sets certain default values. All of them may be overridden in any of | ||||||||||
| 500 | the C<conf/> files. It probably pays to view the code of this module and look | ||||||||||
| 501 | and the C<defaults> sub to see what is being set by default, but here is the | ||||||||||
| 502 | short version: | ||||||||||
| 503 | |||||||||||
| 504 | =head2 charset | ||||||||||
| 505 | |||||||||||
| 506 | Application's charset, which it will by default use to encode the body of the | ||||||||||
| 507 | response (unless charset is set manually for a response). Any encoding | ||||||||||
| 508 | supported by L<Encode> is fine. It should probably stay as default C<UTF-8> | ||||||||||
| 509 | unless you're doing something non-standard. | ||||||||||
| 510 | |||||||||||
| 511 | Can be set to undef to disable response encoding. | ||||||||||
| 512 | |||||||||||
| 513 | =head2 request_charset | ||||||||||
| 514 | |||||||||||
| 515 | Default incoming charset, which will be used to decode requests (unless the | ||||||||||
| 516 | request contains its own). It will always be used to decode URI elements of the | ||||||||||
| 517 | request. It is B<strongly recommended> this stays as default C<UTF-8>, but can | ||||||||||
| 518 | also be set to other one-byte encodings if needed. | ||||||||||
| 519 | |||||||||||
| 520 | Can be set to undef to disable request decoding entirely. | ||||||||||
| 521 | |||||||||||
| 522 | =head2 app_url | ||||||||||
| 523 | |||||||||||
| 524 | Abosulte URL under which the application is available. | ||||||||||
| 525 | |||||||||||
| 526 | C<http://localhost:5000> | ||||||||||
| 527 | |||||||||||
| 528 | =head2 encoders | ||||||||||
| 529 | |||||||||||
| 530 | A hashref of extra encoder configs to be used by L<Kelp/get_encoder>. By | ||||||||||
| 531 | default, only C<encoders.json.internal> is defined and disables C<utf8> flag of | ||||||||||
| 532 | the JSON module. | ||||||||||
| 533 | |||||||||||
| 534 | =head2 modules | ||||||||||
| 535 | |||||||||||
| 536 | An arrayref with module names to load on startup. The default value is | ||||||||||
| 537 | C<['JSON', 'Template']> | ||||||||||
| 538 | |||||||||||
| 539 | =head2 modules_init | ||||||||||
| 540 | |||||||||||
| 541 | A hashref with initializations for each of the loaded modules, except this one, | ||||||||||
| 542 | ironically. | ||||||||||
| 543 | |||||||||||
| 544 | =head2 middleware | ||||||||||
| 545 | |||||||||||
| 546 | An arrayref with middleware to load on startup. The default value is an | ||||||||||
| 547 | empty array. | ||||||||||
| 548 | |||||||||||
| 549 | =head2 middleware_init | ||||||||||
| 550 | |||||||||||
| 551 | A hashref with initialization arguments for each of the loaded middleware. | ||||||||||
| 552 | |||||||||||
| 553 | =head1 SUBCLASSING | ||||||||||
| 554 | |||||||||||
| 555 | You can subclass this module and use other types of configuration files | ||||||||||
| 556 | (for example YAML). You need to override the C<ext> attribute | ||||||||||
| 557 | and the C<load> subroutine. | ||||||||||
| 558 | |||||||||||
| 559 | package Kelp::Module::Config::Custom; | ||||||||||
| 560 | use Kelp::Parent 'Kelp::Module::Config'; | ||||||||||
| 561 | |||||||||||
| 562 | # Set the config file extension to .cus | ||||||||||
| 563 | attr ext => 'cus'; | ||||||||||
| 564 | |||||||||||
| 565 | sub load { | ||||||||||
| 566 | my ( $self, $filename ) = @_; | ||||||||||
| 567 | |||||||||||
| 568 | # Load $filename, parse it and return a hashref | ||||||||||
| 569 | } | ||||||||||
| 570 | |||||||||||
| 571 | 1; | ||||||||||
| 572 | |||||||||||
| 573 | Later ... | ||||||||||
| 574 | |||||||||||
| 575 | # app.psgi | ||||||||||
| 576 | use MyApp; | ||||||||||
| 577 | |||||||||||
| 578 | my $app = MyApp->new( config_module => 'Config::Custom' ); | ||||||||||
| 579 | |||||||||||
| 580 | run; | ||||||||||
| 581 | |||||||||||
| 582 | The above example module will look for C<config/*.cus> to load as configuration. | ||||||||||
| 583 | |||||||||||
| 584 | =head1 TESTING | ||||||||||
| 585 | |||||||||||
| 586 | Since the config files are searched in both C<conf/> and C<../conf/>, you can | ||||||||||
| 587 | use the same configuration set of files for your application and for your tests. | ||||||||||
| 588 | Assuming the all of your test will reside in C<t/>, they should be able to load | ||||||||||
| 589 | and find the config files at C<../conf/>. | ||||||||||
| 590 | |||||||||||
| 591 | =head1 ENVIRONMENT VARIABLES | ||||||||||
| 592 | |||||||||||
| 593 | =head2 KELP_CONFIG_WARN | ||||||||||
| 594 | |||||||||||
| 595 | This module will not warn for missing config and mode files. It will | ||||||||||
| 596 | silently load the default configuration hash. Set KELP_CONFIG_WARN to a | ||||||||||
| 597 | true value to make this module warn about missing files. | ||||||||||
| 598 | |||||||||||
| 599 | $ KELP_CONFIG_WARN=1 plackup app.psgi | ||||||||||
| 600 | |||||||||||
| 601 | =cut | ||||||||||
| 602 | |||||||||||