permissions crap
[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
37564e20 18our $VERSION = '0.06';
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;
126 grep { /^${prefix}[_](.+)$/ && ($env{$1}=$ENV{$_})} keys %ENV;
127
128 foreach my $var (keys %env) {
129 my $val = $env{$var};
130
131 # Decode JSON array/object
132 if ( $val =~ m{^\[.*\]$|^\{.*\}$} ) {
133 $val = JSON::Any->jsonToObj($val);
134 }
135
136 # Special syntax Model__Foo is equivalent to Model::Foo
137 if($var =~ /(Model|View|Controller)(?:::|__)([^_]+)(?:_(.+))?$/) {
138 $var = "${1}::$2";
139
140 # Special syntax Model__Foo_bar (or Model::Foo_bar) will
141 # tweak just the 'bar' subparam for Model::Foo's
142 # config. We can accomplish this using a hash that
143 # specifies just 'bar' ($c->config will merge hashes).
144 if ( defined $3 ) {
145 $val = { $3 => $val };
146 }
147 }
148
149 $c->config( $var => $val );
150 }
151
37564e20 152 return $c->maybe::next::method(@_);
0580e0f8 153}
154
155
156=head1 AUTHOR
157
158Jonathan Rockway, C<< <jrockway at cpan.org> >>
159
160=head1 CONTRIBUTORS
161
162mugwump
163
164Ryan D Johnson, C<< <ryan at innerfence.com> >>
165
ab3db365 166Devin J. Austin C<< <dhoss@cpan.org> >>
167
0580e0f8 168=head1 BUGS
169
170Please report any bugs or feature requests to
171C<bug-catalyst-plugin-configloader-environment at rt.cpan.org>, or through the web interface at
172L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Catalyst-Plugin-ConfigLoader-Environment>.
173I will be notified, and then you'll automatically be notified of progress on
174your bug as I make changes.
175
176=head1 SUPPORT
177
178You can find documentation for this module with the perldoc command.
179
180 perldoc Catalyst::Plugin::ConfigLoader::Environment
181
182You can also look for information at:
183
184=over 4
185
186=item * Catalyst Mailing List
187
188L<mailto:catalyst@lists.rawmode.org>.
189
190=item * RT: CPAN's request tracker
191
192L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Catalyst-Plugin-ConfigLoader-Environment>
193
194=back
195
196=head1 COPYRIGHT & LICENSE
197
198Copyright 2006 Jonathan Rockway, all rights reserved.
199
200This program is free software; you can redistribute it and/or modify it
201under the same terms as Perl itself.
202
203If you'd like to use it under a different license, that's probably OK.
204Please contact the author.
205
206=cut
207
2081; # End of Catalyst::Plugin::ConfigLoader::Environment