Issues

ZF2-9: Zend_Config Factory

Issue Type: New Feature Created: 2009-04-28T17:16:33.000+0000 Last Updated: 2012-03-07T09:48:48.000+0000 Status: Resolved Fix version(s): Reporter: Sebastian Krebs (kingcrunch) Assignee: Ben Scholzen (dasprid) Tags: - Zend\Config

Related issues: Attachments:

Description

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

<pre class="highlight">    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

Posted by Mathieu Delisle (m4d3l) on 2009-06-01T17:28:11.000+0000

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.

<pre class="highlight">
    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;
    }

Posted by Dmitry Babenko (st.general) on 2009-08-05T01:38:40.000+0000

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

Posted by Rob Allen (rob) on 2011-05-27T02:06:36.000+0000

To be considered for ZF2

Posted by Evgheni Poleacov (zendmania) on 2011-09-12T10:38:00.000+0000

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:

<pre class="highlight">
$configObject = new Zend\Config('config_file.ini(xml)(php)');

than:

<pre class="highlight">
$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.

Posted by Evan Coury (evan.pro) on 2012-02-23T19:32:45.000+0000

Ben is working on this.

Posted by Evan Coury (evan.pro) on 2012-03-07T09:48:48.000+0000

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

Have you found an issue?

See the Overview section for more details.

Copyright

© 2006-2016 by Zend, a Rogue Wave Company. Made with by awesome contributors.

This website is built using zend-expressive and it runs on PHP 7.

Contacts