Hatena::Groupcatalyst

dann@catalyst このページをアンテナに追加 RSSフィード

2008-03-16

Catalystに依存しないconfigの読み込み

14:11 |  Catalystに依存しないconfigの読み込み - dann@catalyst を含むブックマーク はてなブックマーク -  Catalystに依存しないconfigの読み込み - dann@catalyst  Catalystに依存しないconfigの読み込み - dann@catalyst のブックマークコメント

Catalystに依存せずにyamlなどの設定を読むようにした。

package MyApp::ConfigLoader;

use strict;
use warnings;

use MyApp::Utils;
use Config::Any;
use File::Spec;

sub new {
    my $class = shift;
    my $self = {};
    bless $self , $class;
    $self->{config} = $self->load;
    return $self;
}

# looks ugly. Fix later
sub app_name {
    'MyApp::Catalyst';
}

sub prefix {
    my $self = shift;
    my $prefix = MyApp::Utils::appprefix( $self->app_name );
    return $prefix;
}

sub config {
    my $self = shift;
    return $self->{config};
}

sub load {
    my $self = shift;
    my @files = $self->find_files;
    my $cfg   = Config::Any->load_files(
        {   files       => \@files,
            use_ext     => 1,
        }
    );

    my $config = {};
    my $config_local = {};
    my $local_suffix = $self->get_config_local_suffix;
    for ( @$cfg ) {

        if ( ( keys %$_ )[ 0 ] =~ m{ $local_suffix \. }xms ) {
            $config_local =  $_->{ (keys %{$_})[0] };
        }
        else {
            $config = {
                %{ $_->{ (keys %{$_})[0] }},
                %{$config} ,
            }
        }
    }

    $config = {
        %{$config},
        %{$config_local} ,
    };
    return $config;
}

sub local_file {
    my $self = shift;
    my $prefix = $self->prefix;
    return File::Spec->catfile($self->get_config_dir_path, $prefix . '_' . $self->get_config_local_suffix);
}

sub find_files {
    my $self = shift;
    my ( $path, $extension ) = $self->get_config_path;
    my $suffix     = $self->get_config_local_suffix;
    my @extensions = @{ Config::Any->extensions };

    my @files;
    if ( $extension ) {
        next unless grep { $_ eq $extension } @extensions;
        ( my $local = $path ) =~ s{\.$extension}{_$suffix.$extension};
        push @files, $path, $local;
    }
    else {
        @files = map { ( "$path.$_", "${path}_${suffix}.$_" ) } @extensions;
    }

    return @files;
}

sub get_config_dir_path {
    my $self = shift;
    my $home = MyApp::Utils->home;
    return File::Spec->catfile( $home , 'conf', $self->prefix . ".yml");

}

sub get_config_path {
    my $self = shift;
    my $path = $self->get_config_dir_path;
    my $extension = 'yml';
    return ( $path, $extension );
}

sub get_config_local_suffix {
    my $self = shift;
    my $suffix = MyApp::Utils::env_value( $self->app_name, 'CONFIG_LOCAL_SUFFIX' ) || "local";
    return $suffix;
}

1;

MyApp::Utils

Catalyst::Utilsの必要なところだけ、Catalystに依存しないように抽出。

package MyApp::Utils;

use strict;
use File::Spec;
use Path::Class;
use URI;
use Class::Inspector;
use Carp qw/croak/;

sub appprefix {
    my $class = shift;
    $class =~ s/::/_/g;
    $class = lc($class);
    return $class;
}

sub class2appclass {
    my $class = shift || '';
    my $appname = '';
    if ( $class =~ /^(.+?)::([MVC]|Model|View|Controller)::.+$/ ) {
        $appname = $1;
    }
    return $appname;
}

sub class2classprefix {
    my $class = shift || '';
    my $prefix;
    if ( $class =~ /^(.+?::([MVC]|Model|View|Controller))::.+$/ ) {
        $prefix = $1;
    }
    return $prefix;
}

sub class2classsuffix {
    my $class = shift || '';
    my $prefix = class2appclass($class) || '';
    $class =~ s/$prefix\:://;
    return $class;
}

sub class2env {
    my $class = shift || '';
    $class =~ s/::/_/g;
    return uc($class);
}

sub class2prefix {
    my $class = shift || '';
    my $case  = shift || 0;
    my $prefix;
    if ( $class =~ /^.+?::([MVC]|Model|View|Controller)::(.+)$/ ) {
        $prefix = $case ? $2 : lc $2;
        $prefix =~ s{::}{/}g;
    }
    return $prefix;
}

sub class2tempdir {
    my $class  = shift || '';
    my $create = shift || 0;
    my @parts = split '::', lc $class;

    my $tmpdir = dir( File::Spec->tmpdir, @parts )->cleanup;

    if ( $create && !-e $tmpdir ) {

        eval { $tmpdir->mkpath };

        if ($@) {
            # FIXME
            #MyApp::Exception->throw(
            #    message => qq/Couldn't create tmpdir '$tmpdir', "$@"/ );
        }
    }

    return $tmpdir->stringify;
}

sub home {
    my $class = shift;

    # make an $INC{ $key } style string from the class name
    (my $file = "$class.pm") =~ s{::}{/}g;

    if ( my $inc_entry = $INC{$file} ) {
        {
            # look for an uninstalled Catalyst app

            # find the @INC entry in which $file was found
            (my $path = $inc_entry) =~ s/$file$//;
            my $home = dir($path)->absolute->cleanup;

            # pop off /lib and /blib if they're there
            $home = $home->parent while $home =~ /b?lib$/;

            # only return the dir if it has a Makefile.PL or Build.PL
            if (-f $home->file("Makefile.PL") or -f $home->file("Build.PL")) {

                # clean up relative path:
                # MyApp/script/.. -> MyApp

                my ($lastdir) = $home->dir_list( -1, 1 );
                if ( $lastdir eq '..' ) {
                    $home = dir($home)->parent->parent;
                }

                return $home->stringify;
            }
        }

        {
            # look for an installed Catalyst app

            # trim the .pm off the thing ( Foo/Bar.pm -> Foo/Bar/ )
            ( my $path = $inc_entry) =~ s/\.pm$//;
            my $home = dir($path)->absolute->cleanup;

            # return if if it's a valid directory
            return $home->stringify if -d $home;
        }
    }

    # we found nothing
    return 0;
}

sub prefix {
    my ( $class, $name ) = @_;
    my $prefix = &class2prefix($class);
    $name = "$prefix/$name" if $prefix;
    return $name;
}

sub env_value {
    my ( $class, $key ) = @_;

    $key = uc($key);
    my @prefixes = ( class2env($class), 'CATALYST' );

    for my $prefix (@prefixes) {
        if ( defined( my $value = $ENV{"${prefix}_${key}"} ) ) {
            return $value;
        }
    }

    return;
}

1;

lcipavlcipav2011/03/24 10:164IGWLn <a href="http://glbzfqoqlaox.com/">glbzfqoqlaox</a>, [url=http://ikimzilitduw.com/]ikimzilitduw[/url], [link=http://koddoycduxbb.com/]koddoycduxbb[/link], http://licjkrlymalo.com/

トラックバック - http://catalyst.g.hatena.ne.jp/dann/20080316