forked from RogerGee/php-git2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
php-odb-backend.cpp
1048 lines (843 loc) · 29.2 KB
/
php-odb-backend.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* php-odb-backend.cpp
*
* Copyright (C) Roger P. Gee
*
* This unit provides the out-of-line implementation for the GitODBBackend
* class.
*/
#include "php-object.h"
#include "php-callback.h"
#include <cstring>
#include <new>
using namespace php_git2;
// Wrapper for efree since it's a macro.
static void efree_wrapper(void* d)
{
efree(d);
}
// Custom class handlers
static zval* odb_backend_read_property(zval* object,
zval* member,
int type,
void** cache_slot,
zval* rv);
#if PHP_API_VERSION >= 20190902
static zval* odb_backend_write_property(zval* object,
zval* member,
zval* value,
void** cache_slot);
#else
static void odb_backend_write_property(zval* object,
zval* member,
zval* value,
void** cache_slot);
#endif
static int odb_backend_has_property(zval* object,
zval* member,
int has_set_exists,
void** cache_slot);
// Closure callbacks
struct foreach_callback_info
{
git_odb_foreach_cb callback;
void* payload;
};
struct transfer_progress_callback_info
{
git_transfer_progress_cb callback;
void* payload;
};
static ZEND_FUNCTION(backend_foreach_callback);
static ZEND_FUNCTION(backend_transfer_progress_callback);
// Class method entries
static PHP_EMPTY_METHOD(GitODBBackend,read);
static PHP_EMPTY_METHOD(GitODBBackend,read_prefix);
static PHP_EMPTY_METHOD(GitODBBackend,read_header);
static PHP_EMPTY_METHOD(GitODBBackend,write);
static PHP_EMPTY_METHOD(GitODBBackend,writestream);
static PHP_EMPTY_METHOD(GitODBBackend,readstream);
static PHP_EMPTY_METHOD(GitODBBackend,exists);
static PHP_EMPTY_METHOD(GitODBBackend,exists_prefix);
static PHP_EMPTY_METHOD(GitODBBackend,refresh);
static PHP_EMPTY_METHOD(GitODBBackend,writepack);
zend_function_entry php_git2::odb_backend_methods[] = {
PHP_ME(GitODBBackend,read,NULL,ZEND_ACC_PUBLIC)
PHP_ME(GitODBBackend,read_prefix,NULL,ZEND_ACC_PUBLIC)
PHP_ME(GitODBBackend,read_header,NULL,ZEND_ACC_PUBLIC)
PHP_ME(GitODBBackend,write,NULL,ZEND_ACC_PUBLIC)
PHP_ME(GitODBBackend,writestream,NULL,ZEND_ACC_PUBLIC)
PHP_ME(GitODBBackend,readstream,NULL,ZEND_ACC_PUBLIC)
PHP_ME(GitODBBackend,exists,NULL,ZEND_ACC_PUBLIC)
PHP_ME(GitODBBackend,exists_prefix,NULL,ZEND_ACC_PUBLIC)
PHP_ME(GitODBBackend,refresh,NULL,ZEND_ACC_PUBLIC)
PHP_ABSTRACT_ME(GitODBBackend,for_each,NULL)
PHP_ME(GitODBBackend,writepack,NULL,ZEND_ACC_PUBLIC)
PHP_FE_END
};
// php_zend_object init function
template<>
zend_object_handlers php_git2::php_zend_object<php_odb_backend_object>::handlers;
template<>
void php_zend_object<php_odb_backend_object>::init(zend_class_entry* ce)
{
handlers.read_property = odb_backend_read_property;
handlers.write_property = odb_backend_write_property;
handlers.has_property = odb_backend_has_property;
handlers.offset = offset();
UNUSED(ce);
}
// Implementation of php_odb_backend_object
php_odb_backend_object::php_odb_backend_object():
backend(nullptr), kind(unset), owner(nullptr)
{
}
php_odb_backend_object::~php_odb_backend_object()
{
// AFAIK we never call the free() function on git_odb_backend objects that
// were returned from a call that referenced a git_odb NOR those applied to
// a git_odb's list of backends. This is the case when kind is either
// 'conventional' or 'custom'.
if (kind == user) {
backend->free(backend);
}
// Free owner ODB in case it is referenced.
if (owner != nullptr) {
git2_resource_base::free_recursive(owner);
}
}
void php_odb_backend_object::create_custom_backend(zval* obj)
{
// Make sure object doesn't already have a backing. This would imply it is
// already in use in an ODB.
if (backend != nullptr) {
throw php_git2_exception(
"Cannot create custom ODB backend: object already in use");
}
// Set kind to 'custom'.
kind = custom;
// Create custom backend. Custom backends are always passed off to git2, so
// we are not responsible for calling its free function.
backend = new (emalloc(sizeof(git_odb_backend_php)))
git_odb_backend_php(Z_OBJ_P(obj));
// Custom ODB backends never have a direct owner. We always assume the
// would-be owner is kept alive circularly since the custom backend stores a
// reference to this object.
assign_owner(nullptr);
}
void php_odb_backend_object::assign_owner(php_git_odb* newOwner)
{
// Free existing owner (if any).
if (owner != nullptr) {
git2_resource_base::free_recursive(owner);
}
owner = newOwner;
// Increment owner refcount to prevent the ODB from freeing while the
// backend object is in use.
if (owner != nullptr) {
owner->up_ref();
}
}
void php_odb_backend_object::unset_backend(zval* obj)
{
// Unset 'odb' property.
zend_unset_property(Z_OBJCE_P(obj),obj,"odb",sizeof("odb")-1);
// Unset backend reference.
backend = nullptr;
kind = unset;
}
/*static*/ int php_odb_backend_object::read(
void** bufferp,
size_t* sizep,
git_object_t* typep,
git_odb_backend* backend,
const git_oid* oid)
{
int result;
zval_array<2> params;
method_wrapper method("read",backend);
// Prepare parameters.
params.make_ref(0);
convert_oid(params[1],oid);
// Call userspace method.
result = method.call(params);
if (result == GIT_OK) {
void* data;
size_t datalen;
zval* retval = method.retval();
if (Z_TYPE_P(retval) == IS_FALSE) {
return GIT_ENOTFOUND;
}
// Copy the returned data to a persistent memory buffer that git2 can
// free later on.
convert_to_string(retval);
datalen = static_cast<size_t>(Z_STRLEN_P(retval));
data = git_odb_backend_data_alloc(backend,datalen);
memcpy(data,Z_STRVAL_P(retval),datalen);
// Return values to caller.
*bufferp = data;
*sizep = Z_STRLEN_P(retval);
params.unref(0);
convert_to_long(params[0]);
*typep = (git_object_t)Z_LVAL_P(params[0]);
}
return result;
}
/*static*/ int php_odb_backend_object::read_prefix(
git_oid* oidp,
void** bufferp,
size_t* sizep,
git_object_t* typep,
git_odb_backend* backend,
const git_oid* prefix,
size_t len)
{
int result;
zval_array<3> params;
method_wrapper method("read_prefix",backend);
// Prepare parameters.
params.make_ref(0);
params.make_ref(1);
convert_oid_prefix(params[2],prefix,len);
// Call userspace method.
result = method.call(params);
if (result == GIT_OK) {
void* data;
size_t datalen;
zval* retval = method.retval();
if (Z_TYPE_P(retval) == IS_FALSE) {
return GIT_ENOTFOUND;
}
// Copy the returned data to a persistent memory buffer that git2 can
// free later on.
convert_to_string(retval);
datalen = static_cast<size_t>(Z_STRLEN_P(retval));
data = git_odb_backend_data_alloc(backend,datalen);
memcpy(data,Z_STRVAL_P(retval),datalen);
// Return values to caller.
*bufferp = data;
*sizep = Z_STRLEN_P(retval);
params.unref(0);
params.unref(1);
convert_to_string(params[0]);
convert_to_long(params[1]);
convert_oid_fromstr(oidp,Z_STRVAL_P(params[0]),Z_STRLEN_P(params[0]));
*typep = (git_object_t)Z_LVAL_P(params[1]);
}
return result;
}
/*static*/ int php_odb_backend_object::read_header(
size_t* sizep,
git_object_t* typep,
git_odb_backend* backend,
const git_oid* oid)
{
int result;
zval_array<3> params;
method_wrapper method("read_header",backend);
// Prepare parameters.
params.make_ref(0);
params.make_ref(1);
convert_oid(params[2],oid);
// Call userspace method.
result = method.call(params);
if (result == GIT_OK) {
if (Z_TYPE_P(method.retval()) == IS_FALSE) {
return GIT_ENOTFOUND;
}
// Return values to caller.
params.unref(0);
params.unref(1);
convert_to_long(params[0]);
convert_to_long(params[1]);
*sizep = Z_LVAL_P(params[0]);
*typep = (git_object_t)Z_LVAL_P(params[1]);
}
return result;
}
/*static*/ int php_odb_backend_object::write(
git_odb_backend* backend,
const git_oid* oid,
const void* data,
size_t size,
git_object_t type)
{
int result;
zval_array<3> params;
method_wrapper method("write",backend);
// Prepare parameters.
convert_oid(params[0],oid);
ZVAL_STRINGL(params[1],(const char*)data,size);
ZVAL_LONG(params[2],type);
// Call userspace method.
result = method.call(params);
return result;
}
/*static*/ int php_odb_backend_object::writestream(
git_odb_stream** streamp,
git_odb_backend* backend,
git_object_size_t length,
git_object_t type)
{
int result;
zval_array<2> params;
method_wrapper method("writestream",backend);
// Prepare parameters.
ZVAL_LONG(params[0],length);
ZVAL_LONG(params[1],type);
// Call userspace method.
result = method.call(params);
if (result != GIT_OK) {
return result;
}
try {
php_object<php_odb_stream_object> obj;
obj.parse_with_context(
method.retval(),
"GitODBBackend::writestream(): return value"
);
php_odb_stream_object* stream = obj.get_storage();
// Now create the custom stream backing. The stream tracks this object
// to keep it alive and provide access to the GitODBBackend derivation.
stream->create_custom_stream(
obj.get_value(),
method.thisobj(),
GIT_STREAM_WRONLY);
// The libgit2 implementation expects the custom backing to provide a
// reference to the ODB backend.
stream->stream->backend = backend;
// Assign the git_odb_stream to the out parameter. This is safe since
// the instance holds its own reference to the object backing, meaning
// the returned object will survive until the free() function is called
// on the git_odb_stream.
*streamp = stream->stream;
} catch (php_git2_exception_base& ex) {
php_git2_giterr_set(GITERR_ODB,ex.what());
result = GIT_EPHP_ERROR;
}
return result;
}
/*static*/ int php_odb_backend_object::readstream(
git_odb_stream** streamp,
size_t* sizep,
git_object_t *typep,
git_odb_backend* backend,
const git_oid* oid)
{
int result;
zval_array<3> params;
method_wrapper method("readstream",backend);
// Prepare parameters.
params.make_ref(0);
params.make_ref(1);
convert_oid(params[2],oid);
// Call userspace method.
result = method.call(params);
if (result != GIT_OK) {
return result;
}
if (Z_TYPE_P(method.retval()) == IS_FALSE) {
return GIT_ENOTFOUND;
}
try {
php_object<php_odb_stream_object> obj;
obj.parse_with_context(
method.retval(),
"GitODBBackend::readstream(): return value"
);
php_odb_stream_object* stream = obj.get_storage();
// Now create the custom stream backing. The stream tracks this object
// to keep it alive and provide access to the GitODBBackend derivation.
stream->create_custom_stream(
obj.get_value(),
method.thisobj(),
GIT_STREAM_RDONLY);
// The libgit2 implementation expects the custom backing to provide a
// reference to the ODB backend.
stream->stream->backend = backend;
// Assign the git_odb_stream to the out parameter. This is safe since
// the instance holds its own reference to the object backing, meaning
// the returned object will survive until the free() function is called
// on the git_odb_stream.
*streamp = stream->stream;
// Return out parameters.
convert_to_long(params[0]);
convert_to_long(params[1]);
*sizep = static_cast<size_t>(Z_LVAL_P(params[0]));
*typep = static_cast<git_object_t>(Z_LVAL_P(params[1]));
} catch (php_git2_exception_base& ex) {
php_git2_giterr_set(GITERR_ODB,ex.what());
result = GIT_EPHP_ERROR;
}
return result;
}
/*static*/ int php_odb_backend_object::exists(
git_odb_backend* backend,
const git_oid* oid)
{
int result;
zval_array<1> params;
method_wrapper method("exists",backend);
// Prepare parameters.
convert_oid(params[0],oid);
// Call userspace method.
result = method.call(params);
if (result == GIT_OK) {
zval* retval = method.retval();
convert_to_boolean(retval);
// NOTE: exists() returns 1 for TRUE and 0 for FALSE.
result = Z_TYPE_P(retval) == IS_TRUE ? 1 : 0;
}
return result;
}
/*static*/ int php_odb_backend_object::exists_prefix(
git_oid* oidp,
git_odb_backend* backend,
const git_oid* prefix,
size_t len)
{
int result;
zval_array<2> params;
method_wrapper method("exists_prefix",backend);
// Prepare parameters.
params.make_ref(0);
convert_oid_prefix(params[1],prefix,len);
// Call userspace method.
result = method.call(params);
if (result == GIT_OK) {
zval* retval = method.retval();
// Return values to caller. NOTE: exists_prefix() returns 0 on success
// (i.e. found) and the GIT_ENOTFOUND error when the element is not
// found.
params.unref(0);
convert_to_string(params[0]);
convert_oid_fromstr(oidp,Z_STRVAL_P(params[0]),Z_STRLEN_P(params[0]));
convert_to_boolean(retval);
result = Z_TYPE_P(retval) == IS_TRUE ? 0 : GIT_ENOTFOUND;
}
return result;
}
/*static*/ int php_odb_backend_object::refresh(git_odb_backend* backend)
{
int result;
method_wrapper method("refresh",backend);
// Call userspace method.
result = method.call();
return result;
}
/*static*/ int php_odb_backend_object::foreach(
git_odb_backend* backend,
git_odb_foreach_cb cb,
void* payload)
{
int result;
zval_array<2> params;
php_closure_object* closure;
method_wrapper method("for_each",backend);
foreach_callback_info callbackInfo = { cb, payload };
// Create GitClosure instance for handling the callback.
object_init_ex(params[0],php_git2::class_entry[php_git2_closure_obj]);
closure = php_zend_object<php_closure_object>::get_storage(Z_OBJ_P(params[0]));
// Set up the closure to call an internal function that will handle calling
// the callback function and passing the correct payload.
closure->func.type = ZEND_INTERNAL_FUNCTION;
closure->func.common.function_name = zend_string_init(
"php_odb_backend_object_foreach_internal_callback",
sizeof("php_odb_backend_object_foreach_internal_callback")-1,
0
);
closure->func.common.scope = php_git2::class_entry[php_git2_closure_obj];
closure->func.internal_function.handler = ZEND_FN(backend_foreach_callback);
closure->hasPayload = true;
closure->payload = &callbackInfo;
// Call userspace method. NOTE: The payload (i.e. params[1]) will be NULL.
result = method.call(params);
return result;
}
/*static*/ int php_odb_backend_object::writepack(
git_odb_writepack** writepackp,
git_odb_backend* backend,
git_odb* odb,
git_transfer_progress_cb progress_cb,
void* progress_payload)
{
int result = GIT_OK;
zval_array<3> params;
method_wrapper method("writepack",backend);
// Prepare parameters The payload zval (params[2]) is unused and will remain
// NULL. (Note that the payload will be passed to the underlying callback
// internally but not converted for use in userspace.)
{
// Create non-freeable resource value for ODB.
php_resource_ref<php_git_odb_nofree> resourceWrapper;
resourceWrapper.set_object(odb);
resourceWrapper.ret(params[0]);
}
// Create callback info passed via the closure's payload.
transfer_progress_callback_info* callbackInfo;
callbackInfo = reinterpret_cast<transfer_progress_callback_info*>(
emalloc(sizeof(transfer_progress_callback_info))
);
callbackInfo->callback = progress_cb;
callbackInfo->payload = progress_payload;
// Create GitClosure object for handling the callback.
php_closure_object* closure;
object_init_ex(params[1],php_git2::class_entry[php_git2_closure_obj]);
closure = php_zend_object<php_closure_object>::get_storage(Z_OBJ_P(params[1]));
// Set up the closure to call an internal function that will handle calling
// the callback function and passing the correct payload.
closure->func.type = ZEND_INTERNAL_FUNCTION;
closure->func.common.function_name = zend_string_init(
"php_odb_backend_object_writepack_internal_callback",
sizeof("php_odb_backend_object_writepack_internal_callback")-1,
0);
closure->func.common.scope = php_git2::class_entry[php_git2_closure_obj];
closure->func.internal_function.handler = ZEND_FN(backend_transfer_progress_callback);
closure->hasPayload = true;
closure->payload = callbackInfo;
closure->payloadDestructor = efree_wrapper;
// Invoke method on odb_backend.
result = method.call(params);
if (result != GIT_OK) {
return result;
}
try {
php_object<php_odb_writepack_object> obj;
obj.parse_with_context(
method.retval(),
"GitODBBackend::writepack(): return value"
);
php_odb_writepack_object* writepack = obj.get_storage();
// Now create the custom writepack backing. The writepack tracks this
// object (via the backendWrapper's owner) and will keep it alive.
writepack->create_custom_writepack(obj.get_value(),method.thisobj());
// Help out the implementation by providing a reference to the ODB
// backend.
writepack->writepack->backend = backend;
// Assign the git_odb_stream to the out parameter. This is safe since
// the instance holds its own reference to the object backing, meaning
// the returned object will survive until the free() function is called
// on the git_odb_writepack.
*writepackp = writepack->writepack;
} catch (php_git2_exception_base& ex) {
php_git2_giterr_set(GITERR_ODB,ex.what());
result = GIT_EPHP_ERROR;
}
return result;
}
/*static*/ void php_odb_backend_object::free(git_odb_backend* backend)
{
method_wrapper::object_wrapper object(backend);
// Make sure object buckets still exist to lookup object (in case the
// destructor was already called). This happens when free() is called after
// PHP has finished cleaning up all objects (but before all variables, which
// may have references to git2 objects that reference this PHP object).
if (EG(objects_store).object_buckets != nullptr) {
// Unassign backend from the object since it is about to get
// destroyed. This also ensures no references remain to any ODB since
// the ODB is (presumably) in the process of freeing right now.
object.backing()->unset_backend(object.thisobj());
}
// Explicitly call the destructor on the custom backend. Then free the block
// of memory that holds the custom backend.
object.object()->~git_odb_backend_php();
efree(backend);
}
// php_odb_backend_object::git_odb_backend_php
php_odb_backend_object::
git_odb_backend_php::git_odb_backend_php(zend_object* obj)
{
// Blank out the base class (which is the structure defined in the git2
// headers).
memset(this,0,sizeof(struct git_odb_backend));
// Assign zend_object to local zval so it is kept alive for duration of
// custom backend.
ZVAL_OBJ(&thisobj,obj);
Z_ADDREF(thisobj);
version = GIT_ODB_BACKEND_VERSION;
// Every custom backend gets the free function (whether it is overloaded in
// userspace or not).
free = php_odb_backend_object::free;
// Include functions that must be implemented by the custom class.
foreach = php_odb_backend_object::foreach;
// We now must select which functions we are going to include in the
// backend. We do this by determining which ones were overloaded.
if (is_method_overridden(obj->ce,"read",sizeof("read")-1)) {
read = php_odb_backend_object::read;
}
if (is_method_overridden(obj->ce,"read_prefix",sizeof("read_prefix")-1)) {
read_prefix = php_odb_backend_object::read_prefix;
}
if (is_method_overridden(obj->ce,"read_header",sizeof("read_header")-1)) {
read_header = php_odb_backend_object::read_header;
}
if (is_method_overridden(obj->ce,"write",sizeof("write")-1)) {
write = php_odb_backend_object::write;
}
if (is_method_overridden(obj->ce,"writestream",sizeof("writestream")-1)) {
writestream = php_odb_backend_object::writestream;
}
if (is_method_overridden(obj->ce,"readstream",sizeof("readstream")-1)) {
readstream = php_odb_backend_object::readstream;
}
if (is_method_overridden(obj->ce,"exists",sizeof("exists")-1)) {
exists = php_odb_backend_object::exists;
}
if (is_method_overridden(obj->ce,"exists_prefix",sizeof("exists_prefix")-1)) {
exists_prefix = php_odb_backend_object::exists_prefix;
}
if (is_method_overridden(obj->ce,"refresh",sizeof("refresh")-1)) {
refresh = php_odb_backend_object::refresh;
}
if (is_method_overridden(obj->ce,"writepack",sizeof("writepack")-1)) {
writepack = php_odb_backend_object::writepack;
}
}
php_odb_backend_object::
git_odb_backend_php::~git_odb_backend_php()
{
zval_ptr_dtor(&thisobj);
}
// Implementation of custom class handlers
zval* odb_backend_read_property(
zval* object,
zval* member,
int type,
void** cache_slot,
zval* rv)
{
zval* retval;
zval tmp_member;
php_odb_backend_object* storage;
// Ensure deep copy of member zval.
if (Z_TYPE_P(member) != IS_STRING) {
ZVAL_STR(&tmp_member,zval_get_string(member));
member = &tmp_member;
cache_slot = nullptr;
}
// Handle special properties of the git_odb_backend.
storage = php_zend_object<php_odb_backend_object>::get_storage(Z_OBJ_P(object));
if (strcmp(Z_STRVAL_P(member),"version") == 0) {
if (storage->backend != nullptr) {
ZVAL_LONG(rv,storage->backend->version);
}
else {
ZVAL_NULL(rv);
}
retval = rv;
}
else if (strcmp(Z_STRVAL_P(member),"odb") == 0) {
retval = zend_hash_find(Z_OBJPROP_P(object),Z_STR_P(member));
if (retval == nullptr) {
retval = rv;
if (storage->backend != nullptr && storage->backend->odb != nullptr) {
zend_resource* zr;
php_git_odb* rsrc;
if (storage->owner == nullptr) {
// Create new resource that will not free the git_odb.
rsrc = php_git2_create_resource<php_git_odb_nofree>();
rsrc->set_handle(storage->backend->odb);
}
else {
// Use owner resource. We must increment the refcount so the
// resource remains valid for the user. (The resource
// destructor will handle the decrement.)
rsrc = storage->owner;
rsrc->up_ref();
}
zr = zend_register_resource(rsrc,php_git_odb::resource_le());
ZVAL_RES(retval,zr);
Z_ADDREF_P(retval);
zend_hash_add(Z_OBJPROP_P(object),Z_STR_P(member),retval);
}
else {
ZVAL_NULL(retval);
}
}
}
else {
const zend_object_handlers* std = zend_get_std_object_handlers();
retval = std->read_property(object,member,type,cache_slot,rv);
}
if (member == &tmp_member) {
zval_dtor(member);
}
return retval;
}
#if PHP_API_VERSION >= 20190902
zval* odb_backend_write_property(
zval* object,
zval* member,
zval* value,
void** cache_slot)
{
zval* result = value;
zval tmp_member;
// Ensure deep copy of member zval.
if (Z_TYPE_P(member) != IS_STRING) {
ZVAL_STR(&tmp_member,zval_get_string(member));
member = &tmp_member;
cache_slot = nullptr;
}
if (strcmp(Z_STRVAL_P(member),"version") == 0
|| strcmp(Z_STRVAL_P(member),"odb") == 0)
{
zend_throw_error(
nullptr,
"Property '%s' of GitODBBackend cannot be updated",
Z_STRVAL_P(member)
);
}
else {
const zend_object_handlers* std = zend_get_std_object_handlers();
result = std->write_property(object,member,value,cache_slot);
}
if (member == &tmp_member) {
zval_dtor(member);
}
return result;
}
#else
void odb_backend_write_property(
zval* object,
zval* member,
zval* value,
void** cache_slot)
{
zval tmp_member;
// Ensure deep copy of member zval.
if (Z_TYPE_P(member) != IS_STRING) {
ZVAL_STR(&tmp_member,zval_get_string(member));
member = &tmp_member;
cache_slot = nullptr;
}
if (strcmp(Z_STRVAL_P(member),"version") == 0
|| strcmp(Z_STRVAL_P(member),"odb") == 0)
{
zend_throw_error(
nullptr,
"Property '%s' of GitODBBackend cannot be updated",
Z_STRVAL_P(member)
);
}
else {
const zend_object_handlers* std = zend_get_std_object_handlers();
std->write_property(object,member,value,cache_slot);
}
if (member == &tmp_member) {
zval_dtor(member);
}
}
#endif
int odb_backend_has_property(
zval* object,
zval* member,
int has_set_exists,
void** cache_slot)
{
int result;
zval tmp_member;
git_odb_backend* backend;
// Ensure deep copy of member zval.
if (Z_TYPE_P(member) != IS_STRING) {
ZVAL_STR(&tmp_member,zval_get_string(member));
member = &tmp_member;
cache_slot = nullptr;
}
backend = php_zend_object<php_odb_backend_object>::get_storage(Z_OBJ_P(object))->backend;
if (strcmp(Z_STRVAL_P(member),"version") == 0) {
result = (backend != nullptr);
}
else if (strcmp(Z_STRVAL_P(member),"odb") == 0) {
if (has_set_exists == 2) {
result = (backend != nullptr);
}
else {
result = (backend != nullptr && backend->odb != nullptr);
}
}
else {
const zend_object_handlers* std = zend_get_std_object_handlers();
result = std->has_property(object,member,has_set_exists,cache_slot);
}
if (member == &tmp_member) {
zval_dtor(member);
}
return result;
}
// Implementation of internal function for custom backend's foreach callback.
ZEND_FUNCTION(backend_foreach_callback)
{
php_bailer bailer;
char* oidS;
size_t oidL;
zval* payload = nullptr;
if (zend_parse_parameters(ZEND_NUM_ARGS(),"s|z",&oidS,&oidL,&payload) == FAILURE) {
return;
}
try {
int result;
git_oid oid;
php_closure_object* object;
const foreach_callback_info* info;
object = php_zend_object<php_closure_object>::get_storage(Z_OBJ_P(getThis()));
info = reinterpret_cast<const foreach_callback_info*>(object->payload);
// Convert string representation to git2 OID struct.
php_git2::convert_oid_fromstr(&oid,oidS,oidL);
// Invoke callback. GIT_EUSER value means that the iteration should stop
// without an exception. We communicate this to userspace by returning
// false.
result = (*info->callback)(&oid,info->payload);
if (result == GIT_EUSER) {
RETURN_FALSE;
}
if (result != GIT_OK) {
php_git2::git_error(result);
}
} catch (php_git2_exception_base& ex) {
php_bailout_context ctx(bailer);
if (BAILOUT_ENTER_REGION(ctx)) {
ex.handle();
}
}
}