package Poet::Conf;
$Poet::Conf::VERSION = '0.16';
use Carp;
use Cwd qw(realpath);
use Data::Rmap qw(rmap_scalar);
use File::Spec::Functions qw(catfile);
use Guard;
use Poet::Moose;
use Poet::Tools qw(read_file);
use Storable qw(dclone);
use Try::Tiny;
use YAML::XS;
use strict;
use warnings;

has 'conf_dir'       => ( required => 1 );
has 'data'           => ( init_arg => undef );
has 'is_development' => ( init_arg => undef, lazy_build => 1 );
has 'is_live'        => ( init_arg => undef, lazy_build => 1 );
has 'layer'          => ( init_arg => undef, lazy_build => 1 );
has 'root_dir'       => ( required => 1 );

our %get_cache;

method BUILD () {
    $self->{data} = $self->read_conf_data();

method initial_conf_data () {
    return ( root_dir => $self->root_dir, root => $self->root_dir );

method read_conf_data () {
    my %data = $self->initial_conf_data();

    # Collect list of conf files in appropriate order
    my @conf_files = $self->ordered_conf_files();

    # Stores the file where each global/* key is declared.
    my %global_keys;

    foreach my $file (@conf_files) {
        if ( defined $file && -f $file ) {

            # Read conf file into hash
            my $new_data = $self->read_conf_file($file);

            # Make sure no keys are defined in multiple global conf files
            if ( $file =~ m{/global/} ) {
                foreach my $key ( keys(%$new_data) ) {
                    if ( my $previous_file = $global_keys{$key} ) {
                        die sprintf(
                            "top-level key '%s' defined in both '%s' and '%s' - global conf files must be mutually exclusive",
                            $key, $previous_file, $file );
                    else {
                        $global_keys{$key} = $file;

            # Merge new hash into current data
            $self->merge_conf_data( \%data, $new_data, $file );

    return \%data;

method _build_layer () {
    my $conf_dir = $self->conf_dir;
    my $local_cfg_file = catfile( $conf_dir, "local.cfg" );
    my $local_cfg =
      ( -f $local_cfg_file )
      ? $self->read_conf_file($local_cfg_file)
      : {};
    my $layer = $local_cfg->{layer}
      || die "must specify layer in '$local_cfg_file'";
    die "invalid layer '$layer' - no such file '$conf_dir/layer/$layer.cfg'"
      unless -f "$conf_dir/layer/$layer.cfg";

    return $layer;

method _build_is_development () {
    return $self->layer eq 'development';

method _build_is_live () {
    return !$self->is_development;

method ordered_conf_files () {
    my $conf_dir = $self->conf_dir();
    my $layer    = $self->layer();

    return (
            ? ("$conf_dir/layer/live.cfg")
            : ()

method read_conf_file ($file) {

    # Read a yaml file into a hash, adding a dummy key pair to handle empty
    # files or files with nothing but comments, and checking for errors.
    # Return the hash.
    my $dummy = "__yaml_init";
    my $yaml  = read_file($file) . "\n\n$dummy: 0";
    my $hash;
    try {
        $hash = YAML::XS::Load($yaml);
    catch {
        die "error parsing conf file '$file': $_";
    die "'$file' did not parse to a hash" unless ref($hash) eq 'HASH';
    delete( $hash->{$dummy} );
    return $hash;

method merge_conf_data ($current_data, $new_data, $file) {
    while ( my ( $key, $value ) = each(%$new_data) ) {
        my $orig_key       = $key;
        my $assign_to_hash = $current_data;
        while ( $key =~ /\./ ) {
            my ( $first, $rest ) = split( /\./, $key, 2 );
            if ( !defined( $assign_to_hash->{$first} ) ) {
                $assign_to_hash->{$first} = {};
            $assign_to_hash = $assign_to_hash->{$first};
            if ( ref($assign_to_hash) ne 'HASH' ) {
                die sprintf( "error assigning to '%s' in '%s'; '%s' already has non-hash value",
                    $orig_key, $file, substr( $orig_key, 0, -1 * length($rest) - 1 ) );
            $key = $rest;
        $assign_to_hash->{$key} = $value;

# Memoize get, since conf can normally not change at runtime. This will
# benefit all get() and get_*() calls. Clear cache on set_local.
method _flush_get_cache () {
    %get_cache = ();

method get ($key, $default) {
    croak "key required" if !defined($key);
    return $get_cache{$key} if exists( $get_cache{$key} );

    my $orig_key = $key;
    my @firsts;
    if ( $key =~ /\./ ) {
        return $self->_get_dotted_key( $key, $default );
    my $value = $self->data->{$key};
    rmap_scalar { $_ = $self->interpolate_value($_) if defined && !ref } $value;
    $get_cache{$key} = $value;
    return defined($value) ? $value : $default;

method _get_dotted_key ($key, $default) {
    my ( $rest, $last ) = ( $key =~ /^(.*)\.([^\.]+)$/ );
    my $value = $self->get_hash($rest)->{$last};
    return defined($value) ? $value : $default;

method interpolate_value ($value) {
    while ( $value =~ /(\$ \{ ([\w\.\-]+) \} )/x ) {
        my $var_decl  = $1;
        my $var_key   = $2;
        my $var_value = $self->get_or_die($var_key);
        $var_value = '' if !defined($var_value);
        $value =~ s/\Q$var_decl\E/$var_value/;
    return $value;

method get_or_die ($key) {
    if ( defined( my $value = $self->get($key) ) ) {
        return $value;
    else {
        croak "could not get conf for '$key'";

method get_list ($key, $default) {
    if ( defined( my $value = $self->get($key) ) ) {
        if ( ref($value) eq 'ARRAY' ) {
            return $value;
        else {
            my $error =
              sprintf( "list value expected for conf key '%s', got non-list '%s'", $key, $value );
    elsif ( defined $default ) {
        return $default;
    else {
        return [];

method get_hash ($key, $default) {
    if ( defined( my $value = $self->get($key) ) ) {
        if ( ref($value) eq 'HASH' ) {
            return $value;
        else {
            my $error =
              sprintf( "hash value expected for conf key '%s', got non-hash '%s'", $key, $value );
    elsif ( defined $default ) {
        return $default;
    else {
        return {};

method get_boolean ($key) {
    my $value = $self->get($key) || 0;
        ( !ref($value) && $value =~ /^(1|t|true|y|yes)$/i ) ? 1
      : ( !ref($value) && $value =~ /^(0|f|false|n|no)$/i ) ? 0
      : croak(
        sprintf( "boolean value expected for conf key '%s', got non-boolean '%s'", $key, $value ) );

method set_local ($pairs) {
    if ( !defined(wantarray) ) {
        warn "result of set_local must be assigned!";
    croak "set_local expects hashref" unless ref($pairs) eq 'HASH';

    # Make a deep copy of current data, then merge in the new pairs
    my $orig_data = dclone( $self->{data} );
    $self->merge_conf_data( $self->{data}, $pairs, "set_local" );

    # Restore original data when $guard goes out of scope
    my $guard = guard { $self->{data} = $orig_data; $self->conf_has_changed() };
    return $guard;

# Get key from secure conf, fallback to normal conf. Maintain separate
# secure_conf hash for each Conf object (e.g. for testing).
my %secure_confs;

method get_secure ($key) {
    die "key required" unless defined($key);
    return $self->_get_secure_conf->{$key} || $self->get($key);

method _get_secure_conf () {
    if ( !$secure_confs{"$self"} ) {
        my $secure_conf_file =
          $self->get( 'conf.secure_conf_file' => $self->conf_dir . "/secure.cfg" );
        $secure_confs{"$self"} =
          ( -f $secure_conf_file )
          ? $self->read_conf_file($secure_conf_file)
          : {};
    return $secure_confs{"$self"};

method generate_dynamic_conf () {
    require MasonX::ProcessDir;
    my $poet       = Poet::Environment->current_env;
    my $source_dir = $self->conf_dir . "/dynamic";
    my $dest_dir   = $poet->data_path("conf/dynamic");
    my $pd         = MasonX::ProcessDir->new(
        source_dir    => $source_dir,
        dest_dir      => $dest_dir,
        ignore_files  => sub { $_[0] =~ /Base\.|\.mi$|gen\.pl|README/ },
        mason_options => {},

method get_keys () {
    return keys( %{ $self->{data} } );

method as_hash () {
    return { map { ( $_, $self->get($_) ) } $self->get_keys() };

method as_string () {
    return YAML::XS::Dump( $self->as_hash );

# Things we need to do whenever the conf changes.
method conf_has_changed () {





=head1 NAME

Poet::Conf -- Poet configuration


    # In a script...
    use Poet::Script qw($conf);

    # In a module...
    use Poet qw($conf);

    # $conf is automatically available in Mason components

    # then...
    my $value = $conf->get('key', 'default');
    my $value = $conf->get_or_die('key');

    my $listref = $conf->get_list('key', ['default']);
    my $hashref = $conf->get_hash('key', {'default' => 5});
    my $bool = $conf->get_boolean('key');

    my @keys = grep { /^foo\./ } $conf->get_keys;

    my $hash = $conf->as_hash;
    print $conf->as_string;

       my $lex = $conf->set_local({'key' => 'new_value'});
       # key has new_value inside this scope only


The Poet::Conf object gives access to the current environment's configuration,
read from configuration files in the conf/ subdirectory.


Poet configuration files are found in the conf/ subdirectory of the environment


The files are read and merged in the following order, with later files taking
precedence over earlier files. None of the files have to exist except


=item *

C<global.cfg> contains various settings for the environment, typically checked
into version control. Having a single file is fine for a simple site and a
single developer, but if this gets too unwieldy, see global/ below.

=item *

The C<global/> directory contains multiple .cfg files, all of which are read in
alphabetical order. This is an alternative to C<global.cfg> when the latter
gets too crowded and you have multiple developers making simultaneous changes.
It is an error for two global files to set the same key.

=item *

The C<layer/> directory contains version-controlled files specific to layers,
e.g. C<development.cfg> and C<production.cfg>.  Only one of these files will be
active at a time, depending on the current layer (as set in C<local.cfg>).

=item *

C<local.cfg> contains settings for this particular instance of the environment.
It is not checked into version control. local.cfg must exist and must contain
at least the layer, e.g.

    layer: development

=item *

If C<$ENV{POET_EXTRA_CONF_FILE}> is defined when configuration initializes, it
is read as an extra conf file whose values override all others.



Basic conf file format is L<YAML|>, e.g.

       driver: Memcached
       servers: ["", ""]

       level: info
       output: poet.log
       layout: "%d{dd/MMM/yyyy:HH:mm:ss.SS} [%p] %c - %m - %F:%L - %P%n"

=head2 Interpolation - referring to other entries

Conf entries can refer to other entries via the syntax C<${key}>. For example:

   # conf file

   foo: 5
   bar: "The number ${foo}"
   baz: ${bar}00

   # then
      => 5
      => "The number 5"
      => "The number 500"

The key must exist or a fatal error will occur.

There is a single built-in entry, C<root_dir>, containing the root directory of
the environment that you can use in other entries, e.g.

         driver: File
         root_dir: ${root_dir}/data/cache

=head2 Dot notation for hash access

Conf entries can use dot (".") notation to refer to hash entries. e.g. this 5

is the same as

         baz: 5

The dot notation is especially useful for I<overriding> individual hash
elements from higher precedence config files. For example, if in
C<global/cache.cfg> you have

         driver: File
         root_dir: $root/data/cache
         depth: 3

and in local.cfg you have

    cache.defaults.depth: 2

then only C<depth> will be overridden; the C<driver> and C<root_dir> will
remain as they were set in C<global/cache.cfg>. If instead local.cfg had

         depth: 3

then this would completely replace the entire hash under C<cache>.


In a script:

    use Poet::Script qw($conf);

In a module:

    use Poet qw($conf);

C<$conf> is automatically available in components.

You can also get it via

    my $conf = Poet::Environment->current_env->conf;

=head1 METHODS

=head2 Methods for getting conf values


=item get (key[, default])

    my $value = $conf->get('key' => 'default');

Get I<key> from configuration. If I<key> is unavailable, return the I<default>,
or undef if no default is given.

The return value may be a scalar, list reference, or hash reference, though we
recommend using L</get_list> and L</get_hash> if you expect a list or hash.

I<key> can contain dot notation to refer to hash entries. e.g. these are



=item get_or_die (key)

    my $value = $conf->get_or_die('key');

Get I<key> from configuration. If I<key> is unavailable, throw a fatal error.

=item get_list (key[, default])

    my $listref = $conf->get_list('key', ['default']);

Get I<key> from configuration. If the value is not a list reference, throw an

If I<key> is unavailable, return the I<default>, or an empty list reference if
no default is given.

=item get_hash (key[, default])

    my $hashref = $conf->get_hash('key', {'default' => 5});

Get I<key> from configuration. If the value is not a hash reference, throw an

If I<key> is unavailable, return the I<default>, or an empty hash reference if
no default is given.

=item get_boolean (key)

    my $bool = $conf->get_boolean('key');

Get I<key> from configuration. Return 1 if the value represents true ("1", "t",
"true", "y", "yes") and 0 if the value represents false ("0", "f", "false",
"n", "no") or is not present in configuration. These are case insensitive
matches. Throws an error if there is a value that is a reference or does not
match one of the valid options.

=item get_secure (key)

    my $password = $conf->get_secure('secret_password');

Get I<key> from a separate, non-version-controlled, secure config file; if it
cannot be found, then fallback to normal config. Useful for passwords,
encryption keys, etc. that might be ok in normal config on development, but
ought to be secure on production.

The location of the secure config file is determined by config entry
conf.secure_conf_file; it defaults to C<conf/secure.cfg>. The file is in plain
YAML format, with no interpolation or dot notation.


=head2 Other methods


=item layer

Returns the current layer, as determined from C<local.cfg>.

=item is_development

Boolean; returns true iff the current layer is 'development'.

=item is_live

Boolean; the opposte of L</is_development>.

=item get_keys

    my @keys = sort $conf->get_keys;

Return a list of all keys in configuration.

=item as_hash

    my $hash = $conf->as_hash;

Return a hash reference mapping keys to their value as returned by C<<
$conf->get >>.

=item as_string

    print $conf->as_string;

Return a printable representation of the keys and values.

=item set_local

    my $lex = $conf->set_local({key => 'value', ...});

Temporarily set each I<key> to I<value>. The original value will be restored
when $lex goes out of scope.

This is intended for specialized use in unit tests and development tools, NOT
for production code. Setting and resetting of configuration values will make it
much more difficult to read and debug code!

=item generate_dynamic_config


This method can be used to dynamically generate configuration files for
external software (e.g. Apache, nginx, logrotate). It uses
L<MasonX::ProcessDir|MasonX::ProcessDir> to process Mason templates in
C<conf/dynamic> and generate destination files in C<data/conf/dynamic>.

For example, if C<conf/dynamic/> contains an Apache configuration
file with Mason dynamic elements, this method will generate a static
configuration file in C<data/conf/dynamic/>, which you can then
feed directly into Apache.



These methods are not intended to be called externally, but may be useful to
override or modify with method modifiers in
L<subclasses|Poet::Manual::Subclassing>. Their APIs will be kept as stable as


=item read_conf_data

This is the main method that finds and parses conf files and returns a hash of
conf keys to values. You can modify this to dynamically compute certain conf

    override 'read_conf_data' => sub {
        my $hash = super();
        $hash->{complex_key} = ...;
        return $hash;

or to completely override how Poet gets its configuration:

    override 'read_conf_data' => sub {
        return {
           some_conf_key => 'some conf value',

=item initial_conf_data

Returns a hash with initial configuration data before any conf files have been
merged in. By default, just contains

    ( root => '/path/to/root' )

=item _build_layer

Determines the current layer before L</read_conf> is called. By default, looks
for a C<layer> key in C<local.cfg>.

=item _build_is_development

Determines the value of L</is_development>, and subsequently its opposite
L</is_live>. By default, true iff layer == 'development'.

=item ordered_conf_files

Returns a list of conf files to read in order from lowest to highest
precedence. You can modify this to insert an additional file, e.g.

    override 'ordered_conf_files' => sub {
        my @list = super();
        return (@list, '/path/to/important.cfg');

=item read_conf_file ($file)

Read a single conf I<$file> and return its hash representation. You can modify
this to use a conf format other than YAML, e.g.

    use Config::INI;

    override 'read_conf_file' => sub {
        my ($self, $file) = @_;
        return Config::INI::Reader->read_file($file);

=item merge_conf_data ($current_data, $new_data, $file)

Merge I<$new_data> from I<$file> into I<$current_data>. I<$new_data> and
I<$current_data> are both hashrefs, and I<$current_data> will be the empty hash
for the first file. By default, this just uses Perl's built-in hash merging
with values from I<$new_data> taking precedence.


=head1 CREDITS

The ideas of merging multiple conf files and variable interpolation came from

=head1 SEE ALSO


=head1 AUTHOR

Jonathan Swartz <>


This software is copyright (c) 2012 by Jonathan Swartz.

This is free software; you can redistribute it and/or modify it under
the same terms as the Perl 5 programming language system itself.