Skip to content

Commit

Permalink
Merge pull request #1487 from sjinks/fastdi
Browse files Browse the repository at this point in the history
Fast DI — offsetUnset()
  • Loading branch information
Phalcon committed Oct 30, 2013
2 parents c4351e6 + 85ceb1e commit 97417ca
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 1 deletion.
32 changes: 31 additions & 1 deletion ext/di.c
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,35 @@ static void phalcon_di_write_dimension(zval *object, zval *offset, zval *value T
}
}

static inline void phalcon_di_unset_dimension_internal(phalcon_di_object *obj, zval *offset)
{
assert(Z_TYPE_P(offset) == IS_STRING);
zend_symtable_del(obj->services, Z_STRVAL_P(offset), Z_STRLEN_P(offset)+1);
}

static void phalcon_di_unset_dimension(zval *object, zval *offset TSRMLS_DC)
{
phalcon_di_object *obj = phalcon_di_get_object(object TSRMLS_CC);
zval tmp;

if (obj->obj.ce->type != ZEND_INTERNAL_CLASS) {
zend_get_std_object_handlers()->unset_dimension(object, offset TSRMLS_CC);
return;
}

if (UNEXPECTED(Z_TYPE_P(offset) != IS_STRING)) {
ZVAL_ZVAL(&tmp, offset, 1, 0);
convert_to_string(&tmp);
offset = &tmp;
}

phalcon_di_unset_dimension_internal(obj, offset);

if (UNEXPECTED(offset == &tmp)) {
zval_dtor(&tmp);
}
}

static HashTable* phalcon_di_get_properties(zval* object TSRMLS_DC)
{
HashTable* props = zend_std_get_properties(object TSRMLS_CC);
Expand Down Expand Up @@ -451,6 +480,7 @@ PHALCON_INIT_CLASS(Phalcon_DI){
phalcon_di_object_handlers.read_dimension = phalcon_di_read_dimension;
phalcon_di_object_handlers.has_dimension = phalcon_di_has_dimension;
phalcon_di_object_handlers.write_dimension = phalcon_di_write_dimension;
phalcon_di_object_handlers.unset_dimension = phalcon_di_unset_dimension;
phalcon_di_object_handlers.get_properties = phalcon_di_get_properties;
phalcon_di_object_handlers.get_method = phalcon_di_get_method;
phalcon_di_object_handlers.call_method = (zend_object_call_method_t)phalcon_di_call_method;
Expand Down Expand Up @@ -540,7 +570,7 @@ PHP_METHOD(Phalcon_DI, remove){
PHALCON_ENSURE_IS_STRING(name);

obj = phalcon_di_get_object(getThis() TSRMLS_CC);
zend_symtable_del(obj->services, Z_STRVAL_PP(name), Z_STRLEN_PP(name)+1);
phalcon_di_unset_dimension_internal(obj, *name);
}

/**
Expand Down
12 changes: 12 additions & 0 deletions ext/tests/issue-1473.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@ class MyDI extends \Phalcon\DI
return parent::offsetExists($offset);
}

public function offsetUnset($offset)
{
echo __METHOD__, PHP_EOL;
return parent::offsetUnset($offset);
}

public function __call($method, $params = null)
{
echo __METHOD__, PHP_EOL;
Expand Down Expand Up @@ -71,6 +77,9 @@ try {
catch (\Phalcon\DI\Exception $e) {
echo $e->getMessage(), PHP_EOL;
}

unset($clone['test']);
var_dump(isset($di['test']));
?>
--EXPECTF--
MyDI::offsetSet
Expand All @@ -91,3 +100,6 @@ MyDI::__call
string(8) "stdClass"
MyDI::__call
Call to undefined method or service 'somethingWeird'
MyDI::offsetUnset
MyDI::offsetExists
bool(true)

0 comments on commit 97417ca

Please sign in to comment.