initial checkin of module files for proper perl installation
[freeside.git] / FS / FS / Conf.pm
1 package FS::Conf;
2
3 use vars qw($default_dir);
4 use IO::File;
5
6 =head1 NAME
7
8 FS::Conf - Read access to Freeside configuration values
9
10 =head1 SYNOPSIS
11
12   use FS::Conf;
13
14   $conf = new FS::Conf "/config/directory";
15
16   $FS::Conf::default_dir = "/config/directory";
17   $conf = new FS::Conf;
18
19   $dir = $conf->dir;
20
21   $value = $conf->config('key');
22   @list  = $conf->config('key');
23   $bool  = $conf->exists('key');
24
25 =head1 DESCRIPTION
26
27 Read access to Freeside configuration values.  Keys currently map to filenames,
28 but this may change in the future.
29
30 =head1 METHODS
31
32 =over 4
33
34 =item new [ DIRECTORY ]
35
36 Create a new configuration object.  A directory arguement is required if
37 $FS::Conf::default_dir has not been set.
38
39 =cut
40
41 sub new {
42   my($proto,$dir) = @_;
43   my($class) = ref($proto) || $proto;
44   my($self) = { 'dir' => $dir || $default_dir } ;
45   bless ($self, $class);
46 }
47
48 =item dir
49
50 Returns the directory.
51
52 =cut
53
54 sub dir {
55   my($self) = @_;
56   my $dir = $self->{dir};
57   -e $dir or die "FATAL: $dir doesn't exist!";
58   -d $dir or die "FATAL: $dir isn't a directory!";
59   -r $dir or die "FATAL: Can't read $dir!";
60   -x $dir or die "FATAL: $dir not searchable (executable)!";
61   $dir;
62 }
63
64 =item config 
65
66 Returns the configuration value or values (depending on context) for key.
67
68 =cut
69
70 sub config {
71   my($self,$file)=@_;
72   my($dir)=$self->dir;
73   my $fh = new IO::File "<$dir/$file" or return;
74   if ( wantarray ) {
75     map {
76       /^(.*)$/
77         or die "Illegal line (array context) in $dir/$file:\n$_\n";
78       $1;
79     } <$fh>;
80   } else {
81     <$fh> =~ /^(.*)$/
82       or die "Illegal line (scalar context) in $dir/$file:\n$_\n";
83     $1;
84   }
85 }
86
87 =item exists
88
89 Returns true if the specified key exists, even if the corresponding value
90 is undefined.
91
92 =cut
93
94 sub exists {
95   my($self,$file)=@_;
96   my($dir) = $self->dir;
97   -e "$dir/$file";
98 }
99
100 =back
101
102 =head1 BUGS
103
104 Write access (with locking) should be implemented.
105
106 =head1 SEE ALSO
107
108 config.html from the base documentation contains a list of configuration files.
109
110 =cut
111
112 1;