1: <?php
2: namespace Datadepo\Api;
3:
4: class IniConfiguration
5: {
6:
7:
8: protected $iniPath;
9:
10:
11: protected $settings;
12:
13:
14: protected $rewriteOptions;
15:
16: 17: 18: 19:
20: public function __construct($iniPath, array $rewriteOptions = array())
21: {
22: $this->iniPath = $iniPath;
23: $this->rewriteOptions = $rewriteOptions;
24: }
25:
26: 27: 28: 29: 30:
31: public function get($section, $variable = NULL)
32: {
33: $this->read();
34: if (!isset($this->settings[$section])) {
35: throw new ConfigurationException('Section ' . $section . ' not found (' . $this->iniPath . ')');
36: }
37: $sectionData = $this->settings[$section];
38: if ($variable !== NULL) {
39: if (!isset($sectionData[$variable])) {
40: throw new ConfigurationException('Variable '.$variable.' not found in ' . $section . ' ('.$this->iniPath.')');
41: }
42: return $sectionData[$variable];
43: }
44: return $sectionData;
45: }
46:
47: 48: 49:
50: public function getTempPath()
51: {
52: $temp = $this->get('local', 'tempPath');
53: if (!is_writable($temp)) {
54: throw new ApiException('Directory ' . $temp . ' is not writable or doesn\'t exist.');
55: }
56: return $temp;
57: }
58:
59: 60:
61: protected function read()
62: {
63: if ($this->settings !== NULL) {
64: return;
65: }
66:
67: if (!is_file($this->iniPath)) {
68: throw new ConfigurationException('Ini file ' . $this->iniPath . ' not found.');
69: }
70:
71: $this->settings = parse_ini_file($this->iniPath, TRUE);
72: $this->settings = array_replace_recursive($this->settings, $this->rewriteOptions);
73: }
74:
75: }