21 namespace Erebot\Config;
89 \Erebot\Intl\TranslatorInterface $translator
91 $this->proxified = null;
92 $this->modules = array();
93 $this->configFile = null;
94 $this->coreTranslator = $translator;
95 $this->
load($configData, $source);
103 parent::__destruct();
112 throw new \Exception(
'Cloning is forbidden');
127 $xpath = new \DOMXPath($domxml);
130 foreach ($wrappers as $wrapper) {
131 for ($i = $wrapper->childNodes->length; $i > 0; $i--) {
132 $wrapper->parentNode->insertBefore(
133 $wrapper->childNodes->item($i - 1),
134 $wrapper->nextSibling
137 $wrapper->parentNode->removeChild($wrapper);
142 public function load($configData, $source)
144 $logger = \Plop\Plop::getInstance();
145 $possibleSources = array(
146 self::LOAD_FROM_FILE,
147 self::LOAD_FROM_STRING,
149 if (!in_array($source, $possibleSources,
true)) {
150 throw new \Erebot\InvalidValueException(
'Invalid $source');
153 if ($source == self::LOAD_FROM_FILE) {
154 if (is_string($configData) && $configData !=
"") {
155 if (!strncasecmp(PHP_OS,
"Win", 3)) {
156 if (!in_array($configData[0], array(
"/",
"\\")) &&
157 strlen($configData) > 1 && $configData[1] !=
":") {
158 $configData = getcwd() . DIRECTORY_SEPARATOR .
161 } elseif ($configData[0] != DIRECTORY_SEPARATOR) {
162 $configData = getcwd() . DIRECTORY_SEPARATOR . $configData;
164 $file = \Erebot\URI::fromAbsPath($configData,
false);
165 } elseif (is_object($configData) &&
166 $configData instanceof \Erebot\URIInterface) {
169 throw new \Erebot\InvalidValueException(
170 "Invalid configuration file"
173 } elseif (!is_string($configData)) {
174 throw new \Erebot\InvalidValueException(
175 "Invalid configuration file"
181 $mainSchema = dirname(dirname(__DIR__)) .
182 DIRECTORY_SEPARATOR .
'data' .
183 DIRECTORY_SEPARATOR .
'config.rng';
184 $mainSchema = file_get_contents($mainSchema);
185 $ue = libxml_use_internal_errors(
true);
186 $domxml = new \Erebot\DOM();
187 if ($source == self::LOAD_FROM_FILE) {
188 $domxml->load((
string) $file);
190 $domxml->loadXML($configData);
193 $domxml->xinclude(LIBXML_NOBASEFIX);
196 $ok = $domxml->relaxNGValidateSource($mainSchema);
197 $errors = $domxml->getErrors();
198 libxml_use_internal_errors($ue);
200 if (!$ok || count($errors)) {
203 $logger->error(print_r($errors,
true));
204 throw new \Erebot\InvalidValueException(
205 'Errors were found while validating the configuration file'
209 $xml = simplexml_import_dom($domxml);
210 parent::__construct($this, $xml);
212 if (!isset($xml[
'version'])) {
213 $this->version = null;
215 $this->version = (string) $xml[
'version'];
218 if (!isset($xml[
'timezone'])) {
219 throw new \Erebot\InvalidValueException(
'No timezone defined');
221 $this->timezone = (string) $xml[
'timezone'];
225 if (function_exists(
'date_default_timezone_set')) {
226 if (!date_default_timezone_set($this->timezone)) {
227 throw \Erebot\InvalidValueException(
229 'Invalid timezone: "%s"',
237 ? $this->
parseBool((
string) $xml[
'daemon'])
239 $userIdentity = isset($xml[
'uid']) ? ((string) $xml[
'uid']) : null;
240 $groupIdentity = isset($xml[
'gid']) ? ((string) $xml[
'gid']) : null;
242 ? ((string) $xml[
'pidfile'])
246 throw new \Erebot\InvalidValueException(
'Invalid "daemon" value');
249 if (!isset($xml[
'commands-prefix'])) {
250 $this->commandsPrefix =
'!';
252 $this->commandsPrefix = (string) $xml[
'commands-prefix'];
253 if (strcspn($this->commandsPrefix,
" \r\n\t") !=
254 strlen($this->commandsPrefix)) {
255 throw new \Erebot\InvalidValueException(
256 'Invalid command prefix'
262 $this->coreTranslator->gettext(
263 'Loaded configuration data'
267 $this->networks = array();
268 foreach ($xml->networks->network as $netCfg) {
270 $newConfig = new \Erebot\Config\Network($this, $netCfg);
271 $this->networks[$newConfig->getName()] = $newConfig;
275 if ($source == self::LOAD_FROM_FILE) {
276 $this->configFile = $configData;
278 $this->configFile = null;
291 if (!isset($this->networks[$network])) {
292 throw new \Erebot\NotFoundException(
'No such network');
294 return $this->networks[$network];
354 if (isset($this->locale)) {
355 $domain = str_replace(
'\\',
'_', ltrim($component,
'\\'));
356 $localedir = static::getBaseDir($component);
357 return \Erebot\Intl\GettextFactory::translation($domain, $localedir, array($this->locale));
getTranslator($component)
$version
The configuration file's version string.
const XMLNS
The XML namespace the content will be wrapped into.
$coreTranslator
Translator used by core files.
$configFile
The (relative or absolute) path to the configuration, if available.
parseBool($module, $param, $default=null)
$networks
A list of Erebot::Config::Network objects.
$daemonize
Whether to daemonize the bot or not.
$pidfile
File where the bot's PID will be written.
$commandsPrefix
The prefix used to recognize commands.
stripXGlobWrappers(&$domxml)
Contains the main (general) configuration for Erebot.
$timezone
The bot's current timezone.
$groupIdentity
Group identity to switch to.
const TAG
The XML tag used to wrap the content.
$userIdentity
User identity to switch to.
load($configData, $source)
__construct($configData, $source,\Erebot\Intl\TranslatorInterface $translator)
A configuration proxy which cascades settings.