Skip to content

Commit

Permalink
Merge pull request #1668 from sjinks/translate
Browse files Browse the repository at this point in the history
Faster Phalcon\Translate implementation using object handlers
  • Loading branch information
Phalcon committed Dec 11, 2013
2 parents 9af252e + 2c6b895 commit 6950c44
Show file tree
Hide file tree
Showing 6 changed files with 211 additions and 94 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,8 @@
- Added Phalcon\Tag::setTitleSeparator() (#1365)
- Added support for <optgroup> (#1422)
- Fixed inconsistency in Tag::stylesheetLink/javascriptInclude w.r.t. local URLs (#1486)
- Phalcon\Translate:
- Optimized Phalcon\Translate\Adapter, Phalcon\Translate\Adapter\NativeArray by using object handlers (#1668)
- Phalcon\Validation:
- Added support for error codes (#1171)
- Bug fixes (#1399)
Expand Down
118 changes: 84 additions & 34 deletions ext/translate/adapter.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,18 +42,85 @@
*/


PHALCON_STATIC zend_object_handlers phalcon_translate_adapter_object_handlers;

static zval* phalcon_translate_adapter_read_dimension(zval *object, zval *offset, int type TSRMLS_DC)
{
zval *ret = NULL;

if (Z_OBJCE_P(object)->type != ZEND_INTERNAL_CLASS) {
return zend_get_std_object_handlers()->read_dimension(object, offset, type TSRMLS_CC);
}

phalcon_call_method_params(ret, &ret, object, SL("query"), zend_inline_hash_func(SS("query")) TSRMLS_CC, 2, offset, PHALCON_GLOBAL(z_null));
return UNEXPECTED(EG(exception) != NULL) ? NULL : ret;
}

static void phalcon_translate_adapter_write_dimension(zval *object, zval *offset, zval *value TSRMLS_DC)
{
if (Z_OBJCE_P(object)->type != ZEND_INTERNAL_CLASS) {
zend_get_std_object_handlers()->write_dimension(object, offset, value TSRMLS_CC);
return;
}

zend_throw_exception_ex(phalcon_translate_exception_ce, 0 TSRMLS_CC, "'%s' is an immutable ArrayAccess object", "Phalcon\\Translate\\Adapter");
}

static int phalcon_translate_adapter_has_dimension(zval *object, zval *offset, int check_empty TSRMLS_DC)
{
zval *exists = NULL;
int retval;

if (Z_OBJCE_P(object)->type != ZEND_INTERNAL_CLASS) {
return zend_get_std_object_handlers()->has_dimension(object, offset, check_empty TSRMLS_CC);
}

phalcon_call_method_params(exists, &exists, object, SL("exists"), zend_inline_hash_func(SS("exists")) TSRMLS_CC, 1, offset);
if (UNEXPECTED(EG(exception) != NULL)) {
return 0;
}

retval = zend_is_true(exists);
zval_ptr_dtor(&exists);
return retval;
}

static void phalcon_translate_adapter_unset_dimension(zval *object, zval *offset TSRMLS_DC)
{
if (Z_OBJCE_P(object)->type != ZEND_INTERNAL_CLASS) {
zend_get_std_object_handlers()->unset_dimension(object, offset TSRMLS_CC);
return;
}

zend_throw_exception_ex(phalcon_translate_exception_ce, 0 TSRMLS_CC, "'%s' is an immutable ArrayAccess object", "Phalcon\\Translate\\Adapter");
}


/**
* Phalcon\Translate\Adapter initializer
*/
PHALCON_INIT_CLASS(Phalcon_Translate_Adapter){

PHALCON_REGISTER_CLASS(Phalcon\\Translate, Adapter, translate_adapter, phalcon_translate_adapter_method_entry, ZEND_ACC_EXPLICIT_ABSTRACT_CLASS);

phalcon_translate_adapter_object_handlers = *zend_get_std_object_handlers();
phalcon_translate_adapter_object_handlers.read_dimension = phalcon_translate_adapter_read_dimension;
phalcon_translate_adapter_object_handlers.write_dimension = phalcon_translate_adapter_write_dimension;
phalcon_translate_adapter_object_handlers.has_dimension = phalcon_translate_adapter_has_dimension;
phalcon_translate_adapter_object_handlers.unset_dimension = phalcon_translate_adapter_unset_dimension;

zend_class_implements(phalcon_translate_adapter_ce TSRMLS_CC, 1, zend_ce_arrayaccess);

return SUCCESS;
}

/**
* Class constructore
*/
PHP_METHOD(Phalcon_Translate_Adapter, __construct)
{
Z_OBJ_HT_P(getThis()) = &phalcon_translate_adapter_object_handlers;
}
/**
* Returns the translation string of the given key
*
Expand All @@ -65,16 +132,16 @@ PHP_METHOD(Phalcon_Translate_Adapter, _){

zval *translate_key, *placeholders = NULL;

PHALCON_MM_GROW();
phalcon_fetch_params(0, 1, 1, &translate_key, &placeholders);

phalcon_fetch_params(1, 1, 1, &translate_key, &placeholders);

if (!placeholders) {
placeholders = PHALCON_GLOBAL(z_null);
}

phalcon_call_method_p2(return_value, this_ptr, "query", translate_key, placeholders);
RETURN_MM();

phalcon_call_method_params(return_value, return_value_ptr, this_ptr, SL("query"), zend_inline_hash_func(SS("query")) TSRMLS_CC, 2, translate_key, placeholders);
if (return_value_ptr && EG(exception)) {
ALLOC_INIT_ZVAL(*return_value_ptr);
}
}

/**
Expand All @@ -85,12 +152,7 @@ PHP_METHOD(Phalcon_Translate_Adapter, _){
*/
PHP_METHOD(Phalcon_Translate_Adapter, offsetSet){

zval *offset, *value;

phalcon_fetch_params(0, 2, 0, &offset, &value);

PHALCON_THROW_EXCEPTION_STRW(phalcon_translate_exception_ce, "Translate is an immutable ArrayAccess object");
return;
zend_throw_exception_ex(phalcon_translate_exception_ce, 0 TSRMLS_CC, "'%s' is an immutable ArrayAccess object", "Phalcon\\Translate\\Adapter");
}

/**
Expand All @@ -101,14 +163,12 @@ PHP_METHOD(Phalcon_Translate_Adapter, offsetSet){
*/
PHP_METHOD(Phalcon_Translate_Adapter, offsetExists){

zval *translate_key;
zval **translate_key;
int exists;

PHALCON_MM_GROW();

phalcon_fetch_params(1, 1, 0, &translate_key);

phalcon_call_method_p1(return_value, this_ptr, "exists", translate_key);
RETURN_MM();
phalcon_fetch_params_ex(1, 0, &translate_key);
exists = phalcon_translate_adapter_has_dimension(getThis(), *translate_key, 1 TSRMLS_CC);
RETURN_BOOL(exists);
}

/**
Expand All @@ -118,12 +178,7 @@ PHP_METHOD(Phalcon_Translate_Adapter, offsetExists){
*/
PHP_METHOD(Phalcon_Translate_Adapter, offsetUnset){

zval *offset;

phalcon_fetch_params(0, 1, 0, &offset);

PHALCON_THROW_EXCEPTION_STRW(phalcon_translate_exception_ce, "Translate is an immutable ArrayAccess object");
return;
zend_throw_exception_ex(phalcon_translate_exception_ce, 0 TSRMLS_CC, "'%s' is an immutable ArrayAccess object", "Phalcon\\Translate\\Adapter");
}

/**
Expand All @@ -134,14 +189,9 @@ PHP_METHOD(Phalcon_Translate_Adapter, offsetUnset){
*/
PHP_METHOD(Phalcon_Translate_Adapter, offsetGet){

zval *translate_key, *null_value;
zval **translate_key, *res;

PHALCON_MM_GROW();

phalcon_fetch_params(1, 1, 0, &translate_key);

PHALCON_INIT_VAR(null_value);
phalcon_call_method_p2(return_value, this_ptr, "query", translate_key, null_value);
RETURN_MM();
phalcon_fetch_params_ex(1, 0, &translate_key);
res = phalcon_translate_adapter_read_dimension(getThis(), *translate_key, BP_VAR_R TSRMLS_CC);
RETURN_ZVAL(res, 1, 0);
}

3 changes: 3 additions & 0 deletions ext/translate/adapter.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,11 @@
*/

extern zend_class_entry *phalcon_translate_adapter_ce;
zend_object_handlers phalcon_translate_adapter_object_handlers;

PHALCON_INIT_CLASS(Phalcon_Translate_Adapter);

PHP_METHOD(Phalcon_Translate_Adapter, __construct);
PHP_METHOD(Phalcon_Translate_Adapter, _);
PHP_METHOD(Phalcon_Translate_Adapter, offsetSet);
PHP_METHOD(Phalcon_Translate_Adapter, offsetExists);
Expand Down Expand Up @@ -50,6 +52,7 @@ ZEND_BEGIN_ARG_INFO_EX(arginfo_phalcon_translate_adapter_offsetget, 0, 0, 1)
ZEND_END_ARG_INFO()

PHALCON_INIT_FUNCS(phalcon_translate_adapter_method_entry){
PHP_ME(Phalcon_Translate_Adapter, __construct, NULL, ZEND_ACC_PUBLIC | ZEND_ACC_CTOR)
PHP_ME(Phalcon_Translate_Adapter, _, arginfo_phalcon_translate_adapter__, ZEND_ACC_PUBLIC)
PHP_ME(Phalcon_Translate_Adapter, offsetSet, arginfo_phalcon_translate_adapter_offsetset, ZEND_ACC_PUBLIC)
PHP_ME(Phalcon_Translate_Adapter, offsetExists, arginfo_phalcon_translate_adapter_offsetexists, ZEND_ACC_PUBLIC)
Expand Down
Loading

0 comments on commit 6950c44

Please sign in to comment.