Config-JFDI-0.065000755000765000024 011507723245 12517 5ustar00robstaff000000000000README000644000765000024 2042311507723245 13500 0ustar00robstaff000000000000Config-JFDI-0.065NAME Config::JFDI - Just * Do it: A Catalyst::Plugin::ConfigLoader-style layer over Config::Any VERSION version 0.065 DESCRIPTION Config::JFDI is an implementation of Catalyst::Plugin::ConfigLoader that exists outside of Catalyst. Essentially, Config::JFDI will scan a directory for files matching a certain name. If such a file is found which also matches an extension that Config::Any can read, then the configuration from that file will be loaded. Config::JFDI will also look for special files that end with a "_local" suffix. Files with this special suffix will take precedence over any other existing configuration file, if any. The precedence takes place by merging the local configuration with the "standard" configuration via Hash::Merge::Simple. Finally, you can override/modify the path search from outside your application, by setting the _CONFIG variable outside your application (where is the uppercase version of what you passed to Config::JFDI->new). SYNPOSIS use Config::JFDI; my $config = Config::JFDI->new(name => "my_application", path => "path/to/my/application"); my $config_hash = $config->get; This will look for something like (depending on what Config::Any will find): path/to/my/application/my_application_local.{yml,yaml,cnf,conf,jsn,json,...} AND path/to/my/application/my_application.{yml,yaml,cnf,conf,jsn,json,...} ... and load the found configuration information appropiately, with _local taking precedence. You can also specify a file directly: my $config = Config::JFDI->new(file => "/path/to/my/application/my_application.cnf"); To later reload your configuration, fresh from disk: $config->reload; Config::Loader We are currently kicking around ideas for a next-generation configuration loader. The goals are: * A universal platform for configuration slurping and post-processing * Use Config::Any to do configuration loading * A sane API so that developers can roll their own loader according to the needs of their application * A friendly interface so that users can have it just DWIM * Host/application/instance specific configuration via _local and %ENV Find more information and contribute at: Roadmap: Mailing list: Behavior change of the 'file' parameter in 0.06 In previous versions, Config::JFDI would treat the file parameter as a path parameter, stripping off the extension (ignoring it) and globbing what remained against all the extensions that Config::Any could provide. That is, it would do this: Config::JFDI->new( file => 'xyzzy.cnf' ); # Transform 'xyzzy.cnf' into 'xyzzy.pl', 'xyzzy.yaml', 'xyzzy_local.pl', ... (depending on what Config::Any could parse) This is probably not what people intended. Config::JFDI will now squeak a warning if you pass 'file' through, but you can suppress the warning with 'no_06_warning' or 'quiet_deprecation' Config::JFDI->new( file => 'xyzzy.cnf', no_06_warning => 1 ); Config::JFDI->new( file => 'xyzzy.cnf', quiet_deprecation => 1 ); # More general If you *do* want the original behavior, simply pass in the file parameter as the path parameter instead: Config::JFDI->new( path => 'xyzzy.cnf' ); # Will work as before METHODS $config = Config::JFDI->new(...) You can configure the $config object by passing the following to new: name The name specifying the prefix of the configuration file to look for and the ENV variable to read. This can be a package name. In any case, :: will be substituted with _ in and the result will be lowercased. To prevent modification of , pass it in as a scalar reference. path The directory to search in file Directly read the configuration from this file. Config::Any must recognize the extension. Setting this will override path no_local Disable lookup of a local configuration. The 'local_suffix' option will be ignored. Off by default local_suffix The suffix to match when looking for a local configuration. "local" By default ("config_local_suffix" will also work so as to be drop-in compatible with C::P::CL) no_env Set this to 1 to disregard anything in the ENV. The 'env_lookup' option will be ignored. Off by default env_lookup Additional ENV to check if $ENV{...} is not found driver A hash consisting of Config:: driver information. This is passed directly through to Config::Any install_accessor Set this to 1 to install a Catalyst-style accessor as ::config You can also specify the package name directly by setting install_accessor to it (e.g. install_accessor => "My::Application") substitute A hash consisting of subroutines called during the substitution phase of configuration preparation. ("substitutions" will also work so as to be drop-in compatible with C::P::CL) A substitution subroutine has the following signature: ($config, [ $argument1, $argument2, ... ]) path_to The path to dir to use for the __path_to(...)__ substitution. If nothing is given, then the 'home' config value will be used ($config->get->{home}). Failing that, the current directory will be used. default A hash filled with default keys/values Returns a new Config::JFDI object $config_hash = Config::JFDI->open( ... ) As an alternative way to load a config, ->open will pass given arguments to ->new( ... ), then attempt to do ->load Unlike ->get or ->load, if no configuration files are found, ->open will return undef (or the empty list) This is so you can do something like: my $config_hash = Config::JFDI->open( "/path/to/application.cnf" ) or croak "Couldn't find config file!" In scalar context, ->open will return the config hash, NOT the config object. If you want the config object, call ->open in list context: my ($config_hash, $config) = Config::JFDI->open( ... ) You can pass any arguments to ->open that you would to ->new $config->get $config->config $config->load Load a config as specified by ->new( ... ) and ENV and return a hash These will only load the configuration once, so it's safe to call them multiple times without incurring any loading-time penalty $config->found Returns a list of files found If the list is empty, then no files were loaded/read $config->clone Return a clone of the configuration hash using Clone This will load the configuration first, if it hasn't already $config->reload Reload the configuration, examining ENV and scanning the path anew Returns a hash of the configuration $config->substitute( , , ... ) For each given , if looks like a substitution specification, then run the substitution macro on and store the result. There are three default substitutions (the same as Catalyst::Plugin::ConfigLoader) * "__HOME__" - replaced with "$c->path_to('')" * "__path_to(foo/bar)__" - replaced with "$c->path_to('foo/bar')" * "__literal(__FOO__)__" - leaves __FOO__ alone (allows you to use "__DATA__" as a config value, for example) The parameter list is split on comma (","). You can define your own substitutions by supplying the substitute option to ->new SEE ALSO Catalyst::Plugin::ConfigLoader Config::Any Catalyst Config::Merge Config::General AUTHOR Robert Krimen COPYRIGHT AND LICENSE This software is copyright (c) 2011 by Robert Krimen. This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself. Changes000644000765000024 457011507723245 14100 0ustar00robstaff000000000000Config-JFDI-0.065TODO: - What if: Config::JFDI->new() # $name is undefined? 0.065 Saturday January 01 14:10:43 PST 2011: - Release because of mis-version 0.065 Friday December 31 14:28:21 PST 2010 - Non-development release 0.064_2 Wednesday December 29 13:43:11 PST 2010: - Conversion to Any::Moose - Drop unneeded deprecation-warning test 0.064_1 Tuesday December 28 17:11:22 PST 2010: - Fixed bug rt53886 (thanks mzrinsky & RUSSELJ) - Add Config::General as a prerequisite because it's so useful - Using Dist::Zilla for release process 0.064 Sunday October 18 21:33:52 PDT 2009: - Non-development release 0.063_3 Friday October 16 00:51:31 PDT 2009: - Merged _2 & _1 with 0.063 0.063_2 Thursday October 15 13:30:27 PDT 2009 - Added mechanism to quiet deprecation warnings in a non-author environment 0.063_1 Wednesday October 14 11:32:06 PDT 2009 - Fixed broken __path_to__ substitution (rt50490) 0.063 Tuesday June 23 11:10:15 PDT 2009: - Fixed undefined warning (thanks kmx rt47237) - Added note about Config::Loader 0.062 Friday May 29 09:54:22 PDT 2009: - Release because of bad testing environment 0.061 Thursday May 28 15:21:43 PDT 2009: - Fix for path extension issue, thanks minty (http://github.com/minty) 0.060 Wednesday May 20 14:44:51 PDT 2009: - Non-developer release - Fixed tests in 17-found.t to use bag() instead of [], since we can't depend on Config::Any to return loaded files in a set order (srezic++) 0.050_2 Thursday May 14 13:27:31 PDT 2009: - Forgot some dependencies in Makefile.PL 0.050_1 Wednesday May 13 19:36:19 PDT 2009: - Fixed t/14-loader-env-lookup.t to ignore external ENV (rt45624) - Require Data::Visitor 0.24 (rt45624) - Changed how the 'file' option to work how the user actually expects it to (and how it is in the SYNOPSIS) - Added ->found - Added ->open 0.050 Wednesday March 18 12:36:57 PDT 2009: - Modified tests to pass on Windows using Path::Class (thanks Dan Dascalescu) 0.040 Thursday October 09 16:06:20 PDT 2008: - Added substitution handling - Added default option 0.030 Tuesday April 29 11:49:21 PDT 2008: - Fixed issue with install_accessor ignoring given package name 0.020 Monday April 28 19:51:17 PDT 2008: - Added env_lookup support - Added install_accessor for Caelum 0.010 Wednesday April 23 14:19:05 PDT 2008: - Initial release META.yml000644000765000024 126111507723245 14050 0ustar00robstaff000000000000Config-JFDI-0.065--- abstract: 'Just * Do it: A Catalyst::Plugin::ConfigLoader-style layer over Config::Any' author: - 'Robert Krimen ' build_requires: Test::Most: 0 configure_requires: ExtUtils::MakeMaker: 6.31 dynamic_config: 0 generated_by: 'Dist::Zilla version 4.102345, CPAN::Meta::Converter version 2.102400' license: perl meta-spec: url: http://module-build.sourceforge.net/META-spec-v1.4.html version: 1.4 name: Config-JFDI requires: Any::Moose: 0 Carp::Clan::Share: 0 Clone: 0 Config::Any: 0 Config::General: 0 Data::Visitor: 0.24 Getopt::Usaginator: 0 Hash::Merge::Simple: 0 List::MoreUtils: 0 Path::Class: 0 Sub::Install: 0 version: 0.065 MANIFEST000644000765000024 157311507723245 13736 0ustar00robstaff000000000000Config-JFDI-0.065Changes MANIFEST META.yml Makefile.PL README lib/Config/JFDI.pm lib/Config/JFDI/Carp.pm lib/Config/JFDI/Source/Loader.pm t/10-loader.t t/11-loader-env.t t/12-loader-file.t t/13-loader-no-env.t t/14-loader-env-lookup.t t/15-install-accessor.t t/16-loader-order.t t/17-found.t t/18-open.t t/20-substitute.t t/21-path-to.t t/22-default.t t/9bug-rt47237-path-undefined.t t/9bug-rt53886.t t/9bug-taking-extension-from-path.t t/Test.pm t/assets/default.pl t/assets/dir.cnf/keep t/assets/dotdot.conf t/assets/order/xyzzy.cnf t/assets/order/xyzzy.conf t/assets/order/xyzzy.perl t/assets/order/xyzzy.pl t/assets/order/xyzzy_local.cnf t/assets/order/xyzzy_local.conf t/assets/order/xyzzy_local.perl t/assets/order/xyzzy_local.pl t/assets/path_to.pl t/assets/some_random_file.pl t/assets/substitute-path-to.pl t/assets/substitute.pl t/assets/xyzzy.pl t/assets/xyzzy_catalyst.pl t/assets/xyzzy_local.pl t000755000765000024 011507723245 12703 5ustar00robstaff000000000000Config-JFDI-0.065Test.pm000644000765000024 6011507723245 14253 0ustar00robstaff000000000000Config-JFDI-0.065/tpackage t::Test; use strict; use warnings; 1; 18-open.t000644000765000024 160311507723245 14416 0ustar00robstaff000000000000Config-JFDI-0.065/tuse strict; use warnings; use Test::Most; plan qw/no_plan/; use Config::JFDI; use t::Test; sub has_Config_General { return eval "require Config::General;"; } { my $config = Config::JFDI->open( 't/assets/some_random_file.pl' ); ok( $config ); ok( keys %{ $config } ); } { my $config = Config::JFDI->open( qw{ name xyzzy path t/assets } ); ok( $config ); ok( keys %{ $config } ); } { my $config = Config::JFDI->open( 't/assets/missing-file.pl' ); ok( ! $config ); } { my $config = Config::JFDI->new( file => 't/assets/some_random_file.pl' ); warning_like { $config->open( '...' ) } qr/You called ->open on an instantiated object with arguments/; } { my ($config_hash, $config) = Config::JFDI->open( qw{ name xyzzy path t/assets } ); ok( $config_hash ); is( ref $config_hash, 'HASH' ); isa( $config, 'Config::JFDI' ); } Makefile.PL000644000765000024 250711507723245 14555 0ustar00robstaff000000000000Config-JFDI-0.065 use strict; use warnings; use ExtUtils::MakeMaker 6.31; my %WriteMakefileArgs = ( 'ABSTRACT' => 'Just * Do it: A Catalyst::Plugin::ConfigLoader-style layer over Config::Any', 'AUTHOR' => 'Robert Krimen ', 'BUILD_REQUIRES' => { 'Test::Most' => '0' }, 'CONFIGURE_REQUIRES' => { 'ExtUtils::MakeMaker' => '6.31' }, 'DISTNAME' => 'Config-JFDI', 'EXE_FILES' => [], 'LICENSE' => 'perl', 'NAME' => 'Config::JFDI', 'PREREQ_PM' => { 'Any::Moose' => '0', 'Carp::Clan::Share' => '0', 'Clone' => '0', 'Config::Any' => '0', 'Config::General' => '0', 'Data::Visitor' => '0.24', 'Getopt::Usaginator' => '0', 'Hash::Merge::Simple' => '0', 'List::MoreUtils' => '0', 'Path::Class' => '0', 'Sub::Install' => '0' }, 'VERSION' => '0.065', 'test' => { 'TESTS' => 't/*.t' } ); unless ( eval { ExtUtils::MakeMaker->VERSION(6.56) } ) { my $br = delete $WriteMakefileArgs{BUILD_REQUIRES}; my $pp = $WriteMakefileArgs{PREREQ_PM}; for my $mod ( keys %$br ) { if ( exists $pp->{$mod} ) { $pp->{$mod} = $br->{$mod} if $br->{$mod} > $pp->{$mod}; } else { $pp->{$mod} = $br->{$mod}; } } } delete $WriteMakefileArgs{CONFIGURE_REQUIRES} unless eval { ExtUtils::MakeMaker->VERSION(6.52) }; WriteMakefile(%WriteMakefileArgs); 17-found.t000644000765000024 220711507723245 14570 0ustar00robstaff000000000000Config-JFDI-0.065/tuse strict; use warnings; use Test::Most; plan qw/no_plan/; use Config::JFDI; sub has_Config_General { return eval "require Config::General;"; } { my $config = Config::JFDI->new( file => 't/assets/some_random_file.pl', quiet_deprecation => 1 ); ok( $config->get ); ok( keys %{ $config->get } ); ok( $config->found ); cmp_deeply( [ $config->found ], bag( 't/assets/some_random_file.pl' ) ); } { my $config = Config::JFDI->new( qw{ name xyzzy path t/assets } ); ok( $config->get ); ok( keys %{ $config->get } ); ok( $config->found ); cmp_deeply( [ $config->found ], bag( 't/assets/xyzzy.pl', 't/assets/xyzzy_local.pl' ) ); } { my $config = Config::JFDI->new( file => 't/assets/missing-file.pl', quiet_deprecation => 1 ); ok( $config->get ); cmp_deeply( $config->get, {} ); ok( !$config->found ); } { my $config = Config::JFDI->new( file => 't/assets/some_random_file.pl', quiet_deprecation => 1 ); ok( $config->found ); # Do ->read via ->found ok( $config->get ); ok( keys %{ $config->get } ); cmp_deeply( [ $config->found ], bag( 't/assets/some_random_file.pl' ) ); } 10-loader.t000644000765000024 136411507723245 14717 0ustar00robstaff000000000000Config-JFDI-0.065/tuse strict; use warnings; use Test::More; plan qw/no_plan/; use Config::JFDI; my $config = Config::JFDI->new(qw{ name xyzzy path t/assets }); ok($config->get); is($config->get->{'Controller::Foo'}->{foo}, 'bar'); is($config->get->{'Controller::Foo'}->{new}, 'key'); is($config->get->{'Model::Baz'}->{qux}, 'xyzzy'); is($config->get->{'Model::Baz'}->{another}, 'new key'); is($config->get->{'view'}, 'View::TT::New'); #is($config->get->{'foo_sub'}, 'x-y'); is($config->get->{'foo_sub'}, '__foo(x,y)__'); #is($config->get->{'literal_macro'}, '__DATA__'); is($config->get->{'literal_macro'}, '__DATA__'); ok(1); 21-path-to.t000644000765000024 64711507723245 15012 0ustar00robstaff000000000000Config-JFDI-0.065/tuse strict; use warnings; use Test::Most; plan qw/no_plan/; use Path::Class; use Config::JFDI; my $config; $config = Config::JFDI->new( qw{ name path_to path t/assets }, ); is($config->get->{path_to}, dir('a-galaxy-far-far-away', 'tatooine')); $config = Config::JFDI->new( qw{ name path_to path t/assets }, path_to => 'a-long-time-ago', ); is($config->get->{path_to}, dir('a-long-time-ago', 'tatooine')); 22-default.t000644000765000024 65311507723245 15060 0ustar00robstaff000000000000Config-JFDI-0.065/tuse strict; use warnings; use Test::Most; plan qw/no_plan/; use Path::Class; use Config::JFDI; my $config; $config = Config::JFDI->new( qw{ name default path t/assets }, default => { home => 'a-galaxy-far-far-away', test => 'alpha', }, ); is($config->get->{home}, 'a-galaxy-far-far-away'); is($config->get->{path_to}, dir('a-galaxy-far-far-away', 'tatooine')); is($config->get->{test}, 'beta'); 9bug-rt53886.t000644000765000024 45611507723245 15123 0ustar00robstaff000000000000Config-JFDI-0.065/t#!/usr/bin/env perl use strict; use warnings; use Test::Most; use Config::JFDI; warning_is { Config::JFDI->new( local_suffix => 'local' ) } undef; warning_like { Config::JFDI->new( file => 'xyzzy',local_suffix => 'local' ) } qr/will be ignored if 'file' is given, use 'path' instead/; done_testing; 11-loader-env.t000644000765000024 134511507723245 15505 0ustar00robstaff000000000000Config-JFDI-0.065/tuse strict; use warnings; use Test::More; plan qw/no_plan/; use Config::JFDI; $ENV{XYZZY_CONFIG} = "t/assets/some_random_file.pl"; my $config = Config::JFDI->new(qw{ name xyzzy path t/assets }); ok($config->get); is($config->get->{'Controller::Foo'}->{foo}, 'bar'); is($config->get->{'Model::Baz'}->{qux}, 'xyzzy'); is($config->get->{'view'}, 'View::TT'); is($config->get->{'random'}, 1); #is($config->get->{'foo_sub'}, '__foo(x,y)__' ); #is($config->get->{'literal_macro'}, '__literal(__DATA__)__'); $ENV{XYZZY_CONFIG} = "t/assets/some_non_existent_file.pl"; $config->reload; ok($config->get); is(scalar keys %{ $config->get }, 0); 20-substitute.t000644000765000024 206311507723245 15662 0ustar00robstaff000000000000Config-JFDI-0.065/tuse strict; use warnings; use Test::Most; plan qw/no_plan/; use Path::Class; use Config::JFDI; { my $config = Config::JFDI->new( qw{ name substitute path t/assets }, substitute => { literal => sub { return "Literally, $_[1]!"; }, two_plus_two => sub { return 2 + 2; }, }, ); ok( $config->get ); #is( $config->get->{default}, dir( 'a-galaxy-far-far-away/' ) ); is( $config->get->{default}, file( 'a-galaxy-far-far-away', '' ) ); # Not dir because path_to treats a non-existent directory as a file is( $config->get->{default_override}, "Literally, this!" ); is( $config->get->{original}, 4 ); is( $config->get->{original_embed}, "2 + 2 = 4" ); } { my $path = dir(qw/ t assets /)->absolute; my $config = Config::JFDI->new( qw{ name substitute-path-to }, path => "$path", ); ok( $config->get ); is( $config->get->{default}, "$path" ); is( $config->get->{template}, $path->file( 'root/template' ) ); } assets000755000765000024 011507723245 14205 5ustar00robstaff000000000000Config-JFDI-0.065/txyzzy.pl000644000765000024 37611507723245 16104 0ustar00robstaff000000000000Config-JFDI-0.065/t/assets{ name => 'TestApp', view => 'View::TT', 'Controller::Foo' => { foo => 'bar' }, 'Model::Baz' => { qux => 'xyzzy' }, foo_sub => '__foo(x,y)__', literal_macro => '__literal(__DATA__)__', } Config000755000765000024 011507723245 14413 5ustar00robstaff000000000000Config-JFDI-0.065/libJFDI.pm000644000765000024 3207211507723245 15650 0ustar00robstaff000000000000Config-JFDI-0.065/lib/Configpackage Config::JFDI; BEGIN { $Config::JFDI::VERSION = '0.065'; } # ABSTRACT: Just * Do it: A Catalyst::Plugin::ConfigLoader-style layer over Config::Any use warnings; use strict; use Any::Moose; use Config::JFDI::Source::Loader; use Config::JFDI::Carp; use Path::Class; use Config::Any; use Hash::Merge::Simple; use Sub::Install; use Data::Visitor::Callback; use Clone qw//; has package => qw/ is ro isa Str /; has source => qw/ is ro /, handles => [qw/ driver local_suffix no_env env_lookup path found /]; has load_once => qw/ is ro required 1 /, default => 1; has loaded => qw/ is ro required 1 /, default => 0; has substitution => qw/ reader _substitution lazy_build 1 isa HashRef /; sub _build_substitution { return {}; } has default => qw/ is ro lazy_build 1 isa HashRef /; sub _build_default { return {}; } has path_to => qw/ reader _path_to lazy_build 1 isa Str /; sub _build_path_to { my $self = shift; return $self->config->{home} if $self->config->{home}; return $self->source->path unless $self->source->path_is_file; return '.'; } has _config => qw/ is rw isa HashRef /; sub BUILD { my $self = shift; my $given = shift; $self->{package} = $given->{name} if defined $given->{name} && ! defined $self->{package} && ! ref $given->{name}; my ($source, %source); if ($given->{file}) { if ( 0 ) { # Deprecate the deprecation warning carp "The behavior of the 'file' option has changed, pass in 'quiet_deprecation' or 'no_06_warning' to disable this warning" unless $given->{quiet_deprecation} || $given->{no_06_warning}; carp "Warning, overriding path setting with file (\"$given->{file}\" instead of \"$given->{path}\")" if $given->{path}; } $given->{path} = $given->{file}; $source{path_is_file} = 1; } { for (qw/ name path driver no_local local_suffix no_env env_lookup /) { $source{$_} = $given->{$_} if exists $given->{$_}; } carp "Warning, 'local_suffix' will be ignored if 'file' is given, use 'path' instead" if exists $source{local_suffix} && exists $given->{file}; $source{local_suffix} = $given->{config_local_suffix} if $given->{config_local_suffix}; $source = Config::JFDI::Source::Loader->new( %source ); } $self->{source} = $source; for (qw/substitute substitutes substitutions substitution/) { if ($given->{$_}) { $self->{substitution} = $given->{$_}; last; } } if (my $package = $given->{install_accessor}) { $package = $self->package if $package eq 1; Sub::Install::install_sub({ code => sub { return $self->config; }, into => $package, as => "config" }); } } sub open { if ( ! ref $_[0] ) { my $class = shift; return $class->new( no_06_warning => 1, 1 == @_ ? (file => $_[0]) : @_ )->open; } my $self = shift; carp "You called ->open on an instantiated object with arguments" if @_; return unless $self->found; return wantarray ? ($self->get, $self) : $self->get; } sub get { my $self = shift; my $config = $self->config; return $config; # TODO Expand to allow dotted key access (?) } sub config { my $self = shift; return $self->_config if $self->loaded; return $self->load; } sub load { my $self = shift; if ($self->loaded && $self->load_once) { return $self->get; } $self->_config($self->default); { my @read = $self->source->read; $self->_load($_) for @read; } $self->{loaded} = 1; { my $visitor = Data::Visitor::Callback->new( plain_value => sub { return unless defined $_; $self->substitute($_); } ); $visitor->visit( $self->config ); } return $self->config; } sub clone { my $self = shift; return Clone::clone($self->config); } sub reload { my $self = shift; $self->{loaded} = 0; return $self->load; } sub substitute { my $self = shift; my $substitution = $self->_substitution; $substitution->{ HOME } ||= sub { shift->path_to( '' ); }; $substitution->{ path_to } ||= sub { shift->path_to( @_ ); }; $substitution->{ literal } ||= sub { return $_[ 1 ]; }; my $matcher = join( '|', keys %$substitution ); for ( @_ ) { s{__($matcher)(?:\((.+?)\))?__}{ $substitution->{ $1 }->( $self, $2 ? split( /,/, $2 ) : () ) }eg; } } sub path_to { my $self = shift; my @path = @_; my $path_to = $self->_path_to; my $path = Path::Class::Dir->new( $path_to, @path ); if ( -d $path ) { return $path; } else { return Path::Class::File->new( $path_to, @path ); } } sub _load { my $self = shift; my $cfg = shift; my ($file, $hash) = %$cfg; $self->{_config} = Hash::Merge::Simple->merge($self->_config, $hash); } 1; __END__ =pod =head1 NAME Config::JFDI - Just * Do it: A Catalyst::Plugin::ConfigLoader-style layer over Config::Any =head1 VERSION version 0.065 =head1 DESCRIPTION Config::JFDI is an implementation of L that exists outside of L. Essentially, Config::JFDI will scan a directory for files matching a certain name. If such a file is found which also matches an extension that Config::Any can read, then the configuration from that file will be loaded. Config::JFDI will also look for special files that end with a "_local" suffix. Files with this special suffix will take precedence over any other existing configuration file, if any. The precedence takes place by merging the local configuration with the "standard" configuration via L. Finally, you can override/modify the path search from outside your application, by setting the _CONFIG variable outside your application (where is the uppercase version of what you passed to Config::JFDI->new). =head1 SYNPOSIS use Config::JFDI; my $config = Config::JFDI->new(name => "my_application", path => "path/to/my/application"); my $config_hash = $config->get; This will look for something like (depending on what Config::Any will find): path/to/my/application/my_application_local.{yml,yaml,cnf,conf,jsn,json,...} AND path/to/my/application/my_application.{yml,yaml,cnf,conf,jsn,json,...} ... and load the found configuration information appropiately, with _local taking precedence. You can also specify a file directly: my $config = Config::JFDI->new(file => "/path/to/my/application/my_application.cnf"); To later reload your configuration, fresh from disk: $config->reload; =head1 Config::Loader We are currently kicking around ideas for a next-generation configuration loader. The goals are: * A universal platform for configuration slurping and post-processing * Use Config::Any to do configuration loading * A sane API so that developers can roll their own loader according to the needs of their application * A friendly interface so that users can have it just DWIM * Host/application/instance specific configuration via _local and %ENV Find more information and contribute at: Roadmap: L Mailing list: L =head1 Behavior change of the 'file' parameter in 0.06 In previous versions, Config::JFDI would treat the file parameter as a path parameter, stripping off the extension (ignoring it) and globbing what remained against all the extensions that Config::Any could provide. That is, it would do this: Config::JFDI->new( file => 'xyzzy.cnf' ); # Transform 'xyzzy.cnf' into 'xyzzy.pl', 'xyzzy.yaml', 'xyzzy_local.pl', ... (depending on what Config::Any could parse) This is probably not what people intended. Config::JFDI will now squeak a warning if you pass 'file' through, but you can suppress the warning with 'no_06_warning' or 'quiet_deprecation' Config::JFDI->new( file => 'xyzzy.cnf', no_06_warning => 1 ); Config::JFDI->new( file => 'xyzzy.cnf', quiet_deprecation => 1 ); # More general If you *do* want the original behavior, simply pass in the file parameter as the path parameter instead: Config::JFDI->new( path => 'xyzzy.cnf' ); # Will work as before =head1 METHODS =head2 $config = Config::JFDI->new(...) You can configure the $config object by passing the following to new: name The name specifying the prefix of the configuration file to look for and the ENV variable to read. This can be a package name. In any case, :: will be substituted with _ in and the result will be lowercased. To prevent modification of , pass it in as a scalar reference. path The directory to search in file Directly read the configuration from this file. Config::Any must recognize the extension. Setting this will override path no_local Disable lookup of a local configuration. The 'local_suffix' option will be ignored. Off by default local_suffix The suffix to match when looking for a local configuration. "local" By default ("config_local_suffix" will also work so as to be drop-in compatible with C::P::CL) no_env Set this to 1 to disregard anything in the ENV. The 'env_lookup' option will be ignored. Off by default env_lookup Additional ENV to check if $ENV{...} is not found driver A hash consisting of Config:: driver information. This is passed directly through to Config::Any install_accessor Set this to 1 to install a Catalyst-style accessor as ::config You can also specify the package name directly by setting install_accessor to it (e.g. install_accessor => "My::Application") substitute A hash consisting of subroutines called during the substitution phase of configuration preparation. ("substitutions" will also work so as to be drop-in compatible with C::P::CL) A substitution subroutine has the following signature: ($config, [ $argument1, $argument2, ... ]) path_to The path to dir to use for the __path_to(...)__ substitution. If nothing is given, then the 'home' config value will be used ($config->get->{home}). Failing that, the current directory will be used. default A hash filled with default keys/values Returns a new Config::JFDI object =head2 $config_hash = Config::JFDI->open( ... ) As an alternative way to load a config, ->open will pass given arguments to ->new( ... ), then attempt to do ->load Unlike ->get or ->load, if no configuration files are found, ->open will return undef (or the empty list) This is so you can do something like: my $config_hash = Config::JFDI->open( "/path/to/application.cnf" ) or croak "Couldn't find config file!" In scalar context, ->open will return the config hash, NOT the config object. If you want the config object, call ->open in list context: my ($config_hash, $config) = Config::JFDI->open( ... ) You can pass any arguments to ->open that you would to ->new =head2 $config->get =head2 $config->config =head2 $config->load Load a config as specified by ->new( ... ) and ENV and return a hash These will only load the configuration once, so it's safe to call them multiple times without incurring any loading-time penalty =head2 $config->found Returns a list of files found If the list is empty, then no files were loaded/read =head2 $config->clone Return a clone of the configuration hash using L This will load the configuration first, if it hasn't already =head2 $config->reload Reload the configuration, examining ENV and scanning the path anew Returns a hash of the configuration =head2 $config->substitute( , , ... ) For each given , if looks like a substitution specification, then run the substitution macro on and store the result. There are three default substitutions (the same as L) =over 4 =item * C<__HOME__> - replaced with C<$c-Epath_to('')> =item * C<__path_to(foo/bar)__> - replaced with C<$c-Epath_to('foo/bar')> =item * C<__literal(__FOO__)__> - leaves __FOO__ alone (allows you to use C<__DATA__> as a config value, for example) =back The parameter list is split on comma (C<,>). You can define your own substitutions by supplying the substitute option to ->new =head1 SEE ALSO L L L L L =head1 AUTHOR Robert Krimen =head1 COPYRIGHT AND LICENSE This software is copyright (c) 2011 by Robert Krimen. This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself. =cut 12-loader-file.t000644000765000024 400611507723245 15632 0ustar00robstaff000000000000Config-JFDI-0.065/tuse strict; use warnings; use Test::Most; plan qw/no_plan/; use Config::JFDI; use t::Test; sub has_Config_General { return eval "require Config::General;"; } { my $config = Config::JFDI->new( file => "t/assets/some_random_file.pl" ); ok($config->get); is($config->get->{'Controller::Foo'}->{foo}, 'bar'); is($config->get->{'Model::Baz'}->{qux}, 'xyzzy'); is($config->get->{'view'}, 'View::TT'); is($config->get->{'random'}, 1); #is($config->get->{'foo_sub'}, '__foo(x,y)__' ); #is($config->get->{'literal_macro'}, '__literal(__DATA__)__'); } SKIP: { skip 'Config::General required' unless has_Config_General; my $config; $config = Config::JFDI->new( path => "t/assets/order/../", name => "dotdot", ); ok($config->load, 'Load a config from a directory path ending with ../'); cmp_deeply( $config->get, { test => 'paths ending with ../', } ); $config = Config::JFDI->new( path => "t/assets/order/xyzzy.cnf" ); cmp_deeply( $config->get, { cnf => 1, last => 'local_cnf', local_cnf => 1, } ); $config = Config::JFDI->new( file => "t/assets/order/xyzzy.cnf" ); cmp_deeply( $config->get, { cnf => 1, last => 'cnf', } ); $config = Config::JFDI->new( path => "t/assets/order/xyzzy.cnf", no_local => 1 ); cmp_deeply( $config->get, { cnf => 1, last => 'cnf', } ); warning_is { $config = Config::JFDI->new( file => "t/assets/order/xyzzy.cnf", ) } undef; warning_is { $config = Config::JFDI->new( file => "t/assets/order/xyzzy.cnf", no_06_warning => 1 ) } ''; warning_is { $config = Config::JFDI->new( file => "t/assets/order/xyzzy.cnf", quiet_deprecation => 1 ) } ''; $config = Config::JFDI->new( file => "t/assets/file-does-not-exist.cnf" ); cmp_deeply( $config->get, { } ); } 16-loader-order.t000644000765000024 62511507723245 16015 0ustar00robstaff000000000000Config-JFDI-0.065/tuse strict; use warnings; use Test::More; plan skip_all => "Config::General is required for this test" unless eval "require Config::General;"; plan qw/no_plan/; use Config::JFDI; my $config = Config::JFDI->new(qw{ name xyzzy path t/assets/order }); ok($config->get); is($config->get->{'last'}, 'local_pl'); is($config->get->{$_}, 1) for qw/pl perl local_pl local_perl cnf local_cnf conf local_conf/; default.pl000644000765000024 10311507723245 16277 0ustar00robstaff000000000000Config-JFDI-0.065/t/assets{ path_to => '__path_to(tatooine)__', test => 'beta', } path_to.pl000644000765000024 12411507723245 16314 0ustar00robstaff000000000000Config-JFDI-0.065/t/assets{ home => 'a-galaxy-far-far-away', path_to => '__path_to(tatooine)__', } 13-loader-no-env.t000644000765000024 124511507723245 16120 0ustar00robstaff000000000000Config-JFDI-0.065/tuse strict; use warnings; use Test::More; plan qw/no_plan/; use Config::JFDI; $ENV{XYZZY_CONFIG} = "t/assets/some_non_existent_file.pl"; my $config = Config::JFDI->new(qw{ name xyzzy path t/assets no_env 1 }); ok($config->get); is($config->get->{'Controller::Foo'}->{foo}, 'bar'); is($config->get->{'Controller::Foo'}->{new}, 'key'); is($config->get->{'Model::Baz'}->{qux}, 'xyzzy'); is($config->get->{'Model::Baz'}->{another}, 'new key'); is($config->get->{'view'}, 'View::TT::New'); is($config->get->{'foo_sub'}, '__foo(x,y)__' ); is($config->get->{'literal_macro'}, '__DATA__'); dotdot.conf000644000765000024 3311507723245 16444 0ustar00robstaff000000000000Config-JFDI-0.065/t/assetstest paths ending with ../ dir.cnf000755000765000024 011507723245 15530 5ustar00robstaff000000000000Config-JFDI-0.065/t/assetskeep000644000765000024 011507723245 16444 0ustar00robstaff000000000000Config-JFDI-0.065/t/assets/dir.cnfsubstitute.pl000644000765000024 37011507723245 17074 0ustar00robstaff000000000000Config-JFDI-0.065/t/assets{ home => 'a-galaxy-far-far-away', default => '__HOME__', default_override => '__literal(this)__', original => '__two_plus_two__', original_embed => '2 + 2 = __two_plus_two__', template => '__path_to(root/template)__', } JFDI000755000765000024 011507723245 15127 5ustar00robstaff000000000000Config-JFDI-0.065/lib/ConfigCarp.pm000644000765000024 12311507723245 16465 0ustar00robstaff000000000000Config-JFDI-0.065/lib/Config/JFDIpackage Config::JFDI::Carp; use strict; use warnings; use Carp::Clan::Share; 1; 15-install-accessor.t000644000765000024 146411507723245 16725 0ustar00robstaff000000000000Config-JFDI-0.065/tuse strict; use warnings; use Test::More; plan qw/no_plan/; use Config::JFDI; my $config = Config::JFDI->new(qw{ name Xyzzy::Catalyst path t/assets install_accessor A::Random::Package }); ok(!Xyzzy::Catalyst->can("config")); ok(A::Random::Package->config); $config = A::Random::Package->config; ok($config); is($config->{'Controller::Foo'}->{foo}, 'bar'); is($config->{'Model::Baz'}->{qux}, 'xyzzy'); is($config->{'view'}, 'View::TT'); $config = Config::JFDI->new(qw{ name Xyzzy::Catalyst path t/assets install_accessor 1 }); ok(Xyzzy::Catalyst->config); $config = Xyzzy::Catalyst->config; ok($config); is($config->{'Controller::Foo'}->{foo}, 'bar'); is($config->{'Model::Baz'}->{qux}, 'xyzzy'); is($config->{'view'}, 'View::TT'); xyzzy_local.pl000644000765000024 20711507723245 17247 0ustar00robstaff000000000000Config-JFDI-0.065/t/assets{ view => 'View::TT::New', 'Controller::Foo' => { new => 'key' }, 'Model::Baz' => { 'another' => 'new key' }, } order000755000765000024 011507723245 15320 5ustar00robstaff000000000000Config-JFDI-0.065/t/assetsxyzzy.pl000644000765000024 5211507723245 17166 0ustar00robstaff000000000000Config-JFDI-0.065/t/assets/order{ last => 'pl', pl => 1, } 14-loader-env-lookup.t000644000765000024 242011507723245 17012 0ustar00robstaff000000000000Config-JFDI-0.065/tuse strict; use warnings; use Test::More; plan qw/no_plan/; use Config::JFDI; eval { delete $ENV{$_} for qw/CATALYST_CONFIG_LOCAL_SUFFIX/; }; $ENV{CATALYST_CONFIG} = "t/assets/some_random_file.pl"; my $config = Config::JFDI->new(qw{ name xyzzy path t/assets env_lookup CATALYST }); ok($config->get); is($config->get->{'Controller::Foo'}->{foo}, 'bar'); is($config->get->{'Model::Baz'}->{qux}, 'xyzzy'); is($config->get->{'view'}, 'View::TT'); #is($config->get->{'foo_sub'}, '__foo(x,y)__' ); #is($config->get->{'literal_macro'}, '__literal(__DATA__)__'); is($config->get->{'random'}, 1); $ENV{XYZZY_CONFIG} = "t/assets/xyzzy.pl"; $config = Config::JFDI->new(qw{ name xyzzy path t/assets }, env_lookup => [qw/CATALYST/]); ok($config->get); is($config->get->{'Controller::Foo'}->{foo}, 'bar'); is($config->get->{'Controller::Foo'}->{new}, 'key'); is($config->get->{'Model::Baz'}->{qux}, 'xyzzy'); is($config->get->{'Model::Baz'}->{another}, 'new key'); is($config->get->{'view'}, 'View::TT::New'); is($config->get->{'foo_sub'}, '__foo(x,y)__' ); is($config->get->{'literal_macro'}, '__DATA__'); xyzzy.cnf000644000765000024 1711507723245 17322 0ustar00robstaff000000000000Config-JFDI-0.065/t/assets/orderlast cnf cnf 1 xyzzy.conf000644000765000024 2111507723245 17474 0ustar00robstaff000000000000Config-JFDI-0.065/t/assets/orderlast conf conf 1 xyzzy.perl000644000765000024 4511507723245 17517 0ustar00robstaff000000000000Config-JFDI-0.065/t/assets/order{ last => 'perl', perl => 1, } xyzzy_catalyst.pl000644000765000024 37611507723245 20010 0ustar00robstaff000000000000Config-JFDI-0.065/t/assets{ name => 'TestApp', view => 'View::TT', 'Controller::Foo' => { foo => 'bar' }, 'Model::Baz' => { qux => 'xyzzy' }, foo_sub => '__foo(x,y)__', literal_macro => '__literal(__DATA__)__', } some_random_file.pl000644000765000024 27711507723245 20171 0ustar00robstaff000000000000Config-JFDI-0.065/t/assets{ name => 'TestApp', view => 'View::TT', 'Controller::Foo' => { foo => 'bar' }, 'Model::Baz' => { qux => 'xyzzy' }, random => 1, } xyzzy_local.pl000644000765000024 5511507723245 20343 0ustar00robstaff000000000000Config-JFDI-0.065/t/assets/order{ last => 'local_pl', local_pl => 1, } substitute-path-to.pl000644000765000024 12111507723245 20440 0ustar00robstaff000000000000Config-JFDI-0.065/t/assets{ default => '__HOME__', template => '__path_to(root/template)__', } xyzzy_local.cnf000644000765000024 3311507723245 20472 0ustar00robstaff000000000000Config-JFDI-0.065/t/assets/orderlast local_cnf local_cnf 1 9bug-rt47237-path-undefined.t000644000765000024 26711507723245 20005 0ustar00robstaff000000000000Config-JFDI-0.065/t#!/usr/bin/env perl use strict; use warnings; use Test::Most; use Config::JFDI; my $config = Config::JFDI->new( name => '' ); warning_is { $config->_path_to } undef; done_testing; xyzzy_local.conf000644000765000024 3511507723245 20653 0ustar00robstaff000000000000Config-JFDI-0.065/t/assets/orderlast local_conf local_conf 1 xyzzy_local.perl000644000765000024 6111507723245 20667 0ustar00robstaff000000000000Config-JFDI-0.065/t/assets/order{ last => 'local_perl', local_perl => 1, } Source000755000765000024 011507723245 16367 5ustar00robstaff000000000000Config-JFDI-0.065/lib/Config/JFDILoader.pm000644000765000024 1117711507723245 20321 0ustar00robstaff000000000000Config-JFDI-0.065/lib/Config/JFDI/Sourcepackage Config::JFDI::Source::Loader; use Any::Moose; use Config::Any; use Carp; use List::MoreUtils qw/ any /; has name => qw/ is ro required 0 isa Str|ScalarRef /; has path => qw/ is ro default . /; has driver => qw/ is ro lazy_build 1 /; sub _build_driver { return {}; } has local_suffix => qw/ is ro required 1 lazy 1 default local /; has no_env => qw/ is ro required 1 /, default => 0; has no_local => qw/ is ro required 1 /, default => 0; has env_lookup => qw/ is ro /, default => sub { [] }; has path_is_file => qw/ is ro default 0 /; has _found => qw/ is rw isa ArrayRef /; sub _env (@) { my $key = uc join "_", @_; $key =~ s/::/_/g; $key =~ s/\W/_/g; return $ENV{$key}; } sub BUILD { my $self = shift; my $given = shift; if (defined( my $name = $self->name )) { if (ref $name eq "SCALAR") { $name = $$name; } else { $name =~ s/::/_/g; $name = lc $name; } $self->{name} = $name; } if (defined $self->env_lookup) { $self->{env_lookup} = [ $self->env_lookup ] unless ref $self->env_lookup eq "ARRAY"; } } sub read { my $self = shift; my @files = $self->_find_files; my $cfg_files = $self->_load_files(\@files); my %cfg_files = map { (%$_)[0] => $_ } reverse @$cfg_files; $self->_found( [ map { (%$_)[0] } @$cfg_files ] ); my (@cfg, @local_cfg); { # Anything that is local takes precedence my $local_suffix = $self->_get_local_suffix; for (sort keys %cfg_files) { my $cfg = $cfg_files{$_}; if (m{$local_suffix\.}ms) { push @local_cfg, $cfg; } else { push @cfg, $cfg; } } } return $self->no_local ? @cfg : (@cfg, @local_cfg); } sub found { my $self = shift; die if @_; return @{ $self->_found }; } around found => sub { my $inner = shift; my $self = shift; $self->read unless $self->{_found}; return $inner->( $self, @_ ); }; sub _load_files { my $self = shift; my $files = shift; return Config::Any->load_files({ files => $files, use_ext => 1, driver_args => $self->driver, }); } sub _find_files { # Doesn't really find files...hurm... my $self = shift; if ($self->path_is_file) { my $path; $path = $self->_env_lookup('CONFIG') unless $self->no_env; $path ||= $self->path; return ($path); } else { my ($path, $extension) = $self->_get_path; my $local_suffix = $self->_get_local_suffix; my @extensions = $self->_get_extensions; my $no_local = $self->no_local; my @files; if ($extension) { croak "Can't handle file extension $extension" unless any { $_ eq $extension } @extensions; push @files, $path; unless ($no_local) { (my $local_path = $path) =~ s{\.$extension$}{_$local_suffix.$extension}; push @files, $local_path; } } else { push @files, map { "$path.$_" } @extensions; push @files, map { "${path}_${local_suffix}.$_" } @extensions unless $no_local; } return @files; } } sub _env_lookup { my $self = shift; my @suffix = @_; my $name = $self->name; my $env_lookup = $self->env_lookup; my @lookup; push @lookup, $name if $name; push @lookup, @$env_lookup; for my $prefix (@lookup) { my $value = _env($prefix, @suffix); return $value if defined $value; } return; } sub _get_local_suffix { my $self = shift; my $name = $self->name; my $suffix; $suffix = $self->_env_lookup('CONFIG_LOCAL_SUFFIX') unless $self->no_env; # $suffix = _env($self->name, 'CONFIG_LOCAL_SUFFIX') if $name && ! $self->no_env; $suffix ||= $self->local_suffix; return $suffix; } sub _get_extensions { return @{ Config::Any->extensions } } sub file_extension ($) { my $path = shift; return if -d $path; my ($extension) = $path =~ m{\.([^/\.]{1,4})$}; return $extension; } sub _get_path { my $self = shift; my $name = $self->name; my $path; # $path = _env($name, 'CONFIG') if $name && ! $self->no_env; $path = $self->_env_lookup('CONFIG') unless $self->no_env; $path ||= $self->path; my $extension = file_extension $path; if (-d $path) { $path =~ s{[\/\\]$}{}; # Remove any trailing slash, e.g. apple/ or apple\ => apple $path .= "/$name"; # Look for a file in path with $self->name, e.g. apple => apple/name } return ($path, $extension); } 1; 9bug-taking-extension-from-path.t000644000765000024 66711507723245 21246 0ustar00robstaff000000000000Config-JFDI-0.065/t#!/usr/bin/env perl use strict; use warnings; use Test::Most; use Config::JFDI::Source::Loader; sub file_extension ($) { Config::JFDI::Source::Loader::file_extension shift } is( file_extension 'test.conf', 'conf' ); is( file_extension '...', undef ); is( file_extension '../.', undef ); is( file_extension '.../.', undef ); is( file_extension 't/assets/order/..', undef ); is( file_extension 't/assets/dir.cnf', undef ); done_testing;