static function init() { $fh = fopen("./log.txt", "w"); try { fputs($fh, "start\n"); $conf = new Conf(dirname(__FILE__) . "/../public/conf01.xml"); print "user: "******"\n"; print "host: " . $conf->get('host') . "\n"; $conf->set("pass", "newpass"); $conf->write(); } catch (FileException $e) { // permissions issue or non-existent file fputs($fh, "file exception\n"); throw $e; } catch (XmlException $e) { fputs($fh, "xml exception\n"); // broken xml } catch (ConfException $e) { fputs($fh, "conf exception\n"); // wrong kind of XML file } catch (Exception $e) { fputs($fh, "general exception\n"); // backstop: should not be called } finally { fputs($fh, "end\n"); fclose($fh); } }
static function init() { try { $config = new Conf("file.xml"); $config->set('sex', 'male'); // назначает значение $config->write(); // записывает в файл echo "sex: " . $config->get('sex'); // выводит значение } catch (FileException $e) { die($e->__toString()); //файл не существует либо не доступен для записи } catch (XmlException $e) { die($e->__toString()); //Повреждённый XML-файл } catch (ConfException $e) { die($e->__toString()); //не корректный формат XML-файла } catch (Exception $e) { die($e->__toString()); // Этот код не долже никогда вызываться } }
static function init() { try { //~ $conf = new Conf( dirname(__FILE__)."/conf.broken.xml" ); //~ $conf = new Conf( dirname(__FILE__)."/conf.unwriteable.xml" ); $conf = new Conf("nonexistent/not_there.xml"); print "user: "******"\n"; print "host: " . $conf->get('host') . "\n"; $conf->set("pass", "newpass"); $conf->write(); } catch (FileException $e) { // permissions issue or non-existent file throw $e; } catch (XmlException $e) { throw $e; // broken xml } catch (ConfException $e) { throw $e; // wrong kind of XML file } catch (Exception $e) { throw $e; // backstop: should not be called } }
$this->xml = simplexml_load_file($file); } function write() { file_put_contents($this->file, $this->xml->asXML()); } function get($str) { $matches = $this->xml->xpath("/conf/item[@name=\"{$str}\"]"); if (count($matches)) { $this->lastmatch = $matches[0]; return (string) $matches[0]; } return null; } function set($key, $value) { if (!is_null($this->get($key))) { $this->lastmatch[0] = $value; return; } $conf = $this->xml->conf; $this->xml->addChild('item', $value)->addAttribute('name', $key); } } $conf = new Conf(dirname(__FILE__) . "/conf01.xml"); print "user: "******"\n"; print "host: " . $conf->get('host') . "\n"; $conf->set("pass", "newpass"); $conf->write();
<?php $hostname = trim(`hostname`); switch ($hostname) { default: // Replace with production values $DEBUG = 0; $API_ENDPOINT = 'http://api.hasoffers.com/Api/json'; break; } /** * Application Debugging * */ Conf::write('DEBUG', $DEBUG); /** * Api Configuraion * */ Conf::write('ApiClient.Endpoint', $API_ENDPOINT); Conf::write('ApiClient.Version', 2); Conf::write('ApiClient.NetworkId', 'demo'); Conf::write('ApiClient.NetworkToken', 'NEThSQ1Ah92G8f7JYqQuO5hnsULkia'); /** * Api Configuraion * */ Conf::write('ENV.DefaultOfferId', 1); Conf::write('ENV.CompanyName', 'HasOffers');