File: config.class.php

package info (click to toggle)
serverstats 0.8.2-10
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd, wheezy
  • size: 604 kB
  • ctags: 796
  • sloc: php: 4,238; perl: 90; sh: 15; makefile: 12
file content (180 lines) | stat: -rw-r--r-- 3,908 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
<?php
/**
 * $Id$
 *
 * Author: David Danier, david.danier@team23.de
 * Project: Serverstats, http://serverstats.berlios.de/
 * License: LGPL v2.1 or later (http://www.gnu.org/licenses/lgpl.html)
 *
 * Copyright (C) 2005 David Danier
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 */

class config implements ArrayAccess, IteratorAggregate
{
	protected $dir;
	protected $vars = array();
	protected $rootConfig;
	
	public function __construct($dir, $rootConfig = null)
	{
		$this->dir = $dir;
		if (isset($rootConfig))
		{
			if ($rootConfig instanceof config)
			{
				$this->rootConfig = $rootConfig;
			}
			else
			{
				throw new Exception('Config: Param error');
			}
		}
		else
		{
			$this->rootConfig = $this;
		}
	}
	
	private function isOffsetValid($offset)
	{
		return preg_match('/^[a-zA-Z0-9_]+$/', $offset);
	}
	
	private function checkOffset($offset)
	{
		if (!$this->isOffsetValid($offset))
		{
			throw new Exception('Offset "' . $offset . '" is not allowed');
		}
	}
	
	public function offsetExists($offset)
	{
		if (isset($this->vars[$offset]))
		{
			return true;
		}
		else
		{
			$this->checkOffset($offset);
			if (is_readable($this->dir . $offset . '.php') || is_dir($this->dir . $offset))
			{
				return true;
			}
			else
			{
				return false;
			}
		}
	}
	
	protected static function loadConfig(&$config, $file, $rootConfig)
	{
		if (require_once($file))
		{
			if (isset($config))
			{
				return $config;
			}
			else
			{
				throw new Exception('Error while loading "' . $file . '" (Not a valid config?)');
			}
		}
		else
		{
			throw new Exception('File "' . $file . '" could not be loaded');
		}
	}
	
	protected function loadOffset($offset)
	{
		$this->checkOffset($offset);
		if (is_readable($this->dir . $offset . '.php'))
		{
			$this->vars[$offset] = array();
			self::loadConfig($this->vars[$offset], $this->dir . $offset . '.php', $this->rootConfig);
		}
		elseif (is_dir($this->dir . $offset))
		{
			$this->vars[$offset] = array();
		}
		else
		{
			throw new Exception('Offset "' . $offset . '" does not exist');
		}
		return $this->offsetGet($offset);
	}
	
	public function offsetGet($offset)
	{
		if (!isset($this->vars[$offset]))
		{
			$this->loadOffset($offset);
		}
		if (is_array($this->vars[$offset]))
		{
			$subconfig = new
				config(
					$this->dir . $offset . DIRECTORY_SEPARATOR,
					$this->rootConfig);
			$subconfig->vars = &$this->vars[$offset];
			return $subconfig;
		}
		else
		{
			return $this->vars[$offset];
		}
	}
	
	public function getIterator()
	{
		if (is_dir($this->dir))
		{
			$files = scandir($this->dir);
			foreach ($files as $file)
			{
				if (preg_match('/^([a-zA-Z0-9_]+)(\.php)?$/', $file, $parts))
				{
					$offset = $parts[1];
					if (!isset($this->vars[$offset]))
					{
						$this->loadOffset($offset);
					}
				}
			}
		}
		$iteratorVars = array();
		foreach (array_keys($this->vars) as $offset)
		{
			$iteratorVars[$offset] = $this->offsetGet($offset);
		}
		return new ArrayIterator($iteratorVars);
	}
	
	public function offsetSet($offset, $value)
	{
		throw new Exception('Config may not be changed');
	}
	
	public function offsetUnset($offset)
	{
		throw new Exception('Config may not be changed');
	}
}

?>