ZF-5551: Zend_Translate_Adapter::setOptions() sets disable_notices option after the option was used
Description
Zend_Translate_Adapter::setOptions() uses foreach to loop over the options arrays. Assume the following options array:
$options = array(
'locale' => 'en',
'disable_notices' => true
);
On the first loop foreach executes setLocale() method which in turn may use disable_notices option to disable/enable throwing a notice. But disable_notices option is set on the second loop, therefore an 'old' value was used.
My proposal is to replace foreach with the following code:
$locale = false;
foreach ($options as $key => $option) {
if ($key == "locale") {
$locale = $option;
} else if ((isset($this->_options[$key]) and ($this->_options[$key] != $option)) or
!isset($this->_options[$key])) {
$this->_options[$key] = $option;
$change = true;
}
}
if($locale !== false) {
$this->setLocale($locale);
}
Comments
Posted by Thomas Weidner (thomas) on 2009-01-15T03:08:44.000+0000
setOptions() is not a static method. It is called at initiation and set when reading the translation file.
To disable notices afterwards in the mid of a script execution is really a no-go which leads to problems in normal cases.
Posted by Tomas Brastavicius (tomas) on 2009-01-15T05:44:09.000+0000
It is not necessary to call setOptions() in the mid of a script. The same applies to the following code:
I encountered the issue on the test script of my application. I agree that it is not a normal case.
Posted by Thomas Weidner (thomas) on 2009-01-15T05:50:31.000+0000
Maybe... but also the second example will not work.
You are giving "lt" and "en" as locale at initiation... now which one should the adapter use ? In this case the notice even makes sense as you are not allowed to set two locales at once.
Posted by Thomas Weidner (thomas) on 2009-01-20T12:07:07.000+0000
Behaviour changed with r13715.
BUT: Setting multiple options with the same method call, when these options are dependent from each other is not supported.
THIS MEANS: When options dependent from each other are given you must seperate them.