In this example, we write a configuration file. Then we read the configuration from the file. Or we extract configuration parameters. We use a PHP configuration file, an INI configuration file, or an XML configuration file.
Components used in this example
- Zend_Config
- Zend_Config_Ini
- Zend_Config_Writer_Array
- Zend_Config_Writer_Ini
- Zend_Config_Writer_Xml
- Zend_Config_Xml
Implementation of the configuration
class MyConfig
{
Processing the configuration- We get the name of the configuration writer and the parameter, from the GET request.
- We create a configuration.
- We write the configuration in a file.
- We read the content of the configuration file.
- Or we get the configuration as an object.
- Or we get the configuration of a section.
- Or we get the configuration of a parameter.
- If we catch an exception, we return the error message.
public function process()
{
// We get the name of the configuration writer and the parameter, from the GET request.
list($class, $view) = $this->_getParameters();
try {
$result = array();
// We create a configuration.
$config = $this->_createConfig();
// We write the configuration in a file.
$writer = new MyConfigWriter($class);
$writer->write($config);
switch ($view) {
case 'Content':
// We read the content of the configuration file.
$result = $writer->read();
break;
case 'Config':
// Or we get the configuration as an object.
$result = $writer->get();
break;
case 'production':
case 'staging':
// Or we get the configuration of a section.
$result = $writer->get($view);
break;
default:
// Or we get the configuration of a parameter.
$result = $writer->getParameter($view);
}
} catch (Exception $e) {
// If we catch an exception, we return the error message.
$result = $e->getMessage();
}
return array($class, $view, $result);
}
Extraction of the parameters from the GET request
private function _getParameters()
{
$class = (isset($_GET['class']) and MyConfigWriter::isValid($_GET['class']))?
$_GET['class'] : 'Zend_Config_Writer_Array';
$view = isset($_GET['view'])? $_GET['view'] : 'Content';
return array($class, $view);
}
Creation of the configuration
private function _createConfig()
{
$config = new Zend_Config(array(), true);
$config->production = array();
$config->production->webhost = 'www.example.com';
$config->production->database = array();
$config->production->database->adapter = 'pdo_mysql';
$config->production->database->params = array();
$config->production->database->params->host = 'db.example.com';
$config->production->database->params->username = 'dbuser';
$config->production->database->params->password = 'secret';
$config->production->database->params->dbname = 'dbname';
$config->staging = array();
$config->setExtend('staging', 'production');
$config->staging->database = array();
$config->staging->database->params = array();
$config->staging->database->params->host = 'dev.example.com';
$config->staging->database->params->username = 'devuser';
$config->staging->database->params->password = 'devsecret';
return $config;
}
}
No comments:
Post a Comment