21 namespace Erebot\Config;
60 \SimpleXMLElement $xml
63 $this->modules = array();
65 if (isset($xml[
'language'])) {
66 $this->locale = (string) $xml[
'language'];
71 if (!isset($xml->modules->module)) {
75 foreach ($xml->modules->module as $module) {
77 $instance = new \Erebot\Config\Module($module);
78 $this->modules[$instance->getName()] = $instance;
99 throw new \Exception(
"Cloning forbidden!");
102 protected static function getBaseDir($component)
104 $reflector = new \ReflectionClass($component);
105 $parts = explode(DIRECTORY_SEPARATOR, $reflector->getFileName());
107 $last = array_pop($parts);
108 }
while ($last !==
'src' && count($parts));
111 $base = implode(DIRECTORY_SEPARATOR, $parts);
118 if (isset($this->locale)) {
119 $domain = str_replace(
'\\',
'_', ltrim($component,
'\\'));
120 $localedir = static::getBaseDir($component);
121 return \Erebot\Intl\GettextFactory::translation($domain, $localedir, array($this->locale));
124 if ($this->proxified === $this) {
125 throw new \Erebot\NotFoundException(
'No translator associated');
128 return $this->proxified->getTranslator($component);
134 if ($this->proxified === $this) {
137 return $this->proxified->getMainCfg();
143 if (!is_bool($recursive)) {
144 throw new \Erebot\InvalidValueException(
145 'Invalid value for recursion'
149 if ($recursive && $this->proxified !== $this) {
150 $inherited = $this->proxified->getModules(
true);
152 $inherited = array();
157 foreach ($this->modules as $name => $module) {
158 if ($module->isActive()) {
165 $inherited = array_diff($inherited, $removed);
166 return array_merge($added, $inherited);
172 $moduleName =
'\\' . ltrim($moduleName,
'\\');
173 if (!isset($this->modules[$moduleName])) {
174 if ($this->proxified !== $this) {
175 return $this->proxified->getModule($moduleName);
177 throw new \Erebot\NotFoundException(
'No such module "' .
180 return $this->modules[$moduleName];
205 $value = strtolower($value);
206 if (in_array($value, array(
'true',
'1',
'on',
'yes'),
true)) {
209 if (in_array($value, array(
'false',
'0',
'off',
'no'),
true)) {
234 if (is_int($value)) {
238 if (ctype_digit($value)) {
242 if (strpos(
'+-', $value[0]) !==
false && ctype_digit(substr($value, 1))) {
264 if (!is_numeric($value)) {
268 return (
double) $value;
322 if (!isset($this->modules[$module])) {
323 throw new \Erebot\NotFoundException(
'No such module');
325 $value = $this->modules[$module]->getParam($param);
326 $value = $parser($value);
327 if ($value !== null) {
330 throw new \Erebot\InvalidValueException(
331 'Bad value in configuration'
333 }
catch (\Erebot\NotFoundException $e) {
334 if ($this->proxified !== $this) {
335 return $this->proxified->$origin($module, $param, $default);
338 if ($default === null) {
339 throw new \Erebot\NotFoundException(
'No such parameter "' .
340 $param .
'" for module "' .
344 if ($checker($default)) {
347 throw new \Erebot\InvalidValueException(
'Bad default value');
352 public function parseBool($module, $param, $default = null)
358 array($this,
'parseBoolHelper'),
378 public function parseInt($module, $param, $default = null)
384 array($this,
'parseIntHelper'),
391 public function parseReal($module, $param, $default = null)
397 array($this,
'parseRealHelper'),
parseReal($module, $param, $default=null)
$locale
The current locale.
parseSomething($module, $param, $default, callable $parser, $origin, callable $checker)
parseBool($module, $param, $default=null)
static parseBoolHelper($value)
getTranslator($component)
__construct(\Erebot\Interfaces\Config\Proxy $proxified,\SimpleXMLElement $xml)
static parseRealHelper($value)
$proxified
Reference to a proxified object.
parseInt($module, $param, $default=null)
parseString($module, $param, $default=null)
static parseIntHelper($value)
A configuration proxy which cascades settings.
$modules
Array of modules loaded at this particular configuration level.