removed useless use of grep in void context stuff, version bump
[catagits/Catalyst-Plugin-ConfigLoader-Environment.git] / lib / Catalyst / Plugin / ConfigLoader / Environment.pm
CommitLineData
0580e0f8 1package Catalyst::Plugin::ConfigLoader::Environment;
2
3use warnings;
4use strict;
5use JSON::Any;
37564e20 6use MRO::Compat;
0580e0f8 7=head1 NAME
8
9Catalyst::Plugin::ConfigLoader::Environment - Configure your
10application with environment variables.
11
12=head1 VERSION
13
14Version 0.05
15
16=cut
17
93a9a442 18our $VERSION = '0.07';
0580e0f8 19
20=head1 SYNOPSIS
21
22Catalyst::Plugin::ConfigLoader::Environment reads environment
23variables and sets up the configuration in your application
24accordingly.
25
26Here's how you use it:
27
28 package MyApp;
29 use Catalyst qw(... ConfigLoader::Environment ...);
30 MyApp->setup;
31
32Then, before you run your application, set some environment
33variables:
34
35 export MYAPP_foo='Hello, world!'
36 export MYAPP_bar="foobar"
37 perl script/myapp_server.pl
38
39Inside your application, C<< $c->config->{foo} >> will be equal to
40C<Hello, world!>, and C<< $c->config->{bar} >> will be equal to
41C<foobar>.
42
43=head2 Compatibility with ConfigLoader
44
45You can use both ConfigLoader and this module in the same application.
46If you specify C<ConfigLoader> before C<ConfigLoader::Environment> in
47the import list to Catalyst, the environment will override any
48configuration files that ConfigLoader may find. This is the
49recommended setup.
50
51You can reverse the order in the import list if you want static config
52files to override the environment, but that's not recommended.
53
54=head1 DETAILS
55
56Here's exactly how environment variables are translated into
57configuration.
58
59First, your application's name is converted to ALL CAPS, any colons
60are converted to underscores (i.e. C<My::App> is translated to
61C<MY_APP>), and a C<_> is appended. Then, any environment variables
62not starting with this prefix are discarded.
63
64The prefix is then stripped, and the remaining variables are then
65converted to elements in the C<config> hash as follows.
66
67Variables starting with C<Model::>, C<View::>, or C<Controller::> and
68then any character other than C<_> are treated as configuration
69options for the corresponding component of your application. The
70prefix is saved as C<prefix> and everything after the first C<_> is
71used as a key into the C<< $c->config->{"prefix"} >> hash.
72
73Any other variables not starting with a special prefix are added
74directly to the C<< $c->config >> hash.
75
76=head1 EXAMPLES
77
78Let's translate a YAML config file:
79
80 ---
81 name: MyApp
82 title: This is My App!
83 View::Foo:
84 EXTENSION: tt
85 EVAL_PERL: 1
86 Model::Bar:
87 root: "/etc"
88 Model::DBIC:
89 connect_info: [ "dbi:Pg:dbname=foo", "username", "password" ]
90
91into environment variables that would setup the same configuration:
92
93 MYAPP_name=MyApp
94 MYAPP_title=This is My App!
95 MYAPP_View__Foo_EXTENSION=tt
96 MYAPP_View__Foo_EVAL_PERL=1
97 MYAPP_Model__Bar_root=/etc
98 MYAPP_Model__DBIC_connect_info=["dbi:Pg:dbname=foo", "username", "password"]
99
100Double colons are converted into double underscores. For
101compatibility's sake, support for the 0.01-style use of
102bourne-incompatible variable names is retained.
103
104Values are JSON-decoded if they look like JSON arrays or objects
105(i.e. if they're enclosed in []s or {}s). Taking advantage of that, we
106can write the same example this way:
107
108 MYAPP_name=MyApp
109 MYAPP_title=This is My App!
110 MYAPP_View__Foo={"EXTENSION":"tt","EVAL_PERL":1}
111 MYAPP_Model__Bar={"root":"/etc"}
112 MYAPP_Model__DBIC={"connect_info":["dbi:Pg:dbname=foo", "username", "password"]}
113
114=head1 FUNCTIONS
115
116=head2 setup
117
118Overriding Catalyst' setup routine.
119
120=cut
121
122sub setup {
37564e20 123 my $c = shift;
0580e0f8 124 my $prefix = Catalyst::Utils::class2env($c);
125 my %env;
93a9a442 126 for (keys %ENV) {
127 m/^${prefix}[_](.+)$/ and $env{$1} = $ENV{$_};
128 }
0580e0f8 129
130 foreach my $var (keys %env) {
131 my $val = $env{$var};
132
133 # Decode JSON array/object
134 if ( $val =~ m{^\[.*\]$|^\{.*\}$} ) {
135 $val = JSON::Any->jsonToObj($val);
136 }
137
138 # Special syntax Model__Foo is equivalent to Model::Foo
139 if($var =~ /(Model|View|Controller)(?:::|__)([^_]+)(?:_(.+))?$/) {
140 $var = "${1}::$2";
141
142 # Special syntax Model__Foo_bar (or Model::Foo_bar) will
143 # tweak just the 'bar' subparam for Model::Foo's
144 # config. We can accomplish this using a hash that
145 # specifies just 'bar' ($c->config will merge hashes).
146 if ( defined $3 ) {
147 $val = { $3 => $val };
148 }
149 }
150
151 $c->config( $var => $val );
152 }
153
37564e20 154 return $c->maybe::next::method(@_);
0580e0f8 155}
156
157
158=head1 AUTHOR
159
160Jonathan Rockway, C<< <jrockway at cpan.org> >>
161
162=head1 CONTRIBUTORS
163
164mugwump
165
166Ryan D Johnson, C<< <ryan at innerfence.com> >>
167
ab3db365 168Devin J. Austin C<< <dhoss@cpan.org> >>
169
0580e0f8 170=head1 BUGS
171
172Please report any bugs or feature requests to
173C<bug-catalyst-plugin-configloader-environment at rt.cpan.org>, or through the web interface at
174L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Catalyst-Plugin-ConfigLoader-Environment>.
175I will be notified, and then you'll automatically be notified of progress on
176your bug as I make changes.
177
178=head1 SUPPORT
179
180You can find documentation for this module with the perldoc command.
181
182 perldoc Catalyst::Plugin::ConfigLoader::Environment
183
184You can also look for information at:
185
186=over 4
187
188=item * Catalyst Mailing List
189
190L<mailto:catalyst@lists.rawmode.org>.
191
192=item * RT: CPAN's request tracker
193
194L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Catalyst-Plugin-ConfigLoader-Environment>
195
196=back
197
198=head1 COPYRIGHT & LICENSE
199
200Copyright 2006 Jonathan Rockway, all rights reserved.
201
202This program is free software; you can redistribute it and/or modify it
203under the same terms as Perl itself.
204
205If you'd like to use it under a different license, that's probably OK.
206Please contact the author.
207
208=cut
209
2101; # End of Catalyst::Plugin::ConfigLoader::Environment