File Coverage

blib/lib/Mojolicious/Plugin/NotYAMLConfig.pm
Criterion Covered Total %
statement 22 22 100.0
branch 6 8 75.0
condition 1 2 50.0
subroutine 6 6 100.0
pod 2 2 100.0
total 37 40 92.5


line stmt bran cond sub pod time code
1             package Mojolicious::Plugin::NotYAMLConfig;
2 1     1   8 use Mojo::Base 'Mojolicious::Plugin::JSONConfig';
  1         2  
  1         8  
3              
4 1     1   665 use CPAN::Meta::YAML;
  1         5964  
  1         72  
5 1     1   11 use Mojo::Util qw(decode encode);
  1         2  
  1         350  
6              
7             sub parse {
8 4     4 1 11 my ($self, $content, $file, $conf, $app) = @_;
9 4         7 my $config = eval { $self->{yaml}->(encode('UTF-8', $self->render($content, $file, $conf, $app))) };
  4         17  
10 4 100       1383 die qq{Can't load configuration from file "$file": $@} if $@;
11 3 50       16 die qq{Configuration file "$file" did not return a YAML mapping} unless ref $config eq 'HASH';
12 3         19 return $config;
13             }
14              
15             sub register {
16 7     7 1 24 my ($self, $app, $conf) = @_;
17              
18 7   50     37 $conf->{ext} //= 'yml';
19 7     4   36 $self->{yaml} = sub { CPAN::Meta::YAML::Load(decode 'UTF-8', shift) };
  4         13  
20 7 100       26 if (my $mod = $conf->{module}) {
21 1 50       36 die qq{YAML module $mod has no Load function} unless $self->{yaml} = $mod->can('Load');
22             }
23              
24 6         25 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