Skip to content

Commit 6f75b0e

Browse files
Allow for subscribe callback to return any value
This commit changes the behaviour of the various subscribe flavors such that the callback can break the loop by returning any non-null value. This would allow, for example, context to be retreived from the message and then used in such a way to return it to the caller.
1 parent e8ad58b commit 6f75b0e

File tree

2 files changed

+14
-16
lines changed

2 files changed

+14
-16
lines changed

README.markdown

+2-2
Original file line numberDiff line numberDiff line change
@@ -2898,7 +2898,7 @@ _**Description**_: Subscribe to channels by pattern
28982898
##### *Parameters*
28992899
*patterns*: An array of patterns to match
29002900
*callback*: Either a string or an array with an object and method. The callback will get four arguments ($redis, $pattern, $channel, $message)
2901-
2901+
*return value*: Mixed. Any non-null return value in the callback will be returned to the caller.
29022902
##### *Example*
29032903
~~~
29042904
function psubscribe($redis, $pattern, $chan, $msg) {
@@ -2928,7 +2928,7 @@ _**Description**_: Subscribe to channels. Warning: this function will probably c
29282928
##### *Parameters*
29292929
*channels*: an array of channels to subscribe to
29302930
*callback*: either a string or an array($instance, 'method_name'). The callback function receives 3 parameters: the redis instance, the channel name, and the message.
2931-
2931+
*return value*: Mixed. Any non-null return value in the callback will be returned to the caller.
29322932
##### *Example*
29332933
~~~
29342934
function f($redis, $chan, $msg) {

redis.c

+12-14
Original file line numberDiff line numberDiff line change
@@ -5764,21 +5764,19 @@ PHP_REDIS_API void generic_subscribe_cmd(INTERNAL_FUNCTION_PARAMETERS, char *sub
57645764
break;
57655765
}
57665766

5767-
/* Free our return value if we have one. If the return value is a bool
5768-
* that is FALSE, break our subscribe loop and return control to the
5769-
* userland code */
5770-
if (z_ret) {
5771-
if(Z_TYPE_P(z_ret) == IS_BOOL && Z_BVAL_P(z_ret) == 0) {
5772-
zval_ptr_dtor(&z_ret);
5773-
zval_dtor(z_tab);
5774-
efree(z_tab);
5775-
break;
5776-
}
5767+
/* Free reply from Redis */
5768+
zval_dtor(z_tab);
5769+
efree(z_tab);
5770+
5771+
/* Check for a non-null return value. If we have one, return it from
5772+
* the subscribe function itself. Otherwise continue our loop. */
5773+
if (z_ret) {
5774+
if (Z_TYPE_P(z_ret) != IS_NULL) {
5775+
RETVAL_ZVAL(z_ret, 0, 1);
5776+
break;
5777+
}
57775778
zval_ptr_dtor(&z_ret);
5778-
}
5779-
5780-
zval_dtor(z_tab);
5781-
efree(z_tab);
5779+
}
57825780
}
57835781
}
57845782

0 commit comments

Comments
 (0)