Acme/Crux/Plugin/Config.pm untested

filter: Statementstmt 0.0% 0 / 36 Branchbran 0.0% 0 / 6 Conditioncond 0.0% 0 / 31 MC/DCmcdc 0.0% 0 / 15 Subroutinesub 0.0% 0 / 7 Podpod 0.0% 0 / 1 totaltotal 0.0% 0 / 96 CC 4 SCAR 22.2
CC
4
Cov
0%
register
20.0
BEGIN
2.0
BEGIN
2.0
CRAP 9.2
1 package Acme::Crux::Plugin::Config;
2 use warnings;
3 use strict;
4 use utf8;
5  
6 =encoding utf-8
7  
8 =head1 NAME
9  
10 Acme::Crux::Plugin::Config - The Acme::Crux plugin for configuration your application
11  
12 =head1 SYNOPSIS
13  
14 # In startup
15 my $config = $app->plugin('Config');
16 my $config = $app->plugin('Config', undef, {file => '/etc/myapp.conf'});
17  
18 # In application
19 my $val = $app->config->get("/foo/bar/baz");
20 my $all = $app->config->conf;
21  
22 my $array = $app->config->array('/foo'); # 'value'
23 # ['value']
24  
25 my $hash = $app->config->hash('/foo'); # { foo => 'first', bar => 'second' }
26 # { foo => 'first', bar => 'second' }
27  
28 my $first = $app->config->first('/foo'); # ['first', 'second', 'third']
29 # first
30  
31 my $latest = $app->config->latest('/foo'); # ['first', 'second', 'third']
32 # third
33  
34 =head1 DESCRIPTION
35  
36 The Acme::Crux plugin for configuration your application
37  
38 =head1 OPTIONS
39  
40 This plugin supports the following options
41  
42 =head2 default
43  
44 $app->plugin(Config => undef, {default => {foo => 'bar'});
45  
46 Sets the default configuration hash
47  
48 Default: no defaults, empty config structure
49  
50 =head2 dirs
51  
52 $app->plugin(Config => undef, {dirs => ['/etc/foo', '/etc/bar']});
53  
54 Paths to additional directories of config files
55  
56 Default: no additional directories
57  
58 =head2 file
59  
60 $app->plugin(Config => undef, {file => '/etc/myapp.conf'});
61  
62 Path to configuration file, absolute or relative to the application root directory,
63 defaults to the value of the C<$moniker.conf> in the application root directory.
64  
65 Default: C<configfile> command line option or C<configfile> application argument
66 or C</etc/$moniker/$moniker.conf> otherwise
67  
68 =head2 noload
69  
70 $app->plugin(Config => undef, {noload => 1});
71  
72 This option disables auto loading config file
73  
74 Default: C<noload> command line option or C<config_noload> application argument
75 or C<0> otherwise
76  
77 =head2 opts, options
78  
79 $app->plugin(Config => undef, {opts => {'-AutoTrue' => 0}});
80 $app->plugin(Config => undef, {options => {'-AutoTrue' => 0}});
81  
82 Sets the L<Config::General> options directly
83  
84 Default: no special options
85  
86 =head2 root
87  
88 $app->plugin(Config => undef, {root => '/etc/myapp'});
89  
90 Sets the root directory to configuration files and directories location
91  
92 Default: C<configroot> command line option or C<root> application argument
93 or C</etc/$moniker> otherwise
94  
95 =head1 METHODS
96  
97 This class inherits all methods from L<Acme::Crux::Plugin> and implements the following new ones
98  
99 =head2 register
100  
101 $plugin->register($app, {file => '/etc/app.conf'});
102  
103 Register plugin in Acme::Crux application and merge configuration
104  
105 =head1 HELPERS
106  
107 All helpers of this plugin are allows get access to configuration object.
108 See L<Acrux::Config> for details
109  
110 =head2 config, conf
111  
112 Returns L<Acrux::Config> object
113  
114 =head1 TO DO
115  
116 See C<TODO> file
117  
118 =head1 SEE ALSO
119  
120 L<Acme::Crux::Plugin>, L<Acrux::Config>
121  
122 =head1 AUTHOR
123  
124 Serż Minus (Sergey Lepenkov) L<https://www.serzik.com> E<lt>abalama@cpan.orgE<gt>
125  
126 =head1 COPYRIGHT
127  
128 Copyright (C) 1998-2026 D&D Corporation
129  
130 =head1 LICENSE
131  
132 This program is distributed under the terms of the Artistic License Version 2.0
133  
134 See the C<LICENSE> file or L<https://opensource.org/license/artistic-2-0> for details
135  
136 =cut
137  
138 use parent 'Acme::Crux::Plugin';
139  
140 use Acrux::Config;
141 use Acrux::RefUtil qw/as_array_ref as_hash_ref is_true_flag/;
142  
143 sub register {
144 my ($self, $app, $args) = @_;
145  
146 # NoLoad flag: PLGARGS || OPTS || ORIG || DEFS
147 my $noload = is_true_flag($args->{noload}) # From plugin arguments first
148 || $app->getopt("noload") # From command line options
149 || $app->orig->{"config_noload"} # From App arguments
150 || 0;
151  
152 # Config file: PLGARGS || OPTS || ORIG || DEFS
153 my $file = $args->{file} || $app->getopt("configfile") || $app->configfile;
154  
155 # Config::General Options: PLGARGS || DEFS
156 my $options = as_hash_ref($args->{options} || $args->{opts});
157  
158 # Merge defaults
159 my $defaults = as_hash_ref($args->{defaults} || $args->{default}) || {};
160  
161 # Config root dir: PLGARGS || OPTS || ORIG || DEFS
162 my $root = $args->{root} || $app->getopt("config_root") || $app->getopt("configroot") || $app->root;
163  
164 # Additional config directories: PLGARGS || DEFS
165 my $dirs = as_array_ref($args->{dirs});
166  
167 # Create instance
168 my $config = Acrux::Config->new(
169 file => $file,
170 options => $options,
171 noload => $noload,
172 defaults => $defaults,
173 root => $root,
174 dirs => $dirs,
175 );
176 if (my $err = $config->error) {
177 if ($app->debugmode) {
178 $app->verbosemode
179 ? warn qq{Can't load configuration file "$file"\n$err\n}
180 : warn qq{Can't load configuration file "$file"\n};
181 }
182 }
183  
184 # Set conf and config helpers (methods)
185 $app->register_method(config => sub { $config });
186 $app->register_method(conf => sub { $config });
187  
188 return $config;
189 }
190  
191 1;
192  
193 __END__