From 60d12d27c2ebef9475bc41383a4121b83ac9a92a Mon Sep 17 00:00:00 2001 From: kamijo Date: Thu, 18 Jul 2013 08:56:26 +0900 Subject: [PATCH 1/2] Update ext/config/adapter/ini.c Dot(.) directive parse --- ext/config/adapter/ini.c | 59 +++++++++++++++++++++++++++------ unit-tests/ConfigTest.php | 31 +++++++++++++++++ unit-tests/config/directive.ini | 13 ++++++++ 3 files changed, 93 insertions(+), 10 deletions(-) create mode 100644 unit-tests/config/directive.ini diff --git a/ext/config/adapter/ini.c b/ext/config/adapter/ini.c index feef3958292..73bf4af21de 100644 --- a/ext/config/adapter/ini.c +++ b/ext/config/adapter/ini.c @@ -71,6 +71,52 @@ * */ +static inline void phalcon_config_adapter_ini_update_zval_directive(zval **arr, zval *section, zval *directive, zval **value, int flags TSRMLS_DC) { + zval *temp1 = NULL, *temp2 = NULL, *index = NULL; + int i, n; + + n = zend_hash_num_elements(Z_ARRVAL_P(directive)); + + if (Z_TYPE_PP(arr) == IS_ARRAY) { + phalcon_array_fetch(&temp1, *arr, section, PH_SILENT_CC); + if (Z_REFCOUNT_P(temp1) > 1) { + phalcon_array_update_zval(arr, section, &temp1, PH_COPY | PH_CTOR TSRMLS_CC); + } + if (Z_TYPE_P(temp1) != IS_ARRAY) { + convert_to_array(temp1); + phalcon_array_update_zval(arr, section, &temp1, PH_COPY TSRMLS_CC); + } + + for (i = 0; i < n - 1; i++) { + phalcon_array_fetch_long(&index, directive, i, PH_NOISY_CC); + + phalcon_array_fetch(&temp2, temp1, index, PH_SILENT_CC); + if (Z_REFCOUNT_P(temp2) > 1) { + phalcon_array_update_zval(&temp1, index, &temp2, PH_COPY | PH_CTOR TSRMLS_CC); + } + if (Z_TYPE_P(temp2) != IS_ARRAY) { + convert_to_array(temp2); + phalcon_array_update_zval(&temp1, index, &temp2, PH_COPY TSRMLS_CC); + } + zval_ptr_dtor(&index); + + if (temp1 != NULL) { + zval_ptr_dtor(&temp1); + } + temp1 = temp2; + temp2 = NULL; + } + + phalcon_array_fetch_long(&index, directive, n - 1, PH_NOISY_CC); + phalcon_array_update_zval(&temp1, index, value, PH_COPY TSRMLS_CC); + + zval_ptr_dtor(&index); + + if (temp1 != NULL) { + zval_ptr_dtor(&temp1); + } + } +} /** * Phalcon\Config\Adapter\Ini initializer @@ -91,8 +137,7 @@ PHP_METHOD(Phalcon_Config_Adapter_Ini, __construct){ zval *file_path, *process_sections, *ini_config; zval *exception_message, *config, *directives = NULL; - zval *section = NULL, *value = NULL, *key = NULL, *directive_parts = NULL, *left_part = NULL; - zval *right_part = NULL; + zval *section = NULL, *value = NULL, *key = NULL, *directive_parts = NULL; HashTable *ah0, *ah1; HashPosition hp0, hp1; zval **hd; @@ -155,14 +200,8 @@ PHP_METHOD(Phalcon_Config_Adapter_Ini, __construct){ if (phalcon_memnstr_str(key, SL("."))) { PHALCON_INIT_NVAR(directive_parts); - phalcon_fast_explode_str(directive_parts, SL("."), key); - - PHALCON_OBS_NVAR(left_part); - phalcon_array_fetch_long(&left_part, directive_parts, 0, PH_NOISY); - - PHALCON_OBS_NVAR(right_part); - phalcon_array_fetch_long(&right_part, directive_parts, 1, PH_NOISY); - phalcon_array_update_zval_zval_zval_multi_3(&config, section, left_part, right_part, &value, 0); + phalcon_fast_explode_str(directive_parts, SL("."), key TSRMLS_CC); + phalcon_config_adapter_ini_update_zval_directive(&config, section, directive_parts, &value, 0 TSRMLS_CC); } else { phalcon_array_update_multi_2(&config, section, key, &value, 0); } diff --git a/unit-tests/ConfigTest.php b/unit-tests/ConfigTest.php index 87049759cc7..e77d2f511c0 100644 --- a/unit-tests/ConfigTest.php +++ b/unit-tests/ConfigTest.php @@ -253,5 +253,36 @@ public function testIssue829() $this->assertEquals($actual, $expected); } + + public function testIniConfigDirective() + { + $config = new \Phalcon\Config\Adapter\Ini('unit-tests/config/directive.ini'); + $actual = $config->toArray(); + $expected = array( + 'test' => array( + 'parent' => array( + 'property' => 1, + 'property2' => 'yeah', + 'property3' => array('baseuri' => '/phalcon/'), + 'property4' => array( + 'models' => array('metadata' => 'memory'), + ), + 'property5' => array( + 'database' => array( + 'adapter' => 'mysql', + 'host' => 'localhost', + 'username' => 'user', + 'password' => 'passwd', + 'name' => 'demo'), + ), + 'property6' => array( + 'test' => array('a', 'b', 'c'), + ), + ), + ), + ); + + $this->assertEquals($actual, $expected); + } } diff --git a/unit-tests/config/directive.ini b/unit-tests/config/directive.ini new file mode 100644 index 00000000000..e402dbf153d --- /dev/null +++ b/unit-tests/config/directive.ini @@ -0,0 +1,13 @@ +[test] +parent.property = On +parent.property2 = "yeah" +parent.property3.baseuri = /phalcon/ +parent.property4.models.metadata = memory +parent.property5.database.adapter = mysql +parent.property5.database.host = localhost +parent.property5.database.username = user +parent.property5.database.password = passwd +parent.property5.database.name = demo +parent.property6.test[] = a +parent.property6.test[] = b +parent.property6.test[] = c From fa9962985d065111bd928067618d169f288eb845 Mon Sep 17 00:00:00 2001 From: kamijo Date: Thu, 18 Jul 2013 12:11:39 +0900 Subject: [PATCH 2/2] Fixed dot directive parse in Config/Adapter/Ini --- ext/config/adapter/ini.c | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/ext/config/adapter/ini.c b/ext/config/adapter/ini.c index 73bf4af21de..aeebac16ed2 100644 --- a/ext/config/adapter/ini.c +++ b/ext/config/adapter/ini.c @@ -78,25 +78,25 @@ static inline void phalcon_config_adapter_ini_update_zval_directive(zval **arr, n = zend_hash_num_elements(Z_ARRVAL_P(directive)); if (Z_TYPE_PP(arr) == IS_ARRAY) { - phalcon_array_fetch(&temp1, *arr, section, PH_SILENT_CC); + phalcon_array_fetch(&temp1, *arr, section, PH_SILENT); if (Z_REFCOUNT_P(temp1) > 1) { - phalcon_array_update_zval(arr, section, &temp1, PH_COPY | PH_CTOR TSRMLS_CC); + phalcon_array_update_zval(arr, section, &temp1, PH_COPY | PH_CTOR); } if (Z_TYPE_P(temp1) != IS_ARRAY) { convert_to_array(temp1); - phalcon_array_update_zval(arr, section, &temp1, PH_COPY TSRMLS_CC); + phalcon_array_update_zval(arr, section, &temp1, PH_COPY); } for (i = 0; i < n - 1; i++) { - phalcon_array_fetch_long(&index, directive, i, PH_NOISY_CC); + phalcon_array_fetch_long(&index, directive, i, PH_NOISY); - phalcon_array_fetch(&temp2, temp1, index, PH_SILENT_CC); + phalcon_array_fetch(&temp2, temp1, index, PH_SILENT); if (Z_REFCOUNT_P(temp2) > 1) { - phalcon_array_update_zval(&temp1, index, &temp2, PH_COPY | PH_CTOR TSRMLS_CC); + phalcon_array_update_zval(&temp1, index, &temp2, PH_COPY | PH_CTOR); } if (Z_TYPE_P(temp2) != IS_ARRAY) { convert_to_array(temp2); - phalcon_array_update_zval(&temp1, index, &temp2, PH_COPY TSRMLS_CC); + phalcon_array_update_zval(&temp1, index, &temp2, PH_COPY); } zval_ptr_dtor(&index); @@ -107,8 +107,8 @@ static inline void phalcon_config_adapter_ini_update_zval_directive(zval **arr, temp2 = NULL; } - phalcon_array_fetch_long(&index, directive, n - 1, PH_NOISY_CC); - phalcon_array_update_zval(&temp1, index, value, PH_COPY TSRMLS_CC); + phalcon_array_fetch_long(&index, directive, n - 1, PH_NOISY); + phalcon_array_update_zval(&temp1, index, value, PH_COPY); zval_ptr_dtor(&index); @@ -200,7 +200,7 @@ PHP_METHOD(Phalcon_Config_Adapter_Ini, __construct){ if (phalcon_memnstr_str(key, SL("."))) { PHALCON_INIT_NVAR(directive_parts); - phalcon_fast_explode_str(directive_parts, SL("."), key TSRMLS_CC); + phalcon_fast_explode_str(directive_parts, SL("."), key); phalcon_config_adapter_ini_update_zval_directive(&config, section, directive_parts, &value, 0 TSRMLS_CC); } else { phalcon_array_update_multi_2(&config, section, key, &value, 0);