Skip to content

Commit 50d095b

Browse files
committed
[odb] wip: backend_foreach_callback
1 parent 2d352b3 commit 50d095b

File tree

2 files changed

+33
-3
lines changed

2 files changed

+33
-3
lines changed

example/odb_backend.php

+13-2
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,13 @@ class Pool
5959
"refresh" => function() {
6060

6161
},
62-
"foreach" => function() {
63-
62+
"foreach" => function($foreach_cb, &$payload) { // this payload was passed by git_odb_foreach callback.
63+
foreach (Pool::$pool as $oid => $value) {
64+
$retval = $foreach_cb($oid, $payload);
65+
if ($retval == GIT_EUSER) {
66+
return $retval;
67+
}
68+
}
6469
},
6570
"writepack" => function() {
6671

@@ -85,4 +90,10 @@ class Pool
8590

8691
$obj = git_odb_read_prefix($odb, substr($oid, 0, 10));
8792
var_dump($obj);
93+
94+
95+
$payload = array();
96+
git_odb_foreach($odb, function($oid, &$payload) {
97+
echo $oid . PHP_EOL;
98+
}, $payload);
8899
exit;

odb.c

+20-1
Original file line numberDiff line numberDiff line change
@@ -933,8 +933,27 @@ static int php_git2_odb_backend_exists(git_odb_backend *backend, const git_oid *
933933
return !retval;
934934

935935
}
936-
static int php_git2_odb_backend_foreach(git_odb_backend *_backend, git_odb_foreach_cb cb, void *data)
936+
static int php_git2_odb_backend_foreach(git_odb_backend *backend, git_odb_foreach_cb cb, void *data)
937937
{
938+
php_git2_t *result;
939+
php_git2_odb_backend *php_backend = (php_git2_odb_backend*)backend;
940+
zval *param_callback = NULL, *retval_ptr = NULL, *param_payload = (zval*)data;
941+
php_git2_multi_cb_t *p = php_backend->multi;
942+
int i = 0, retval = 0;
943+
GIT2_TSRMLS_SET(p->tsrm_ls);
944+
945+
MAKE_STD_ZVAL(param_callback);
946+
// TODO(chobie): wrap git_odb_foreach_cb with closure
947+
// see zend_create_closure
948+
949+
if (php_git2_call_function_v(&p->callbacks[6].fci, &p->callbacks[6].fcc TSRMLS_CC, &retval_ptr, 2,
950+
&param_callback, &param_payload)) {
951+
return GIT_EUSER;
952+
}
953+
954+
retval = Z_LVAL_P(retval_ptr);
955+
zval_ptr_dtor(&retval_ptr);
956+
return retval;
938957
}
939958
static void php_git2_odb_backend_free(git_odb_backend *_backend)
940959
{

0 commit comments

Comments
 (0)