File: NotYAMLConfig.pm

package info (click to toggle)
libmojolicious-perl 9.39%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 4,324 kB
  • sloc: perl: 10,319; makefile: 31; javascript: 1
file content (139 lines) | stat: -rw-r--r-- 3,994 bytes parent folder | download | duplicates (3)
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
package Mojolicious::Plugin::NotYAMLConfig;
use Mojo::Base 'Mojolicious::Plugin::JSONConfig';

use CPAN::Meta::YAML;
use Mojo::Util qw(decode encode);

sub parse {
  my ($self, $content, $file, $conf, $app) = @_;
  my $config = eval { $self->{yaml}->(encode('UTF-8', $self->render($content, $file, $conf, $app))) };
  die qq{Can't load configuration from file "$file": $@} if $@;
  die qq{Configuration file "$file" did not return a YAML mapping} unless ref $config eq 'HASH';
  return $config;
}

sub register {
  my ($self, $app, $conf) = @_;

  $conf->{ext} //= 'yml';
  $self->{yaml} = sub { CPAN::Meta::YAML::Load(decode 'UTF-8', shift) };
  if (my $mod = $conf->{module}) {
    die qq{YAML module $mod has no Load function} unless $self->{yaml} = $mod->can('Load');
  }

  return $self->SUPER::register($app, $conf);
}

1;

=encoding utf8

=head1 NAME

Mojolicious::Plugin::NotYAMLConfig - Not quite YAML configuration plugin

=head1 SYNOPSIS

  # myapp.yml (it's just YAML with embedded Perl)
  ---
  foo: bar
  baz:
    - ♥
  music_dir: <%= app->home->child('music') %>

  # Mojolicious
  my $config = $app->plugin('NotYAMLConfig');
  say $config->{foo};

  # Mojolicious::Lite
  my $config = plugin 'NotYAMLConfig';
  say $config->{foo};

  # foo.html.ep
  %= config->{foo}

  # The configuration is available application-wide
  my $config = app->config;
  say $config->{foo};

  # Everything can be customized with options
  my $config = plugin NotYAMLConfig => {file => '/etc/myapp.conf'};

=head1 DESCRIPTION

L<Mojolicious::Plugin::NotYAMLConfig> is a YAML configuration plugin that preprocesses its input with L<Mojo::Template>.
By default it uses L<CPAN::Meta::YAML> for parsing, which is not the best YAML module available, but good enough for
most config files. If you need something more correct you can use a different module like L<YAML::XS> with the
L</"module"> option.

The application object can be accessed via C<$app> or the C<app> function. A default configuration filename in the
application home directory will be generated from the value of L<Mojolicious/"moniker"> (C<$moniker.yml>). You can
extend the normal configuration file C<$moniker.yml> with C<mode> specific ones like C<$moniker.$mode.yml>, which will
be detected automatically.

These configuration values are currently reserved:

=over 2

=item C<config_override>

If this configuration value has been set in L<Mojolicious/"config"> when this plugin is loaded, it will not do anything
besides loading deployment specific plugins.

=item C<plugins>

  plugins:
    - SetUserGroup:
        user: sri
        group: staff

One or more deployment specific plugins that should be loaded right after this plugin has been loaded.

=back

The code of this plugin is a good example for learning to build new plugins, you're welcome to fork it.

See L<Mojolicious::Plugins/"PLUGINS"> for a list of plugins that are available by default.

=head1 OPTIONS

L<Mojolicious::Plugin::NotYAMLConfig> inherits all options from L<Mojolicious::Plugin::JSONConfig> and supports the
following new ones.

=head2 module

  # Mojolicious::Lite
  plugin NotYAMLConfig => {module => 'YAML::PP'};

Alternative YAML module to use for parsing.

=head1 METHODS

L<Mojolicious::Plugin::NotYAMLConfig> inherits all methods from L<Mojolicious::Plugin::JSONConfig> and implements the
following new ones.

=head2 parse

  $plugin->parse($content, $file, $conf, $app);

Process content with L<Mojolicious::Plugin::JSONConfig/"render"> and parse it with L<CPAN::Meta::YAML>.

  sub parse ($self, $content, $file, $conf, $app) {
    ...
    $content = $self->render($content, $file, $conf, $app);
    ...
    return $hash;
  }

=head2 register

  my $config = $plugin->register(Mojolicious->new);
  my $config = $plugin->register(Mojolicious->new, {file => '/etc/foo.conf'});

Register plugin in L<Mojolicious> application and merge configuration.

=head1 SEE ALSO

L<Mojolicious>, L<Mojolicious::Guides>, L<https://mojolicious.org>.

=cut