Php Zend_配置注入字符串
以以下配置为例:Php Zend_配置注入字符串,php,zend-framework,Php,Zend Framework,以以下配置为例: array( 'key' => 'value', 'key2' => array( 'key' => '@@INJECT@@', 'key2' => 'value' ), 'key3' => '@@INJECT@@' ); 然后该数组被转换为Zend\u Config对象 但我试图找到一种方法,在转换Zend_Config对象后,用一个特定的值替换@@INJECT@ 对于基本的arr
array(
'key' => 'value',
'key2' => array(
'key' => '@@INJECT@@',
'key2' => 'value'
),
'key3' => '@@INJECT@@'
);
然后该数组
被转换为Zend\u Config
对象
但我试图找到一种方法,在转换Zend_Config对象后,用一个特定的值替换@@INJECT@
对于基本的
array
,我使用array\u walk\u recursive()
function configWalkRecursive(Zend_Config $config, $callback)
{
foreach ($config as $key => $item) {
if ($item instanceof Zend_Config) {
// recurse into child Zend_Config objects
configWalkRecursive($item, $callback);
} else {
// run the callback against the element
$callback($key, $item, $config);
}
}
}
Zend_Config Object
(
[_allowModifications:protected] => 1
[_index:protected] => 0
[_count:protected] => 3
[_data:protected] => Array
(
[key] => value
[key2] => Zend_Config Object
(
[_allowModifications:protected] => 1
[_index:protected] => 0
[_count:protected] => 2
[_data:protected] => Array
(
[key] => @@INJECT@@
[key2] => value
)
[_skipNextIteration:protected] =>
[_loadedSection:protected] =>
[_extends:protected] => Array
(
)
[_loadFileErrorStr:protected] =>
)
[key3] => @@INJECT@@
)
[_skipNextIteration:protected] =>
[_loadedSection:protected] =>
[_extends:protected] => Array
(
)
[_loadFileErrorStr:protected] =>
)
Zend_Config Object
(
[_allowModifications:protected] => 1
[_index:protected] => 3
[_count:protected] => 3
[_data:protected] => Array
(
[key] => value
[key2] => Zend_Config Object
(
[_allowModifications:protected] => 1
[_index:protected] => 2
[_count:protected] => 2
[_data:protected] => Array
(
[key] => somevalue
[key2] => value
)
[_skipNextIteration:protected] =>
[_loadedSection:protected] =>
[_extends:protected] => Array
(
)
[_loadFileErrorStr:protected] =>
)
[key3] => somevalue
)
[_skipNextIteration:protected] =>
[_loadedSection:protected] =>
[_extends:protected] => Array
(
)
[_loadFileErrorStr:protected] =>
)
它迭代Zend_配置中的元素,如果找到另一个Zend_配置,则递归,否则将当前项传递给回调函数
然后,对于回调,您可以执行以下操作来测试项目,并根据需要进行替换:
$array = array(
'key' => 'value',
'key2' => array(
'key' => '@@INJECT@@',
'key2' => 'value'
),
'key3' => '@@INJECT@@'
);
$config = new Zend_Config($array, true);
print_r($config);
configWalkRecursive($config, function($key, $item, $configObject) {
if ('@@INJECT@@' === $item) {
$configObject->$key = 'somevalue';
}
);
print_r($config);
输出如下所示:
function configWalkRecursive(Zend_Config $config, $callback)
{
foreach ($config as $key => $item) {
if ($item instanceof Zend_Config) {
// recurse into child Zend_Config objects
configWalkRecursive($item, $callback);
} else {
// run the callback against the element
$callback($key, $item, $config);
}
}
}
Zend_Config Object
(
[_allowModifications:protected] => 1
[_index:protected] => 0
[_count:protected] => 3
[_data:protected] => Array
(
[key] => value
[key2] => Zend_Config Object
(
[_allowModifications:protected] => 1
[_index:protected] => 0
[_count:protected] => 2
[_data:protected] => Array
(
[key] => @@INJECT@@
[key2] => value
)
[_skipNextIteration:protected] =>
[_loadedSection:protected] =>
[_extends:protected] => Array
(
)
[_loadFileErrorStr:protected] =>
)
[key3] => @@INJECT@@
)
[_skipNextIteration:protected] =>
[_loadedSection:protected] =>
[_extends:protected] => Array
(
)
[_loadFileErrorStr:protected] =>
)
Zend_Config Object
(
[_allowModifications:protected] => 1
[_index:protected] => 3
[_count:protected] => 3
[_data:protected] => Array
(
[key] => value
[key2] => Zend_Config Object
(
[_allowModifications:protected] => 1
[_index:protected] => 2
[_count:protected] => 2
[_data:protected] => Array
(
[key] => somevalue
[key2] => value
)
[_skipNextIteration:protected] =>
[_loadedSection:protected] =>
[_extends:protected] => Array
(
)
[_loadFileErrorStr:protected] =>
)
[key3] => somevalue
)
[_skipNextIteration:protected] =>
[_loadedSection:protected] =>
[_extends:protected] => Array
(
)
[_loadFileErrorStr:protected] =>
)