Catalyst::Plugin::Config::JSON - Configure your Catalyst application via an external


Catalyst-Plugin-Config-JSON documentation Contained in the Catalyst-Plugin-Config-JSON distribution.

Index


Code Index:

NAME

Top

Catalyst::Plugin::Config::JSON - Configure your Catalyst application via an external JSON file

SYNOPSIS

Top

    use Catalyst 'Config::JSON';

    __PACKAGE__->config('config_file' => 'config.json');

DESCRIPTION

Top

This Catalyst plugin enables you to configure your Catalyst application with an external JSON file instead of somewhere in your application code.

This is useful for example if you want to quickly change the configuration for different deployment environments (like development, testing or production) without changing your code.

The configuration file is assumed to be in your application home. Its name can be specified with the config parameter config_file (default is config.json).

For any keys in the configuration file that start with Catalyst::, the corresponding value is taken as the configuration for that class.

EXTENDED METHODS

setup

SEE ALSO

Top

Catalyst, JSON.

AUTHOR

Top

Catalyst::Plugin::Config::YAML by Bernhard Bauer, <bauerb@in.tum.de>

Tweaked for JSON by Sam Vilain, <samv@cpan.org>

COPYRIGHT AND LICENSE

Top


Catalyst-Plugin-Config-JSON documentation Contained in the Catalyst-Plugin-Config-JSON distribution.
package Catalyst::Plugin::Config::JSON;

use strict;
use warnings;

use UNIVERSAL 'isa';

use NEXT;
use JSON;
use Path::Class 'file';

our $VERSION = '0.03';

sub setup {
	my $c = shift;
	my $config_file = $c->config->{'config_file'} || 'config.json';
	$config_file = file($c->config->{'home'}, $config_file) unless file($config_file)->is_absolute;
	open CONFIG, "<$config_file"
	    or die "failed to open $config_file for reading; $!";
	local($/)=undef;
	my $config = <CONFIG>;
	close CONFIG;
	my $options = jsonToObj($config);
	foreach my $key (keys %$options) {
		if (isa($key, 'Catalyst::Base')) {
			$key->config(delete $options->{$key});
		}
	}
	$c->config($options);
	$c->NEXT::setup;
}