1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195
|
# Copyright (C) 2008-2010, Sebastian Riedel.
package Mojolicious::Plugin::JsonConfig;
use strict;
use warnings;
use base 'Mojolicious::Plugin';
require File::Basename;
require File::Spec;
use Mojo::JSON;
use Mojo::Template;
# And so we say goodbye to our beloved pet, Nibbler, who's gone to a place
# where I, too, hope one day to go. The toilet.
sub register {
my ($self, $app, $conf) = @_;
# Plugin config
$conf ||= {};
# File
my $file = $conf->{file};
unless ($file) {
# Basename
$file = File::Basename::basename($0);
# Remove .pl, .p6 and .t extentions
$file =~ s/(?:\.p(?:l|6))|\.t$//i;
# Default extension
$file .= '.json';
}
# Absolute path
$file = $app->home->rel_file($file)
unless File::Spec->file_name_is_absolute($file);
# Read config file
my $config = {};
my $template = $conf->{template} || {};
if (-e $file) { $config = $self->_read_config($file, $template, $app) }
# Check for default
else {
# All missing
die qq/Config file "$file" missing, maybe you need to create it?\n/
unless $conf->{default};
# Debug
$app->log->debug(
qq/Config file "$file" missing, using default config./);
}
# Stash key
my $stash_key = $conf->{stash_key} || 'config';
# Merge
$config = {%{$conf->{default}}, %$config} if $conf->{default};
# Add hook
$app->plugins->add_hook(
before_dispatch => sub {
my ($self, $c) = @_;
# Stash
$c->stash($stash_key => $config);
}
);
return $config;
}
sub _read_config {
my ($self, $file, $template, $app) = @_;
# Debug
$app->log->debug(qq/Reading config file "$file"./);
# Slurp UTF-8 file
open FILE, "<:encoding(UTF-8)", $file
or die qq/Couldn't open config file "$file": $!/;
my $encoded = do { local $/; <FILE> };
close FILE;
# Instance
my $prepend = 'my $app = shift;';
# Be less strict
$prepend .= q/no strict 'refs'; no warnings 'redefine';/;
# Helper
$prepend .= "sub app; *app = sub { \$app };";
# Be strict again
$prepend .= q/use strict; use warnings;/;
# Render
my $mt = Mojo::Template->new($template);
$mt->prepend($prepend);
$encoded = $mt->render($encoded, $app);
utf8::encode $encoded;
# Parse
my $json = Mojo::JSON->new;
my $config = $json->decode($encoded);
my $error = $json->error;
die qq/Couldn't parse config file "$file": $error/ if !$config && $error;
die qq/Invalid config file "$file"./ if !$config || ref $config ne 'HASH';
return $config;
}
1;
__END__
=head1 NAME
Mojolicious::Plugin::JsonConfig - JSON Configuration Plugin
=head1 SYNOPSIS
# myapp.json
{
"foo" : "bar",
"music_dir" : "<%= app->home->rel_dir('music') %>"
}
# Mojolicious
$self->plugin('json_config');
# Mojolicious::Lite
plugin 'json_config';
# Reads myapp.json by default and puts the parsed version into the stash
my $config = $self->stash('config');
# Everything can be customized with options
my $config = plugin json_config => {
file => '/etc/myapp.conf',
stash_key => 'conf'
};
=head1 DESCRIPTION
L<Mojolicous::Plugin::JsonConfig> is a JSON configuration plugin that
preprocesses it's input with L<Mojo::Template>.
The application object can be accessed via C<$app> or the C<app> helper.
=head1 OPTIONS
=head2 C<default>
# Mojolicious::Lite
plugin json_config => {default => {foo => 'bar'}};
=head2 C<file>
# Mojolicious::Lite
plugin json_config => {file => 'myapp.conf'};
plugin json_config => {file => '/etc/foo.json'};
By default C<myapp.json> is searched in the application home directory.
=head2 C<stash_key>
# Mojolicious::Lite
plugin json_config => {stash_key => 'conf'};
=head2 C<template>
# Mojolicious::Lite
plugin json_config => {template => {line_start => '.'}};
=head1 METHODS
L<Mojolicious::Plugin::JsonConfig> inherits all methods from
L<Mojolicious::Plugin> and implements the following new ones.
=head2 C<register>
$plugin->register;
Register plugin hooks in L<Mojolicious> application.
=head1 SEE ALSO
L<Mojolicious>, L<Mojolicious::Guides>, L<http://mojolicious.org>.
=cut
|