Skip to content

Commit

Permalink
Merge pull request #1279 from sjinks/issue-1277
Browse files Browse the repository at this point in the history
[1.3.0] Fix #1277 (ability to clone \Phalcon\Config)
  • Loading branch information
Phalcon committed Sep 23, 2013
2 parents 731f242 + 5a7bc48 commit a3dccb4
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 2 deletions.
18 changes: 16 additions & 2 deletions ext/config.c
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,7 @@ static void phalcon_config_object_dtor(void* v TSRMLS_DC)
*/
static zend_object_value phalcon_config_object_ctor(zend_class_entry* ce TSRMLS_DC)
{
phalcon_config_object* obj = ecalloc(1, sizeof(phalcon_config_object));
phalcon_config_object *obj = ecalloc(1, sizeof(phalcon_config_object));
zend_object_value retval;

zend_object_std_init(&obj->obj, ce TSRMLS_CC);
Expand All @@ -331,14 +331,27 @@ static zend_object_value phalcon_config_object_ctor(zend_class_entry* ce TSRMLS_
obj,
(zend_objects_store_dtor_t)zend_objects_destroy_object,
phalcon_config_object_dtor,
NULL TSRMLS_CC
NULL
TSRMLS_CC
);

retval.handlers = &phalcon_config_object_handlers;

return retval;
}

static zend_object_value phalcon_config_clone_obj(zval *object TSRMLS_DC)
{
phalcon_config_object *orig = fetchPhalconConfigObject(object TSRMLS_CC);
zend_object_value result = phalcon_config_object_ctor(Z_OBJCE_P(object) TSRMLS_CC);
phalcon_config_object *clone = zend_object_store_get_object_by_handle(result.handle TSRMLS_CC);

zend_objects_clone_members(&clone->obj, result, &orig->obj, Z_OBJ_HANDLE_P(object) TSRMLS_CC);
zend_hash_copy(clone->props, orig->props, (copy_ctor_func_t)zval_add_ref, NULL, sizeof(zval*));

return result;
}

/**
* Phalcon\Config initializer
*/
Expand All @@ -360,6 +373,7 @@ PHALCON_INIT_CLASS(Phalcon_Config){
phalcon_config_object_handlers.has_dimension = phalcon_config_has_dimension;
phalcon_config_object_handlers.get_properties = phalcon_config_get_properties;
phalcon_config_object_handlers.compare_objects = phalcon_config_compare_objects;
phalcon_config_object_handlers.clone_obj = phalcon_config_clone_obj;

zend_class_implements(phalcon_config_ce TSRMLS_CC, 2, zend_ce_arrayaccess, spl_ce_Countable);

Expand Down
21 changes: 21 additions & 0 deletions ext/tests/issue-1277.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
--TEST--
Phalcon\Config after clone is empty - https://github.com/phalcon/cphalcon/issues/1277
--SKIPIF--
<?php include('skipif.inc'); ?>
--FILE--
<?php
$c1 = new Phalcon\Config();
$c1["test"] = 1;
$c2 = clone $c1;
var_dump($c1);
var_dump($c2);
?>
--EXPECT--
object(Phalcon\Config)#1 (1) {
["test"]=>
int(1)
}
object(Phalcon\Config)#2 (1) {
["test"]=>
int(1)
}
9 changes: 9 additions & 0 deletions unit-tests/ConfigTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -331,4 +331,13 @@ public function testIssue1024()
),
);
}

public function testIssue1277()
{
$c1 = new \Phalcon\Config();
$c1["test"] = 1;
$c2 = clone $c1;
$this->assertEquals($c1, $c2);
$this->assertEquals($c1->toArray(), $c2->toArray());
}
}

0 comments on commit a3dccb4

Please sign in to comment.