Quantcast

Merging two Zend_Configs

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Merging two Zend_Configs

Steve-203
Hi,
 
I've been trying to figure out the best way to merge two Zend_Config objects together.
 
What I want to be able to do is have one master INI file in my application directory and then one INI file per application instance, which will allow me to override settings in the master INI file. I took a look around the documentation and the code for Zend_Config but wasn't able to find an existing way to do this.
 
What I think I'll have to do is write a recursive iterator or a combination of asArray() and a custom array_merge function but I wanted to check here if I'm missing something and if there is an easier way to accomplish this?
 
Thanks,
 
Steve
 
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Merging two Zend_Configs

Simon Mundy
Hi Steve,

You only need the one ini file for that. You can break up the ini  
file into 'sections', with each section having the ability to inherit  
from previous sections. For example:-

[all]
db.username = foo
db.password = bar
db.database = mydatabase

[development:all]
db.username = foodev
db.password = bardev

[local:development]
db.username = foolocal


When you need to load the config file, simply use the following (cut  
and pasted from the manual):-

$config = new Zend_Config_Ini('/path/to/config.ini', 'development');

...where 'development' means that you'll end up by overriding the  
password and username for the 'db' directives in your config file. If  
you'd used 'local' then you'd get the 'foolocal' username, the  
'bardev' password and the 'mydatabase' database name.

Hope that helps

> Hi,
>
> I've been trying to figure out the best way to merge two  
> Zend_Config objects together.
>
> What I want to be able to do is have one master INI file in my  
> application directory and then one INI file per application  
> instance, which will allow me to override settings in the master  
> INI file. I took a look around the documentation and the code for  
> Zend_Config but wasn't able to find an existing way to do this.
>
> What I think I'll have to do is write a recursive iterator or a  
> combination of asArray() and a custom array_merge function but I  
> wanted to check here if I'm missing something and if there is an  
> easier way to accomplish this?
>
> Thanks,
>
> Steve
>
>

--

Simon Mundy | Director | PEPTOLAB

""" " "" """""" "" "" """"""" " "" """"" " """"" "  """""" "" "
202/258 Flinders Lane | Melbourne | Victoria | Australia | 3000
Voice +61 (0) 3 9654 4324 | Mobile 0438 046 061 | Fax +61 (0) 3 9654  
4124
http://www.peptolab.com


Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Merging two Zend_Configs

Steve-203
Hi Simon,

Yeah, I realise that's possible but I need to be able to have two seperate
INI files for a couple of reasons. Primarily, it's a multi-developer/user
environment and we need to prevent the majority of developers/users being
able to access the master INI file but still be able to configure their own
site.

Thanks,

Steve


----- Original Message -----
From: "Simon Mundy" <[hidden email]>
To: "Zend Framework" <[hidden email]>
Sent: Friday, December 22, 2006 12:22 PM
Subject: Re: [fw-general] Merging two Zend_Configs


> Hi Steve,
>
> You only need the one ini file for that. You can break up the ini  file
> into 'sections', with each section having the ability to inherit  from
> previous sections. For example:-
>
> [all]
> db.username = foo
> db.password = bar
> db.database = mydatabase
>
> [development:all]
> db.username = foodev
> db.password = bardev
>
> [local:development]
> db.username = foolocal
>
>
> When you need to load the config file, simply use the following (cut  and
> pasted from the manual):-
>
> $config = new Zend_Config_Ini('/path/to/config.ini', 'development');
>
> ...where 'development' means that you'll end up by overriding the
> password and username for the 'db' directives in your config file. If
> you'd used 'local' then you'd get the 'foolocal' username, the  'bardev'
> password and the 'mydatabase' database name.
>
> Hope that helps
>
>> Hi,
>>
>> I've been trying to figure out the best way to merge two  Zend_Config
>> objects together.
>>
>> What I want to be able to do is have one master INI file in my
>> application directory and then one INI file per application  instance,
>> which will allow me to override settings in the master  INI file. I took
>> a look around the documentation and the code for  Zend_Config but wasn't
>> able to find an existing way to do this.
>>
>> What I think I'll have to do is write a recursive iterator or a
>> combination of asArray() and a custom array_merge function but I  wanted
>> to check here if I'm missing something and if there is an  easier way to
>> accomplish this?
>>
>> Thanks,
>>
>> Steve
>>
>>
>
> --
>
> Simon Mundy | Director | PEPTOLAB
>
> """ " "" """""" "" "" """"""" " "" """"" " """"" "  """""" "" "
> 202/258 Flinders Lane | Melbourne | Victoria | Australia | 3000
> Voice +61 (0) 3 9654 4324 | Mobile 0438 046 061 | Fax +61 (0) 3 9654  4124
> http://www.peptolab.com
>
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Merging two Zend_Configs

Matthew Ratzloff
In reply to this post by Steve-203
There's no merge function.  This will work, however:

$config = new Zend_Config($subordinate->asArray() + $master->asArray());

This will allow master values to be redefined in the subordinate.  If you
want to prevent that, swap the order.

It would be kind of neat if you could do something like this:

$config = $subordinate->mergeInto($master);

Internally, it would just do what I wrote above, except it wouldn't need to
create a new object and it would honor $allowModifications (I suppose; I
never saw the point of that parameter when it's so easily sidestepped).
Zend_Config::isModifiable() would have to be added in that case.  Rob?

-Matt

----- Original Message -----
From: "Steve" <[hidden email]>
To: <[hidden email]>
Sent: Friday, December 22, 2006 4:03 AM
Subject: [fw-general] Merging two Zend_Configs


Hi,

I've been trying to figure out the best way to merge two Zend_Config objects
together.

What I want to be able to do is have one master INI file in my application
directory and then one INI file per application instance, which will allow
me to override settings in the master INI file. I took a look around the
documentation and the code for Zend_Config but wasn't able to find an
existing way to do this.

What I think I'll have to do is write a recursive iterator or a combination
of asArray() and a custom array_merge function but I wanted to check here if
I'm missing something and if there is an easier way to accomplish this?

Thanks,

Steve

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Merging two Zend_Configs

Nico Edtinger
In reply to this post by Steve-203
You could use array_merge():

<?php

require_once 'Zend.php';
Zend::loadClass('Zend_Config');

$master_config = new Zend_Config(array('x' => 1, 'y' => 2, 'z' => 3));
$user_config     = new Zend_Config(array('y' => 4, 'a' => 5));

$config = new Zend_Config(array_merge($master_config->asArray(),  
$user_config->asArray()));
var_dump($config->asArray());

/* output:
array(4) {
   ["x"]=>
   int(1)
   ["y"]=>
   int(4)
   ["z"]=>
   int(3)
   ["a"]=>
   int(5)
}
*/

?>

nico


[22.12.2006 13:03] Steve wrote:

> Hi,
>
> I've been trying to figure out the best way to merge two  
> Zend_Config objects together.
>
> What I want to be able to do is have one master INI file in my  
> application directory and then one INI file per application  
> instance, which will allow me to override settings in the master  
> INI file. I took a look around the documentation and the code for  
> Zend_Config but wasn't able to find an existing way to do this.
>
> What I think I'll have to do is write a recursive iterator or a  
> combination of asArray() and a custom array_merge function but I  
> wanted to check here if I'm missing something and if there is an  
> easier way to accomplish this?
>
> Thanks,
>
> Steve


Loading...