Zend_Config_Ini with variable expansion

3 messages Options
Embed this post
Permalink
wizhippo

Zend_Config_Ini with variable expansion

Reply Threaded More More options
Print post
Permalink
I have liked other platforms were in the ini file you can do something like

[globla]
rootDir=/tmp
configDir=${rootDir}/etc

and have the ${variable} expanded.

so my solution was to extend Zend_Config_Ini to support this.
I also added the ability to pass an array to Zend_Config_Ini just like ot can to Zend_Config so the application can pass initial parameters that may be needed in the ini file to be used to expand variables.

<?php
class My_Config_Ini extends Zend_Config_Ini
{

    public function __construct($filename, $section = null, $options = false, array $array = array())
    {
                parent::__construct($filename, $section, $options);
                $this->merge(new Zend_Config($array));
                $this->_postProcessValues($this);
        }

    protected function _postProcessValues($config)
    {
                foreach ($config as $key => $value) {
                        if (is_a($value, "Zend_Config")) {
                                $this->_postProcessValues($value);
                        } else {
                                $config->__set($key, preg_replace_callback("|\\\${(.*)}|",
                                        array($this, 'callback'), $value));
                        }
        }
        }
       
        public function callback($matches)
        {
                if (isset($matches[1])) {
                        $path = explode($this->_nestSeparator, $matches[1]);
                       
                        $result = $this;
                        foreach($path as $value) {
                                if(!isset($result->$value)) {
                                        return $matches[0];
                                }
                                $result = $result->$value;
                        }
                        return $result;
                }
                return $matches[0];
        }
}


This is how I use it:

                $config = new My_Config_Ini(self::normalizePath($this->configDir . '/config.ini'), $configSection, true, array('rootDir' => $this->rootDir, 'configDir' => $this->configDir, 'configDir' => $this->configDir));


then in my ini file i can do stuff like


[global]
a = 1
b = 2

c.a = 1
c.b = 1

d.a = ${a}/foo
d.b = ${c.a}/bar



Should this be something I submit to the proposals or it's not interseting enough?




weierophinney

Re: Zend_Config_Ini with variable expansion

Reply Threaded More More options
Print post
Permalink
-- wizhippo <[hidden email]> wrote
(on Wednesday, 05 November 2008, 07:16 AM -0800):

> I have liked other platforms were in the ini file you can do something like
>
> [globla]
> rootDir=/tmp
> configDir=${rootDir}/etc
>
> and have the ${variable} expanded.
>
> so my solution was to extend Zend_Config_Ini to support this.
> I also added the ability to pass an array to Zend_Config_Ini just like ot
> can to Zend_Config so the application can pass initial parameters that may
> be needed in the ini file to be used to expand variables.

<snip - implementation>

> then in my ini file i can do stuff like
>
> [global]
> a = 1
> b = 2
>
> c.a = 1
> c.b = 1
>
> d.a = ${a}/foo
> d.b = ${c.a}/bar
>
> Should this be something I submit to the proposals or it's not interseting
> enough?

Yes, please submit a proposal; I think many people would like to see
this. Currently, you *can* use constants as part of the INI value, but
that's it; variable support would be very useful.

That said... if you *do* submit a proposal, I'd like to see similar
functionality for Zend_Config_Xml. :)

--
Matthew Weier O'Phinney
Software Architect       | [hidden email]
Zend Framework           | http://framework.zend.com/
wizhippo

Re: Zend_Config_Ini with variable expansion

Reply Threaded More More options
Print post
Permalink
Will do once my CLA is aproved.


Matthew Weier O'Phinney-3 wrote:
-- wizhippo <wizhippo@gmail.com> wrote
(on Wednesday, 05 November 2008, 07:16 AM -0800):
> I have liked other platforms were in the ini file you can do something like
>
> [globla]
> rootDir=/tmp
> configDir=${rootDir}/etc
>
> and have the ${variable} expanded.
>
> so my solution was to extend Zend_Config_Ini to support this.
> I also added the ability to pass an array to Zend_Config_Ini just like ot
> can to Zend_Config so the application can pass initial parameters that may
> be needed in the ini file to be used to expand variables.

<snip - implementation>

> then in my ini file i can do stuff like
>
> [global]
> a = 1
> b = 2
>
> c.a = 1
> c.b = 1
>
> d.a = ${a}/foo
> d.b = ${c.a}/bar
>
> Should this be something I submit to the proposals or it's not interseting
> enough?

Yes, please submit a proposal; I think many people would like to see
this. Currently, you *can* use constants as part of the INI value, but
that's it; variable support would be very useful.

That said... if you *do* submit a proposal, I'd like to see similar
functionality for Zend_Config_Xml. :)

--
Matthew Weier O'Phinney
Software Architect       | matthew@zend.com
Zend Framework           | http://framework.zend.com/