Issues

ZF2-9: Zend_Config Factory

Description

Either as a static class, or simply as a method of Zend_Config. See Zend_Application::_loadConfig()

    protected function _loadConfig($file)
    {
        $environment = $this->getEnvironment();
        $suffix      = strtolower(pathinfo($file, PATHINFO_EXTENSION));
        
        switch ($suffix) {
            case 'ini':
                $config = new Zend_Config_Ini($file, $environment);
                break;
                
            case 'xml':
                $config = new Zend_Config_Xml($file, $environment);
                break;
                
            case 'php':
            case 'inc':
                $config = include $file;
                if (!is_array($config)) {
                    throw new Zend_Application_Exception('Invalid configuration file provided; PHP file does not return array value');
                }
                return $config;
                break;
                
            default:
                throw new Zend_Application_Exception('Invalid configuration file provided; unknown config type');
        }
        
        return $config->toArray();
    }

Comments

I approve this, Zend_Application must not manage the config loader. Zend_Config does.

Here is what I have add to my project in zend_config.


    static public function factory($config = array(), $section= null, $options = false){
        if (is_string($config) and file_exists($config)) {
            $suffix      = strtolower(pathinfo($file, PATHINFO_EXTENSION));

            switch ($suffix){
                case "ini":
                    $configObj = new Zend_Config_Ini($config, $section, $options);
                    break;
                case "xml":
                    $configObj = new Zend_Config_Xml($config, $section, $options);
                    break;
                case "php":
                case "inc":
                    $configArr = include $config;
                    if (!is_array($config)) {
                        throw new Zend_Config_Exception('Invalid configuration file provided; PHP file does not return array value');
                    }   
                    $configObj = new Zend_Config($configArr, $options);
                    break;
                default:
                    throw new Zend_Config_Exception("Invalid configuration file provided; unknown config type");
                    break;
            }
        }elseif(is_array($config)){
            $configObj = new Zend_Config($config, $options);
        }else {
            throw new Zend_Config_Exception("Invalid configuration file provided; unknown config type");
        }
        return $configObj;
    }

I've created a factory for config too. Also, I've created almost the same factory for config writers.

To be considered for ZF2

I think illogically to add this feature into ZF2\Zend\Application, because for the first main goal (how i know) of it, it's minimum of factories and static methods. For second, what improvements you would provide by this feature for application? It seems like feature for Zend\Config, not for Zend\Application, and in this case why it should be static?

Better to be like this:


$configObject = new Zend\Config('config_file.ini(xml)(php)');

than:


$configObject = Zend\Config::loadConfig('config_file.ini(xml)(php)');

Of course it's same, but i think first method it's more modern and logical.

Ben is working on this.

Zend\Config\Factory is imlemented in the latest ZF2 master.