This commit was manufactured by cvs2svn to create branch 'freeside_import'.
[freeside.git] / site_perl / Conf.pm
1 package FS::Conf;
2
3 use vars qw($default_dir);
4 use IO::File;
5
6 $default_dir='/var/spool/freeside/conf';
7
8 =head1 NAME
9
10 FS::Conf - Read access to Freeside configuration values
11
12 =head1 SYNOPSIS
13
14   use FS::Conf;
15
16   $conf = new FS::Conf;
17   $conf = new FS::Conf "/non/standard/config/directory";
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.  Optionally, a non-default directory may
37 be specified.
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   $self->{dir};
57 }
58
59 =item config 
60
61 Returns the configuration value or values (depending on context) for key.
62
63 =cut
64
65 sub config {
66   my($self,$file)=@_;
67   my($dir)=$self->dir;
68   my $fh = new IO::File "<$dir/$file" or return;
69   if ( wantarray ) {
70     map {
71       /^(.*)$/ or die "Illegal line in $dir/$file:\n$_\n";
72       $1;
73     } <$fh>;
74   } else {
75     <$fh> =~ /^(.*)$/ or die "Illegal line in $dir/$file:\n$_\n";
76     $1;
77   }
78 }
79
80 =item exists
81
82 Returns true if the specified key exists, even if the corresponding value
83 is undefined.
84
85 =cut
86
87 sub exists {
88   my($self,$file)=@_;
89   my($dir) = $self->dir;
90   -e "$dir/$file";
91 }
92
93 =back
94
95 =head1 BUGS
96
97 The option to specify a non-default directory should probably be removed.
98
99 Write access (with locking) should be implemented.
100
101 =head1 SEE ALSO
102
103 config.html from the base documentation contains a list of configuration files.
104
105 =head1 HISTORY
106
107 Ivan Kohler <ivan@sisd.com> 98-sep-6
108
109 sub exists forgot to fetch $dir ivan@sisd.com 98-sep-27
110
111 =cut
112
113 1;