Minima

 view release on metacpan or  search on metacpan

lib/Minima/Setup.pm  view on Meta::CPAN

use v5.40;

package Minima::Setup;

use Carp;
use Minima::App;
use Path::Tiny;
use Plack::Test;
use YAML::XS 'LoadFile';

our $config = {};
our $app;
our $base;

sub import
{
    shift; # discard package name

    # If we were called from a .psgi, save
    # its parent as the base directory
    my $caller = path( (caller)[1] );
    if ($caller->basename =~ /\.psgi$/) {
        $base = $caller->absolute->parent;
    } else {
        $base = path('.')->absolute;
    }

    prepare(@_);
}

sub prepare ($file = undef)
{
    my $default_prefix = $base->child('etc/config');
    my @files = map { "$default_prefix.$_" } qw/ yaml yml pl /;

    if ($file) {
        my $file_abs = path($file)->absolute;

        croak "Config file `$file` does not exist.\n"
            unless -e $file_abs;

        $file = $file_abs;
    }
    unshift @files, $file;

    my $adjective;

    for (my $i = 0; $i < @files; $i++) {

        my $f = $files[$i];
        next unless defined $f && -e $f;

        $adjective = $i ? 'default ' : '';

        if ($f =~ /\.ya?ml$/) {
            try {
                $config = LoadFile $f;
            } catch ($e) {
                croak "Failed to parse ${adjective}config file "
                    . "`$f`:\n$e\n";
            }
        } else {
            $config = do $f;
            croak "Failed to parse ${adjective}config file "
                . "`$f`: $@\n" if $@;
        }

        last;
    }

    croak "Config is not a hash reference.\n"
        unless ref $config eq ref {};

    # If the loaded config does not set base_dir,
    # set it to the saved .psgi directory
    $config->{base_dir} = $base->stringify
        unless defined $config->{base_dir};

    # Initialize app
    $app = Minima::App->new(
        configuration => $config,



( run in 2.335 seconds using v1.01-cache-2.11-cpan-cdf2f3d4e48 )