[BUG] Phalcon\Config. Property name can be an integer, but only with "0" among other
print_r(new Phalcon\Config([
'some_property' => [
0 => 'a',
1 => 'b',
]
]));
print_r(new Phalcon\Config([
'some_property' => [
1 => 'a',
2 => 'b',
]
]));
The first record is Ok.
But second alerts warning
Warning: Phalcon\Config::__construct(): Property should be string
I thing Phalcon\Config should allow integers as property names (consider as an array.). curl options for example
// E_WARNING here
$config = new Phalcon\Config([
'curlOptions' => [
CURLOPT_HEADER => true,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_SSL_VERIFYPEER => false,
],
]);
Quick fix
// It's Ok, no warning
$config = new Phalcon\Config([
'curlOptions' => [
0 => 'just ignore me',
CURLOPT_HEADER => true,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_SSL_VERIFYPEER => false,
],
]);
Actually, we only check for index 0 to realize if it's a numeric array, check every possible number would be very slow, if the array is very big or the started first numeric index is very high it would take several milliseconds. Unless someone have a better idea?
I understand the reasons, but I don't think that config files may contain huge numeric arrays without index 0.
I see two ways to solve problem. If index 0 is absent:
- iterate over the array and check the keys type. UPD: and stop iteration if key type is not numeric
- cast numeric index as a string
OK, what if we check the type of the first key instead of relying upon $tem[0] being set?
What I mean is:
--- a/ext/config.c
+++ b/ext/config.c
@@ -108,7 +108,7 @@ PHP_METHOD(Phalcon_Config, __construct){
PHALCON_GET_HVALUE(value);
if (Z_TYPE_P(value) == IS_ARRAY) {
- if (!phalcon_array_isset_long(value, 0)) {
+ if (zend_hash_get_current_key_type(Z_ARRVAL_P(value)) != HASH_KEY_IS_LONG) {
PHALCON_INIT_NVAR(config_value);
object_init_ex(config_value, phalcon_config_ce);
phalcon_call_method_p1_noret(config_value, "__construct", value);
Then the example in the OP will produce the following result:
Phalcon\Config Object
(
[some_property] => Array
(
[0] => a
[1] => b
)
)
Phalcon\Config Object
(
[some_property] => Array
(
[1] => a
[2] => b
)
)
If that's cool, I can write a test and submit a pull request :-)
@sjinks thanks, but then this config initialization will produce the same warning:
new Phalcon\Config([
'some_property' => [
'x' => 'y',
'a',
'b',
]
]);
Think this solution will be acceptable if the docs will clearly describe that the type is defined by the first key.
Perhaps in such cases we must make it clear that this is exactly array? Something like this
new Phalcon\Config([
'some_property' => new Phalcon\Config\Array([
'x' => 'y',
'a',
'b',
]),
]);
Well… According to
- iterate over the array and check the keys type. UPD: and stop iteration if key type is not numeric
this will be the expected behavior. 'x' is the first key, it is not numeric => will be passed to Phalcon\Config\Object::__construct().
This alternative:
- cast numeric index as a string
will require many changes. While it is trivial to add type cast to the constructor, there will be issues in different places: for example, with
Phalcon\Config Object
(
[some_property] => Phalcon\Config Object
(
[1] => a
[2] => b
)
)
attempts to access $x->some_property[1] or $x->some_property["1"] will result in PHP Notice: Phalcon\Config::offsetGet(): Cannot access empty property 1
$x->some_property["2"] = 'c' will raise a fatal error PHP Fatal error: Uncaught exception 'Phalcon\Config\Exception' with message 'Index key must be string'.
Thus we will need to add index type cast at least to both offsetGet() and offsetSet(). I feel the same will be true for offsetUnset() and offsetExists() and much likely to get().
That's doable, of course, but it would be nice to hear what the devs think about this (the patch for this case is almost ready).
Phalcon\Config\Array
I am afraid that the class name will have to be called differently as array is the reserved word.
It's just hasty example to show my idea, it could be called whatever you like Phalcon\Config\RawArray.
Think the best way would be your fix + the ability to specify RawArray
OK, here's Plan B (the diff is approximate):
PHP_METHOD(Phalcon_Config, __construct){
zval *array_config = NULL, *value = NULL, *key = NULL, *config_value = NULL;
+ zval *tmp = NULL, **key_or_tmp;
HashTable *ah0;
HashPosition hp0;
zval **hd;
@@ -106,6 +107,15 @@
PHALCON_GET_HKEY(key, ah0, hp0);
PHALCON_GET_HVALUE(value);
+
+ if (likely(Z_TYPE_P(key) == IS_STRING)) {
+ key_or_tmp = &key;
+ }
+ else {
+ PHALCON_INIT_NVAR(tmp);
+ phalcon_cast(tmp, key, IS_STRING);
+ key_or_tmp = &tmp;
+ }
if (Z_TYPE_P(value) == IS_ARRAY) {
if (!phalcon_array_isset_long(value, 0)) {
@@ -113,12 +123,12 @@
object_init_ex(config_value, phalcon_config_ce);
phalcon_call_method_p1_noret(config_value, "__construct", value);
- phalcon_update_property_zval_zval(this_ptr, key, config_value TSRMLS_CC);
+ phalcon_update_property_zval_zval(this_ptr, *key_or_tmp, config_value TSRMLS_CC);
} else {
- phalcon_update_property_zval_zval(this_ptr, key, value TSRMLS_CC);
+ phalcon_update_property_zval_zval(this_ptr, *key_or_tmp, value TSRMLS_CC);
}
} else {
- phalcon_update_property_zval_zval(this_ptr, key, value TSRMLS_CC);
+ phalcon_update_property_zval_zval(this_ptr, *key_or_tmp, value TSRMLS_CC);
}
zend_hash_move_forward_ex(ah0, &hp0);
@@ -146,14 +156,28 @@
*/
PHP_METHOD(Phalcon_Config, offsetExists){
- zval *index;
+ zval *index, *tmp, **index_or_tmp;
+
+ PHALCON_MM_GROW();
phalcon_fetch_params(0, 1, 0, &index);
+ index_or_tmp = &index;
+
+ if (unlikely(Z_TYPE_P(index) != IS_STRING)) {
+ PHALCON_INIT_VAR(tmp);
+ phalcon_cast(tmp, index, IS_STRING);
+ index_or_tmp = &tmp;
+ }
- if (phalcon_isset_property_zval(this_ptr, index TSRMLS_CC)) {
- RETURN_TRUE;
+ if (phalcon_isset_property_zval(this_ptr, *index_or_tmp TSRMLS_CC)) {
+ RETVAL_TRUE;
}
- RETURN_FALSE;
+ else {
+ RETVAL_FALSE;
+ }
+
+ PHALCON_MM_RESTORE();
+ return;
}
/**
@@ -204,14 +228,22 @@
*/
PHP_METHOD(Phalcon_Config, offsetGet){
- zval *index, *value;
+ zval *index, *value, *tmp;
PHALCON_MM_GROW();
phalcon_fetch_params(1, 1, 0, &index);
PHALCON_OBS_VAR(value);
- phalcon_read_property_zval(&value, this_ptr, index, PH_NOISY_CC);
+ if (likely(Z_TYPE_P(index) == IS_STRING)) {
+ phalcon_read_property_zval(&value, this_ptr, index, PH_NOISY_CC);
+ }
+ else {
+ PHALCON_INIT_VAR(tmp);
+ phalcon_cast(tmp, index, IS_STRING);
+ phalcon_read_property_zval(&value, this_ptr, tmp, PH_NOISY_CC);
+ }
+
RETURN_CCTOR(value);
}
@@ -227,15 +259,18 @@
*/
PHP_METHOD(Phalcon_Config, offsetSet){
- zval *index, *value, *array_value = NULL;
+ zval *index, *value, *tmp, *array_value = NULL;
+ zval **index_or_tmp;
PHALCON_MM_GROW();
phalcon_fetch_params(1, 2, 0, &index, &value);
+ index_or_tmp = &index;
- if (Z_TYPE_P(index) != IS_STRING) {
- PHALCON_THROW_EXCEPTION_STR(phalcon_config_exception_ce, "Index key must be string");
- return;
+ if (unlikely(Z_TYPE_P(index) != IS_STRING)) {
+ PHALCON_INIT_VAR(tmp);
+ phalcon_cast(tmp, index, IS_STRING);
+ index_or_tmp = &tmp;
}
if (Z_TYPE_P(value) == IS_ARRAY) {
PHALCON_INIT_VAR(array_value);
@@ -246,7 +281,7 @@
PHALCON_CPY_WRT(array_value, value);
}
- phalcon_update_property_zval_zval(this_ptr, index, array_value TSRMLS_CC);
+ phalcon_update_property_zval_zval(this_ptr, *index_or_tmp, array_value TSRMLS_CC);
PHALCON_MM_RESTORE();
}
@@ -282,6 +317,7 @@
PHP_METHOD(Phalcon_Config, merge){
zval *config, *array_config, *value = NULL, *key = NULL, *active_value = NULL;
+ zval *tmp = NULL, **key_or_tmp;
HashTable *ah0;
HashPosition hp0;
zval **hd;
@@ -304,12 +340,21 @@
PHALCON_GET_HKEY(key, ah0, hp0);
PHALCON_GET_HVALUE(value);
-
+
+ if (Z_TYPE_P(key) == IS_STRING) {
+ key_or_tmp = &key;
+ }
+ else {
+ PHALCON_INIT_NVAR(tmp);
+ phalcon_cast(tmp, key, IS_STRING);
+ key_or_tmp = &tmp;
+ }
+
if (Z_TYPE_P(value) == IS_OBJECT) {
- if (phalcon_isset_property_zval(this_ptr, key TSRMLS_CC)) {
+ if (phalcon_isset_property_zval(this_ptr, *key_or_tmp TSRMLS_CC)) {
PHALCON_OBS_NVAR(active_value);
- phalcon_read_property_zval(&active_value, this_ptr, key, PH_NOISY_CC);
+ phalcon_read_property_zval(&active_value, this_ptr, *key_or_tmp, PH_NOISY_CC);
if (Z_TYPE_P(active_value) == IS_OBJECT) {
if (phalcon_method_exists_ex(active_value, SS("merge") TSRMLS_CC) == SUCCESS) {
phalcon_call_method_p1_noret(active_value, "merge", value);
@@ -319,8 +364,8 @@
}
}
}
- phalcon_update_property_zval_zval(this_ptr, key, value TSRMLS_CC);
-
+ phalcon_update_property_zval_zval(this_ptr, *key_or_tmp, value TSRMLS_CC);
+
zend_hash_move_forward_ex(ah0, &hp0);
}
Please grab this file to test.
This is the implementation of
- cast numeric index as a string
Comments/bug reports are welcome :-)
I added a fix scanning the sub-array checking if it has numeric keys leaving it as an array. Mixing strings/numeric keys will result in leaving the array as it is.
OK, I have closed the pull request
Great! Thanx