File Coverage

blib/lib/Mojolicious/Plugin/NotYAMLConfig.pm
Criterion Covered Total %
statement 22 22 100.0
branch 7 8 87.5
condition 2 2 100.0
subroutine 6 6 100.0
pod 2 2 100.0
total 39 40 97.5


line stmt bran cond sub pod time code
1             package Mojolicious::Plugin::NotYAMLConfig;
2 1     1   10 use Mojo::Base 'Mojolicious::Plugin::JSONConfig';
  1         2  
  1         9  
3              
4 1     1   738 use CPAN::Meta::YAML;
  1         6190  
  1         70  
5 1     1   8 use Mojo::Util qw(decode encode);
  1         3  
  1         362  
6              
7             sub parse {
8 5     5 1 14 my ($self, $content, $file, $conf, $app) = @_;
9 5         9 my $config = eval { $self->{yaml}->(encode('UTF-8', $self->render($content, $file, $conf, $app))) };
  5         23  
10 5 100       1441 die qq{Can't load configuration from file "$file": $@} if $@;
11 4 50       19 die qq{Configuration file "$file" did not return a YAML mapping} unless ref $config eq 'HASH';
12 4         20 return $config;
13             }
14              
15             sub register {
16 8     8 1 22 my ($self, $app, $conf) = @_;
17              
18 8   100     47 $conf->{ext} //= 'yml';
19 8     4   43 $self->{yaml} = sub { CPAN::Meta::YAML::Load(decode 'UTF-8', shift) };
  4         13  
20 8 100       28 if (my $mod = $conf->{module}) {
21 2 100       43 die qq{YAML module $mod has no Load function} unless $self->{yaml} = $mod->can('Load');
22             }
23              
24 7         35 return $self->SUPER::register($app, $conf);
25             }
26              
27             1;
28              
29             =encoding utf8
30              
31             =head1 NAME
32              
33             Mojolicious::Plugin::NotYAMLConfig - Not quite YAML configuration plugin
34              
35             =head1 SYNOPSIS
36              
37             # myapp.yml (it's just YAML with embedded Perl)
38             ---
39             foo: bar
40             baz:
41             - ♥
42             music_dir: <%= app->home->child('music') %>
43              
44             # Mojolicious
45             my $config = $app->plugin('NotYAMLConfig');
46             say $config->{foo};
47              
48             # Mojolicious::Lite
49             my $config = plugin 'NotYAMLConfig';
50             say $config->{foo};
51              
52             # foo.html.ep
53             %= config->{foo}
54              
55             # The configuration is available application-wide
56             my $config = app->config;
57             say $config->{foo};
58              
59             # Everything can be customized with options
60             my $config = plugin NotYAMLConfig => {file => '/etc/myapp.conf'};
61              
62             =head1 DESCRIPTION
63              
64             L is a YAML configuration plugin that preprocesses its input with L.
65             By default it uses L for parsing, which is not the best YAML module available, but good enough for
66             most config files. If you need something more correct you can use a different module like L with the
67             L option.
68              
69             The application object can be accessed via C<$app> or the C function. A default configuration filename in the
70             application home directory will be generated from the value of L (C<$moniker.yml>). You can
71             extend the normal configuration file C<$moniker.yml> with C specific ones like C<$moniker.$mode.yml>, which will
72             be detected automatically.
73              
74             These configuration values are currently reserved:
75              
76             =over 2
77              
78             =item C
79              
80             If this configuration value has been set in L when this plugin is loaded, it will not do anything
81             besides loading deployment specific plugins.
82              
83             =item C
84              
85             plugins:
86             - SetUserGroup:
87             user: sri
88             group: staff
89              
90             One or more deployment specific plugins that should be loaded right after this plugin has been loaded.
91              
92             =back
93              
94             The code of this plugin is a good example for learning to build new plugins, you're welcome to fork it.
95              
96             See L for a list of plugins that are available by default.
97              
98             =head1 OPTIONS
99              
100             L inherits all options from L and supports the
101             following new ones.
102              
103             =head2 module
104              
105             # Mojolicious::Lite
106             plugin NotYAMLConfig => {module => 'YAML::PP'};
107              
108             Alternative YAML module to use for parsing.
109              
110             =head1 METHODS
111              
112             L inherits all methods from L and implements the
113             following new ones.
114              
115             =head2 parse
116              
117             $plugin->parse($content, $file, $conf, $app);
118              
119             Process content with L and parse it with L.
120              
121             sub parse ($self, $content, $file, $conf, $app) {
122             ...
123             $content = $self->render($content, $file, $conf, $app);
124             ...
125             return $hash;
126             }
127              
128             =head2 register
129              
130             my $config = $plugin->register(Mojolicious->new);
131             my $config = $plugin->register(Mojolicious->new, {file => '/etc/foo.conf'});
132              
133             Register plugin in L application and merge configuration.
134              
135             =head1 SEE ALSO
136              
137             L, L, L.
138              
139             =cut