File Coverage

blib/lib/OpenInteract2/Config.pm
Criterion Covered Total %
statement 13 15 86.6
branch n/a
condition n/a
subroutine 5 5 100.0
pod n/a
total 18 20 90.0


line stmt bran cond sub pod time code
1             package OpenInteract2::Config;
2              
3             # $Id: Config.pm,v 1.16 2005/03/18 04:09:48 lachoy Exp $
4              
5 85     85   537 use strict;
  85         187  
  85         3991  
6 85     85   591 use base qw( Class::Factory );
  85         199  
  85         99610  
7 85     85   281716 use Log::Log4perl qw( get_logger );
  85         248  
  85         1156  
8 85     85   6054 use OpenInteract2::Constants qw( :log );
  85         207  
  85         28561  
9 85     85   61548 use OpenInteract2::Exception qw( oi_error );
  0            
  0            
10              
11             $OpenInteract2::Config::VERSION = sprintf("%d.%02d", q$Revision: 1.16 $ =~ /(\d+)\.(\d+)/);
12              
13             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             sub new {
23             my ( $pkg, $type, @params ) = @_;
24             unless ( $type ) {
25             my @types = __PACKAGE__->get_loaded_types;
26             oi_error "You must specify a configuration type in 'new()'; ",
27             "valid types are: ", join( ', ', @types );
28             }
29             my $class = eval { $pkg->get_factory_class( $type ) };
30             oi_error $@ if ( $@ ) ;
31             my $data = $class->read_config( @params );
32             return bless( $data, $class );
33             }
34              
35              
36             sub is_file_valid {
37             my ( $class, $filename ) = @_;
38             unless ( -f $filename ) {
39             oi_error "Config file '$filename' does not exist";
40             }
41             }
42              
43              
44             sub read_file {
45             my ( $class, $filename ) = @_;
46             $log ||= get_logger( LOG_CONFIG );
47             $log->is_debug &&
48             $log->debug( "Config trying to read file '$filename'" );
49             $class->is_file_valid( $filename );
50             open( CONF, '<', $filename )
51             || oi_error "Cannot read config '$filename': $!";
52             my @lines = ;
53             close( CONF );
54             return \@lines;
55             }
56              
57              
58             ##############################
59             # OBJECT METHODS
60              
61             sub translate_dirs {
62             my ( $self ) = @_;
63             return unless ( ref $self->{dir} eq 'HASH' );
64             $log ||= get_logger( LOG_CONFIG );
65             if ( $self->{dir}{_IS_TRANSLATED_} ) {
66             $log->is_info &&
67             $log->info( "Directories already translated, no action" );
68             return;
69             }
70             my $site_dir = $self->{dir}{website};
71             if ( $site_dir =~ s#(\\|/)$## ) {
72             $self->{dir}{website} = $site_dir;
73             }
74             unless ( $site_dir ) {
75             $log->error( "The config key 'dir.website' must be defined" );
76             oi_error "Define 'dir.website' before continuing";
77             }
78              
79             while ( my ( $dir_type, $dir_spec ) = each %{ $self->{dir} } ) {
80             next unless ( $dir_spec );
81             next if ( $dir_spec eq 'website' );
82             my @pieces = split /\//, $dir_spec;
83             if ( $pieces[0] eq '$WEBSITE' ) {
84             $pieces[0] = $site_dir;
85             }
86             my $full_path = File::Spec->catdir( @pieces );
87             $self->{dir}{ $dir_type } = $full_path;
88             $log->is_debug &&
89             $log->debug( "Set $dir_type = $full_path" );
90             }
91             return $self->{dir}{_IS_TRANSLATED_} = 1;
92             }
93              
94              
95             ########################################
96             # SUBCLASS INTERFACE
97              
98             # Subclasses should override these
99              
100             sub read_config {
101             oi_error 'Implementation must define read_config()';
102             }
103              
104             sub save_config {
105             oi_error 'Implementation must define save_config()';
106             }
107              
108             ########################################
109             # FACTORY
110              
111             sub factory_log {
112             my ( $self, @msg ) = @_;
113             get_logger( LOG_CONFIG )->info( @msg );
114             }
115              
116             sub factory_error {
117             my ( $self, @msg ) = @_;
118             get_logger( LOG_CONFIG )->error( @msg );
119             die @msg, "\n";
120             }
121              
122             # Initialize built-in configuration types
123              
124             __PACKAGE__->register_factory_type(
125             perl => 'OpenInteract2::Config::PerlFile' );
126             __PACKAGE__->register_factory_type(
127             ini => 'OpenInteract2::Config::IniFile' );
128              
129             1;
130              
131             __END__