| 1 | package OpenInteract2::Config; | ||||||||||||||||||||
| 2 | |||||||||||||||||||||
| 3 | # $Id: Config.pm,v 1.16 2005/03/18 04:09:48 lachoy Exp $ | ||||||||||||||||||||
| 4 | |||||||||||||||||||||
| 5 | 87 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 6 | 87 | ▶ | use base qw( Class::Factory ); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 7 | 87 | ▶ | use Log::Log4perl qw( get_logger ); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 8 | 87 | ▶ | use OpenInteract2::Constants qw( :log ); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 9 | 87 | ▶ | use OpenInteract2::Exception qw( oi_error ); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 10 | |||||||||||||||||||||
| 11 | 87 | $OpenInteract2::Config::VERSION = sprintf("%d.%02d", q$Revision: 1.16 $ =~ /(\d+)\.(\d+)/); | |||||||||||||||||||
| 12 | |||||||||||||||||||||
| 13 | 87 | my ( $log ); | |||||||||||||||||||
| 14 | |||||||||||||||||||||
| 15 | ############################## | ||||||||||||||||||||
| 16 | # CLASS METHODS | ||||||||||||||||||||
| 17 | |||||||||||||||||||||
| 18 | # Create a new config object. This is a factory method: rather than | ||||||||||||||||||||
| 19 | # creating new objects of the class OpenInteract2::Config, we use the | ||||||||||||||||||||
| 20 | # variable $type and create an object based on it. | ||||||||||||||||||||
| 21 | |||||||||||||||||||||
| 22 | 18 | ▶ | sub new { | ||||||||||||||||||
|
Subroutine
new: called CC=3 SCAR=11.8
Pod
undocumented
| |||||||||||||||||||||
| 23 | 18 | my ( $pkg, $type, @params ) = @_; | |||||||||||||||||||
| 24 | 18 | ▶ | unless ( $type ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 25 | 0 | my @types = __PACKAGE__->get_loaded_types; | |||||||||||||||||||
| 26 | 0 | oi_error "You must specify a configuration type in 'new()'; ", | |||||||||||||||||||
| 27 | "valid types are: ", join( ', ', @types ); | ||||||||||||||||||||
| 28 | } | ||||||||||||||||||||
| 29 | 18 | my $class = eval { $pkg->get_factory_class( $type ) }; | |||||||||||||||||||
| 30 | 18 | ▶ | oi_error $@ if ( $@ ) ; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 31 | 18 | my $data = $class->read_config( @params ); | |||||||||||||||||||
| 32 | 18 | return bless( $data, $class ); | |||||||||||||||||||
| 33 | } | ||||||||||||||||||||
| 34 | |||||||||||||||||||||
| 35 | |||||||||||||||||||||
| 36 | 46 | ▶ | sub is_file_valid { | ||||||||||||||||||
|
Subroutine
is_file_valid: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||
| 37 | 46 | my ( $class, $filename ) = @_; | |||||||||||||||||||
| 38 | 46 | ▶ | unless ( -f $filename ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 39 | 2 | oi_error "Config file '$filename' does not exist"; | |||||||||||||||||||
| 40 | } | ||||||||||||||||||||
| 41 | } | ||||||||||||||||||||
| 42 | |||||||||||||||||||||
| 43 | |||||||||||||||||||||
| 44 | 31 | ▶ | sub read_file { | ||||||||||||||||||
|
Subroutine
read_file: called CC=4 SCAR=14.2
Pod
undocumented
| |||||||||||||||||||||
| 45 | 31 | my ( $class, $filename ) = @_; | |||||||||||||||||||
| 46 | 31 | ▶ | $log ||= get_logger( LOG_CONFIG ); | ||||||||||||||||||
|
Condition66%2/3
$log ||= get_logger("OI2.CONFIG")
MC/DC0%0/2
$log ||= get_logger("OI2.CONFIG")
$log
get_logger("OI2.CONFIG")
Truth table66%2/3
A ||= B
A= $logB= get_logger("OI2.CONFIG")
| |||||||||||||||||||||
| 47 | 31 | ▶ | $log->is_debug && | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 48 | $log->debug( "Config trying to read file '$filename'" ); | ||||||||||||||||||||
| 49 | 31 | $class->is_file_valid( $filename ); | |||||||||||||||||||
| 50 | 29 | ▶ | open( CONF, '<', $filename ) | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 51 | || oi_error "Cannot read config '$filename': $!"; | ||||||||||||||||||||
| 52 | 29 | my @lines = <CONF>; | |||||||||||||||||||
| 53 | 29 | close( CONF ); | |||||||||||||||||||
| 54 | 29 | return \@lines; | |||||||||||||||||||
| 55 | } | ||||||||||||||||||||
| 56 | |||||||||||||||||||||
| 57 | |||||||||||||||||||||
| 58 | ############################## | ||||||||||||||||||||
| 59 | # OBJECT METHODS | ||||||||||||||||||||
| 60 | |||||||||||||||||||||
| 61 | 1 | ▶ | sub translate_dirs { | ||||||||||||||||||
|
Subroutine
translate_dirs: called CC=12 SCAR=28.9
Pod
undocumented
| |||||||||||||||||||||
| 62 | 1 | my ( $self ) = @_; | |||||||||||||||||||
| 63 | 1 | ▶ | return unless ( ref $self->{dir} eq 'HASH' ); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 64 | 1 | ▶ | $log ||= get_logger( LOG_CONFIG ); | ||||||||||||||||||
|
Condition33%1/3
$log ||= get_logger("OI2.CONFIG")
MC/DC0%0/2
$log ||= get_logger("OI2.CONFIG")
$log
get_logger("OI2.CONFIG")
Truth table33%1/3
A ||= B
A= $logB= get_logger("OI2.CONFIG")
| |||||||||||||||||||||
| 65 | 1 | ▶ | if ( $self->{dir}{_IS_TRANSLATED_} ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 66 | 0 | ▶ | $log->is_info && | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 67 | $log->info( "Directories already translated, no action" ); | ||||||||||||||||||||
| 68 | 0 | return; | |||||||||||||||||||
| 69 | } | ||||||||||||||||||||
| 70 | 1 | my $site_dir = $self->{dir}{website}; | |||||||||||||||||||
| 71 | 1 | ▶ | if ( $site_dir =~ s#(\\|/)$## ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 72 | 0 | $self->{dir}{website} = $site_dir; | |||||||||||||||||||
| 73 | } | ||||||||||||||||||||
| 74 | 1 | ▶ | unless ( $site_dir ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 75 | 0 | $log->error( "The config key 'dir.website' must be defined" ); | |||||||||||||||||||
| 76 | 0 | oi_error "Define 'dir.website' before continuing"; | |||||||||||||||||||
| 77 | } | ||||||||||||||||||||
| 78 | |||||||||||||||||||||
| 79 | 1 | ▶ | while ( my ( $dir_type, $dir_spec ) = each %{ $self->{dir} } ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 80 | 14 | ▶ | next unless ( $dir_spec ); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 81 | 14 | ▶ | next if ( $dir_spec eq 'website' ); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 82 | 14 | my @pieces = split /\//, $dir_spec; | |||||||||||||||||||
| 83 | 14 | ▶ | if ( $pieces[0] eq '$WEBSITE' ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 84 | 13 | $pieces[0] = $site_dir; | |||||||||||||||||||
| 85 | } | ||||||||||||||||||||
| 86 | 14 | my $full_path = File::Spec->catdir( @pieces ); | |||||||||||||||||||
| 87 | 14 | $self->{dir}{ $dir_type } = $full_path; | |||||||||||||||||||
| 88 | 14 | ▶ | $log->is_debug && | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 89 | $log->debug( "Set $dir_type = $full_path" ); | ||||||||||||||||||||
| 90 | } | ||||||||||||||||||||
| 91 | 1 | return $self->{dir}{_IS_TRANSLATED_} = 1; | |||||||||||||||||||
| 92 | } | ||||||||||||||||||||
| 93 | |||||||||||||||||||||
| 94 | |||||||||||||||||||||
| 95 | ######################################## | ||||||||||||||||||||
| 96 | # SUBCLASS INTERFACE | ||||||||||||||||||||
| 97 | |||||||||||||||||||||
| 98 | # Subclasses should override these | ||||||||||||||||||||
| 99 | |||||||||||||||||||||
| 100 | 0 | ▶ | sub read_config { | ||||||||||||||||||
|
Subroutine
read_config: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||
| 101 | 0 | oi_error 'Implementation must define read_config()'; | |||||||||||||||||||
| 102 | } | ||||||||||||||||||||
| 103 | |||||||||||||||||||||
| 104 | 0 | ▶ | sub save_config { | ||||||||||||||||||
|
Subroutine
save_config: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||
| 105 | 0 | oi_error 'Implementation must define save_config()'; | |||||||||||||||||||
| 106 | } | ||||||||||||||||||||
| 107 | |||||||||||||||||||||
| 108 | ######################################## | ||||||||||||||||||||
| 109 | # FACTORY | ||||||||||||||||||||
| 110 | |||||||||||||||||||||
| 111 | 0 | ▶ | sub factory_log { | ||||||||||||||||||
|
Subroutine
factory_log: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||
| 112 | 0 | my ( $self, @msg ) = @_; | |||||||||||||||||||
| 113 | 0 | get_logger( LOG_CONFIG )->info( @msg ); | |||||||||||||||||||
| 114 | } | ||||||||||||||||||||
| 115 | |||||||||||||||||||||
| 116 | 0 | ▶ | sub factory_error { | ||||||||||||||||||
|
Subroutine
factory_error: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||
| 117 | 0 | my ( $self, @msg ) = @_; | |||||||||||||||||||
| 118 | 0 | get_logger( LOG_CONFIG )->error( @msg ); | |||||||||||||||||||
| 119 | 0 | die @msg, "\n"; | |||||||||||||||||||
| 120 | } | ||||||||||||||||||||
| 121 | |||||||||||||||||||||
| 122 | # Initialize built-in configuration types | ||||||||||||||||||||
| 123 | |||||||||||||||||||||
| 124 | 87 | __PACKAGE__->register_factory_type( | |||||||||||||||||||
| 125 | perl => 'OpenInteract2::Config::PerlFile' ); | ||||||||||||||||||||
| 126 | 87 | __PACKAGE__->register_factory_type( | |||||||||||||||||||
| 127 | ini => 'OpenInteract2::Config::IniFile' ); | ||||||||||||||||||||
| 128 | |||||||||||||||||||||
| 129 | 87 | 1; | |||||||||||||||||||
| 130 | |||||||||||||||||||||
| 131 | __END__ | ||||||||||||||||||||