1 | package Mojolicious::Plugin::JSONConfig;
|
---|
2 | use Mojo::Base 'Mojolicious::Plugin::Config';
|
---|
3 |
|
---|
4 | use Mojo::JSON 'from_json';
|
---|
5 | use Mojo::Template;
|
---|
6 |
|
---|
7 | sub parse {
|
---|
8 | my ($self, $content, $file, $conf, $app) = @_;
|
---|
9 |
|
---|
10 | my $config = eval { from_json $self->render($content, $file, $conf, $app) };
|
---|
11 | die qq{Can't parse config "$file": $@} if $@;
|
---|
12 | die qq{Invalid config "$file"} unless ref $config eq 'HASH';
|
---|
13 |
|
---|
14 | return $config;
|
---|
15 | }
|
---|
16 |
|
---|
17 | sub register { shift->SUPER::register(shift, {ext => 'json', %{shift()}}) }
|
---|
18 |
|
---|
19 | sub render {
|
---|
20 | my ($self, $content, $file, $conf, $app) = @_;
|
---|
21 |
|
---|
22 | # Application instance and helper
|
---|
23 | my $prepend = q[no strict 'refs'; no warnings 'redefine';];
|
---|
24 | $prepend .= q[my $app = shift; sub app; local *app = sub { $app };];
|
---|
25 | $prepend .= q[use Mojo::Base -strict; no warnings 'ambiguous';];
|
---|
26 |
|
---|
27 | my $mt = Mojo::Template->new($conf->{template} || {})->name($file);
|
---|
28 | my $output = $mt->prepend($prepend . $mt->prepend)->render($content, $app);
|
---|
29 | return ref $output ? die $output : $output;
|
---|
30 | }
|
---|
31 |
|
---|
32 | 1;
|
---|
33 |
|
---|
34 | =encoding utf8
|
---|
35 |
|
---|
36 | =head1 NAME
|
---|
37 |
|
---|
38 | Mojolicious::Plugin::JSONConfig - JSON configuration plugin
|
---|
39 |
|
---|
40 | =head1 SYNOPSIS
|
---|
41 |
|
---|
42 | # myapp.json (it's just JSON with embedded Perl)
|
---|
43 | {
|
---|
44 | %# Just a value
|
---|
45 | "foo": "bar",
|
---|
46 |
|
---|
47 | %# Nested data structures are fine too
|
---|
48 | "baz": ["â¥"],
|
---|
49 |
|
---|
50 | %# You have full access to the application
|
---|
51 | "music_dir": "<%= app->home->child('music') %>"
|
---|
52 | }
|
---|
53 |
|
---|
54 | # Mojolicious
|
---|
55 | my $config = $app->plugin('JSONConfig');
|
---|
56 | say $config->{foo};
|
---|
57 |
|
---|
58 | # Mojolicious::Lite
|
---|
59 | my $config = plugin 'JSONConfig';
|
---|
60 | say $config->{foo};
|
---|
61 |
|
---|
62 | # foo.html.ep
|
---|
63 | %= $config->{foo}
|
---|
64 |
|
---|
65 | # The configuration is available application-wide
|
---|
66 | my $config = app->config;
|
---|
67 | say $config->{foo};
|
---|
68 |
|
---|
69 | # Everything can be customized with options
|
---|
70 | my $config = plugin JSONConfig => {file => '/etc/myapp.conf'};
|
---|
71 |
|
---|
72 | =head1 DESCRIPTION
|
---|
73 |
|
---|
74 | L<Mojolicious::Plugin::JSONConfig> is a JSON configuration plugin that
|
---|
75 | preprocesses its input with L<Mojo::Template>.
|
---|
76 |
|
---|
77 | The application object can be accessed via C<$app> or the C<app> function. A
|
---|
78 | default configuration filename in the application home directory will be
|
---|
79 | generated from the value of L<Mojolicious/"moniker"> (C<$moniker.json>). You can
|
---|
80 | extend the normal configuration file C<$moniker.json> with C<mode> specific ones
|
---|
81 | like C<$moniker.$mode.json>, which will be detected automatically.
|
---|
82 |
|
---|
83 | If the configuration value C<config_override> has been set in
|
---|
84 | L<Mojolicious/"config"> when this plugin is loaded, it will not do anything.
|
---|
85 |
|
---|
86 | The code of this plugin is a good example for learning to build new plugins,
|
---|
87 | you're welcome to fork it.
|
---|
88 |
|
---|
89 | See L<Mojolicious::Plugins/"PLUGINS"> for a list of plugins that are available
|
---|
90 | by default.
|
---|
91 |
|
---|
92 | =head1 OPTIONS
|
---|
93 |
|
---|
94 | L<Mojolicious::Plugin::JSONConfig> inherits all options from
|
---|
95 | L<Mojolicious::Plugin::Config> and supports the following new ones.
|
---|
96 |
|
---|
97 | =head2 template
|
---|
98 |
|
---|
99 | # Mojolicious::Lite
|
---|
100 | plugin JSONConfig => {template => {line_start => '.'}};
|
---|
101 |
|
---|
102 | Attribute values passed to L<Mojo::Template> object used to preprocess
|
---|
103 | configuration files.
|
---|
104 |
|
---|
105 | =head1 METHODS
|
---|
106 |
|
---|
107 | L<Mojolicious::Plugin::JSONConfig> inherits all methods from
|
---|
108 | L<Mojolicious::Plugin::Config> and implements the following new ones.
|
---|
109 |
|
---|
110 | =head2 parse
|
---|
111 |
|
---|
112 | $plugin->parse($content, $file, $conf, $app);
|
---|
113 |
|
---|
114 | Process content with L</"render"> and parse it with L<Mojo::JSON>.
|
---|
115 |
|
---|
116 | sub parse {
|
---|
117 | my ($self, $content, $file, $conf, $app) = @_;
|
---|
118 | ...
|
---|
119 | $content = $self->render($content, $file, $conf, $app);
|
---|
120 | ...
|
---|
121 | return $hash;
|
---|
122 | }
|
---|
123 |
|
---|
124 | =head2 register
|
---|
125 |
|
---|
126 | my $config = $plugin->register(Mojolicious->new);
|
---|
127 | my $config = $plugin->register(Mojolicious->new, {file => '/etc/foo.conf'});
|
---|
128 |
|
---|
129 | Register plugin in L<Mojolicious> application and merge configuration.
|
---|
130 |
|
---|
131 | =head2 render
|
---|
132 |
|
---|
133 | $plugin->render($content, $file, $conf, $app);
|
---|
134 |
|
---|
135 | Process configuration file with L<Mojo::Template>.
|
---|
136 |
|
---|
137 | sub render {
|
---|
138 | my ($self, $content, $file, $conf, $app) = @_;
|
---|
139 | ...
|
---|
140 | return $content;
|
---|
141 | }
|
---|
142 |
|
---|
143 | =head1 SEE ALSO
|
---|
144 |
|
---|
145 | L<Mojolicious>, L<Mojolicious::Guides>, L<https://mojolicious.org>.
|
---|
146 |
|
---|
147 | =cut
|
---|