/** * Load configuration from a file and set it to the EZPDO manager. * * If config file is not specified, it tries to load config.xml first. * Then config.ini if config.xml not found from the current directory. * * @param string $file * @return bool */ function epLoadConfig($file = false) { // use default config file? if (!$file) { // try config.ini first if (file_exists($file = 'config.ini')) { $file = 'config.ini'; } else { if (file_exists('config.xml')) { $file = 'config.xml'; } else { return false; } } } else { // check if the specified config file exists if (!file_exists($file)) { return false; } } // load the config file include_once EP_SRC_BASE . '/epConfig.php'; if (!($cfg =& epConfig::load($file))) { return false; } // set config to the EZPDO manager return epManager::instance()->setConfig($cfg); }
/** * Return the DSN for a database type from config.xml * @return string */ function _getDsn($dbtype) { // load the config file if (!$this->cfg) { // load config.xml $this->cfg =& epConfig::load(dirname(__FILE__) . "/config.xml"); $this->assertTrue(!empty($this->cfg)); } $this->assertTrue($dsn = $this->cfg->get('dbs/' . strtolower($dbtype))); return $dsn; }
/** * setup before each test * @param string $dbal (adodb or peardb) * @param string $db (mysql, pgsql, or sqlite) */ function _setUp($dbal, $db) { // destroy singletons $this->_destroy(); // load config.xml for compiler include_once EP_SRC_BASE . '/epConfig.php'; $cfg =& epConfig::load(dirname(__FILE__) . "/config.xml"); $this->assertTrue(!empty($cfg)); // set dblib $cfg->set('db_lib', $dbal); // make input/output path absolute (fixed) $source_dirs = EP_TESTS . '/classes/'; $cfg->set('source_dirs', $source_dirs); // set compiled dir switch ($db) { case 'mysql': $compiled_file = $cfg->get('test/compiled_file/mysql'); $default_dsn = $cfg->get('test/default_dsn/mysql'); break; case 'pgsql': $compiled_file = $cfg->get('test/compiled_file/pgsql'); $default_dsn = $cfg->get('test/default_dsn/pgsql'); $cfg->set('default_oid_column', 'eoid'); // oid is special in pgsql break; case 'sqlite': $compiled_file = $cfg->get('test/compiled_file/sqlite/' . $dbal); $default_dsn = $cfg->get('test/default_dsn/sqlite/' . $dbal); break; } $cfg->set('compiled_file', $compiled_file); $cfg->set('default_dsn', $default_dsn); // force compile so default_dsn gets into class map $cfg->set('force_compile', true); // get epManager instance include_once EP_SRC_RUNTIME . '/epManager.php'; $this->m = null; // force a new instance $this->m =& epManager::instance(); $this->assertTrue($this->m); // set config to manager $this->assertTrue($this->m->setConfig($cfg)); // assert source_dirs is correct $this->assertTrue($this->m->getConfigOption('source_dirs') === $source_dirs); // assert source_dirs is correct $this->assertTrue($this->m->getConfigOption('compiled_file') === $compiled_file); // assert default_dsn is correct $this->assertTrue($this->m->getConfigOption('default_dsn') === $default_dsn); }
/** * Methods runs before every test */ function setUp() { // setup compiler not already if (!$this->c) { // load config.xml for compiler include_once EP_SRC_BASE . '/epConfig.php'; $this->assertTrue($cfg = epConfig::load(realpath(dirname(__FILE__)) . "/config.xml")); $this->assertTrue(!empty($cfg)); // create a compiler object include_once EP_SRC_COMPILER . '/epCompiler.php'; $this->assertTrue($this->c = new epClassCompiler($cfg)); // no validation to suppress errors $this->c->setConfigOption('validate_after_compile', false); } }
/** * test epLog */ function testLog() { // load config.xml $log_cfg =& epConfig::load(realpath(dirname(__FILE__)) . "/input/config.xml"); $this->assertTrue(!empty($log_cfg)); // make log file absolute path $log_file = realpath(dirname(__FILE__)) . "/output/test.log"; // delete it first so it won't accumulate and become a huge file @unlink($log_file); $this->assertTrue($log_cfg->set('log_file', $log_file)); $this->assertTrue($log_file == $log_cfg->get('log_file')); // config log with the log section only $logger =& epLog::instance(); $this->assertTrue($logger); $logger->setConfig($log_cfg); // generate all messages $msgs = array(); for ($i = 0; $i < 10; $i++) { $msg = 'log test message id (' . rand(0, 100000) . ') at ' . date('H:i:s'); $msgs[] = $msg; } // log all messages foreach ($msgs as $msg) { $status = $logger->log($msg); $this->assertTrue($status); } // figure out the log file $log_file = $logger->getConfigOption('log_file'); $this->assertTrue(file_exists($log_file)); // check if the messages are in the log file $content = file_get_contents($log_file); $this->assertTrue(!empty($content)); foreach ($msgs as $msg) { $this->assertTrue(strstr($content, $content) != false); } }
/** * Get the source file for the config * @return string */ public function getConfigSource() { return $this->config->getSource(); }
/** * Loads options from an XML file (see class description for XML format) * Returns either an array of options (if $ret_array is set to true) or epConfiginstance * @param string xml config file name * @param bool return array if true * @return mixed array or epConfig * @throws epExceptionConfig * @access protected * @static */ public static function &load($cfg_file = DEF_CONFIG_FILE, $ret_array = false) { // if empty file, return either an empty array or config object $cfg_file = trim($cfg_file); if (!$cfg_file) { if ($ret_array) { return array(); } else { $cfg = new epConfig(); return $cfg; } } // load xml config file switch (epFileExtension($cfg_file)) { case 'xml': if (false === ($options = epConfig::_loadXml($cfg_file))) { return false; } break; case 'ini': if (false === ($options = epConfig::_loadIni($cfg_file))) { return false; } break; default: throw new epExceptionConfig('Unrecognized config file (should be either .ini or .xml)'); return self::$false; } // return array if required if ($ret_array) { return $options; } // return epConfig $config = new epConfig(); $config->merge($options); $config->setSource($cfg_file); return $config; }
/** * Implement abstract method {@link epConfigurable::defConfig()} * @access public */ public function defConfig() { // collect parent's default config $cfg = new epConfig(parent::defConfig()); if ($cfg) { $cfg->set('compiled_file', 'compiled.ezpdo'); } return $cfg; }
/** * test configurable (defConfig() retrurns array) */ function testConfigurable2() { $co = new epConfigurableTest2(); $this->assertTrue($co != null); // check default options for ($i = 0; $i < 10; $i++) { // set an option-value pair $option = 'def_option' . $i; $value_set = 'def_value' . $i; $value_get = $co->getConfigOption($option); $this->assertTrue($value_get == $value_set); } // make a cfg (to be set to co) $cfg = new epConfig(); for ($i = 0; $i < 10; $i++) { // set an option-value pair $option = 'cfg_option' . $i; $value_set = 'cfg_value' . $i; $cfg->set($option, $value_set); } // set cfg to co $co->setConfig($cfg); // check cfg options for ($i = 0; $i < 10; $i++) { // set an option-value pair $option = 'cfg_option' . $i; $value_set = 'cfg_value' . $i; $value_get = $co->getConfigOption($option); $this->assertTrue($value_get == $value_set); } // also check default options for ($i = 0; $i < 10; $i++) { // set an option-value pair $option = 'def_option' . $i; $value_set = 'def_value' . $i; $value_get = $co->getConfigOption($option); $this->assertTrue($value_get == $value_set); } }