Overview

Packages

  • Kolab
    • Config

Classes

  • Horde_Kolab_Config

Exceptions

  • Horde_Kolab_Config_Exception
  • Overview
  • Package
  • Class
  • Tree
  1: <?php
  2: /**
  3:  * The Kolab Server configuration handler.
  4:  *
  5:  * PHP version 5
  6:  *
  7:  * @category Kolab
  8:  * @package  Kolab_Config
  9:  * @author   Gunnar Wrobel <wrobel@pardus.de>
 10:  * @license  http://www.horde.org/licenses/lgpl21 LGPL 2.1
 11:  * @link     http://pear.horde.org/index.php?package=Kolab_Config
 12:  */
 13: 
 14: /**
 15:  * The Kolab Server configuration handler.
 16:  *
 17:  * Copyright 2010 Klarälvdalens Datakonsult AB
 18:  *
 19:  * See the enclosed file COPYING for license information (LGPL). If you
 20:  * did not receive this file, see http://www.horde.org/licenses/lgpl21.
 21:  *
 22:  * @category Kolab
 23:  * @package  Kolab_Config
 24:  * @author   Gunnar Wrobel <wrobel@pardus.de>
 25:  * @license  http://www.horde.org/licenses/lgpl21 LGPL 2.1
 26:  * @link     http://pear.horde.org/index.php?package=Kolab_Config
 27:  */
 28: class Horde_Kolab_Config
 29: implements ArrayAccess
 30: {
 31:     /**
 32:      * The path to the directory holding the configuration files.
 33:      *
 34:      * @var string
 35:      */
 36:     private $_directory;
 37: 
 38:     /**
 39:      * Name of the global configuration file.
 40:      *
 41:      * @var string
 42:      */
 43:     private $_global;
 44: 
 45:     /**
 46:      * Name of the local configuration file.
 47:      *
 48:      * @var string
 49:      */
 50:     private $_local;
 51: 
 52:     /**
 53:      * The configuration data.
 54:      *
 55:      * @var array
 56:      */
 57:     private $_data;
 58: 
 59:     /**
 60:      * Constructor.
 61:      *
 62:      * @param string $directory The path to the directory holding the
 63:      *                          configuration files.
 64:      * @param string $global    Name of the global configuration file.
 65:      * @param string $local     Name of the local configuration file.
 66:      */
 67:     public function __construct(
 68:         $directory = '@kolab_server_dir@/etc/kolab',
 69:         $global = 'kolab.globals',
 70:         $local = 'kolab.conf'
 71:     ) {
 72:         if ($directory === '@kolab_server_dir@/etc/kolab') {
 73:             $this->_directory = '/kolab/etc/kolab';
 74:         } else {            
 75:             $this->_directory = realpath($directory);
 76:         }
 77:         $this->_global    = $global;
 78:         $this->_local     = $local;
 79:     }
 80: 
 81:     /**
 82:      * Read the configuration files.
 83:      *
 84:      * @return NULL
 85:      */
 86:     public function read()
 87:     {
 88:         if (!file_exists($this->_getGlobalConfigFilePath())
 89:             && !file_exists($this->_getLocalConfigFilePath())) {
 90:             throw new Horde_Kolab_Config_Exception(
 91:                 'No configuration files found in ' . $this->_directory . '.'
 92:             );
 93:         }
 94: 
 95:         if (file_exists($this->_getGlobalConfigFilePath())) {
 96:             $this->_loadConfigurationFile(
 97:                 $this->_getGlobalConfigFilePath()
 98:             );
 99:         }
100: 
101:         if (file_exists($this->_getLocalConfigFilePath())) {
102:             $this->_loadConfigurationFile(
103:                 $this->_getLocalConfigFilePath()
104:             );
105:         }
106:     }
107: 
108:     private function _loadConfigurationFile($path)
109:     {
110:         if ($this->_data === null) {
111:             $this->_data = array();
112:         }
113: 
114:         $fh = fopen($path, 'r');
115: 
116:         while (!feof($fh)) {
117:             $line = trim(fgets($fh));
118:             if ($line && preg_match('/^([^:#]+):(.*)/', $line, $matches)) {
119:                 $this->_data[trim($matches[1])] = trim($matches[2]);
120:             }
121:         }
122:         fclose($fh);
123:     }
124: 
125:     /**
126:      * Return the path to the global configuration file.
127:      *
128:      * @return NULL
129:      */
130:     private function _getGlobalConfigFilePath()
131:     {
132:         return $this->_directory . DIRECTORY_SEPARATOR . $this->_global;
133:     }
134: 
135:     /**
136:      * Return the path to the local configuration file.
137:      *
138:      * @return NULL
139:      */
140:     private function _getLocalConfigFilePath()
141:     {
142:         return $this->_directory . DIRECTORY_SEPARATOR . $this->_local;
143:     }
144: 
145:     /**
146:      * Initialize this object if this has not happened yet.
147:      *
148:      * @return NULL
149:      */
150:     private function _init()
151:     {
152:         if ($this->_data === null) {
153:             $this->read();
154:         }
155:     }
156: 
157:     /**
158:      * Return the value for the given array key.
159:      *
160:      * @param string $key The key.
161:      *
162:      * @return mixed The value for the given key.
163:      */
164:     public function offsetGet($key)
165:     {
166:         if (!$this->offsetExists($key)) {
167:             throw new Horde_Kolab_Config_Exception(
168:                 'Parameter "' . $key . '" has no value!'
169:             );
170:         }
171:         return $this->_data[$key];
172:     }
173: 
174:     /**
175:      * Does the requested array value exist in the configuration?
176:      *
177:      * @param string $key The key.
178:      *
179:      * @return boolean True if the configuration value exists.
180:      */
181:     public function offsetExists($key)
182:     {
183:         $this->_init();
184:         if (!is_string($key) || empty($key)) {
185:             throw new InvalidArgumentException(
186:                 'The key must be a non-empty string!'
187:             );
188:         }
189:         return isset($this->_data[$key]);
190:     }
191: 
192:     /**
193:      * Set the given key to the provided value.
194:      *
195:      * @param string $key   The key.
196:      * @param mixed  $value The value that should be stored.
197:      *
198:      * @return NULL
199:      */
200:     public function offsetSet($key, $value)
201:     {
202:     }
203: 
204:     /**
205:      * Delete the value identified by the given key.
206:      *
207:      * @param string $key The key.
208:      *
209:      * @return NULL
210:      */
211:     public function offsetUnset($key)
212:     {
213:     }
214: }
API documentation generated by ApiGen