File Coverage

blib/lib/CGI/Application/Plugin/Config/Std.pm
Criterion Covered Total %
statement 7 9 77.7
branch n/a
condition n/a
subroutine 3 3 100.0
pod n/a
total 10 12 83.3


line stmt bran cond sub pod time code
1             package CGI::Application::Plugin::Config::Std;
2 1     1   1190 use common::sense 3.4;
  1         29  
  1         9  
3              
4 1     1   5342 use CGI::Application;
  1         12860  
  1         46  
5 1     1   656 use Config::Std;
  0            
  0            
6              
7             use Hash::Merge 0.12 qw(merge);
8              
9             use Sub::Exporter 0.982
10             -setup => { exports => [qw(config_file config_param config)] };
11              
12             Hash::Merge::set_behavior('RIGHT_PRECEDENT');
13              
14             # Version set by dist.ini. Do not change here.
15             our $VERSION = '1.003'; # VERSION
16              
17              
18             # Recursively search the config hash for 'foo.bar' style params.
19             sub _find_keys {
20             my ($c,@keys) = @_;
21              
22             my $k = shift(@keys);
23            
24             if(exists($c->{$k})) {
25             if(@keys) {
26             _find_keys( $c->{$k}, @keys);
27             }
28             else {
29             return $c->{$k};
30             }
31             }
32             else {
33             return;
34             }
35             }
36              
37             sub config_param {
38             my $self = shift;
39             my @params = @_;
40              
41             my $conf = $self->config();
42             #if there aren't any params then we want the entire config structure as a hash ref
43             if(scalar(@params) == 0) {
44             return $conf;
45             }
46             elsif(scalar(@params) == 1) {
47             #if there is just one then we want just that value
48             my @keys = split(/\./,$params[0]);
49             return _find_keys($conf,@keys);
50             }
51             #else we might be setting some values
52             else {
53             my %params = (@params);
54             my %addconf;
55             for my $k (keys %params) {
56             my(@keys) = split(/\./,$k);
57             my $last = pop(@keys);
58             if(@keys) {
59             $addconf{$keys[0]} = {$last => $params{$k} } ;
60             }
61             else {
62             $addconf{$last} = $params{$k};
63             }
64             }
65             my $c = merge( $conf, \%addconf );
66             $self->{__CONFIG_STD}->{__CONFIG_OBJ} = $c; #Ugh
67             }
68             }
69              
70              
71             sub commit_config {
72             my $self = shift;
73              
74             my $conf = $self->config;
75             write_config( $conf, $self->config_file );
76             return $conf;
77             }
78              
79            
80             sub config {
81             my $self = shift;
82             #if we don't already have a config object or if the file name has changed on us then create it
83             my $create = !$self->{__CONFIG_STD}->{__CONFIG_OBJ}
84             || $self->{__CONFIG_STD}->{__FILE_CHANGED};
85             if($create) {
86             #get the file name from config_file()
87             my $file_name = $self->config_file or die "No config file specified!";
88             read_config( $file_name, my %conf );
89             $self->{__CONFIG_STD}->{__CONFIG_OBJ} = \%conf;
90             $self->{__CONFIG_STD}->{__FILE_CHANGED} = 0;
91             }
92             return $self->{__CONFIG_STD}->{__CONFIG_OBJ};
93             }
94            
95            
96             sub config_file {
97             my ($self, $file_name) = @_;
98             #if we have a file name to set
99             if(defined $file_name) {
100             $self->{__CONFIG_STD}->{__FILE_NAME} = $file_name;
101             $self->{__CONFIG_STD}->{__FILE_CHANGED} = 1;
102             } else {
103             #else we are getting the filename
104             $file_name = $self->{__CONFIG_STD}->{__FILE_NAME}
105             }
106             #if we don't have the file_name then get it from %ENV, but untaint it
107             if(!$file_name) {
108             $ENV{CGIAPP_CONFIG_FILE} =~ /(.*)/;
109             $file_name = $1;
110             }
111             return $file_name;
112             }
113              
114             1;
115              
116             # ABSTRACT: Add Config::Std support to CGI::Application
117              
118              
119              
120             =pod
121              
122             =head1 NAME
123              
124             CGI::Application::Plugin::Config::Std - Add Config::Std support to CGI::Application
125              
126             =head1 VERSION
127              
128             version 1.003
129              
130             =head1 SYNOPSIS
131              
132             in your CGI::Application-based module
133              
134             use CGI::Application::Plugin::Config::Std;
135              
136             sub cgiapp_init {
137             my $self = shift;
138             #set my config file
139             $self->config_file('myapp.conf');
140              
141             #
142             #do other stuff
143             #
144             }
145              
146             #later on in a run mode
147             sub run_mode1 {
148             my $self = shift;
149              
150             #just get a single parameter from my config file
151             my $value = $self->config_param('my_param');
152              
153             #get a parameter in a block (if using ini style files)
154             $value = $self->config_param('my_block.my_param');
155              
156             #the entire config hash reference
157             my $config_vars = $self->config_param();
158              
159             #get my Config::Simple object for direct access
160             my $config = $self->config;
161             }
162              
163             =head1 DESCRIPTION
164              
165             This module acts as a plugin for L<Config::Std> to be used within a
166             L<CGI::Application> module.
167              
168             Three methods are exported into your L<CGI::Application> module and they
169             are described below.
170              
171             This module borrows the lazy loading idea from Cees Hek's
172             L<CGI::Application::Plugin::Session> module. Much of the code and tests are
173             borrowed from L<CGI::Application::Plugin:::Config::Simple> by Michael Peters.
174             The three-signature behaviour of config() is also borrowed from Michael's
175             implementation.
176              
177             This module is hosted on github:
178             L<https://github.com/stephenca/CGI-Application-Plugin-Config-Std>.
179              
180             =head1 NAME
181              
182             CGI::Application::Plugin::Config::Std - Add Config::Std support to CGI::Application
183              
184             =head1 METHODS
185              
186             =head2 config_param()
187              
188             This method acts as an accessor/mutator for configuration variables coming from the
189             configuration file.
190              
191             This method will behave in three different ways depending on how many parameters it
192             is passed:
193              
194             - zero parameters: Config::Std::Hash object returned.
195             - one parameters: assumed to be config lookup. Will return value associated
196             with parameter, or undef if none exists. Note that 'dot' notation parameters
197             are supported, e.g. $self->config_param('foo.bar') will be translated to
198             something like $conf->{foo}{bar}.
199             - more than 1 parameter: treated as name/value pairs. Returns true if successful. The same 'dot notation'
200             is supported as per a single paremeter. Existing config params will be
201             over-written by this form of the method call.
202            
203             #get the complete config object. This is the same as calling
204             #$self->config().
205             my $config_hash = $self->config_param();
206             #just get one config value
207             my $value = $self->config_param($parameter);
208             #set multiple config values
209             my $success = $self->config_param(param1 => $value1, param2 => $value2);
210              
211             Failing to set the name of the configuration file either using the L<config_file()> method
212             or the CGIAPP_CONFIG_FILE environment variable before calling this method it
213             will generate a fatal exception.
214              
215             =head2 commit_config
216              
217             This method writes the current contents of the configuration object back to
218             the config file (possibly a different one to that from which the config was
219             read).
220              
221             Returns the current configuration object on success. A fatal exception is
222             raised if the write fails.
223              
224             This method is potentially dangerous, so is not exported by default.
225              
226             =head2 config()
227              
228             This method will return the underlying Config::Std object for more direct use by your
229             application.
230              
231             Failing to set the name of the configuration file either using the L<config_file()> method
232             or the CGIAPP_CONFIG_FILE environment variable before calling this method or
233             it raise a fatal exception.
234              
235             my $conf = $self->config();
236              
237             =head2 config_file([$file_name])
238              
239             Get/set the name of the current config file or change/initialize it.
240              
241             This method must be called to initialize the name of the config file before
242             any call can be made to either L<config()> or L<config_param()> unless the
243             'CGIAPP_CONFIG_FILE' environment variable has been set.
244              
245             If this environment variable is set it will be used as the initial value of
246             the config file. This is useful if we are running in a mod_perl environment
247             when can use a statement like this in your httpd.conf file:
248              
249             PerlSetEnv CGIAPP_CONFIG_FILE /path/to/my/conf
250              
251             It is typical to set the name of the config file in the cgiapp_init() phase of your application.
252              
253             If a value is passed as a parameter then the config file with that name is used. It will always
254             return the name of the current config file.
255              
256             #get the value of the CGIAPP_CONFIG_FILE environment variable (if there is one)
257             #since we haven't set the config file's name with config_file() yet.
258             my $file_name = $self->config_file();
259            
260             #set the config file's name
261             $self->config_file('myapp.conf');
262            
263             #get the name of the config file
264             $file_name = $self->config_file();
265              
266             =head1 CAVEATS
267              
268             The CGI::Application object is implemented as a hash and we store the variables used by this
269             module's methods inside of it as a hash named __CONFIG_STD. If you use any other CGI::Application
270             plugins there would be problems if they also used $self->{__CONFIG_STD} but in practice this should
271             never actually happen.
272              
273             =head1 ACKNOWLEDGEMENTS
274              
275             The implementation, tests and documentation are heavily based on Michael
276             Peters' L<CGI::Application::Plugin::Config::Simple>.
277              
278             =head1 SEE ALSO
279              
280             =over 8
281              
282             =item * L<CGI::Application>
283              
284             =item * L<CGI::Application::Plugin::Config::Simple>
285              
286             =item * L<Config::Std>
287              
288             =back
289              
290             =head1 BUGS
291              
292             Please use github for bug reports:
293             L<https://github.com/stephenca/CGI-Application-Plugin-Config-Std/issues>
294              
295             =head1 AUTHOR
296              
297             Stephen Cardie <stephenca@cpan.org>
298              
299             =head1 COPYRIGHT AND LICENSE
300              
301             This software is copyright (c) 2011 by Stephen Cardie.
302              
303             This is free software; you can redistribute it and/or modify it under
304             the same terms as the Perl 5 programming language system itself.
305              
306             =cut
307              
308              
309             __END__
310              
311