| 1 | package Ubic::ServiceLoader::Ext::ini; | ||
| 2 | $Ubic::ServiceLoader::Ext::ini::VERSION = '1.59'; | ||
| 3 | # ABSTRACT: loader for ini-style configs | ||
| 4 | |||
| 5 | |||
| 6 | use strict; | ||
| 7 | use warnings; | ||
| 8 | |||
| 9 | use parent qw( Ubic::ServiceLoader::Base ); | ||
| 10 | |||
| 11 | use Config::Tiny; | ||
| 12 | |||
| 13 | sub new { | ||
| 14 | my $class = shift; | ||
| 15 | return bless {} => $class; | ||
| 16 | } | ||
| 17 | |||
| 18 | sub load { | ||
| 19 | my $self = shift; | ||
| 20 | my ($file) = @_; | ||
| 21 | |||
| 22 | my $config = Config::Tiny->read($file); | ||
| 23 | unless ($config) { | ||
| 24 | die Config::Tiny->errstr; | ||
| 25 | } | ||
| 26 | |||
| 27 | my $root_section = delete $config->{_}; | ||
| 28 | my $module = delete $root_section->{module} || 'Ubic::Service::SimpleDaemon'; | ||
| 29 | if (keys %$root_section) { | ||
| 30 | die "Unknown option ".join(', ', keys %$root_section)." in file $file"; | ||
| 31 | } | ||
| 32 | |||
| 33 | my $options = delete $config->{options}; | ||
| 34 | if (keys %$config) { | ||
| 35 | die "Unknown section ".join(', ', keys %$config)." in file $file"; | ||
| 36 | } | ||
| 37 | |||
| 38 | $module =~ /^[\w:]+$/ or die "Invalid module name '$module'"; | ||
| 39 | eval "require $module"; # TODO - Class::Load? | ||
| 40 | if ($@) { | ||
| 41 | die $@; | ||
| 42 | } | ||
| 43 | |||
| 44 | my @options = (); | ||
| 45 | @options = ($options) if $options; # some modules can have zero options, I guess | ||
| 46 | return $module->new(@options); | ||
| 47 | } | ||
| 48 | |||
| 49 | 1; | ||
| 50 | |||
| 51 | __END__ |