Showing posts with label Zend_Config. Show all posts
Showing posts with label Zend_Config. Show all posts

Thursday, December 17, 2009

Manipulate a configuration


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
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;
    }

}

Tuesday, December 1, 2009

Connect to a database


In this example, we implement a connection to a database.

Components used in this example
Implementation of the connection

class MyDbConnection
{
The connection process
  • We get the connection method from the GET request.
  • We connect with the database adapter, the factory method, or the configuration.
  • We get the configuration details.

    public function process()
    {
        
// We get the connection method from the GET request.
        
list($method) = $this->_getParameters();

        
// We connect with the database adapter, the factory method, or the configuration.
        
switch ($method) {
            case 
'Zend_Db Adapter':
                
$db = new Zend_Db_Adapter_Pdo_Sqlite(array('dbname' => ':memory:'));
                break;

            case 
'Zend_Db Factory':
                
$db = Zend_Db::factory('Pdo_Sqlite', array('dbname' => ':memory:'));
                break;

            case 
'Zend_Config':
                
$config = new Zend_Config(array(
                    
'database' => array(
                        
'adapter' => 'Pdo_Sqlite',
                        
'params'  => array('dbname' => ':memory:')
                    )
                ));
                
$db = Zend_Db::factory($config->database);
                break;
        }

        
// We get the configuration details.
        
$message = isset($db)? $db->getConfig() : array();

        return array(
$method, $message);
    }
Extraction of the parameters from the GET request

    private function _getParameters()
    {
        
$method = isset($_GET['method'])? $_GET['method'] : '';

        return array(
$method);
    }

}