Catalyst::Plugin::ConfigLoader::Environment - Configure your


Catalyst-Plugin-ConfigLoader-Environment documentation Contained in the Catalyst-Plugin-ConfigLoader-Environment distribution.

Index


Code Index:

NAME

Top

Catalyst::Plugin::ConfigLoader::Environment - Configure your application with environment variables.

VERSION

Top

Version 0.05

SYNOPSIS

Top

Catalyst::Plugin::ConfigLoader::Environment reads environment variables and sets up the configuration in your application accordingly.

Here's how you use it:

    package MyApp;
    use Catalyst qw(... ConfigLoader::Environment ...);
    MyApp->setup;

Then, before you run your application, set some environment variables:

    export MYAPP_foo='Hello, world!'
    export MYAPP_bar="foobar"
    perl script/myapp_server.pl

Inside your application, $c->config->{foo} will be equal to Hello, world!, and $c->config->{bar} will be equal to foobar.

Compatibility with ConfigLoader

You can use both ConfigLoader and this module in the same application. If you specify ConfigLoader before ConfigLoader::Environment in the import list to Catalyst, the environment will override any configuration files that ConfigLoader may find. This is the recommended setup.

You can reverse the order in the import list if you want static config files to override the environment, but that's not recommended.

DETAILS

Top

Here's exactly how environment variables are translated into configuration.

First, your application's name is converted to ALL CAPS, any colons are converted to underscores (i.e. My::App is translated to MY_APP), and a _ is appended. Then, any environment variables not starting with this prefix are discarded.

The prefix is then stripped, and the remaining variables are then converted to elements in the config hash as follows.

Variables starting with Model::, View::, or Controller:: and then any character other than _ are treated as configuration options for the corresponding component of your application. The prefix is saved as prefix and everything after the first _ is used as a key into the $c->config->{"prefix"} hash.

Any other variables not starting with a special prefix are added directly to the $c->config hash.

EXAMPLES

Top

Let's translate a YAML config file:

    ---
    name: MyApp
    title: This is My App!
    View::Foo:
      EXTENSION: tt
      EVAL_PERL: 1
    Model::Bar:
      root: "/etc"
    Model::DBIC:
      connect_info: [ "dbi:Pg:dbname=foo", "username", "password" ]

into environment variables that would setup the same configuration:

    MYAPP_name=MyApp
    MYAPP_title=This is My App!
    MYAPP_View__Foo_EXTENSION=tt
    MYAPP_View__Foo_EVAL_PERL=1
    MYAPP_Model__Bar_root=/etc
    MYAPP_Model__DBIC_connect_info=["dbi:Pg:dbname=foo", "username", "password"]

Double colons are converted into double underscores. For compatibility's sake, support for the 0.01-style use of bourne-incompatible variable names is retained.

Values are JSON-decoded if they look like JSON arrays or objects (i.e. if they're enclosed in []s or {}s). Taking advantage of that, we can write the same example this way:

    MYAPP_name=MyApp
    MYAPP_title=This is My App!
    MYAPP_View__Foo={"EXTENSION":"tt","EVAL_PERL":1}
    MYAPP_Model__Bar={"root":"/etc"}
    MYAPP_Model__DBIC={"connect_info":["dbi:Pg:dbname=foo", "username", "password"]}

FUNCTIONS

Top

setup

Overriding Catalyst' setup routine.

AUTHOR

Top

Jonathan Rockway, <jrockway at cpan.org>

CONTRIBUTORS

Top

mugwump

Ryan D Johnson, <ryan at innerfence.com>

Devin J. Austin <dhoss@cpan.org>

BUGS

Top

Please report any bugs or feature requests to bug-catalyst-plugin-configloader-environment at rt.cpan.org, or through the web interface at http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Catalyst-Plugin-ConfigLoader-Environment. I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.

SUPPORT

Top

You can find documentation for this module with the perldoc command.

    perldoc Catalyst::Plugin::ConfigLoader::Environment

You can also look for information at:

* Catalyst Mailing List

mailto:catalyst@lists.rawmode.org.

* RT: CPAN's request tracker

http://rt.cpan.org/NoAuth/Bugs.html?Dist=Catalyst-Plugin-ConfigLoader-Environment

COPYRIGHT & LICENSE

Top


Catalyst-Plugin-ConfigLoader-Environment documentation Contained in the Catalyst-Plugin-ConfigLoader-Environment distribution.
package Catalyst::Plugin::ConfigLoader::Environment;

use warnings;
use strict;
use JSON::Any;
use MRO::Compat;
our $VERSION = '0.07';

sub setup {
    my $c    = shift;
    my $prefix = Catalyst::Utils::class2env($c);
    my %env;
    for (keys %ENV) { 
        m/^${prefix}[_](.+)$/ and $env{$1} = $ENV{$_}; 
    }

    foreach my $var (keys %env) {
        my $val = $env{$var};

        # Decode JSON array/object 
        if ( $val =~ m{^\[.*\]$|^\{.*\}$} ) {
            $val = JSON::Any->jsonToObj($val);
        }

        # Special syntax Model__Foo is equivalent to Model::Foo
        if($var =~ /(Model|View|Controller)(?:::|__)([^_]+)(?:_(.+))?$/) {
            $var = "${1}::$2";

            # Special syntax Model__Foo_bar (or Model::Foo_bar) will
            # tweak just the 'bar' subparam for Model::Foo's
            # config. We can accomplish this using a hash that
            # specifies just 'bar' ($c->config will merge hashes).
            if ( defined $3 ) {
                $val = { $3 => $val };
            }
        }

        $c->config( $var => $val );
    }
    
    return $c->maybe::next::method(@_);
}


1; # End of Catalyst::Plugin::ConfigLoader::Environment