Overwrite Zend_Config and access parent nodes

I want to overwrite the Zend_Config __set ($ name, $ value) method, but I have the same problem.

$ name - return the current key for the rewrite configuration value, for example:

$this->config->something->other->more = 'crazy variable'; // $name in __set() will return 'more' 

Since each node in config is a new Zend_Config () class.

So, how can I get access to the parent node names from the rewritten __set () method?

My application: I have to overwrite the same configuration value in the controller, but have write control and not allow overwriting other configuration variables, I want to specify in the same different configuration variable, in the array tree of rewritable allowed configuration keys.

+4
source share
2 answers

Zend_Config is read-only if you did not set $ allowModifications to true at build time.

From Zend_Config_Ini::__constructor() docblock : -

 /** The $options parameter may be provided as either a boolean or an array. * If provided as a boolean, this sets the $allowModifications option of * Zend_Config. If provided as an array, there are three configuration * directives that may be set. For example: * * $options = array( * 'allowModifications' => false, * 'nestSeparator' => ':', * 'skipExtends' => false, * ); */ public function __construct($filename, $section = null, $options = false) 

This means that you will need to do something like this: -

 $inifile = APPLICATION_PATH . '/configs/application.ini'; $section = 'production'; $allowModifications = true; $config = new Zend_Config_ini($inifile, $section, $allowModifications); $config->resources->db->params->username = 'test'; var_dump($config->resources->db->params->username); 

Result

string 'test' (length = 4)

In response to the comment

In this case, you can simply extend Zend_Config_Ini and override the __construct() and __set() methods as follows: -

 class Application_Model_Config extends Zend_Config_Ini { private $allowed = array(); public function __construct($filename, $section = null, $options = false) { $this->allowed = array( 'list', 'of', 'allowed', 'variables' ); parent::__construct($filename, $section, $options); } public function __set($name, $value) { if(in_array($name, $this->allowed)){ $this->_allowModifications = true; parent::__set($name, $value); $this->setReadOnly(); } else { parent::__set($name, $value);} //will raise exception as expected. } } 
+3
source

There is always another way :)

 $arrSettings = $oConfig->toArray(); $arrSettings['params']['dbname'] = 'new_value'; $oConfig= new Zend_Config($arrSettings); 
0
source

Source: https://habr.com/ru/post/1396854/


All Articles