aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/php
diff options
context:
space:
mode:
Diffstat (limited to 'src/php')
-rw-r--r--src/php/README.md43
-rw-r--r--src/php/composer.json4
-rw-r--r--src/php/ext/grpc/call.c545
-rw-r--r--src/php/ext/grpc/call.h30
-rw-r--r--src/php/ext/grpc/call_credentials.c141
-rwxr-xr-xsrc/php/ext/grpc/call_credentials.h29
-rw-r--r--src/php/ext/grpc/channel.c168
-rwxr-xr-xsrc/php/ext/grpc/channel.h17
-rw-r--r--src/php/ext/grpc/channel_credentials.c117
-rwxr-xr-xsrc/php/ext/grpc/channel_credentials.h28
-rw-r--r--src/php/ext/grpc/package.xml156
-rw-r--r--src/php/ext/grpc/php7_wrapper.h166
-rw-r--r--src/php/ext/grpc/server.c115
-rwxr-xr-xsrc/php/ext/grpc/server.h18
-rw-r--r--src/php/ext/grpc/server_credentials.c98
-rwxr-xr-xsrc/php/ext/grpc/server_credentials.h25
-rw-r--r--src/php/ext/grpc/timeval.c129
-rwxr-xr-xsrc/php/ext/grpc/timeval.h20
18 files changed, 506 insertions, 1343 deletions
diff --git a/src/php/README.md b/src/php/README.md
index 8abedc40a3..7e9819b256 100644
--- a/src/php/README.md
+++ b/src/php/README.md
@@ -9,23 +9,12 @@ GA
## Environment
-Prerequisite: `php` >=5.5, `phpize`, `pecl`, `phpunit`
-
-**Linux (Debian):**
-
-```sh
-$ sudo apt-get install php5 php5-dev php-pear
-```
-
-**Linux (CentOS):**
-
-```sh
-$ yum install php55w
-$ yum --enablerepo=remi,remi-php55 install php-devel php-pear
-```
-
-**Mac OS X:**
+Prerequisite:
+* `php` 5.5 or above, 7.0 or above
+* `pear` and `pecl`
+* `phpunit`
+**PEAR:**
```sh
$ curl -O http://pear.php.net/go-pear.phar
$ sudo php -d detect_unicode=0 go-pear.phar
@@ -72,13 +61,7 @@ $ sudo make install
### gRPC PHP extension
-Install the gRPC PHP extension from PECL
-
-```sh
-$ sudo pecl install grpc
-```
-
-Or, compile from source
+Compile the gRPC PHP extension
```sh
$ cd grpc/src/php/ext/grpc
@@ -148,12 +131,8 @@ Alternatively, you can download `protoc` binaries from [the protocol buffers Git
You need to install `protoc-gen-php` to generate stub class `.php` files from service definition `.proto` files.
```sh
-$ cd grpc/src/php/vendor/stanley-cheung/protobuf-php # if you had run `composer install` in the previous step
-
-OR
-
-$ git clone https://github.com/stanley-cheung/Protobuf-PHP # clone from github repo
-
+$ git clone https://github.com/stanley-cheung/Protobuf-PHP
+$ cd Protobuf-PHP
$ gem install rake ronn
$ rake pear:package version=1.0
$ sudo pear install Protobuf-1.0.tgz
@@ -175,7 +154,7 @@ Run a local server serving the math services. Please see [Node][] for how to run
```sh
$ cd grpc
$ npm install
-$ nodejs src/node/test/math/math_server.js
+$ node src/node/test/math/math_server.js
```
### Run test client
@@ -212,7 +191,7 @@ Make sure the Node math server is still running, as above.
```sh
$ cd grpc
$ npm install
-$ nodejs src/node/test/math/math_server.js
+$ node src/node/test/math/math_server.js
```
Make sure you have run `composer install` to generate the `vendor/autoload.php` file
@@ -282,7 +261,7 @@ Make sure the Node math server is still running, as above.
```sh
$ cd grpc
$ npm install
-$ nodejs src/node/test/math/math_server.js
+$ node src/node/test/math/math_server.js
```
Make sure you have run `composer install` to generate the `vendor/autoload.php` file
diff --git a/src/php/composer.json b/src/php/composer.json
index 88418206e8..1eacc643a2 100644
--- a/src/php/composer.json
+++ b/src/php/composer.json
@@ -8,7 +8,9 @@
"version": "1.0.0",
"require": {
"php": ">=5.5.0",
- "stanley-cheung/protobuf-php": "v0.6",
+ "stanley-cheung/protobuf-php": "v0.6"
+ },
+ "require-dev": {
"google/auth": "v0.9"
},
"autoload": {
diff --git a/src/php/ext/grpc/call.c b/src/php/ext/grpc/call.c
index d455002de6..66ca1513ed 100644
--- a/src/php/ext/grpc/call.c
+++ b/src/php/ext/grpc/call.c
@@ -58,66 +58,44 @@
#include "byte_buffer.h"
zend_class_entry *grpc_ce_call;
-
-#if PHP_MAJOR_VERSION < 7
+#if PHP_MAJOR_VERSION >= 7
+static zend_object_handlers call_ce_handlers;
+#endif
/* Frees and destroys an instance of wrapped_grpc_call */
-void free_wrapped_grpc_call(void *object TSRMLS_DC) {
- wrapped_grpc_call *call = (wrapped_grpc_call *)object;
- if (call->owned && call->wrapped != NULL) {
- grpc_call_destroy(call->wrapped);
+PHP_GRPC_FREE_WRAPPED_FUNC_START(wrapped_grpc_call)
+ if (p->owned && p->wrapped != NULL) {
+ grpc_call_destroy(p->wrapped);
}
- zend_object_std_dtor(&call->std TSRMLS_CC);
- efree(call);
-}
+PHP_GRPC_FREE_WRAPPED_FUNC_END()
/* Initializes an instance of wrapped_grpc_call to be associated with an object
* of a class specified by class_type */
-zend_object_value create_wrapped_grpc_call(zend_class_entry *class_type
- TSRMLS_DC) {
- zend_object_value retval;
- wrapped_grpc_call *intern;
-
- intern = (wrapped_grpc_call *)emalloc(sizeof(wrapped_grpc_call));
- memset(intern, 0, sizeof(wrapped_grpc_call));
-
+php_grpc_zend_object create_wrapped_grpc_call(zend_class_entry *class_type
+ TSRMLS_DC) {
+ PHP_GRPC_ALLOC_CLASS_OBJECT(wrapped_grpc_call);
zend_object_std_init(&intern->std, class_type TSRMLS_CC);
object_properties_init(&intern->std, class_type);
- retval.handle = zend_objects_store_put(
- intern, (zend_objects_store_dtor_t)zend_objects_destroy_object,
- free_wrapped_grpc_call, NULL TSRMLS_CC);
- retval.handlers = zend_get_std_object_handlers();
- return retval;
-}
-
-/* Wraps a grpc_call struct in a PHP object. Owned indicates whether the struct
- should be destroyed at the end of the object's lifecycle */
-zval *grpc_php_wrap_call(grpc_call *wrapped, bool owned TSRMLS_DC) {
- zval *call_object;
- MAKE_STD_ZVAL(call_object);
- object_init_ex(call_object, grpc_ce_call);
- wrapped_grpc_call *call =
- (wrapped_grpc_call *)zend_object_store_get_object(call_object TSRMLS_CC);
- call->wrapped = wrapped;
- call->owned = owned;
- return call_object;
+ PHP_GRPC_FREE_CLASS_OBJECT(wrapped_grpc_call, call_ce_handlers);
}
/* Creates and returns a PHP array object with the data in a
* grpc_metadata_array. Returns NULL on failure */
-zval *grpc_parse_metadata_array(grpc_metadata_array *metadata_array TSRMLS_DC) {
+zval *grpc_parse_metadata_array(grpc_metadata_array
+ *metadata_array TSRMLS_DC) {
int count = metadata_array->count;
grpc_metadata *elements = metadata_array->metadata;
- int i;
zval *array;
- zval **data = NULL;
+ PHP_GRPC_MAKE_STD_ZVAL(array);
+ array_init(array);
+ int i;
HashTable *array_hash;
zval *inner_array;
char *str_key;
char *str_val;
size_t key_len;
- MAKE_STD_ZVAL(array);
- array_init(array);
+ zval *data = NULL;
+
array_hash = Z_ARRVAL_P(array);
grpc_metadata *elem;
for (i = 0; i < count; i++) {
@@ -127,9 +105,9 @@ zval *grpc_parse_metadata_array(grpc_metadata_array *metadata_array TSRMLS_DC) {
memcpy(str_key, elem->key, key_len);
str_val = ecalloc(elem->value_length + 1, sizeof(char));
memcpy(str_val, elem->value, elem->value_length);
- if (zend_hash_find(array_hash, str_key, key_len, (void **)data) ==
- SUCCESS) {
- if (Z_TYPE_P(*data) != IS_ARRAY) {
+ if (php_grpc_zend_hash_find(array_hash, str_key, key_len, (void **)&data)
+ == SUCCESS) {
+ if (Z_TYPE_P(data) != IS_ARRAY) {
zend_throw_exception(zend_exception_get_default(TSRMLS_C),
"Metadata hash somehow contains wrong types.",
1 TSRMLS_CC);
@@ -137,11 +115,13 @@ zval *grpc_parse_metadata_array(grpc_metadata_array *metadata_array TSRMLS_DC) {
efree(str_val);
return NULL;
}
- add_next_index_stringl(*data, str_val, elem->value_length, false);
+ php_grpc_add_next_index_stringl(data, str_val, elem->value_length,
+ false);
} else {
- MAKE_STD_ZVAL(inner_array);
+ PHP_GRPC_MAKE_STD_ZVAL(inner_array);
array_init(inner_array);
- add_next_index_stringl(inner_array, str_val, elem->value_length, false);
+ php_grpc_add_next_index_stringl(inner_array, str_val,
+ elem->value_length, false);
add_assoc_zval(array, str_key, inner_array);
}
}
@@ -151,185 +131,64 @@ zval *grpc_parse_metadata_array(grpc_metadata_array *metadata_array TSRMLS_DC) {
/* Populates a grpc_metadata_array with the data in a PHP array object.
Returns true on success and false on failure */
bool create_metadata_array(zval *array, grpc_metadata_array *metadata) {
- zval **inner_array;
- zval **value;
HashTable *array_hash;
- HashPosition array_pointer;
HashTable *inner_array_hash;
- HashPosition inner_array_pointer;
- char *key;
- uint key_len;
- ulong index;
- if (Z_TYPE_P(array) != IS_ARRAY) {
- return false;
- }
- grpc_metadata_array_init(metadata);
- array_hash = Z_ARRVAL_P(array);
- for (zend_hash_internal_pointer_reset_ex(array_hash, &array_pointer);
- zend_hash_get_current_data_ex(array_hash, (void**)&inner_array,
- &array_pointer) == SUCCESS;
- zend_hash_move_forward_ex(array_hash, &array_pointer)) {
- if (zend_hash_get_current_key_ex(array_hash, &key, &key_len, &index, 0,
- &array_pointer) != HASH_KEY_IS_STRING) {
- return false;
- }
- if (Z_TYPE_P(*inner_array) != IS_ARRAY) {
- return false;
- }
- inner_array_hash = Z_ARRVAL_P(*inner_array);
- metadata->capacity += zend_hash_num_elements(inner_array_hash);
- }
- metadata->metadata = gpr_malloc(metadata->capacity * sizeof(grpc_metadata));
- for (zend_hash_internal_pointer_reset_ex(array_hash, &array_pointer);
- zend_hash_get_current_data_ex(array_hash, (void**)&inner_array,
- &array_pointer) == SUCCESS;
- zend_hash_move_forward_ex(array_hash, &array_pointer)) {
- if (zend_hash_get_current_key_ex(array_hash, &key, &key_len, &index, 0,
- &array_pointer) != HASH_KEY_IS_STRING) {
- return false;
- }
- inner_array_hash = Z_ARRVAL_P(*inner_array);
- for (zend_hash_internal_pointer_reset_ex(inner_array_hash,
- &inner_array_pointer);
- zend_hash_get_current_data_ex(inner_array_hash, (void**)&value,
- &inner_array_pointer) == SUCCESS;
- zend_hash_move_forward_ex(inner_array_hash, &inner_array_pointer)) {
- if (Z_TYPE_P(*value) != IS_STRING) {
- return false;
- }
- metadata->metadata[metadata->count].key = key;
- metadata->metadata[metadata->count].value = Z_STRVAL_P(*value);
- metadata->metadata[metadata->count].value_length = Z_STRLEN_P(*value);
- metadata->count += 1;
- }
- }
- return true;
-}
-
-#else
-
-static zend_object_handlers call_ce_handlers;
-
-/* Frees and destroys an instance of wrapped_grpc_call */
-static void free_wrapped_grpc_call(zend_object *object) {
- wrapped_grpc_call *call = wrapped_grpc_call_from_obj(object);
- if (call->owned && call->wrapped != NULL) {
- grpc_call_destroy(call->wrapped);
- }
- zend_object_std_dtor(&call->std);
-}
-
-/* Initializes an instance of wrapped_grpc_call to be associated with an
- * object of a class specified by class_type */
-zend_object *create_wrapped_grpc_call(zend_class_entry *class_type) {
- wrapped_grpc_call *intern;
- intern = ecalloc(1, sizeof(wrapped_grpc_call) +
- zend_object_properties_size(class_type));
- zend_object_std_init(&intern->std, class_type);
- object_properties_init(&intern->std, class_type);
- intern->std.handlers = &call_ce_handlers;
- return &intern->std;
-}
-
-/* Wraps a grpc_call struct in a PHP object. Owned indicates whether the
- struct should be destroyed at the end of the object's lifecycle */
-void grpc_php_wrap_call(grpc_call *wrapped, bool owned, zval *call_object) {
- object_init_ex(call_object, grpc_ce_call);
- wrapped_grpc_call *call = Z_WRAPPED_GRPC_CALL_P(call_object);
- call->wrapped = wrapped;
- call->owned = owned;
-}
-
-/* Creates and returns a PHP array object with the data in a
- * grpc_metadata_array. Returns NULL on failure */
-void grpc_parse_metadata_array(grpc_metadata_array *metadata_array,
- zval *array) {
- int count = metadata_array->count;
- grpc_metadata *elements = metadata_array->metadata;
- int i;
- zval *data;
- HashTable *array_hash;
- zval inner_array;
- char *str_key;
- char *str_val;
- size_t key_len;
-
- array_init(array);
- array_hash = HASH_OF(array);
- grpc_metadata *elem;
- for (i = 0; i < count; i++) {
- elem = &elements[i];
- key_len = strlen(elem->key);
- str_key = ecalloc(key_len + 1, sizeof(char));
- memcpy(str_key, elem->key, key_len);
- str_val = ecalloc(elem->value_length + 1, sizeof(char));
- memcpy(str_val, elem->value, elem->value_length);
- if ((data = zend_hash_str_find(array_hash, str_key, key_len)) != NULL) {
- if (Z_TYPE_P(data) != IS_ARRAY) {
- zend_throw_exception(zend_exception_get_default(),
- "Metadata hash somehow contains wrong types.",
- 1);
- efree(str_key);
- efree(str_val);
- return;
- }
- add_next_index_stringl(data, str_val, elem->value_length);
- } else {
- array_init(&inner_array);
- add_next_index_stringl(&inner_array, str_val, elem->value_length);
- add_assoc_zval(array, str_key, &inner_array);
- }
- }
-}
-
-/* Populates a grpc_metadata_array with the data in a PHP array object.
- Returns true on success and false on failure */
-bool create_metadata_array(zval *array, grpc_metadata_array *metadata) {
- zval *inner_array;
zval *value;
- HashTable *array_hash;
- HashTable *inner_array_hash;
- zend_string *key;
+ zval *inner_array;
if (Z_TYPE_P(array) != IS_ARRAY) {
return false;
}
grpc_metadata_array_init(metadata);
- array_hash = HASH_OF(array);
+ array_hash = Z_ARRVAL_P(array);
- ZEND_HASH_FOREACH_STR_KEY_VAL(array_hash, key, inner_array) {
- if (key == NULL) {
+ char *key;
+ int key_type;
+ PHP_GRPC_HASH_FOREACH_STR_KEY_VAL_START(array_hash, key, key_type,
+ inner_array)
+ if (key_type != HASH_KEY_IS_STRING || key == NULL) {
return false;
}
if (Z_TYPE_P(inner_array) != IS_ARRAY) {
return false;
}
- inner_array_hash = HASH_OF(inner_array);
+ inner_array_hash = Z_ARRVAL_P(inner_array);
metadata->capacity += zend_hash_num_elements(inner_array_hash);
- }
- ZEND_HASH_FOREACH_END();
+ PHP_GRPC_HASH_FOREACH_END()
metadata->metadata = gpr_malloc(metadata->capacity * sizeof(grpc_metadata));
- ZEND_HASH_FOREACH_STR_KEY_VAL(array_hash, key, inner_array) {
- if (key == NULL) {
+ char *key1 = NULL;
+ int key_type1;
+ PHP_GRPC_HASH_FOREACH_STR_KEY_VAL_START(array_hash, key1, key_type1,
+ inner_array)
+ if (key_type1 != HASH_KEY_IS_STRING) {
return false;
}
- inner_array_hash = HASH_OF(inner_array);
-
- ZEND_HASH_FOREACH_VAL(inner_array_hash, value) {
+ inner_array_hash = Z_ARRVAL_P(inner_array);
+ PHP_GRPC_HASH_FOREACH_VAL_START(inner_array_hash, value)
if (Z_TYPE_P(value) != IS_STRING) {
return false;
}
- metadata->metadata[metadata->count].key = ZSTR_VAL(key);
+ metadata->metadata[metadata->count].key = key1;
metadata->metadata[metadata->count].value = Z_STRVAL_P(value);
metadata->metadata[metadata->count].value_length = Z_STRLEN_P(value);
metadata->count += 1;
- } ZEND_HASH_FOREACH_END();
- } ZEND_HASH_FOREACH_END();
+ PHP_GRPC_HASH_FOREACH_END()
+ PHP_GRPC_HASH_FOREACH_END()
return true;
}
-#endif
+/* Wraps a grpc_call struct in a PHP object. Owned indicates whether the
+ struct should be destroyed at the end of the object's lifecycle */
+zval *grpc_php_wrap_call(grpc_call *wrapped, bool owned TSRMLS_DC) {
+ zval *call_object;
+ PHP_GRPC_MAKE_STD_ZVAL(call_object);
+ object_init_ex(call_object, grpc_ce_call);
+ wrapped_grpc_call *call = Z_WRAPPED_GRPC_CALL_P(call_object);
+ call->wrapped = wrapped;
+ call->owned = owned;
+ return call_object;
+}
/**
* Constructs a new instance of the Call class.
@@ -379,29 +238,17 @@ PHP_METHOD(Call, __construct) {
* @return object Object with results of all actions
*/
PHP_METHOD(Call, startBatch) {
-#if PHP_MAJOR_VERSION < 7
- zval **value;
- zval **inner_value;
- HashPosition array_pointer;
- zval **message_value;
- zval **message_flags;
- char *key;
- uint key_len;
- ulong index;
zval *result;
- zval *recv_status;
- MAKE_STD_ZVAL(result);
+ PHP_GRPC_MAKE_STD_ZVAL(result);
object_init(result);
-#else
+ php_grpc_ulong index;
+ zval *recv_status;
+ PHP_GRPC_MAKE_STD_ZVAL(recv_status);
+ object_init(recv_status);
zval *value;
zval *inner_value;
zval *message_value;
zval *message_flags;
- zend_string *key;
- zend_ulong index;
- zval recv_status;
- object_init(return_value);
-#endif
wrapped_grpc_call *call = Z_WRAPPED_GRPC_CALL_P(getThis());
grpc_op ops[8];
@@ -424,7 +271,6 @@ PHP_METHOD(Call, startBatch) {
char *message_str;
size_t message_len;
-
grpc_metadata_array_init(&metadata);
grpc_metadata_array_init(&trailing_metadata);
grpc_metadata_array_init(&recv_metadata);
@@ -439,67 +285,63 @@ PHP_METHOD(Call, startBatch) {
goto cleanup;
}
-#if PHP_MAJOR_VERSION < 7
-
array_hash = Z_ARRVAL_P(array);
- for (zend_hash_internal_pointer_reset_ex(array_hash, &array_pointer);
- zend_hash_get_current_data_ex(array_hash, (void**)&value,
- &array_pointer) == SUCCESS;
- zend_hash_move_forward_ex(array_hash, &array_pointer)) {
- if (zend_hash_get_current_key_ex(array_hash, &key, &key_len, &index, 0,
- &array_pointer) != HASH_KEY_IS_LONG) {
+
+ char *key = NULL;
+ int key_type;
+ PHP_GRPC_HASH_FOREACH_LONG_KEY_VAL_START(array_hash, key, key_type, index,
+ value)
+ if (key_type != HASH_KEY_IS_LONG || key != NULL) {
zend_throw_exception(spl_ce_InvalidArgumentException,
"batch keys must be integers", 1 TSRMLS_CC);
goto cleanup;
}
switch(index) {
case GRPC_OP_SEND_INITIAL_METADATA:
- if (!create_metadata_array(*value, &metadata)) {
+ if (!create_metadata_array(value, &metadata)) {
zend_throw_exception(spl_ce_InvalidArgumentException,
"Bad metadata value given", 1 TSRMLS_CC);
goto cleanup;
}
- ops[op_num].data.send_initial_metadata.count =
- metadata.count;
- ops[op_num].data.send_initial_metadata.metadata =
- metadata.metadata;
+ ops[op_num].data.send_initial_metadata.count = metadata.count;
+ ops[op_num].data.send_initial_metadata.metadata = metadata.metadata;
break;
case GRPC_OP_SEND_MESSAGE:
- if (Z_TYPE_PP(value) != IS_ARRAY) {
+ if (Z_TYPE_P(value) != IS_ARRAY) {
zend_throw_exception(spl_ce_InvalidArgumentException,
"Expected an array for send message",
1 TSRMLS_CC);
goto cleanup;
}
- message_hash = Z_ARRVAL_PP(value);
- if (zend_hash_find(message_hash, "flags", sizeof("flags"),
+ message_hash = Z_ARRVAL_P(value);
+ if (php_grpc_zend_hash_find(message_hash, "flags", sizeof("flags"),
(void **)&message_flags) == SUCCESS) {
- if (Z_TYPE_PP(message_flags) != IS_LONG) {
+ if (Z_TYPE_P(message_flags) != IS_LONG) {
zend_throw_exception(spl_ce_InvalidArgumentException,
"Expected an int for message flags",
1 TSRMLS_CC);
}
- ops[op_num].flags = Z_LVAL_PP(message_flags) & GRPC_WRITE_USED_MASK;
+ ops[op_num].flags = Z_LVAL_P(message_flags) & GRPC_WRITE_USED_MASK;
}
- if (zend_hash_find(message_hash, "message", sizeof("message"),
+ if (php_grpc_zend_hash_find(message_hash, "message", sizeof("message"),
(void **)&message_value) != SUCCESS ||
- Z_TYPE_PP(message_value) != IS_STRING) {
+ Z_TYPE_P(message_value) != IS_STRING) {
zend_throw_exception(spl_ce_InvalidArgumentException,
"Expected a string for send message",
1 TSRMLS_CC);
goto cleanup;
}
ops[op_num].data.send_message =
- string_to_byte_buffer(Z_STRVAL_PP(message_value),
- Z_STRLEN_PP(message_value));
+ string_to_byte_buffer(Z_STRVAL_P(message_value),
+ Z_STRLEN_P(message_value));
break;
case GRPC_OP_SEND_CLOSE_FROM_CLIENT:
break;
case GRPC_OP_SEND_STATUS_FROM_SERVER:
- status_hash = Z_ARRVAL_PP(value);
- if (zend_hash_find(status_hash, "metadata", sizeof("metadata"),
+ status_hash = Z_ARRVAL_P(value);
+ if (php_grpc_zend_hash_find(status_hash, "metadata", sizeof("metadata"),
(void **)&inner_value) == SUCCESS) {
- if (!create_metadata_array(*inner_value, &trailing_metadata)) {
+ if (!create_metadata_array(inner_value, &trailing_metadata)) {
zend_throw_exception(spl_ce_InvalidArgumentException,
"Bad trailing metadata value given",
1 TSRMLS_CC);
@@ -510,32 +352,32 @@ PHP_METHOD(Call, startBatch) {
ops[op_num].data.send_status_from_server.trailing_metadata_count =
trailing_metadata.count;
}
- if (zend_hash_find(status_hash, "code", sizeof("code"),
+ if (php_grpc_zend_hash_find(status_hash, "code", sizeof("code"),
(void**)&inner_value) == SUCCESS) {
- if (Z_TYPE_PP(inner_value) != IS_LONG) {
+ if (Z_TYPE_P(inner_value) != IS_LONG) {
zend_throw_exception(spl_ce_InvalidArgumentException,
"Status code must be an integer",
1 TSRMLS_CC);
goto cleanup;
}
ops[op_num].data.send_status_from_server.status =
- Z_LVAL_PP(inner_value);
+ Z_LVAL_P(inner_value);
} else {
zend_throw_exception(spl_ce_InvalidArgumentException,
"Integer status code is required",
1 TSRMLS_CC);
goto cleanup;
}
- if (zend_hash_find(status_hash, "details", sizeof("details"),
+ if (php_grpc_zend_hash_find(status_hash, "details", sizeof("details"),
(void**)&inner_value) == SUCCESS) {
- if (Z_TYPE_PP(inner_value) != IS_STRING) {
+ if (Z_TYPE_P(inner_value) != IS_STRING) {
zend_throw_exception(spl_ce_InvalidArgumentException,
"Status details must be a string",
1 TSRMLS_CC);
goto cleanup;
}
ops[op_num].data.send_status_from_server.status_details =
- Z_STRVAL_PP(inner_value);
+ Z_STRVAL_P(inner_value);
} else {
zend_throw_exception(spl_ce_InvalidArgumentException,
"String status details is required",
@@ -570,132 +412,7 @@ PHP_METHOD(Call, startBatch) {
ops[op_num].flags = 0;
ops[op_num].reserved = NULL;
op_num++;
- }
-
-#else
-
-array_hash = HASH_OF(array);
- ZEND_HASH_FOREACH_KEY_VAL(array_hash, index, key, value) {
- if (key) {
- zend_throw_exception(spl_ce_InvalidArgumentException,
- "batch keys must be integers", 1);
- goto cleanup;
- }
-
- switch(index) {
- case GRPC_OP_SEND_INITIAL_METADATA:
- if (!create_metadata_array(value, &metadata)) {
- zend_throw_exception(spl_ce_InvalidArgumentException,
- "Bad metadata value given", 1);
- goto cleanup;
- }
- ops[op_num].data.send_initial_metadata.count = metadata.count;
- ops[op_num].data.send_initial_metadata.metadata = metadata.metadata;
- break;
- case GRPC_OP_SEND_MESSAGE:
- if (Z_TYPE_P(value) != IS_ARRAY) {
- zend_throw_exception(spl_ce_InvalidArgumentException,
- "Expected an array for send message", 1);
- goto cleanup;
- }
- message_hash = HASH_OF(value);
- if ((message_flags =
- zend_hash_str_find(message_hash, "flags",
- sizeof("flags") - 1)) != NULL) {
- if (Z_TYPE_P(message_flags) != IS_LONG) {
- zend_throw_exception(spl_ce_InvalidArgumentException,
- "Expected an int for message flags", 1);
- }
- ops[op_num].flags = Z_LVAL_P(message_flags) & GRPC_WRITE_USED_MASK;
- }
- if ((message_value = zend_hash_str_find(message_hash, "message",
- sizeof("message") - 1))
- == NULL || Z_TYPE_P(message_value) != IS_STRING) {
- zend_throw_exception(spl_ce_InvalidArgumentException,
- "Expected a string for send message", 1);
- goto cleanup;
- }
- ops[op_num].data.send_message =
- string_to_byte_buffer(Z_STRVAL_P(message_value),
- Z_STRLEN_P(message_value));
- break;
- case GRPC_OP_SEND_CLOSE_FROM_CLIENT:
- break;
- case GRPC_OP_SEND_STATUS_FROM_SERVER:
- status_hash = HASH_OF(value);
- if ((inner_value = zend_hash_str_find(status_hash, "metadata",
- sizeof("metadata") - 1))
- != NULL) {
- if (!create_metadata_array(inner_value, &trailing_metadata)) {
- zend_throw_exception(spl_ce_InvalidArgumentException,
- "Bad trailing metadata value given", 1);
- goto cleanup;
- }
- ops[op_num].data.send_status_from_server.trailing_metadata =
- trailing_metadata.metadata;
- ops[op_num].data.send_status_from_server.trailing_metadata_count =
- trailing_metadata.count;
- }
- if ((inner_value = zend_hash_str_find(status_hash, "code",
- sizeof("code") - 1)) != NULL) {
- if (Z_TYPE_P(inner_value) != IS_LONG) {
- zend_throw_exception(spl_ce_InvalidArgumentException,
- "Status code must be an integer", 1);
- goto cleanup;
- }
- ops[op_num].data.send_status_from_server.status =
- Z_LVAL_P(inner_value);
- } else {
- zend_throw_exception(spl_ce_InvalidArgumentException,
- "Integer status code is required", 1);
- goto cleanup;
- }
- if ((inner_value = zend_hash_str_find(status_hash, "details",
- sizeof("details") - 1)) != NULL) {
- if (Z_TYPE_P(inner_value) != IS_STRING) {
- zend_throw_exception(spl_ce_InvalidArgumentException,
- "Status details must be a string", 1);
- goto cleanup;
- }
- ops[op_num].data.send_status_from_server.status_details =
- Z_STRVAL_P(inner_value);
- } else {
- zend_throw_exception(spl_ce_InvalidArgumentException,
- "String status details is required", 1);
- goto cleanup;
- }
- break;
- case GRPC_OP_RECV_INITIAL_METADATA:
- ops[op_num].data.recv_initial_metadata = &recv_metadata;
- break;
- case GRPC_OP_RECV_MESSAGE:
- ops[op_num].data.recv_message = &message;
- break;
- case GRPC_OP_RECV_STATUS_ON_CLIENT:
- ops[op_num].data.recv_status_on_client.trailing_metadata =
- &recv_trailing_metadata;
- ops[op_num].data.recv_status_on_client.status = &status;
- ops[op_num].data.recv_status_on_client.status_details =
- &status_details;
- ops[op_num].data.recv_status_on_client.status_details_capacity =
- &status_details_capacity;
- break;
- case GRPC_OP_RECV_CLOSE_ON_SERVER:
- ops[op_num].data.recv_close_on_server.cancelled = &cancelled;
- break;
- default:
- zend_throw_exception(spl_ce_InvalidArgumentException,
- "Unrecognized key in batch", 1);
- goto cleanup;
- }
- ops[op_num].op = (grpc_op_type)index;
- ops[op_num].flags = 0;
- ops[op_num].reserved = NULL;
- op_num++;
- }
- ZEND_HASH_FOREACH_END();
-
-#endif
+ PHP_GRPC_HASH_FOREACH_END()
error = grpc_call_start_batch(call->wrapped, ops, op_num, call->wrapped,
NULL);
@@ -707,7 +424,9 @@ array_hash = HASH_OF(array);
}
grpc_completion_queue_pluck(completion_queue, call->wrapped,
gpr_inf_future(GPR_CLOCK_REALTIME), NULL);
-#if PHP_MAJOR_VERSION < 7
+#if PHP_MAJOR_VERSION >= 7
+ zval recv_md;
+#endif
for (int i = 0; i < op_num; i++) {
switch(ops[i].op) {
case GRPC_OP_SEND_INITIAL_METADATA:
@@ -723,29 +442,38 @@ array_hash = HASH_OF(array);
add_property_bool(result, "send_status", true);
break;
case GRPC_OP_RECV_INITIAL_METADATA:
+#if PHP_MAJOR_VERSION < 7
array = grpc_parse_metadata_array(&recv_metadata TSRMLS_CC);
add_property_zval(result, "metadata", array);
- Z_DELREF_P(array);
+#else
+ recv_md = *grpc_parse_metadata_array(&recv_metadata);
+ add_property_zval(result, "metadata", &recv_md);
+#endif
+ PHP_GRPC_DELREF(array);
break;
case GRPC_OP_RECV_MESSAGE:
byte_buffer_to_string(message, &message_str, &message_len);
if (message_str == NULL) {
add_property_null(result, "message");
} else {
- add_property_stringl(result, "message", message_str, message_len,
- false);
+ php_grpc_add_property_stringl(result, "message", message_str,
+ message_len, false);
}
break;
case GRPC_OP_RECV_STATUS_ON_CLIENT:
- MAKE_STD_ZVAL(recv_status);
- object_init(recv_status);
+#if PHP_MAJOR_VERSION < 7
array = grpc_parse_metadata_array(&recv_trailing_metadata TSRMLS_CC);
add_property_zval(recv_status, "metadata", array);
- Z_DELREF_P(array);
+#else
+ recv_md = *grpc_parse_metadata_array(&recv_trailing_metadata);
+ add_property_zval(recv_status, "metadata", &recv_md);
+#endif
+ PHP_GRPC_DELREF(array);
add_property_long(recv_status, "code", status);
- add_property_string(recv_status, "details", status_details, true);
+ php_grpc_add_property_string(recv_status, "details", status_details,
+ true);
add_property_zval(result, "status", recv_status);
- Z_DELREF_P(recv_status);
+ PHP_GRPC_DELREF(recv_status);
break;
case GRPC_OP_RECV_CLOSE_ON_SERVER:
add_property_bool(result, "cancelled", cancelled);
@@ -754,50 +482,6 @@ array_hash = HASH_OF(array);
break;
}
}
-#else
- for (int i = 0; i < op_num; i++) {
- switch(ops[i].op) {
- case GRPC_OP_SEND_INITIAL_METADATA:
- add_property_bool(return_value, "send_metadata", true);
- break;
- case GRPC_OP_SEND_MESSAGE:
- add_property_bool(return_value, "send_message", true);
- break;
- case GRPC_OP_SEND_CLOSE_FROM_CLIENT:
- add_property_bool(return_value, "send_close", true);
- break;
- case GRPC_OP_SEND_STATUS_FROM_SERVER:
- add_property_bool(return_value, "send_status", true);
- break;
- case GRPC_OP_RECV_INITIAL_METADATA:
- grpc_parse_metadata_array(&recv_metadata, array);
- add_property_zval(return_value, "metadata", array);
- break;
- case GRPC_OP_RECV_MESSAGE:
- byte_buffer_to_string(message, &message_str, &message_len);
- if (message_str == NULL) {
- add_property_null(return_value, "message");
- } else {
- add_property_stringl(return_value, "message", message_str,
- message_len);
- }
- break;
- case GRPC_OP_RECV_STATUS_ON_CLIENT:
- object_init(&recv_status);
- grpc_parse_metadata_array(&recv_trailing_metadata, array);
- add_property_zval(&recv_status, "metadata", array);
- add_property_long(&recv_status, "code", status);
- add_property_string(&recv_status, "details", status_details);
- add_property_zval(return_value, "status", &recv_status);
- break;
- case GRPC_OP_RECV_CLOSE_ON_SERVER:
- add_property_bool(return_value, "cancelled", cancelled);
- break;
- default:
- break;
- }
- }
-#endif
cleanup:
grpc_metadata_array_destroy(&metadata);
@@ -815,11 +499,7 @@ cleanup:
grpc_byte_buffer_destroy(message);
}
}
-#if PHP_MAJOR_VERSION < 7
RETURN_DESTROY_ZVAL(result);
-#else
- RETURN_DESTROY_ZVAL(return_value);
-#endif
}
/**
@@ -880,10 +560,5 @@ void grpc_init_call(TSRMLS_D) {
INIT_CLASS_ENTRY(ce, "Grpc\\Call", call_methods);
ce.create_object = create_wrapped_grpc_call;
grpc_ce_call = zend_register_internal_class(&ce TSRMLS_CC);
-#if PHP_MAJOR_VERSION >= 7
- memcpy(&call_ce_handlers, zend_get_std_object_handlers(),
- sizeof(zend_object_handlers));
- call_ce_handlers.offset = XtOffsetOf(wrapped_grpc_call, std);
- call_ce_handlers.free_obj = free_wrapped_grpc_call;
-#endif
+ PHP_GRPC_INIT_HANDLER(wrapped_grpc_call, call_ce_handlers);
}
diff --git a/src/php/ext/grpc/call.h b/src/php/ext/grpc/call.h
index 4931bf392e..e49f9b382a 100644
--- a/src/php/ext/grpc/call.h
+++ b/src/php/ext/grpc/call.h
@@ -48,34 +48,19 @@
/* Class entry for the Call PHP class */
extern zend_class_entry *grpc_ce_call;
-#if PHP_MAJOR_VERSION < 7
-
/* Wrapper struct for grpc_call that can be associated with a PHP object */
-typedef struct wrapped_grpc_call {
- zend_object std;
+PHP_GRPC_WRAP_OBJECT_START(wrapped_grpc_call)
bool owned;
grpc_call *wrapped;
-} wrapped_grpc_call;
-
-/* Creates a Call object that wraps the given grpc_call struct */
-zval *grpc_php_wrap_call(grpc_call *wrapped, bool owned TSRMLS_DC);
+PHP_GRPC_WRAP_OBJECT_END(wrapped_grpc_call)
-/* Creates and returns a PHP associative array of metadata from a C array of
- * call metadata */
-zval *grpc_parse_metadata_array(grpc_metadata_array *metadata_array TSRMLS_DC);
+#if PHP_MAJOR_VERSION < 7
#define Z_WRAPPED_GRPC_CALL_P(zv) \
(wrapped_grpc_call *)zend_object_store_get_object(zv TSRMLS_CC)
#else
-/* Wrapper struct for grpc_call that can be associated with a PHP object */
-typedef struct wrapped_grpc_call {
- bool owned;
- grpc_call *wrapped;
- zend_object std;
-} wrapped_grpc_call;
-
static inline wrapped_grpc_call
*wrapped_grpc_call_from_obj(zend_object *obj) {
return (wrapped_grpc_call*)((char*)(obj) -
@@ -84,15 +69,14 @@ static inline wrapped_grpc_call
#define Z_WRAPPED_GRPC_CALL_P(zv) wrapped_grpc_call_from_obj(Z_OBJ_P((zv)))
-/* Creates a Call object that wraps the given grpc_call struct */
-void grpc_php_wrap_call(grpc_call *wrapped, bool owned, zval *call_object);
+#endif /* PHP_MAJOR_VERSION */
/* Creates and returns a PHP associative array of metadata from a C array of
* call metadata */
-void grpc_parse_metadata_array(grpc_metadata_array *metadata_array,
- zval *array);
+zval *grpc_parse_metadata_array(grpc_metadata_array *metadata_array TSRMLS_DC);
-#endif /* PHP_MAJOR_VERSION */
+/* Creates a Call object that wraps the given grpc_call struct */
+zval *grpc_php_wrap_call(grpc_call *wrapped, bool owned TSRMLS_DC);
/* Initializes the Call PHP class */
void grpc_init_call(TSRMLS_D);
diff --git a/src/php/ext/grpc/call_credentials.c b/src/php/ext/grpc/call_credentials.c
index 24ee0a465e..6921a5df17 100644
--- a/src/php/ext/grpc/call_credentials.c
+++ b/src/php/ext/grpc/call_credentials.c
@@ -52,88 +52,39 @@
#include <grpc/grpc_security.h>
zend_class_entry *grpc_ce_call_credentials;
-
-#if PHP_MAJOR_VERSION < 7
+#if PHP_MAJOR_VERSION >= 7
+static zend_object_handlers call_credentials_ce_handlers;
+#endif
/* Frees and destroys an instance of wrapped_grpc_call_credentials */
-void free_wrapped_grpc_call_credentials(void *object TSRMLS_DC) {
- wrapped_grpc_call_credentials *creds =
- (wrapped_grpc_call_credentials *)object;
- if (creds->wrapped != NULL) {
- grpc_call_credentials_release(creds->wrapped);
+PHP_GRPC_FREE_WRAPPED_FUNC_START(wrapped_grpc_call_credentials)
+ if (p->wrapped != NULL) {
+ grpc_call_credentials_release(p->wrapped);
}
- zend_object_std_dtor(&creds->std TSRMLS_CC);
- efree(creds);
-}
+PHP_GRPC_FREE_WRAPPED_FUNC_END()
/* Initializes an instance of wrapped_grpc_call_credentials to be
* associated with an object of a class specified by class_type */
-zend_object_value create_wrapped_grpc_call_credentials(
+php_grpc_zend_object create_wrapped_grpc_call_credentials(
zend_class_entry *class_type TSRMLS_DC) {
- zend_object_value retval;
- wrapped_grpc_call_credentials *intern;
-
- intern = (wrapped_grpc_call_credentials *)emalloc(
- sizeof(wrapped_grpc_call_credentials));
- memset(intern, 0, sizeof(wrapped_grpc_call_credentials));
-
+ PHP_GRPC_ALLOC_CLASS_OBJECT(wrapped_grpc_call_credentials);
zend_object_std_init(&intern->std, class_type TSRMLS_CC);
object_properties_init(&intern->std, class_type);
- retval.handle = zend_objects_store_put(
- intern, (zend_objects_store_dtor_t)zend_objects_destroy_object,
- free_wrapped_grpc_call_credentials, NULL TSRMLS_CC);
- retval.handlers = zend_get_std_object_handlers();
- return retval;
+ PHP_GRPC_FREE_CLASS_OBJECT(wrapped_grpc_call_credentials,
+ call_credentials_ce_handlers);
}
-zval *grpc_php_wrap_call_credentials(grpc_call_credentials *wrapped TSRMLS_DC) {
+zval *grpc_php_wrap_call_credentials(grpc_call_credentials
+ *wrapped TSRMLS_DC) {
zval *credentials_object;
- MAKE_STD_ZVAL(credentials_object);
- object_init_ex(credentials_object, grpc_ce_call_credentials);
- wrapped_grpc_call_credentials *credentials =
- (wrapped_grpc_call_credentials *)zend_object_store_get_object(
- credentials_object TSRMLS_CC);
- credentials->wrapped = wrapped;
- return credentials_object;
-}
-
-#else
-
-static zend_object_handlers call_credentials_ce_handlers;
-
-/* Frees and destroys an instance of wrapped_grpc_call_credentials */
-static void free_wrapped_grpc_call_credentials(zend_object *object) {
- wrapped_grpc_call_credentials *creds =
- wrapped_grpc_call_creds_from_obj(object);
- if (creds->wrapped != NULL) {
- grpc_call_credentials_release(creds->wrapped);
- }
- zend_object_std_dtor(&creds->std);
-}
-
-/* Initializes an instance of wrapped_grpc_call_credentials to be
- * associated with an object of a class specified by class_type */
-zend_object *create_wrapped_grpc_call_credentials(zend_class_entry
- *class_type) {
- wrapped_grpc_call_credentials *intern;
- intern = ecalloc(1, sizeof(wrapped_grpc_call_credentials) +
- zend_object_properties_size(class_type));
- zend_object_std_init(&intern->std, class_type);
- object_properties_init(&intern->std, class_type);
- intern->std.handlers = &call_credentials_ce_handlers;
- return &intern->std;
-}
-
-void grpc_php_wrap_call_credentials(grpc_call_credentials *wrapped,
- zval *credentials_object) {
+ PHP_GRPC_MAKE_STD_ZVAL(credentials_object);
object_init_ex(credentials_object, grpc_ce_call_credentials);
wrapped_grpc_call_credentials *credentials =
Z_WRAPPED_GRPC_CALL_CREDS_P(credentials_object);
credentials->wrapped = wrapped;
+ return credentials_object;
}
-#endif
-
/**
* Create composite credentials from two existing credentials.
* @param CallCredentials cred1 The first credential
@@ -160,13 +111,10 @@ PHP_METHOD(CallCredentials, createComposite) {
grpc_call_credentials *creds =
grpc_composite_call_credentials_create(cred1->wrapped, cred2->wrapped,
NULL);
-#if PHP_MAJOR_VERSION < 7
- zval *creds_object = grpc_php_wrap_call_credentials(creds TSRMLS_CC);
+ zval *creds_object;
+ PHP_GRPC_MAKE_STD_ZVAL(creds_object);
+ creds_object = grpc_php_wrap_call_credentials(creds TSRMLS_CC);
RETURN_DESTROY_ZVAL(creds_object);
-#else
- grpc_php_wrap_call_credentials(creds, return_value);
- RETURN_DESTROY_ZVAL(return_value);
-#endif
}
/**
@@ -207,13 +155,10 @@ PHP_METHOD(CallCredentials, createFromPlugin) {
grpc_call_credentials *creds =
grpc_metadata_credentials_create_from_plugin(plugin, NULL);
-#if PHP_MAJOR_VERSION < 7
- zval *creds_object = grpc_php_wrap_call_credentials(creds TSRMLS_CC);
+ zval *creds_object;
+ PHP_GRPC_MAKE_STD_ZVAL(creds_object);
+ creds_object = grpc_php_wrap_call_credentials(creds TSRMLS_CC);
RETURN_DESTROY_ZVAL(creds_object);
-#else
- grpc_php_wrap_call_credentials(creds, return_value);
- RETURN_DESTROY_ZVAL(return_value);
-#endif
}
/* Callback function for plugin creds API */
@@ -226,37 +171,28 @@ void plugin_get_metadata(void *ptr, grpc_auth_metadata_context context,
/* prepare to call the user callback function with info from the
* grpc_auth_metadata_context */
-#if PHP_MAJOR_VERSION < 7
- zval **params[1];
zval *arg;
- zval *retval;
- MAKE_STD_ZVAL(arg);
+ PHP_GRPC_MAKE_STD_ZVAL(arg);
object_init(arg);
- add_property_string(arg, "service_url", context.service_url, true);
- add_property_string(arg, "method_name", context.method_name, true);
+ php_grpc_add_property_string(arg, "service_url", context.service_url, true);
+ php_grpc_add_property_string(arg, "method_name", context.method_name, true);
+ zval *retval;
+ PHP_GRPC_MAKE_STD_ZVAL(retval);
+#if PHP_MAJOR_VERSION < 7
+ zval **params[1];
params[0] = &arg;
- state->fci->param_count = 1;
state->fci->params = params;
state->fci->retval_ptr_ptr = &retval;
#else
- zval arg;
- zval retval;
- object_init(&arg);
- add_property_string(&arg, "service_url", context.service_url);
- add_property_string(&arg, "method_name", context.method_name);
- state->fci->param_count = 1;
- state->fci->params = &arg;
- state->fci->retval = &retval;
+ state->fci->params = arg;
+ state->fci->retval = retval;
#endif
+ state->fci->param_count = 1;
/* call the user callback function */
zend_call_function(state->fci, state->fci_cache TSRMLS_CC);
-#if PHP_MAJOR_VERSION < 7
if (Z_TYPE_P(retval) != IS_ARRAY) {
-#else
- if (Z_TYPE_P(&retval) != IS_ARRAY) {
-#endif
zend_throw_exception(spl_ce_InvalidArgumentException,
"plugin callback must return metadata array",
1 TSRMLS_CC);
@@ -264,11 +200,7 @@ void plugin_get_metadata(void *ptr, grpc_auth_metadata_context context,
}
grpc_metadata_array metadata;
-#if PHP_MAJOR_VERSION < 7
if (!create_metadata_array(retval, &metadata)) {
-#else
- if (!create_metadata_array(&retval, &metadata)) {
-#endif
zend_throw_exception(spl_ce_InvalidArgumentException,
"invalid metadata", 1 TSRMLS_CC);
grpc_metadata_array_destroy(&metadata);
@@ -303,13 +235,6 @@ void grpc_init_call_credentials(TSRMLS_D) {
INIT_CLASS_ENTRY(ce, "Grpc\\CallCredentials", call_credentials_methods);
ce.create_object = create_wrapped_grpc_call_credentials;
grpc_ce_call_credentials = zend_register_internal_class(&ce TSRMLS_CC);
-#if PHP_MAJOR_VERSION >= 7
- memcpy(&call_credentials_ce_handlers,
- zend_get_std_object_handlers(),
- sizeof(zend_object_handlers));
- call_credentials_ce_handlers.offset =
- XtOffsetOf(wrapped_grpc_call_credentials, std);
- call_credentials_ce_handlers.free_obj =
- free_wrapped_grpc_call_credentials;
-#endif
+ PHP_GRPC_INIT_HANDLER(wrapped_grpc_call_credentials,
+ call_credentials_ce_handlers);
}
diff --git a/src/php/ext/grpc/call_credentials.h b/src/php/ext/grpc/call_credentials.h
index e9688288de..c1d85c0fb2 100755
--- a/src/php/ext/grpc/call_credentials.h
+++ b/src/php/ext/grpc/call_credentials.h
@@ -49,36 +49,27 @@
/* Class entry for the CallCredentials PHP class */
extern zend_class_entry *grpc_ce_call_credentials;
-#if PHP_MAJOR_VERSION < 7
-
/* Wrapper struct for grpc_call_credentials that can be associated
* with a PHP object */
-typedef struct wrapped_grpc_call_credentials {
- zend_object std;
+PHP_GRPC_WRAP_OBJECT_START(wrapped_grpc_call_credentials)
grpc_call_credentials *wrapped;
-} wrapped_grpc_call_credentials;
+PHP_GRPC_WRAP_OBJECT_END(wrapped_grpc_call_credentials)
+
+#if PHP_MAJOR_VERSION < 7
#define Z_WRAPPED_GRPC_CALL_CREDS_P(zv) \
(wrapped_grpc_call_credentials *)zend_object_store_get_object(zv TSRMLS_CC)
-#else
-/* Wrapper struct for grpc_call_credentials that can be associated
- * with a PHP object */
-typedef struct wrapped_grpc_call_credentials {
- grpc_call_credentials *wrapped;
- zend_object std;
-} wrapped_grpc_call_credentials;
+#else
static inline wrapped_grpc_call_credentials
-*wrapped_grpc_call_creds_from_obj(zend_object *obj) {
- return
- (wrapped_grpc_call_credentials*)((char*)(obj) -
- XtOffsetOf(wrapped_grpc_call_credentials,
- std));
+*wrapped_grpc_call_credentials_from_obj(zend_object *obj) {
+ return (wrapped_grpc_call_credentials*)(
+ (char*)(obj) - XtOffsetOf(wrapped_grpc_call_credentials, std));
}
-#define Z_WRAPPED_GRPC_CALL_CREDS_P(zv) \
- wrapped_grpc_call_creds_from_obj(Z_OBJ_P((zv)))
+#define Z_WRAPPED_GRPC_CALL_CREDS_P(zv) \
+ wrapped_grpc_call_credentials_from_obj(Z_OBJ_P((zv)))
#endif /* PHP_MAJOR_VERSION */
diff --git a/src/php/ext/grpc/channel.c b/src/php/ext/grpc/channel.c
index 085a51b560..b5a2c9f6ba 100644
--- a/src/php/ext/grpc/channel.c
+++ b/src/php/ext/grpc/channel.c
@@ -56,123 +56,52 @@
#include "timeval.h"
zend_class_entry *grpc_ce_channel;
-
-#if PHP_MAJOR_VERSION < 7
+#if PHP_MAJOR_VERSION >= 7
+static zend_object_handlers channel_ce_handlers;
+#endif
/* Frees and destroys an instance of wrapped_grpc_channel */
-void free_wrapped_grpc_channel(void *object TSRMLS_DC) {
- wrapped_grpc_channel *channel = (wrapped_grpc_channel *)object;
- if (channel->wrapped != NULL) {
- grpc_channel_destroy(channel->wrapped);
+PHP_GRPC_FREE_WRAPPED_FUNC_START(wrapped_grpc_channel)
+ if (p->wrapped != NULL) {
+ grpc_channel_destroy(p->wrapped);
}
- zend_object_std_dtor(&channel->std TSRMLS_CC);
- efree(channel);
-}
+PHP_GRPC_FREE_WRAPPED_FUNC_END()
/* Initializes an instance of wrapped_grpc_channel to be associated with an
* object of a class specified by class_type */
-zend_object_value create_wrapped_grpc_channel(zend_class_entry *class_type
- TSRMLS_DC) {
- zend_object_value retval;
- wrapped_grpc_channel *intern;
- intern = (wrapped_grpc_channel *)emalloc(sizeof(wrapped_grpc_channel));
- memset(intern, 0, sizeof(wrapped_grpc_channel));
+php_grpc_zend_object create_wrapped_grpc_channel(zend_class_entry *class_type
+ TSRMLS_DC) {
+ PHP_GRPC_ALLOC_CLASS_OBJECT(wrapped_grpc_channel);
zend_object_std_init(&intern->std, class_type TSRMLS_CC);
object_properties_init(&intern->std, class_type);
- retval.handle = zend_objects_store_put(
- intern, (zend_objects_store_dtor_t)zend_objects_destroy_object,
- free_wrapped_grpc_channel, NULL TSRMLS_CC);
- retval.handlers = zend_get_std_object_handlers();
- return retval;
+ PHP_GRPC_FREE_CLASS_OBJECT(wrapped_grpc_channel, channel_ce_handlers);
}
void php_grpc_read_args_array(zval *args_array,
grpc_channel_args *args TSRMLS_DC) {
HashTable *array_hash;
- HashPosition array_pointer;
int args_index;
- zval **data;
- char *key;
- uint key_len;
- ulong index;
array_hash = Z_ARRVAL_P(args_array);
- args->num_args = zend_hash_num_elements(array_hash);
- args->args = ecalloc(args->num_args, sizeof(grpc_arg));
- args_index = 0;
- for (zend_hash_internal_pointer_reset_ex(array_hash, &array_pointer);
- zend_hash_get_current_data_ex(array_hash, (void **)&data,
- &array_pointer) == SUCCESS;
- zend_hash_move_forward_ex(array_hash, &array_pointer)) {
- if (zend_hash_get_current_key_ex(array_hash, &key, &key_len, &index, 0,
- &array_pointer) != HASH_KEY_IS_STRING) {
- zend_throw_exception(spl_ce_InvalidArgumentException,
- "args keys must be strings", 1 TSRMLS_CC);
- return;
- }
- args->args[args_index].key = key;
- switch (Z_TYPE_P(*data)) {
- case IS_LONG:
- args->args[args_index].value.integer = (int)Z_LVAL_P(*data);
- args->args[args_index].type = GRPC_ARG_INTEGER;
- break;
- case IS_STRING:
- args->args[args_index].value.string = Z_STRVAL_P(*data);
- args->args[args_index].type = GRPC_ARG_STRING;
- break;
- default:
- zend_throw_exception(spl_ce_InvalidArgumentException,
- "args values must be int or string", 1 TSRMLS_CC);
- return;
- }
- args_index++;
- }
-}
-
-#else
-
-static zend_object_handlers channel_ce_handlers;
-
-/* Frees and destroys an instance of wrapped_grpc_channel */
-static void free_wrapped_grpc_channel(zend_object *object) {
- wrapped_grpc_channel *channel = wrapped_grpc_channel_from_obj(object);
- if (channel->wrapped != NULL) {
- grpc_channel_destroy(channel->wrapped);
- }
- zend_object_std_dtor(&channel->std);
-}
-
-/* Initializes an instance of wrapped_grpc_channel to be associated with an
- * object of a class specified by class_type */
-zend_object *create_wrapped_grpc_channel(zend_class_entry *class_type) {
- wrapped_grpc_channel *intern;
- intern = ecalloc(1, sizeof(wrapped_grpc_channel) +
- zend_object_properties_size(class_type));
- zend_object_std_init(&intern->std, class_type);
- object_properties_init(&intern->std, class_type);
- intern->std.handlers = &channel_ce_handlers;
- return &intern->std;
-}
-
-void php_grpc_read_args_array(zval *args_array, grpc_channel_args *args) {
- HashTable *array_hash;
- int args_index;
- zval *data;
- zend_string *key;
- array_hash = HASH_OF(args_array);
if (!array_hash) {
zend_throw_exception(spl_ce_InvalidArgumentException,
- "array_hash is NULL", 1);
+ "array_hash is NULL", 1 TSRMLS_CC);
return;
}
args->num_args = zend_hash_num_elements(array_hash);
args->args = ecalloc(args->num_args, sizeof(grpc_arg));
args_index = 0;
- ZEND_HASH_FOREACH_STR_KEY_VAL(array_hash, key, data) {
- if (key == NULL) {
+
+ char *key = NULL;
+ zval *data;
+ int key_type;
+
+ PHP_GRPC_HASH_FOREACH_STR_KEY_VAL_START(array_hash, key, key_type, data)
+ if (key_type != HASH_KEY_IS_STRING) {
zend_throw_exception(spl_ce_InvalidArgumentException,
- "args keys must be strings", 1);
+ "args keys must be strings", 1 TSRMLS_CC);
+ return;
}
- args->args[args_index].key = ZSTR_VAL(key);
+ args->args[args_index].key = key;
switch (Z_TYPE_P(data)) {
case IS_LONG:
args->args[args_index].value.integer = (int)Z_LVAL_P(data);
@@ -184,15 +113,13 @@ void php_grpc_read_args_array(zval *args_array, grpc_channel_args *args) {
break;
default:
zend_throw_exception(spl_ce_InvalidArgumentException,
- "args values must be int or string", 1);
+ "args values must be int or string", 1 TSRMLS_CC);
return;
}
args_index++;
- } ZEND_HASH_FOREACH_END();
+ PHP_GRPC_HASH_FOREACH_END()
}
-#endif
-
/**
* Construct an instance of the Channel class. If the $args array contains a
* "credentials" key mapping to a ChannelCredentials object, a secure channel
@@ -202,11 +129,7 @@ void php_grpc_read_args_array(zval *args_array, grpc_channel_args *args) {
*/
PHP_METHOD(Channel, __construct) {
wrapped_grpc_channel *channel = Z_WRAPPED_GRPC_CHANNEL_P(getThis());
-#if PHP_MAJOR_VERSION < 7
- zval **creds_obj = NULL;
-#else
zval *creds_obj = NULL;
-#endif
char *target;
php_grpc_int target_length;
zval *args_array = NULL;
@@ -221,43 +144,23 @@ PHP_METHOD(Channel, __construct) {
"Channel expects a string and an array", 1 TSRMLS_CC);
return;
}
-#if PHP_MAJOR_VERSION < 7
array_hash = Z_ARRVAL_P(args_array);
- if (zend_hash_find(array_hash, "credentials", sizeof("credentials"),
+ if (php_grpc_zend_hash_find(array_hash, "credentials", sizeof("credentials"),
(void **)&creds_obj) == SUCCESS) {
- if (Z_TYPE_P(*creds_obj) == IS_NULL) {
- creds = NULL;
- zend_hash_del(array_hash, "credentials", 12);
- } else if (zend_get_class_entry(*creds_obj TSRMLS_CC) !=
- grpc_ce_channel_credentials) {
- zend_throw_exception(spl_ce_InvalidArgumentException,
- "credentials must be a ChannelCredentials object",
- 1 TSRMLS_CC);
- return;
- } else {
- creds = (wrapped_grpc_channel_credentials *)zend_object_store_get_object(
- *creds_obj TSRMLS_CC);
- zend_hash_del(array_hash, "credentials", 12);
- }
- }
-#else
- array_hash = HASH_OF(args_array);
- if ((creds_obj = zend_hash_str_find(array_hash, "credentials",
- sizeof("credentials") - 1)) != NULL) {
if (Z_TYPE_P(creds_obj) == IS_NULL) {
creds = NULL;
- zend_hash_str_del(array_hash, "credentials", sizeof("credentials") - 1);
- } else if (Z_OBJ_P(creds_obj)->ce != grpc_ce_channel_credentials) {
+ php_grpc_zend_hash_del(array_hash, "credentials", sizeof("credentials"));
+ } else if (PHP_GRPC_GET_CLASS_ENTRY(creds_obj) !=
+ grpc_ce_channel_credentials) {
zend_throw_exception(spl_ce_InvalidArgumentException,
"credentials must be a ChannelCredentials object",
- 1);
+ 1 TSRMLS_CC);
return;
} else {
creds = Z_WRAPPED_GRPC_CHANNEL_CREDS_P(creds_obj);
- zend_hash_str_del(array_hash, "credentials", sizeof("credentials") - 1);
+ php_grpc_zend_hash_del(array_hash, "credentials", sizeof("credentials"));
}
}
-#endif
php_grpc_read_args_array(args_array, &args TSRMLS_CC);
if (creds == NULL) {
channel->wrapped = grpc_insecure_channel_create(target, &args, NULL);
@@ -313,8 +216,7 @@ PHP_METHOD(Channel, watchConnectivityState) {
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "lO",
&last_state, &deadline_obj, grpc_ce_timeval) == FAILURE) {
zend_throw_exception(spl_ce_InvalidArgumentException,
- "watchConnectivityState expects 1 long 1 timeval",
- 1 TSRMLS_CC);
+ "watchConnectivityState expects 1 long 1 timeval", 1 TSRMLS_CC);
return;
}
@@ -354,11 +256,5 @@ void grpc_init_channel(TSRMLS_D) {
INIT_CLASS_ENTRY(ce, "Grpc\\Channel", channel_methods);
ce.create_object = create_wrapped_grpc_channel;
grpc_ce_channel = zend_register_internal_class(&ce TSRMLS_CC);
-#if PHP_MAJOR_VERSION >= 7
- memcpy(&channel_ce_handlers, zend_get_std_object_handlers(),
- sizeof(zend_object_handlers));
- channel_ce_handlers.offset =
- XtOffsetOf(wrapped_grpc_channel, std);
- channel_ce_handlers.free_obj = free_wrapped_grpc_channel;
-#endif
+ PHP_GRPC_INIT_HANDLER(wrapped_grpc_channel, channel_ce_handlers);
}
diff --git a/src/php/ext/grpc/channel.h b/src/php/ext/grpc/channel.h
index 94c633408e..0b815657d3 100755
--- a/src/php/ext/grpc/channel.h
+++ b/src/php/ext/grpc/channel.h
@@ -48,32 +48,25 @@
/* Class entry for the PHP Channel class */
extern zend_class_entry *grpc_ce_channel;
-#if PHP_MAJOR_VERSION < 7
-
/* Wrapper struct for grpc_channel that can be associated with a PHP object */
-typedef struct wrapped_grpc_channel {
- zend_object std;
+PHP_GRPC_WRAP_OBJECT_START(wrapped_grpc_channel)
grpc_channel *wrapped;
-} wrapped_grpc_channel;
+PHP_GRPC_WRAP_OBJECT_END(wrapped_grpc_channel)
+
+#if PHP_MAJOR_VERSION < 7
#define Z_WRAPPED_GRPC_CHANNEL_P(zv) \
(wrapped_grpc_channel *)zend_object_store_get_object(zv TSRMLS_CC)
#else
-/* Wrapper struct for grpc_channel that can be associated with a PHP object */
-typedef struct wrapped_grpc_channel {
- grpc_channel *wrapped;
- zend_object std;
-} wrapped_grpc_channel;
-
static inline wrapped_grpc_channel
*wrapped_grpc_channel_from_obj(zend_object *obj) {
return (wrapped_grpc_channel*)((char*)(obj) -
XtOffsetOf(wrapped_grpc_channel, std));
}
-#define Z_WRAPPED_GRPC_CHANNEL_P(zv) \
+#define Z_WRAPPED_GRPC_CHANNEL_P(zv) \
wrapped_grpc_channel_from_obj(Z_OBJ_P((zv)))
#endif /* PHP_MAJOR_VERSION */
diff --git a/src/php/ext/grpc/channel_credentials.c b/src/php/ext/grpc/channel_credentials.c
index e255b3004b..0b356aa25f 100644
--- a/src/php/ext/grpc/channel_credentials.c
+++ b/src/php/ext/grpc/channel_credentials.c
@@ -52,6 +52,9 @@
#include <grpc/grpc_security.h>
zend_class_entry *grpc_ce_channel_credentials;
+#if PHP_MAJOR_VERSION >= 7
+static zend_object_handlers channel_credentials_ce_handlers;
+#endif
static char *default_pem_root_certs = NULL;
static grpc_ssl_roots_override_result get_ssl_roots_override(
@@ -63,88 +66,35 @@ static grpc_ssl_roots_override_result get_ssl_roots_override(
return GRPC_SSL_ROOTS_OVERRIDE_OK;
}
-#if PHP_MAJOR_VERSION < 7
-
/* Frees and destroys an instance of wrapped_grpc_channel_credentials */
-void free_wrapped_grpc_channel_credentials(void *object TSRMLS_DC) {
- wrapped_grpc_channel_credentials *creds =
- (wrapped_grpc_channel_credentials *)object;
- if (creds->wrapped != NULL) {
- grpc_channel_credentials_release(creds->wrapped);
+PHP_GRPC_FREE_WRAPPED_FUNC_START(wrapped_grpc_channel_credentials)
+ if (p->wrapped != NULL) {
+ grpc_channel_credentials_release(p->wrapped);
}
- zend_object_std_dtor(&creds->std TSRMLS_CC);
- efree(creds);
-}
+PHP_GRPC_FREE_WRAPPED_FUNC_END()
/* Initializes an instance of wrapped_grpc_channel_credentials to be
* associated with an object of a class specified by class_type */
-zend_object_value create_wrapped_grpc_channel_credentials(
+php_grpc_zend_object create_wrapped_grpc_channel_credentials(
zend_class_entry *class_type TSRMLS_DC) {
- zend_object_value retval;
- wrapped_grpc_channel_credentials *intern;
-
- intern = (wrapped_grpc_channel_credentials *)emalloc(
- sizeof(wrapped_grpc_channel_credentials));
- memset(intern, 0, sizeof(wrapped_grpc_channel_credentials));
-
+ PHP_GRPC_ALLOC_CLASS_OBJECT(wrapped_grpc_channel_credentials);
zend_object_std_init(&intern->std, class_type TSRMLS_CC);
object_properties_init(&intern->std, class_type);
- retval.handle = zend_objects_store_put(
- intern, (zend_objects_store_dtor_t)zend_objects_destroy_object,
- free_wrapped_grpc_channel_credentials, NULL TSRMLS_CC);
- retval.handlers = zend_get_std_object_handlers();
- return retval;
+ PHP_GRPC_FREE_CLASS_OBJECT(wrapped_grpc_channel_credentials,
+ channel_credentials_ce_handlers);
}
zval *grpc_php_wrap_channel_credentials(grpc_channel_credentials
*wrapped TSRMLS_DC) {
zval *credentials_object;
- MAKE_STD_ZVAL(credentials_object);
- object_init_ex(credentials_object, grpc_ce_channel_credentials);
- wrapped_grpc_channel_credentials *credentials =
- (wrapped_grpc_channel_credentials *)zend_object_store_get_object(
- credentials_object TSRMLS_CC);
- credentials->wrapped = wrapped;
- return credentials_object;
-}
-
-#else
-
-static zend_object_handlers channel_credentials_ce_handlers;
-
-/* Frees and destroys an instance of wrapped_grpc_channel_credentials */
-static void free_wrapped_grpc_channel_credentials(zend_object *object) {
- wrapped_grpc_channel_credentials *creds =
- wrapped_grpc_channel_creds_from_obj(object);
- if (creds->wrapped != NULL) {
- grpc_channel_credentials_release(creds->wrapped);
- }
- zend_object_std_dtor(&creds->std);
-}
-
-/* Initializes an instance of wrapped_grpc_channel_credentials to be
- * associated with an object of a class specified by class_type */
-zend_object *create_wrapped_grpc_channel_credentials(zend_class_entry
- *class_type) {
- wrapped_grpc_channel_credentials *intern;
- intern = ecalloc(1, sizeof(wrapped_grpc_channel_credentials) +
- zend_object_properties_size(class_type));
- zend_object_std_init(&intern->std, class_type);
- object_properties_init(&intern->std, class_type);
- intern->std.handlers = &channel_credentials_ce_handlers;
- return &intern->std;
-}
-
-void grpc_php_wrap_channel_credentials(grpc_channel_credentials *wrapped,
- zval *credentials_object) {
+ PHP_GRPC_MAKE_STD_ZVAL(credentials_object);
object_init_ex(credentials_object, grpc_ce_channel_credentials);
wrapped_grpc_channel_credentials *credentials =
Z_WRAPPED_GRPC_CHANNEL_CREDS_P(credentials_object);
credentials->wrapped = wrapped;
+ return credentials_object;
}
-#endif
-
/**
* Set default roots pem.
* @param string pem_roots PEM encoding of the server root certificates
@@ -171,13 +121,10 @@ PHP_METHOD(ChannelCredentials, setDefaultRootsPem) {
*/
PHP_METHOD(ChannelCredentials, createDefault) {
grpc_channel_credentials *creds = grpc_google_default_credentials_create();
-#if PHP_MAJOR_VERSION < 7
- zval *creds_object = grpc_php_wrap_channel_credentials(creds TSRMLS_CC);
+ zval *creds_object;
+ PHP_GRPC_MAKE_STD_ZVAL(creds_object);
+ creds_object = grpc_php_wrap_channel_credentials(creds TSRMLS_CC);
RETURN_DESTROY_ZVAL(creds_object);
-#else
- grpc_php_wrap_channel_credentials(creds, return_value);
- RETURN_DESTROY_ZVAL(return_value);
-#endif
}
/**
@@ -213,13 +160,10 @@ PHP_METHOD(ChannelCredentials, createSsl) {
grpc_channel_credentials *creds = grpc_ssl_credentials_create(
pem_root_certs,
pem_key_cert_pair.private_key == NULL ? NULL : &pem_key_cert_pair, NULL);
-#if PHP_MAJOR_VERSION < 7
- zval *creds_object = grpc_php_wrap_channel_credentials(creds TSRMLS_CC);
+ zval *creds_object;
+ PHP_GRPC_MAKE_STD_ZVAL(creds_object);
+ creds_object = grpc_php_wrap_channel_credentials(creds TSRMLS_CC);
RETURN_DESTROY_ZVAL(creds_object);
-#else
- grpc_php_wrap_channel_credentials(creds, return_value);
- RETURN_DESTROY_ZVAL(return_value);
-#endif
}
/**
@@ -244,19 +188,13 @@ PHP_METHOD(ChannelCredentials, createComposite) {
Z_WRAPPED_GRPC_CHANNEL_CREDS_P(cred1_obj);
wrapped_grpc_call_credentials *cred2 =
Z_WRAPPED_GRPC_CALL_CREDS_P(cred2_obj);
-#if PHP_MAJOR_VERSION < 7
grpc_channel_credentials *creds =
grpc_composite_channel_credentials_create(cred1->wrapped, cred2->wrapped,
NULL);
- zval *creds_object = grpc_php_wrap_channel_credentials(creds TSRMLS_CC);
+ zval *creds_object;
+ PHP_GRPC_MAKE_STD_ZVAL(creds_object);
+ creds_object = grpc_php_wrap_channel_credentials(creds TSRMLS_CC);
RETURN_DESTROY_ZVAL(creds_object);
-#else
- grpc_channel_credentials *creds =
- grpc_composite_channel_credentials_create(cred1->wrapped,
- cred2->wrapped, NULL);
- grpc_php_wrap_channel_credentials(creds, return_value);
- RETURN_DESTROY_ZVAL(return_value);
-#endif
}
/**
@@ -288,13 +226,6 @@ void grpc_init_channel_credentials(TSRMLS_D) {
grpc_set_ssl_roots_override_callback(get_ssl_roots_override);
ce.create_object = create_wrapped_grpc_channel_credentials;
grpc_ce_channel_credentials = zend_register_internal_class(&ce TSRMLS_CC);
-#if PHP_MAJOR_VERSION >= 7
- memcpy(&channel_credentials_ce_handlers,
- zend_get_std_object_handlers(),
- sizeof(zend_object_handlers));
- channel_credentials_ce_handlers.offset =
- XtOffsetOf(wrapped_grpc_channel_credentials, std);
- channel_credentials_ce_handlers.free_obj =
- free_wrapped_grpc_channel_credentials;
-#endif
+ PHP_GRPC_INIT_HANDLER(wrapped_grpc_channel_credentials,
+ channel_credentials_ce_handlers);
}
diff --git a/src/php/ext/grpc/channel_credentials.h b/src/php/ext/grpc/channel_credentials.h
index 0f7759daa6..b043d91fa6 100755
--- a/src/php/ext/grpc/channel_credentials.h
+++ b/src/php/ext/grpc/channel_credentials.h
@@ -49,37 +49,27 @@
/* Class entry for the ChannelCredentials PHP class */
extern zend_class_entry *grpc_ce_channel_credentials;
-#if PHP_MAJOR_VERSION < 7
-
/* Wrapper struct for grpc_channel_credentials that can be associated
* with a PHP object */
-typedef struct wrapped_grpc_channel_credentials {
- zend_object std;
+PHP_GRPC_WRAP_OBJECT_START(wrapped_grpc_channel_credentials)
grpc_channel_credentials *wrapped;
-} wrapped_grpc_channel_credentials;
+PHP_GRPC_WRAP_OBJECT_END(wrapped_grpc_channel_credentials)
+
+#if PHP_MAJOR_VERSION < 7
#define Z_WRAPPED_GRPC_CHANNEL_CREDS_P(zv) \
(wrapped_grpc_channel_credentials *)zend_object_store_get_object(zv TSRMLS_CC)
#else
-/* Wrapper struct for grpc_channel_credentials that can be associated
- * with a PHP object */
-typedef struct wrapped_grpc_channel_credentials {
- grpc_channel_credentials *wrapped;
- zend_object std;
-} wrapped_grpc_channel_credentials;
-
static inline wrapped_grpc_channel_credentials
-*wrapped_grpc_channel_creds_from_obj(zend_object *obj) {
- return
- (wrapped_grpc_channel_credentials *)
- ((char*)(obj) -
- XtOffsetOf(wrapped_grpc_channel_credentials, std));
+*wrapped_grpc_channel_credentials_from_obj(zend_object *obj) {
+ return (wrapped_grpc_channel_credentials *)(
+ (char*)(obj) - XtOffsetOf(wrapped_grpc_channel_credentials, std));
}
-#define Z_WRAPPED_GRPC_CHANNEL_CREDS_P(zv) \
- wrapped_grpc_channel_creds_from_obj(Z_OBJ_P((zv)))
+#define Z_WRAPPED_GRPC_CHANNEL_CREDS_P(zv) \
+ wrapped_grpc_channel_credentials_from_obj(Z_OBJ_P((zv)))
#endif /* PHP_MAJOR_VERSION */
diff --git a/src/php/ext/grpc/package.xml b/src/php/ext/grpc/package.xml
deleted file mode 100644
index daf2ee5a53..0000000000
--- a/src/php/ext/grpc/package.xml
+++ /dev/null
@@ -1,156 +0,0 @@
-<?xml version="1.0" encoding="UTF-8"?>
-<package packagerversion="1.9.5" version="2.0" xmlns="http://pear.php.net/dtd/package-2.0" xmlns:tasks="http://pear.php.net/dtd/tasks-1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://pear.php.net/dtd/tasks-1.0 http://pear.php.net/dtd/tasks-1.0.xsd http://pear.php.net/dtd/package-2.0 http://pear.php.net/dtd/package-2.0.xsd">
- <name>grpc</name>
- <channel>pecl.php.net</channel>
- <summary>A high performance, open source, general RPC framework that puts mobile and HTTP/2 first.</summary>
- <description>Remote Procedure Calls (RPCs) provide a useful abstraction for building distributed applications and services. The libraries in this repository provide a concrete implementation of the gRPC protocol, layered over HTTP/2. These libraries enable communication between clients and servers using any combination of the supported languages.</description>
- <lead>
- <name>Stanley Cheung</name>
- <user>stanleycheung</user>
- <email>grpc-packages@google.com</email>
- <active>yes</active>
- </lead>
- <date>2016-01-13</date>
- <time>16:06:07</time>
- <version>
- <release>0.7.0</release>
- <api>0.7.0</api>
- </version>
- <stability>
- <release>beta</release>
- <api>beta</api>
- </stability>
- <license>BSD</license>
- <notes>
-- Breaking change to Credentials class (removed) #3765
-- Replaced by ChannelCredentials and CallCredentials class #3765
-- New plugin based metadata auth API #4394
-- Explicit ChannelCredentials::createInsecure() call
- </notes>
- <contents>
- <dir baseinstalldir="/" name="/">
- <file baseinstalldir="/" md5sum="f201d644fdbd8228ffd1d4a69cc44f1f" name="tests/grpc-basic.phpt" role="test" />
- <file baseinstalldir="/" md5sum="6f19828fb869b7b8a590cbb76b4f996d" name="byte_buffer.c" role="src" />
- <file baseinstalldir="/" md5sum="c8de0f819499c48adfc8d7f472c0196b" name="byte_buffer.h" role="src" />
- <file baseinstalldir="/" md5sum="ee7eb7757f9e6f0e36f8f616b6bd0af5" name="call.c" role="src" />
- <file baseinstalldir="/" md5sum="44c56bd9912d2538cbd6059e3e0452b6" name="call.h" role="src" />
- <file baseinstalldir="/" md5sum="ff90f6c03ed44b5f4170bf3259a6704e" name="call_credentials.c" role="src" />
- <file baseinstalldir="/" md5sum="3c3860e1d84f43cb6b2fbaa8d2ae1ab7" name="call_credentials.h" role="src" />
- <file baseinstalldir="/" md5sum="aee9b63f790522aec2c682055240cc61" name="channel.c" role="src" />
- <file baseinstalldir="/" md5sum="ed4b00c0cf3702b115d0cfa87450dc09" name="channel.h" role="src" />
- <file baseinstalldir="/" md5sum="1a51c76d0b7b7d3ab570ed7d60c2ea46" name="channel_credentials.c" role="src" />
- <file baseinstalldir="/" md5sum="a86250e03f610ce6c2c7595a84e08821" name="channel_credentials.h" role="src" />
- <file baseinstalldir="/" md5sum="55ab7a42f9dd9bfc7e28a61cfc5fca63" name="completion_queue.c" role="src" />
- <file baseinstalldir="/" md5sum="f10b5bb232d74a6878e829e2e76cdaa2" name="completion_queue.h" role="src" />
- <file baseinstalldir="/" md5sum="cafed254127007ff2271dad7d56a06c8" name="config.m4" role="src" />
- <file baseinstalldir="/" md5sum="38a1bc979d810c36ebc2a52d4b7b5319" name="CREDITS" role="doc" />
- <file baseinstalldir="/" md5sum="8847cf67b1b54c981d47ecbb0d139a0c" name="LICENSE" role="doc" />
- <file baseinstalldir="/" md5sum="3131a8af38fe5918e5409016b89d6cdb" name="php_grpc.c" role="src" />
- <file baseinstalldir="/" md5sum="673b07859d9f69232f8a754c56780686" name="php_grpc.h" role="src" />
- <file baseinstalldir="/" md5sum="7533a6d3ea02c78cad23a9651de0825d" name="README.md" role="doc" />
- <file baseinstalldir="/" md5sum="3e4e960454ebb2fc7b78a840493f5315" name="server.c" role="src" />
- <file baseinstalldir="/" md5sum="4b730f06d14cbbb0642bdbd194749595" name="server.h" role="src" />
- <file baseinstalldir="/" md5sum="34ea881f1fe960d190d0713422cf8916" name="server_credentials.c" role="src" />
- <file baseinstalldir="/" md5sum="9c4b4cc06356a8a39a16a085a9b85996" name="server_credentials.h" role="src" />
- <file baseinstalldir="/" md5sum="7646ec78cb133f66ba59e03c6f451e39" name="timeval.c" role="src" />
- <file baseinstalldir="/" md5sum="496e27a100b4d93ca3fb35c924c5e163" name="timeval.h" role="src" />
- </dir>
- </contents>
- <dependencies>
- <required>
- <php>
- <min>5.5.0</min>
- </php>
- <pearinstaller>
- <min>1.4.0</min>
- </pearinstaller>
- </required>
- </dependencies>
- <providesextension>grpc</providesextension>
- <extsrcrelease />
- <changelog>
- <release>
- <version>
- <release>0.5.0</release>
- <api>0.5.0</api>
- </version>
- <stability>
- <release>alpha</release>
- <api>alpha</api>
- </stability>
- <date>2015-06-16</date>
- <license>BSD</license>
- <notes>
-First alpha release
- </notes>
- </release>
- <release>
- <version>
- <release>0.5.1</release>
- <api>0.5.1</api>
- </version>
- <stability>
- <release>alpha</release>
- <api>alpha</api>
- </stability>
- <date>2015-07-09</date>
- <license>BSD</license>
- <notes>
-Update to wrap gRPC C Core version 0.10.0
- </notes>
- </release>
- <release>
- <version>
- <release>0.6.0</release>
- <api>0.6.0</api>
- </version>
- <stability>
- <release>beta</release>
- <api>beta</api>
- </stability>
- <date>2015-09-24</date>
- <license>BSD</license>
- <notes>
-- support per message compression disable
-- expose per-call host override option
-- expose connectivity API
-- expose channel target and call peer
-- add user-agent
-- update to wrap gRPC C core library beta version 0.11.0
- </notes>
- </release>
- <release>
- <version>
- <release>0.6.1</release>
- <api>0.6.0</api>
- </version>
- <stability>
- <release>beta</release>
- <api>beta</api>
- </stability>
- <date>2015-10-21</date>
- <license>BSD</license>
- <notes>
-- fixed undefined constant fatal error when run with apache/nginx #2275
- </notes>
- </release>
- <release>
- <version>
- <release>0.7.0</release>
- <api>0.7.0</api>
- </version>
- <stability>
- <release>beta</release>
- <api>beta</api>
- </stability>
- <date>2016-01-13</date>
- <license>BSD</license>
- <notes>
-- Breaking change to Credentials class (removed) #3765
-- Replaced by ChannelCredentials and CallCredentials class #3765
-- New plugin based metadata auth API #4394
-- Explicit ChannelCredentials::createInsecure() call
- </notes>
- </release>
- </changelog>
-</package>
diff --git a/src/php/ext/grpc/php7_wrapper.h b/src/php/ext/grpc/php7_wrapper.h
index e4d0d4c90b..fd8d35636f 100644
--- a/src/php/ext/grpc/php7_wrapper.h
+++ b/src/php/ext/grpc/php7_wrapper.h
@@ -39,13 +39,179 @@
#define php_grpc_int int
#define php_grpc_long long
+#define php_grpc_ulong ulong
+#define php_grpc_zend_object zend_object_value
+#define php_grpc_add_property_string(arg, name, context, b) \
+ add_property_string(arg, name, context, b)
+#define php_grpc_add_property_stringl(res, name, str, len, b) \
+ add_property_stringl(res, name, str, len, b)
+#define php_grpc_add_next_index_stringl(data, str, len, b) \
+ add_next_index_stringl(data, str, len, b)
+
#define PHP_GRPC_RETURN_STRING(val, dup) RETURN_STRING(val, dup)
+#define PHP_GRPC_MAKE_STD_ZVAL(pzv) MAKE_STD_ZVAL(pzv)
+#define PHP_GRPC_DELREF(zv) Z_DELREF_P(zv)
+
+#define PHP_GRPC_WRAP_OBJECT_START(name) \
+ typedef struct name { \
+ zend_object std;
+#define PHP_GRPC_WRAP_OBJECT_END(name) \
+ } name;
+
+#define PHP_GRPC_FREE_WRAPPED_FUNC_START(class_object) \
+ void free_##class_object(void *object TSRMLS_DC) { \
+ class_object *p = (class_object *)object;
+#define PHP_GRPC_FREE_WRAPPED_FUNC_END() \
+ zend_object_std_dtor(&p->std TSRMLS_CC); \
+ efree(p); \
+ }
+
+#define PHP_GRPC_ALLOC_CLASS_OBJECT(class_object) \
+ class_object *intern; \
+ zend_object_value retval; \
+ intern = (class_object *)emalloc(sizeof(class_object)); \
+ memset(intern, 0, sizeof(class_object));
+
+#define PHP_GRPC_FREE_CLASS_OBJECT(class_object, handler) \
+ retval.handle = zend_objects_store_put( \
+ intern, (zend_objects_store_dtor_t)zend_objects_destroy_object, \
+ free_##class_object, NULL TSRMLS_CC); \
+ retval.handlers = zend_get_std_object_handlers(); \
+ return retval;
+
+#define PHP_GRPC_HASH_FOREACH_VAL_START(ht, data) \
+ zval **tmp_data = NULL; \
+ for (zend_hash_internal_pointer_reset(ht); \
+ zend_hash_get_current_data(ht, (void**)&tmp_data) == SUCCESS; \
+ zend_hash_move_forward(ht)) { \
+ data = *tmp_data;
+
+#define PHP_GRPC_HASH_FOREACH_STR_KEY_VAL_START(ht, key, key_type, data) \
+ zval **tmp##key = NULL; \
+ ulong index##key; \
+ uint len##key; \
+ for (zend_hash_internal_pointer_reset(ht); \
+ zend_hash_get_current_data(ht, (void**)&tmp##key) == SUCCESS; \
+ zend_hash_move_forward(ht)) { \
+ key_type = zend_hash_get_current_key_ex(ht, &key, &len##key, &index##key,\
+ 0, NULL); \
+ data = *tmp##key;
+
+#define PHP_GRPC_HASH_FOREACH_LONG_KEY_VAL_START(ht, key, key_type, index,\
+ data) \
+ zval **tmp##key = NULL; \
+ uint len##key; \
+ for (zend_hash_internal_pointer_reset(ht); \
+ zend_hash_get_current_data(ht, (void**)&tmp##key) == SUCCESS; \
+ zend_hash_move_forward(ht)) { \
+ key_type = zend_hash_get_current_key_ex(ht, &key, &len##key, &index,\
+ 0, NULL); \
+ data = *tmp##key;
+
+#define PHP_GRPC_HASH_FOREACH_END() }
+
+static inline int php_grpc_zend_hash_find(HashTable *ht, char *key, int len,
+ void **value) {
+ zval **data = NULL;
+ if (zend_hash_find(ht, key, len, (void **)&data) == SUCCESS) {
+ *value = *data;
+ return SUCCESS;
+ } else {
+ *value = NULL;
+ return FAILURE;
+ }
+}
+
+#define php_grpc_zend_hash_del zend_hash_del
+
+#define PHP_GRPC_GET_CLASS_ENTRY(object) zend_get_class_entry(object TSRMLS_CC)
+
+#define PHP_GRPC_INIT_HANDLER(class_object, handler_name)
#else
#define php_grpc_int size_t
#define php_grpc_long zend_long
+#define php_grpc_ulong zend_ulong
+#define php_grpc_zend_object zend_object*
+#define php_grpc_add_property_string(arg, name, context, b) \
+ add_property_string(arg, name, context)
+#define php_grpc_add_property_stringl(res, name, str, len, b) \
+ add_property_stringl(res, name, str, len)
+#define php_grpc_add_next_index_stringl(data, str, len, b) \
+ add_next_index_stringl(data, str, len)
+
#define PHP_GRPC_RETURN_STRING(val, dup) RETURN_STRING(val)
+#define PHP_GRPC_MAKE_STD_ZVAL(pzv) \
+ zval _stack_zval_##pzv; \
+ pzv = &(_stack_zval_##pzv)
+#define PHP_GRPC_DELREF(zv)
+
+#define PHP_GRPC_WRAP_OBJECT_START(name) \
+ typedef struct name {
+#define PHP_GRPC_WRAP_OBJECT_END(name) \
+ zend_object std; \
+ } name;
+
+#define WRAPPED_OBJECT_FROM_OBJ(class_object, obj) \
+ class_object##_from_obj(obj);
+
+#define PHP_GRPC_FREE_WRAPPED_FUNC_START(class_object) \
+ static void free_##class_object(zend_object *object) { \
+ class_object *p = WRAPPED_OBJECT_FROM_OBJ(class_object, object)
+#define PHP_GRPC_FREE_WRAPPED_FUNC_END() \
+ zend_object_std_dtor(&p->std); \
+ }
+
+#define PHP_GRPC_ALLOC_CLASS_OBJECT(class_object) \
+ class_object *intern; \
+ intern = ecalloc(1, sizeof(class_object) + \
+ zend_object_properties_size(class_type));
+
+#define PHP_GRPC_FREE_CLASS_OBJECT(class_object, handler) \
+ intern->std.handlers = &handler; \
+ return &intern->std;
+
+#define PHP_GRPC_HASH_FOREACH_VAL_START(ht, data) \
+ ZEND_HASH_FOREACH_VAL(ht, data) {
+
+#define PHP_GRPC_HASH_FOREACH_STR_KEY_VAL_START(ht, key, key_type, data) \
+ zend_string *(zs_##key); \
+ ZEND_HASH_FOREACH_STR_KEY_VAL(ht, (zs_##key), data) { \
+ if ((zs_##key) == NULL) {key = NULL; key_type = HASH_KEY_IS_LONG;} \
+ else {key = (zs_##key)->val; key_type = HASH_KEY_IS_STRING;}
+
+#define PHP_GRPC_HASH_FOREACH_LONG_KEY_VAL_START(ht, key, key_type, index, \
+ data) \
+ zend_string *(zs_##key); \
+ ZEND_HASH_FOREACH_KEY_VAL(ht, index, zs_##key, data) { \
+ if ((zs_##key) == NULL) {key = NULL; key_type = HASH_KEY_IS_LONG;} \
+ else {key = (zs_##key)->val; key_type = HASH_KEY_IS_STRING;}
+
+#define PHP_GRPC_HASH_FOREACH_END() } ZEND_HASH_FOREACH_END();
+
+static inline int php_grpc_zend_hash_find(HashTable *ht, char *key, int len,
+ void **value) {
+ zval *value_tmp = zend_hash_str_find(ht, key, len -1);
+ if (value_tmp == NULL) {
+ return FAILURE;
+ } else {
+ *value = (void *)value_tmp;
+ return SUCCESS;
+ }
+}
+
+static inline int php_grpc_zend_hash_del(HashTable *ht, char *key, int len) {
+ return zend_hash_str_del(ht, key, len - 1);
+}
+
+#define PHP_GRPC_GET_CLASS_ENTRY(object) Z_OBJ_P(object)->ce
+
+#define PHP_GRPC_INIT_HANDLER(class_object, handler_name) \
+ memcpy(&handler_name, zend_get_std_object_handlers(), \
+ sizeof(zend_object_handlers)); \
+ handler_name.offset = XtOffsetOf(class_object, std); \
+ handler_name.free_obj = free_##class_object
#endif /* PHP_MAJOR_VERSION */
diff --git a/src/php/ext/grpc/server.c b/src/php/ext/grpc/server.c
index e67e62c34e..fc20c42b16 100644
--- a/src/php/ext/grpc/server.c
+++ b/src/php/ext/grpc/server.c
@@ -57,73 +57,31 @@
#include "timeval.h"
zend_class_entry *grpc_ce_server;
-
-#if PHP_MAJOR_VERSION < 7
-
-/* Frees and destroys an instance of wrapped_grpc_server */
-void free_wrapped_grpc_server(void *object TSRMLS_DC) {
- wrapped_grpc_server *server = (wrapped_grpc_server *)object;
- if (server->wrapped != NULL) {
- grpc_server_shutdown_and_notify(server->wrapped, completion_queue, NULL);
- grpc_server_cancel_all_calls(server->wrapped);
- grpc_completion_queue_pluck(completion_queue, NULL,
- gpr_inf_future(GPR_CLOCK_REALTIME), NULL);
- grpc_server_destroy(server->wrapped);
- }
- zend_object_std_dtor(&server->std TSRMLS_CC);
- efree(server);
-}
-
-/* Initializes an instance of wrapped_grpc_call to be associated with an object
- * of a class specified by class_type */
-zend_object_value create_wrapped_grpc_server(zend_class_entry *class_type
- TSRMLS_DC) {
- zend_object_value retval;
- wrapped_grpc_server *intern;
-
- intern = (wrapped_grpc_server *)emalloc(sizeof(wrapped_grpc_server));
- memset(intern, 0, sizeof(wrapped_grpc_server));
-
- zend_object_std_init(&intern->std, class_type TSRMLS_CC);
- object_properties_init(&intern->std, class_type);
- retval.handle = zend_objects_store_put(
- intern, (zend_objects_store_dtor_t)zend_objects_destroy_object,
- free_wrapped_grpc_server, NULL TSRMLS_CC);
- retval.handlers = zend_get_std_object_handlers();
- return retval;
-}
-
-#else
-
+#if PHP_MAJOR_VERSION >= 7
static zend_object_handlers server_ce_handlers;
+#endif
/* Frees and destroys an instance of wrapped_grpc_server */
-static void free_wrapped_grpc_server(zend_object *object) {
- wrapped_grpc_server *server = wrapped_grpc_server_from_obj(object);
- if (server->wrapped != NULL) {
- grpc_server_shutdown_and_notify(server->wrapped, completion_queue, NULL);
- grpc_server_cancel_all_calls(server->wrapped);
+PHP_GRPC_FREE_WRAPPED_FUNC_START(wrapped_grpc_server)
+ if (p->wrapped != NULL) {
+ grpc_server_shutdown_and_notify(p->wrapped, completion_queue, NULL);
+ grpc_server_cancel_all_calls(p->wrapped);
grpc_completion_queue_pluck(completion_queue, NULL,
gpr_inf_future(GPR_CLOCK_REALTIME), NULL);
- grpc_server_destroy(server->wrapped);
+ grpc_server_destroy(p->wrapped);
}
- zend_object_std_dtor(&server->std);
-}
+PHP_GRPC_FREE_WRAPPED_FUNC_END()
/* Initializes an instance of wrapped_grpc_call to be associated with an object
* of a class specified by class_type */
-zend_object *create_wrapped_grpc_server(zend_class_entry *class_type) {
- wrapped_grpc_server *intern;
- intern = ecalloc(1, sizeof(wrapped_grpc_server) +
- zend_object_properties_size(class_type));
- zend_object_std_init(&intern->std, class_type);
+php_grpc_zend_object create_wrapped_grpc_server(zend_class_entry *class_type
+ TSRMLS_DC) {
+ PHP_GRPC_ALLOC_CLASS_OBJECT(wrapped_grpc_server);
+ zend_object_std_init(&intern->std, class_type TSRMLS_CC);
object_properties_init(&intern->std, class_type);
- intern->std.handlers = &server_ce_handlers;
- return &intern->std;
+ PHP_GRPC_FREE_CLASS_OBJECT(wrapped_grpc_server, server_ce_handlers);
}
-#endif
-
/**
* Constructs a new instance of the Server class
* @param array $args The arguments to pass to the server (optional)
@@ -168,13 +126,9 @@ PHP_METHOD(Server, requestCall) {
grpc_event event;
wrapped_grpc_server *server = Z_WRAPPED_GRPC_SERVER_P(getThis());
-#if PHP_MAJOR_VERSION < 7
zval *result;
- MAKE_STD_ZVAL(result);
+ PHP_GRPC_MAKE_STD_ZVAL(result);
object_init(result);
-#else
- object_init(return_value);
-#endif
grpc_call_details_init(&details);
grpc_metadata_array_init(&metadata);
@@ -195,40 +149,32 @@ PHP_METHOD(Server, requestCall) {
1 TSRMLS_CC);
goto cleanup;
}
+ php_grpc_add_property_string(result, "method", details.method, true);
+ php_grpc_add_property_string(result, "host", details.host, true);
#if PHP_MAJOR_VERSION < 7
add_property_zval(result, "call", grpc_php_wrap_call(call, true TSRMLS_CC));
- add_property_string(result, "method", details.method, true);
- add_property_string(result, "host", details.host, true);
add_property_zval(result, "absolute_deadline",
grpc_php_wrap_timeval(details.deadline TSRMLS_CC));
add_property_zval(result, "metadata", grpc_parse_metadata_array(&metadata
TSRMLS_CC));
-
-cleanup:
- grpc_call_details_destroy(&details);
- grpc_metadata_array_destroy(&metadata);
- RETURN_DESTROY_ZVAL(result);
-
#else
-
zval zv_call;
zval zv_timeval;
zval zv_md;
- grpc_php_wrap_call(call, true, &zv_call);
- grpc_php_wrap_timeval(details.deadline, &zv_timeval);
- grpc_parse_metadata_array(&metadata, &zv_md);
-
- add_property_zval(return_value, "call", &zv_call);
- add_property_string(return_value, "method", details.method);
- add_property_string(return_value, "host", details.host);
- add_property_zval(return_value, "absolute_deadline", &zv_timeval);
- add_property_zval(return_value, "metadata", &zv_md);
+ //TODO(thinkerou): why use zval* to unit test error?
+ zv_call = *grpc_php_wrap_call(call, true);
+ zv_timeval = *grpc_php_wrap_timeval(details.deadline);
+ zv_md = *grpc_parse_metadata_array(&metadata);
+
+ add_property_zval(result, "call", &zv_call);
+ add_property_zval(result, "absolute_deadline", &zv_timeval);
+ add_property_zval(result, "metadata", &zv_md);
+#endif
cleanup:
grpc_call_details_destroy(&details);
grpc_metadata_array_destroy(&metadata);
- RETURN_DESTROY_ZVAL(return_value);
-#endif
+ RETURN_DESTROY_ZVAL(result);
}
/**
@@ -287,7 +233,7 @@ static zend_function_entry server_methods[] = {
PHP_ME(Server, addHttp2Port, NULL, ZEND_ACC_PUBLIC)
PHP_ME(Server, addSecureHttp2Port, NULL, ZEND_ACC_PUBLIC)
PHP_ME(Server, start, NULL, ZEND_ACC_PUBLIC)
- PHP_FE_END
+ PHP_FE_END
};
void grpc_init_server(TSRMLS_D) {
@@ -295,10 +241,5 @@ void grpc_init_server(TSRMLS_D) {
INIT_CLASS_ENTRY(ce, "Grpc\\Server", server_methods);
ce.create_object = create_wrapped_grpc_server;
grpc_ce_server = zend_register_internal_class(&ce TSRMLS_CC);
-#if PHP_MAJOR_VERSION >= 7
- memcpy(&server_ce_handlers, zend_get_std_object_handlers(),
- sizeof(zend_object_handlers));
- server_ce_handlers.offset = XtOffsetOf(wrapped_grpc_server, std);
- server_ce_handlers.free_obj = free_wrapped_grpc_server;
-#endif
+ PHP_GRPC_INIT_HANDLER(wrapped_grpc_server, server_ce_handlers);
}
diff --git a/src/php/ext/grpc/server.h b/src/php/ext/grpc/server.h
index 603b9a7102..a635bc11df 100755
--- a/src/php/ext/grpc/server.h
+++ b/src/php/ext/grpc/server.h
@@ -48,33 +48,25 @@
/* Class entry for the Server PHP class */
extern zend_class_entry *grpc_ce_server;
-#if PHP_MAJOR_VERSION < 7
-
/* Wrapper struct for grpc_server that can be associated with a PHP object */
-typedef struct wrapped_grpc_server {
- zend_object std;
+PHP_GRPC_WRAP_OBJECT_START(wrapped_grpc_server)
grpc_server *wrapped;
-} wrapped_grpc_server;
+PHP_GRPC_WRAP_OBJECT_END(wrapped_grpc_server)
+
+#if PHP_MAJOR_VERSION < 7
#define Z_WRAPPED_GRPC_SERVER_P(zv) \
(wrapped_grpc_server *)zend_object_store_get_object(zv TSRMLS_CC)
#else
-/* Wrapper struct for grpc_server that can be associated with a PHP object */
-typedef struct wrapped_grpc_server {
- grpc_server *wrapped;
- zend_object std;
-} wrapped_grpc_server;
-
static inline wrapped_grpc_server
*wrapped_grpc_server_from_obj(zend_object *obj) {
return (wrapped_grpc_server*)((char*)(obj) -
XtOffsetOf(wrapped_grpc_server, std));
}
-#define Z_WRAPPED_GRPC_SERVER_P(zv) \
- wrapped_grpc_server_from_obj(Z_OBJ_P((zv)))
+#define Z_WRAPPED_GRPC_SERVER_P(zv) wrapped_grpc_server_from_obj(Z_OBJ_P((zv)))
#endif /* PHP_MAJOR_VERSION */
diff --git a/src/php/ext/grpc/server_credentials.c b/src/php/ext/grpc/server_credentials.c
index e61fc5b4a1..b05896af4a 100644
--- a/src/php/ext/grpc/server_credentials.c
+++ b/src/php/ext/grpc/server_credentials.c
@@ -50,89 +50,39 @@
#include <grpc/grpc_security.h>
zend_class_entry *grpc_ce_server_credentials;
-
-#if PHP_MAJOR_VERSION < 7
+#if PHP_MAJOR_VERSION >= 7
+static zend_object_handlers server_credentials_ce_handlers;
+#endif
/* Frees and destroys an instace of wrapped_grpc_server_credentials */
-void free_wrapped_grpc_server_credentials(void *object TSRMLS_DC) {
- wrapped_grpc_server_credentials *creds =
- (wrapped_grpc_server_credentials *)object;
- if (creds->wrapped != NULL) {
- grpc_server_credentials_release(creds->wrapped);
+PHP_GRPC_FREE_WRAPPED_FUNC_START(wrapped_grpc_server_credentials)
+ if (p->wrapped != NULL) {
+ grpc_server_credentials_release(p->wrapped);
}
- zend_object_std_dtor(&creds->std TSRMLS_CC);
- efree(creds);
-}
+PHP_GRPC_FREE_WRAPPED_FUNC_END()
/* Initializes an instace of wrapped_grpc_server_credentials to be associated
* with an object of a class specified by class_type */
-zend_object_value create_wrapped_grpc_server_credentials(
+php_grpc_zend_object create_wrapped_grpc_server_credentials(
zend_class_entry *class_type TSRMLS_DC) {
- zend_object_value retval;
- wrapped_grpc_server_credentials *intern;
-
- intern = (wrapped_grpc_server_credentials *)emalloc(
- sizeof(wrapped_grpc_server_credentials));
- memset(intern, 0, sizeof(wrapped_grpc_server_credentials));
-
+ PHP_GRPC_ALLOC_CLASS_OBJECT(wrapped_grpc_server_credentials);
zend_object_std_init(&intern->std, class_type TSRMLS_CC);
object_properties_init(&intern->std, class_type);
- retval.handle = zend_objects_store_put(
- intern, (zend_objects_store_dtor_t)zend_objects_destroy_object,
- free_wrapped_grpc_server_credentials, NULL TSRMLS_CC);
- retval.handlers = zend_get_std_object_handlers();
- return retval;
+ PHP_GRPC_FREE_CLASS_OBJECT(wrapped_grpc_server_credentials,
+ server_credentials_ce_handlers);
}
zval *grpc_php_wrap_server_credentials(grpc_server_credentials
*wrapped TSRMLS_DC) {
zval *server_credentials_object;
- MAKE_STD_ZVAL(server_credentials_object);
- object_init_ex(server_credentials_object, grpc_ce_server_credentials);
- wrapped_grpc_server_credentials *server_credentials =
- (wrapped_grpc_server_credentials *)zend_object_store_get_object(
- server_credentials_object TSRMLS_CC);
- server_credentials->wrapped = wrapped;
- return server_credentials_object;
-}
-
-#else
-
-static zend_object_handlers server_credentials_ce_handlers;
-
-/* Frees and destroys an instace of wrapped_grpc_server_credentials */
-static void free_wrapped_grpc_server_credentials(zend_object *object) {
- wrapped_grpc_server_credentials *creds =
- wrapped_grpc_server_creds_from_obj(object);
- if (creds->wrapped != NULL) {
- grpc_server_credentials_release(creds->wrapped);
- }
- zend_object_std_dtor(&creds->std);
-}
-
-/* Initializes an instace of wrapped_grpc_server_credentials to be associated
- * with an object of a class specified by class_type */
-zend_object *create_wrapped_grpc_server_credentials(zend_class_entry
- *class_type) {
- wrapped_grpc_server_credentials *intern;
- intern = ecalloc(1, sizeof(wrapped_grpc_server_credentials) +
- zend_object_properties_size(class_type));
- zend_object_std_init(&intern->std, class_type);
- object_properties_init(&intern->std, class_type);
- intern->std.handlers = &server_credentials_ce_handlers;
- return &intern->std;
-}
-
-void grpc_php_wrap_server_credentials(grpc_server_credentials *wrapped,
- zval *server_credentials_object) {
+ PHP_GRPC_MAKE_STD_ZVAL(server_credentials_object);
object_init_ex(server_credentials_object, grpc_ce_server_credentials);
wrapped_grpc_server_credentials *server_credentials =
Z_WRAPPED_GRPC_SERVER_CREDS_P(server_credentials_object);
server_credentials->wrapped = wrapped;
+ return server_credentials_object;
}
-#endif
-
/**
* Create SSL credentials.
* @param string pem_root_certs PEM encoding of the server root certificates
@@ -163,19 +113,16 @@ PHP_METHOD(ServerCredentials, createSsl) {
grpc_server_credentials *creds = grpc_ssl_server_credentials_create_ex(
pem_root_certs, &pem_key_cert_pair, 1,
GRPC_SSL_DONT_REQUEST_CLIENT_CERTIFICATE, NULL);
-#if PHP_MAJOR_VERSION < 7
- zval *creds_object = grpc_php_wrap_server_credentials(creds TSRMLS_CC);
+ zval *creds_object;
+ PHP_GRPC_MAKE_STD_ZVAL(creds_object);
+ creds_object = grpc_php_wrap_server_credentials(creds TSRMLS_CC);
RETURN_DESTROY_ZVAL(creds_object);
-#else
- grpc_php_wrap_server_credentials(creds, return_value);
- RETURN_DESTROY_ZVAL(return_value);
-#endif
}
static zend_function_entry server_credentials_methods[] = {
PHP_ME(ServerCredentials, createSsl, NULL,
ZEND_ACC_PUBLIC | ZEND_ACC_STATIC)
- PHP_FE_END
+ PHP_FE_END
};
void grpc_init_server_credentials(TSRMLS_D) {
@@ -183,13 +130,6 @@ void grpc_init_server_credentials(TSRMLS_D) {
INIT_CLASS_ENTRY(ce, "Grpc\\ServerCredentials", server_credentials_methods);
ce.create_object = create_wrapped_grpc_server_credentials;
grpc_ce_server_credentials = zend_register_internal_class(&ce TSRMLS_CC);
-#if PHP_MAJOR_VERSION >= 7
- memcpy(&server_credentials_ce_handlers,
- zend_get_std_object_handlers(),
- sizeof(zend_object_handlers));
- server_credentials_ce_handlers.offset =
- XtOffsetOf(wrapped_grpc_server_credentials, std);
- server_credentials_ce_handlers.free_obj =
- free_wrapped_grpc_server_credentials;
-#endif
+ PHP_GRPC_INIT_HANDLER(wrapped_grpc_server_credentials,
+ server_credentials_ce_handlers);
}
diff --git a/src/php/ext/grpc/server_credentials.h b/src/php/ext/grpc/server_credentials.h
index 9df5950fa7..6781a614b1 100755
--- a/src/php/ext/grpc/server_credentials.h
+++ b/src/php/ext/grpc/server_credentials.h
@@ -49,34 +49,27 @@
/* Class entry for the Server_Credentials PHP class */
extern zend_class_entry *grpc_ce_server_credentials;
-#if PHP_MAJOR_VERSION < 7
-
/* Wrapper struct for grpc_server_credentials that can be associated with a PHP
* object */
-typedef struct wrapped_grpc_server_credentials {
- zend_object std;
+PHP_GRPC_WRAP_OBJECT_START(wrapped_grpc_server_credentials)
grpc_server_credentials *wrapped;
-} wrapped_grpc_server_credentials;
+PHP_GRPC_WRAP_OBJECT_END(wrapped_grpc_server_credentials)
+
+#if PHP_MAJOR_VERSION < 7
#define Z_WRAPPED_GRPC_SERVER_CREDS_P(zv) \
(wrapped_grpc_server_credentials *)zend_object_store_get_object(zv TSRMLS_CC)
#else
-typedef struct wrapped_grpc_server_credentials {
- grpc_server_credentials *wrapped;
- zend_object std;
-} wrapped_grpc_server_credentials;
-
static inline wrapped_grpc_server_credentials
-*wrapped_grpc_server_creds_from_obj(zend_object *obj) {
- return (wrapped_grpc_server_credentials*)
- ((char*)(obj) -
- XtOffsetOf(wrapped_grpc_server_credentials, std));
+*wrapped_grpc_server_credentials_from_obj(zend_object *obj) {
+ return (wrapped_grpc_server_credentials*)(
+ (char*)(obj) - XtOffsetOf(wrapped_grpc_server_credentials, std));
}
-#define Z_WRAPPED_GRPC_SERVER_CREDS_P(zv) \
- wrapped_grpc_server_creds_from_obj(Z_OBJ_P((zv)))
+#define Z_WRAPPED_GRPC_SERVER_CREDS_P(zv) \
+ wrapped_grpc_server_credentials_from_obj(Z_OBJ_P((zv)))
#endif /* PHP_MAJOR_VERSION */
diff --git a/src/php/ext/grpc/timeval.c b/src/php/ext/grpc/timeval.c
index a3ec62f13f..e145d96772 100644
--- a/src/php/ext/grpc/timeval.c
+++ b/src/php/ext/grpc/timeval.c
@@ -51,74 +51,33 @@
#include <grpc/support/time.h>
zend_class_entry *grpc_ce_timeval;
-
-#if PHP_MAJOR_VERSION < 7
+#if PHP_MAJOR_VERSION >= 7
+static zend_object_handlers timeval_ce_handlers;
+#endif
/* Frees and destroys an instance of wrapped_grpc_call */
-void free_wrapped_grpc_timeval(void *object TSRMLS_DC) {
- wrapped_grpc_timeval *timeval = (wrapped_grpc_timeval *)object;
- zend_object_std_dtor(&timeval->std TSRMLS_CC);
- efree(timeval);
-}
+PHP_GRPC_FREE_WRAPPED_FUNC_START(wrapped_grpc_timeval)
+PHP_GRPC_FREE_WRAPPED_FUNC_END()
/* Initializes an instance of wrapped_grpc_timeval to be associated with an
* object of a class specified by class_type */
-zend_object_value create_wrapped_grpc_timeval(zend_class_entry *class_type
- TSRMLS_DC) {
- zend_object_value retval;
- wrapped_grpc_timeval *intern;
- intern = (wrapped_grpc_timeval *)emalloc(sizeof(wrapped_grpc_timeval));
- memset(intern, 0, sizeof(wrapped_grpc_timeval));
+php_grpc_zend_object create_wrapped_grpc_timeval(zend_class_entry *class_type
+ TSRMLS_DC) {
+ PHP_GRPC_ALLOC_CLASS_OBJECT(wrapped_grpc_timeval);
zend_object_std_init(&intern->std, class_type TSRMLS_CC);
object_properties_init(&intern->std, class_type);
- retval.handle = zend_objects_store_put(
- intern, (zend_objects_store_dtor_t)zend_objects_destroy_object,
- free_wrapped_grpc_timeval, NULL TSRMLS_CC);
- retval.handlers = zend_get_std_object_handlers();
- return retval;
+ PHP_GRPC_FREE_CLASS_OBJECT(wrapped_grpc_timeval, timeval_ce_handlers);
}
zval *grpc_php_wrap_timeval(gpr_timespec wrapped TSRMLS_DC) {
zval *timeval_object;
- MAKE_STD_ZVAL(timeval_object);
- object_init_ex(timeval_object, grpc_ce_timeval);
- wrapped_grpc_timeval *timeval =
- (wrapped_grpc_timeval *)zend_object_store_get_object(
- timeval_object TSRMLS_CC);
- memcpy(&timeval->wrapped, &wrapped, sizeof(gpr_timespec));
- return timeval_object;
-}
-
-#else
-
-static zend_object_handlers timeval_ce_handlers;
-
-/* Frees and destroys an instance of wrapped_grpc_call */
-static void free_wrapped_grpc_timeval(zend_object *object) {
- wrapped_grpc_timeval *timeval = wrapped_grpc_timeval_from_obj(object);
- zend_object_std_dtor(&timeval->std);
-}
-
-/* Initializes an instance of wrapped_grpc_timeval to be associated with an
- * object of a class specified by class_type */
-zend_object *create_wrapped_grpc_timeval(zend_class_entry *class_type) {
- wrapped_grpc_timeval *intern;
- intern = ecalloc(1, sizeof(wrapped_grpc_timeval) +
- zend_object_properties_size(class_type));
- zend_object_std_init(&intern->std, class_type);
- object_properties_init(&intern->std, class_type);
- intern->std.handlers = &timeval_ce_handlers;
- return &intern->std;
-}
-
-void grpc_php_wrap_timeval(gpr_timespec wrapped, zval *timeval_object) {
+ PHP_GRPC_MAKE_STD_ZVAL(timeval_object);
object_init_ex(timeval_object, grpc_ce_timeval);
wrapped_grpc_timeval *timeval = Z_WRAPPED_GRPC_TIMEVAL_P(timeval_object);
memcpy(&timeval->wrapped, &wrapped, sizeof(gpr_timespec));
+ return timeval_object;
}
-#endif
-
/**
* Constructs a new instance of the Timeval class
* @param long $usec The number of microseconds in the interval
@@ -156,16 +115,12 @@ PHP_METHOD(Timeval, add) {
}
wrapped_grpc_timeval *self = Z_WRAPPED_GRPC_TIMEVAL_P(getThis());
wrapped_grpc_timeval *other = Z_WRAPPED_GRPC_TIMEVAL_P(other_obj);
-#if PHP_MAJOR_VERSION < 7
- zval *sum =
+ zval *sum;
+ PHP_GRPC_MAKE_STD_ZVAL(sum);
+ sum =
grpc_php_wrap_timeval(gpr_time_add(self->wrapped, other->wrapped)
TSRMLS_CC);
RETURN_DESTROY_ZVAL(sum);
-#else
- grpc_php_wrap_timeval(gpr_time_add(self->wrapped, other->wrapped),
- return_value);
- RETURN_DESTROY_ZVAL(return_value);
-#endif
}
/**
@@ -186,16 +141,12 @@ PHP_METHOD(Timeval, subtract) {
}
wrapped_grpc_timeval *self = Z_WRAPPED_GRPC_TIMEVAL_P(getThis());
wrapped_grpc_timeval *other = Z_WRAPPED_GRPC_TIMEVAL_P(other_obj);
-#if PHP_MAJOR_VERSION < 7
- zval *diff =
+ zval *diff;
+ PHP_GRPC_MAKE_STD_ZVAL(diff);
+ diff =
grpc_php_wrap_timeval(gpr_time_sub(self->wrapped, other->wrapped)
TSRMLS_CC);
RETURN_DESTROY_ZVAL(diff);
-#else
- grpc_php_wrap_timeval(gpr_time_sub(self->wrapped, other->wrapped),
- return_value);
- RETURN_DESTROY_ZVAL(return_value);
-#endif
}
/**
@@ -255,13 +206,10 @@ PHP_METHOD(Timeval, similar) {
* @return Timeval The current time
*/
PHP_METHOD(Timeval, now) {
-#if PHP_MAJOR_VERSION < 7
- zval *now = grpc_php_wrap_timeval(gpr_now(GPR_CLOCK_REALTIME) TSRMLS_CC);
+ zval *now;
+ PHP_GRPC_MAKE_STD_ZVAL(now);
+ now = grpc_php_wrap_timeval(gpr_now(GPR_CLOCK_REALTIME) TSRMLS_CC);
RETURN_DESTROY_ZVAL(now);
-#else
- grpc_php_wrap_timeval(gpr_now(GPR_CLOCK_REALTIME), return_value);
- RETURN_DESTROY_ZVAL(return_value);
-#endif
}
/**
@@ -269,18 +217,13 @@ PHP_METHOD(Timeval, now) {
* @return Timeval Zero length time interval
*/
PHP_METHOD(Timeval, zero) {
-#if PHP_MAJOR_VERSION < 7
- zval *grpc_php_timeval_zero =
+ zval *grpc_php_timeval_zero;
+ PHP_GRPC_MAKE_STD_ZVAL(grpc_php_timeval_zero);
+ grpc_php_timeval_zero =
grpc_php_wrap_timeval(gpr_time_0(GPR_CLOCK_REALTIME) TSRMLS_CC);
RETURN_ZVAL(grpc_php_timeval_zero,
false, /* Copy original before returning? */
true /* Destroy original before returning */);
-#else
- grpc_php_wrap_timeval(gpr_time_0(GPR_CLOCK_REALTIME), return_value);
- RETURN_ZVAL(return_value,
- false, /* Copy original before returning? */
- true /* Destroy original before returning */);
-#endif
}
/**
@@ -288,14 +231,11 @@ PHP_METHOD(Timeval, zero) {
* @return Timeval Infinite future time value
*/
PHP_METHOD(Timeval, infFuture) {
-#if PHP_MAJOR_VERSION < 7
- zval *grpc_php_timeval_inf_future =
+ zval *grpc_php_timeval_inf_future;
+ PHP_GRPC_MAKE_STD_ZVAL(grpc_php_timeval_inf_future);
+ grpc_php_timeval_inf_future =
grpc_php_wrap_timeval(gpr_inf_future(GPR_CLOCK_REALTIME) TSRMLS_CC);
RETURN_DESTROY_ZVAL(grpc_php_timeval_inf_future);
-#else
- grpc_php_wrap_timeval(gpr_inf_future(GPR_CLOCK_REALTIME), return_value);
- RETURN_DESTROY_ZVAL(return_value);
-#endif
}
/**
@@ -303,14 +243,11 @@ PHP_METHOD(Timeval, infFuture) {
* @return Timeval Infinite past time value
*/
PHP_METHOD(Timeval, infPast) {
-#if PHP_MAJOR_VERSION < 7
- zval *grpc_php_timeval_inf_past =
+ zval *grpc_php_timeval_inf_past;
+ PHP_GRPC_MAKE_STD_ZVAL(grpc_php_timeval_inf_past);
+ grpc_php_timeval_inf_past =
grpc_php_wrap_timeval(gpr_inf_past(GPR_CLOCK_REALTIME) TSRMLS_CC);
RETURN_DESTROY_ZVAL(grpc_php_timeval_inf_past);
-#else
- grpc_php_wrap_timeval(gpr_inf_past(GPR_CLOCK_REALTIME), return_value);
- RETURN_DESTROY_ZVAL(return_value);
-#endif
}
/**
@@ -341,13 +278,7 @@ void grpc_init_timeval(TSRMLS_D) {
INIT_CLASS_ENTRY(ce, "Grpc\\Timeval", timeval_methods);
ce.create_object = create_wrapped_grpc_timeval;
grpc_ce_timeval = zend_register_internal_class(&ce TSRMLS_CC);
-#if PHP_MAJOR_VERSION >= 7
- memcpy(&timeval_ce_handlers, zend_get_std_object_handlers(),
- sizeof(zend_object_handlers));
- timeval_ce_handlers.offset =
- XtOffsetOf(wrapped_grpc_timeval, std);
- timeval_ce_handlers.free_obj = free_wrapped_grpc_timeval;
-#endif
+ PHP_GRPC_INIT_HANDLER(wrapped_grpc_timeval, timeval_ce_handlers);
}
void grpc_shutdown_timeval(TSRMLS_D) {}
diff --git a/src/php/ext/grpc/timeval.h b/src/php/ext/grpc/timeval.h
index d362460c05..63a1d702f3 100755
--- a/src/php/ext/grpc/timeval.h
+++ b/src/php/ext/grpc/timeval.h
@@ -50,30 +50,24 @@
extern zend_class_entry *grpc_ce_timeval;
/* Wrapper struct for timeval that can be associated with a PHP object */
-#if PHP_MAJOR_VERSION < 7
-
-typedef struct wrapped_grpc_timeval {
- zend_object std;
+PHP_GRPC_WRAP_OBJECT_START(wrapped_grpc_timeval)
gpr_timespec wrapped;
-} wrapped_grpc_timeval;
+PHP_GRPC_WRAP_OBJECT_END(wrapped_grpc_timeval)
+
+#if PHP_MAJOR_VERSION < 7
#define Z_WRAPPED_GRPC_TIMEVAL_P(zv) \
(wrapped_grpc_timeval *)zend_object_store_get_object(zv TSRMLS_CC)
#else
-typedef struct wrapped_grpc_timeval {
- gpr_timespec wrapped;
- zend_object std;
-} wrapped_grpc_timeval;
-
static inline wrapped_grpc_timeval
*wrapped_grpc_timeval_from_obj(zend_object *obj) {
return (wrapped_grpc_timeval*)((char*)(obj) -
XtOffsetOf(wrapped_grpc_timeval, std));
}
-#define Z_WRAPPED_GRPC_TIMEVAL_P(zv) \
+#define Z_WRAPPED_GRPC_TIMEVAL_P(zv) \
wrapped_grpc_timeval_from_obj(Z_OBJ_P((zv)))
#endif /* PHP_MAJOR_VERSION */
@@ -85,10 +79,6 @@ void grpc_init_timeval(TSRMLS_D);
void grpc_shutdown_timeval(TSRMLS_D);
/* Creates a Timeval object that wraps the given timeval struct */
-#if PHP_MAJOR_VERSION < 7
zval *grpc_php_wrap_timeval(gpr_timespec wrapped TSRMLS_DC);
-#else
-void grpc_php_wrap_timeval(gpr_timespec wrapped, zval *timeval_object);
-#endif /* PHP_MAJOR_VERSION */
#endif /* NET_GRPC_PHP_GRPC_TIMEVAL_H_ */