-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathphpClass.pas
1131 lines (987 loc) · 30.3 KB
/
phpClass.pas
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
{*******************************************************}
{ PHP4Delphi }
{ PHP classes support }
{ }
{ Author: }
{ Serhiy Perevoznyk }
{ http://users.chello.be/ws36637 }
{*******************************************************}
{$I PHP.INC}
{ $Id: phpClass.pas,v 6.2 02/2006 delphi32 Exp $ }
unit phpClass;
interface
uses
Windows, SysUtils, Classes, PHPCommon,
ZendTypes, ZendAPI, phpTypes, PHPAPI,
phpFunctions;
type
// Instance of the class
TPHPClassInstance = class;
//Execute method
TClassMethodExecute = procedure (Sender : TPHPClassInstance; Parameters : TFunctionParams;
var ReturnValue : variant; ZendValue : pzval; this_ptr : pzval; TSRMLS_DC : pointer) of object;
//Property
TClassProperty = class(TCollectionItem)
private
FName : string;
FValue : string;
function GetAsBoolean: boolean;
function GetAsFloat: double;
function GetAsInteger: integer;
procedure SetAsBoolean(const Value: boolean);
procedure SetAsFloat(const Value: double);
procedure SetAsInteger(const Value: integer);
protected
function GetDisplayName : string; override;
public
procedure Assign(Source : TPersistent); override;
property AsInteger : integer read GetAsInteger write SetAsInteger;
property AsBoolean : boolean read GetAsBoolean write SetAsBoolean;
property AsString : string read FValue write FValue;
property AsFloat : double read GetAsFloat write SetAsFloat;
published
property Name : string read FName write FName;
property Value : string read FValue write FValue;
end;
//Collection of the class properties
TClassProperties = class(TCollection)
private
FOwner : TComponent;
procedure SetItem(Index: Integer; const Value: TClassProperty);
function GetItem(Index: Integer): TClassProperty;
protected
function GetOwner : TPersistent; override;
public
function Add: TClassProperty;
constructor Create(AOwner: TComponent);
function GetVariables : string;
function IndexOf(AName : string) : integer;
function ByName(AName : string) : TClassProperty;
property Items[Index: Integer]: TClassProperty read GetItem write SetItem; default;
end;
//The method of the class
TPHPClassMethod = class(TCollectionItem)
private
FOnExecute : TClassMethodExecute;
FName: string;
FTag : integer;
FFunctionParams: TFunctionParams;
FZendVar : TZendVariable;
procedure SetFunctionParams(const Value: TFunctionParams);
public
ReturnValue : variant;
constructor Create(Collection : TCollection); override;
destructor Destroy; override;
function GetDisplayName: string; override;
procedure SetDisplayName(const Value: string); override;
procedure AssignTo(Dest: TPersistent); override;
property ZendVar: TZendVariable read FZendVar;
published
property Name : string read FName write SetDisplayName;
property Tag : integer read FTag write FTag;
property Parameters: TFunctionParams read FFunctionParams write SetFunctionParams;
property OnExecute : TClassMethodExecute read FOnExecute write FOnExecute;
end;
//Collection of the class methods
TPHPClassMethods = class(TCollection)
private
FOwner: TPersistent;
function GetItem(Index: Integer): TPHPClassMethod;
procedure SetItem(Index: Integer; Value: TPHPClassMethod);
protected
function GetOwner: TPersistent; override;
procedure SetItemName(Item: TCollectionItem); override;
public
constructor Create(Owner: TPersistent; ItemClass: TCollectionItemClass);
function Add : TPHPClassMethod;
function ByName(AName : string) : TPHPClassMethod;
property Items[Index: Integer]: TPHPClassMethod read GetItem write SetItem; default;
end;
//Represents PHP class
TPHPClass = class(TPHPComponent)
private
FProperties : TClassProperties;
FMethods : TPHPClassMethods;
FClassObject: pzend_class_entry;
FClassName : string;
FClassEntry : tzend_class_entry;
{$IFDEF PHP5}
FClassFunction : array[0..1] of zend_function_entry;
{$ENDIF}
procedure SetProperties(const Value: TClassProperties);
procedure SetMethods(const Value : TPHPClassMethods);
procedure SetClassName(const Value: string);
protected
function GetClassEntry : pzend_class_entry; virtual;
procedure Loaded; override;
function InstanceConstructor(return_value : pzval) : TPHPClassInstance;
property ZendClassObject : pzend_class_entry read FClassObject write FClassObject;
public
constructor Create(AOwner : TComponent); override;
destructor Destroy; override;
procedure ClassRegister(AModuleNumber : integer); virtual;
procedure ProduceInstance(AValue : pzval); virtual;
published
property Properties : TClassProperties read FProperties write SetProperties;
property Methods : TPHPClassMethods read FMethods write SetMethods;
property PHPClassName : string read FClassName write SetClassName;
end;
TPHPClassInstance = class(TComponent)
private
FProperties : TClassProperties;
FProducer : TPHPClass;
procedure SetProperties(const Value: TClassProperties);
protected
property Producer : TPHPClass read FProducer;
public
constructor Create(AOwner : TComponent); override;
destructor Destroy; override;
property Properties : TClassProperties read FProperties write SetProperties;
end;
{$IFDEF PHP5}
procedure RegisterClassHandlers;
{$ENDIF}
implementation
uses
phpModules;
var
le_classresource : integer = 0;
{$IFDEF PHP5}
ClassObjectHandlers : zend_object_handlers;
{$ENDIF}
const
{$IFDEF PHP4}
le_classresource_name = 'TPHPCLASS4';
{$ELSE}
le_classresource_name = 'TPHPCLASS5';
{$ENDIF}
procedure class_destructor_handler(rsrc : PZend_rsrc_list_entry; TSRMLS_D : pointer); cdecl;
var
resource : TPHPClassInstance;
begin
if rsrc = nil then
Exit;
resource := rsrc^.ptr;
if Assigned(Resource) then
try
resource.free;
except
end;
end;
{ TphpClass }
//when object created from script using "new"
function class_call_constructor(AClassName : string; return_value : pzval) : TPHPClassInstance;
var
Extension : TPHPExtension;
idp : pointer;
id : integer;
cnt : integer;
begin
Result := nil;
idp := ts_resource_ex(integer(app_globals_id), nil);
id := integer(idp^);
if id <= 0 then
Exit;
Extension := TPHPExtension(id);
for cnt := 0 to Extension.ComponentCount - 1 do
begin
if (Extension.Components[cnt] is TphpClass) then
begin
if SameText(AClassName, TphpClass(Extension.Components[cnt]).PHPClassName) then
begin
Result := TPHPClass(Extension.Components[cnt]).InstanceConstructor(return_value);
break;
end;
end;
end;
end;
{$IFDEF PHP4}
//PHP4
function class_set_property_handler(property_reference : Pzend_property_reference; value : pzval) : integer; cdecl;
var
this_ptr : pzval;
OBJ : TPHPClassInstance;
data: ^ppzval;
element : pzend_list_element;
prop : pzend_overloaded_element;
p : pointer;
propname : string;
tsrmls : pointer;
param : TClassProperty;
begin
tsrmls := ts_resource_ex(0, nil);
element := property_reference^.elements_list^.head;
p := @element^.data;
prop := pzend_overloaded_element(p);
propname := prop^.element.value.str.val;
this_ptr := property_reference^._object;
new(data);
if zend_hash_find(this_ptr^.value.obj.properties, 'instance', strlen('instance') + 1, data) = SUCCESS then
Obj := zend_fetch_resource(data^, TSRMLS, -1, 'class resource', nil, 1, le_classresource)
else
OBJ := nil;
freemem(data);
if Assigned(Obj) then
begin
convert_to_string(value);
param := Obj.Properties.ByName(propname);
if Assigned(param) then
param.Value := value.value.str.val;
end;
Result := SUCCESS;
end;
//PHP4
procedure class_call_function(ht : integer; return_value : pzval; this_ptr : pzval;
return_value_used : integer; TSRMLS_DC : pointer; property_reference : Pzend_property_reference ); cdecl;
var
OBJ : TPHPClassInstance;
Producer : TPHPClass;
M : TPHPClassMethod;
data: ^ppzval;
element : pzend_list_element;
prop : pzend_overloaded_element;
p : pointer;
MethodName : string;
Params : pzval_array;
j : integer;
begin
element := property_reference^.elements_list^.head;
p := @element^.data;
prop := pzend_overloaded_element(p);
MethodName := prop^.element.value.str.val;
this_ptr := property_reference^._object;
new(data);
if (zend_hash_find(this_ptr^.value.obj.properties, 'instance', strlen('instance') + 1, data) = SUCCESS) then
Obj := zend_fetch_resource(data^, TSRMLS_DC, -1, 'class resource', nil, 1, le_classresource)
else
Obj := nil;
freemem(data);
if not Assigned(Obj) then
begin
//not assigned obj = new instance (constructor)
class_call_constructor(MethodName, this_ptr);
exit;
end;
if ht > 0 then
begin
if ( not (zend_get_parameters_ex(ht, Params) = SUCCESS )) then
begin
zend_wrong_param_count(TSRMLS_DC);
Exit;
end;
end;
Producer := Obj.Producer;
if Assigned(Producer) then
begin
M := Producer.Methods.ByName(MethodName);
if Assigned(M) then
begin
if Assigned(M.FOnExecute) then
begin
if M.Parameters.Count <> ht then
begin
zend_wrong_param_count(TSRMLS_DC);
Exit;
end;
if ht > 0 then
begin
for j := 0 to ht - 1 do
begin
if not IsParamTypeCorrect(M.Parameters[j].ParamType, Params[j]^) then
begin
zend_error(E_WARNING, PChar(Format('Wrong parameter type for %s()', [get_active_function_name(TSRMLS_DC)])));
Exit;
end;
M.Parameters[j].Value := zval2variant(Params[j]^^);
end;
end; // if ht > 0
M.ZendVar.AsZendVariable := return_value; //direct access to zend variable
M.FOnExecute(Obj, M.Parameters, M.ReturnValue, M.FZendVar.AsZendVariable, this_ptr, TSRMLS_DC);
if M.ZendVar.ISNull then //perform variant conversion
variant2zval(M.ReturnValue, return_value);
end;
end;
end;
dispose_pzval_array(Params);
end;
//PHP4
procedure class_get_property_handler(val : pzval; property_reference : PZend_property_reference); cdecl;
var
this_ptr : pzval;
element : pzend_list_element;
prop : pzend_overloaded_element;
obj : TPHPClassInstance;
p : pointer;
propname : string;
tsrmls : pointer;
data: ^ppzval;
param : TClassProperty;
begin
tsrmls := ts_resource_ex(0, nil);
element := property_reference^.elements_list^.head;
p := @element^.data;
prop := pzend_overloaded_element(p);
propname := prop^.element.value.str.val;
this_ptr := property_reference^._object;
new(data);
if ( zend_hash_find(this_ptr^.value.obj.properties, 'instance', strlen('instance') + 1, data) <> SUCCESS) then
Obj := nil
else
Obj := zend_fetch_resource(data^, TSRMLS, -1, 'class resource', nil, 1, le_classresource);
freemem(data);
if Assigned(Obj) then
begin
param := Obj.Properties.ByName(propname);
if Assigned(param) then
ZVAL_STRING(val, PChar(param.value), true)
else
ZVAL_EMPTY_STRING(val)
end
else
ZVAL_STRING(val, 'undefined', true);
end;
//PHP4
procedure _class_get_property_wrapper; assembler;
asm
push ebp
mov ebp,esp
sub esp,50h
push ebx
push esi
push edi
lea edi,[ebp-50h]
mov ecx,14h
mov eax,0CCCCCCCCh
rep stosd
mov eax,dword ptr [ebp+0Ch]
push eax
lea ecx,[ebp-10h]
push ecx
call class_get_property_handler
add esp,8
mov edx,dword ptr [ebp+8]
mov eax,dword ptr [ebp-10h]
mov dword ptr [edx],eax
mov ecx,dword ptr [ebp-0Ch]
mov dword ptr [edx+4],ecx
mov eax,dword ptr [ebp-8]
mov dword ptr [edx+8],eax
mov ecx,dword ptr [ebp-4]
mov dword ptr [edx+0Ch],ecx
mov eax,dword ptr [ebp+8]
pop edi
pop esi
pop ebx
add esp,50h
cmp ebp,esp
mov esp,ebp
pop ebp
ret
end;
{$ENDIF}
{$IFDEF PHP5}
// Read object property value (getter)
//PHP5
function class_get_property_handler(_object : pzval; member : pzval; _type : integer; TSRMLS_DC : pointer) : pzval; cdecl;
var
retval : pzval;
obj : TPHPClassInstance;
data: ^ppzval;
propname : string;
object_properties : PHashTable;
param : TClassProperty;
begin
retval := emalloc(sizeof(zval));
FillChar(retval^, sizeof(zval), 0);
propname := member^.value.str.val;
new(data);
try
object_properties := Z_OBJPROP(_object^);
if zend_hash_find(object_properties, 'instance', strlen('instance') + 1, data) = SUCCESS then
Obj := zend_fetch_resource(data^, TSRMLS_DC, -1, 'class resource', nil, 1, le_classresource)
else
Obj := nil;
finally
freemem(data);
end;
if Assigned(Obj) then
begin
param := Obj.Properties.ByName(propname);
if Assigned(param) then
ZVAL_STRING(retval, PChar(param.value), true)
else
ZVAL_EMPTY_STRING(retval);
end
else
ZVAL_STRING(retval, 'undefined', true);
Result := retval;
end;
// Write object property value (setter)
//PHP5
procedure class_set_property_handler(_object : pzval; member : pzval; value : pzval; TSRMLS_DC : pointer); cdecl;
var
OBJ : TPHPClassInstance;
data: ^ppzval;
propname : string;
object_properties : PHashTable;
param : TClassProperty;
begin
propname := member^.value.str.val;
new(data);
try
object_properties := Z_OBJPROP(_object^);
if zend_hash_find(object_properties, 'instance', strlen('instance') + 1, data) = SUCCESS then
Obj := zend_fetch_resource(data^, TSRMLS_DC, -1, 'class resource', nil, 1, le_classresource)
else
Obj := nil;
finally
freemem(data);
end;
if Assigned(Obj) then
begin
convert_to_string(value);
param := Obj.Properties.ByName(propname);
if Assigned(param) then
param.Value := value.value.str.val;
end;
end;
{$IFDEF PHP510}
function class_call_method(method : pchar; ht : integer; return_value : pzval; return_value_ptr : ppzval;
this_ptr : pzval; return_value_used : integer; TSRMLS_DC : pointer) : integer; cdecl;
{$ELSE}
function class_call_method(method : pchar; ht : integer; return_value : pzval; this_ptr : pzval;
return_value_used : integer; TSRMLS_DC : pointer) : integer; cdecl;
{$ENDIF}
var
OBJ : TPHPClassInstance;
data: ^ppzval;
Params : pzval_array;
Producer : TPHPClass;
M : TPHPClassMethod;
j : integer;
begin
new(data);
if zend_hash_find(this_ptr^.value.obj.handlers.get_properties(this_ptr, TSRMLS_DC), 'instance', strlen('instance') + 1, data) = SUCCESS then
Obj := zend_fetch_resource(data^, TSRMLS_DC, -1, 'class resource', nil, 1, le_classresource)
else
Obj := nil;
freemem(data);
if not Assigned(Obj) then
begin
//not assigned obj = new instance (constructor)
class_call_constructor(Method, this_ptr);
Result := SUCCESS;
exit;
end;
if ht > 0 then
begin
if ( not (zend_get_parameters_ex(ht, Params) = SUCCESS )) then
begin
zend_wrong_param_count(TSRMLS_DC);
Result := FAILURE;
Exit;
end;
end;
Producer := Obj.Producer;
if Assigned(Producer) then
begin
M := Producer.Methods.ByName(Method);
if Assigned(M) then
begin
if Assigned(M.FOnExecute) then
begin
if M.Parameters.Count <> ht then
begin
zend_wrong_param_count(TSRMLS_DC);
Result := FAILURE;
Exit;
end;
if ht > 0 then
begin
for j := 0 to ht - 1 do
begin
if not IsParamTypeCorrect(M.Parameters[j].ParamType, Params[j]^) then
begin
zend_error(E_WARNING, PChar(Format('Wrong parameter type for %s()', [get_active_function_name(TSRMLS_DC)])));
Result := FAILURE;
Exit;
end;
M.Parameters[j].ZendValue := (Params[j]^);
end;
end; // if ht > 0
M.ZendVar.AsZendVariable := return_value; //direct access to zend variable
M.FOnExecute(Obj, M.Parameters, M.ReturnValue, M.FZendVar.AsZendVariable, this_ptr, TSRMLS_DC);
if M.ZendVar.ISNull then //perform variant conversion
variant2zval(M.ReturnValue, return_value);
end;
end;
end;
dispose_pzval_array(Params);
result := SUCCESS;
end;
//PHP5
function class_get_method(_object : pzval; method_name : pchar; method_len : integer; TSRMLS_DC : pointer) : PzendFunction; cdecl;
var
fnc : pZendFunction;
begin
fnc := emalloc(sizeof(TZendFunction));
FillChar(fnc^, sizeOf(TZendFunction), 0);
fnc^.internal_function._type := ZEND_OVERLOADED_FUNCTION;
fnc^.internal_function.function_name := estrndup(method_name, method_len);
fnc^.internal_function.handler := @class_call_method;
result := fnc;
end;
{$IFDEF PHP510}
procedure class_init_new(ht : integer; return_value : pzval; return_value_ptr : ppzval;
this_ptr : pzval; return_value_used : integer; TSRMLS_DC : pointer); cdecl;
{$ELSE}
procedure class_init_new(ht : integer; return_value : pzval; this_ptr : pzval;
return_value_used : integer; TSRMLS_DC : pointer); cdecl;
{$ENDIF}
var
AClassName : PChar;
len : integer;
Extension : TPHPExtension;
idp : pointer;
id : integer;
cnt : integer;
begin
if not Assigned(Application) then
Exit;
AClassName := '';
this_ptr^.value.obj.handlers.get_class_name(this_ptr, @AClassName, @len, 0, TSRMLS_DC);
idp := ts_resource_ex(integer(app_globals_id), nil);
id := integer(idp^);
if id <= 0 then
Exit;
Extension := TPHPExtension(id);
for cnt := 0 to Extension.ComponentCount - 1 do
begin
if (Extension.Components[cnt] is TphpClass) then
begin
if SameText(AClassName, TphpClass(Extension.Components[cnt]).PHPClassName) then
begin
TPHPClass(Extension.Components[cnt]).ProduceInstance(this_ptr);
break;
end;
end;
end;
end;
{$ENDIF}
procedure TPHPClass.ClassRegister(AModuleNumber : integer);
var
tsrmls : pointer;
begin
if not Assigned(Application) then
Exit;
if not Application.Loading then
Exit;
if (csDesigning in ComponentState) then
Exit;
{$IFDEF PHP5}
RegisterClassHandlers;
{$ENDIF}
if le_classresource = 0 then
le_classresource := zend_register_list_destructors_ex(@class_destructor_handler, nil, PChar(le_classresource_name), AModuleNumber);
if (Application.GetPHPClass(FClassName) = nil) then
begin
tsrmls := ts_resource_ex(0, nil);
FillChar(FClassEntry, SizeOf(TZend_Class_Entry), #0);
{$IFDEF PHP5}
FClassFunction[0].fname := PChar(FClassName);
FClassFunction[0].handler := @class_init_new;
INIT_CLASS_ENTRY(FClassEntry, PChar(FClassName) , @FClassFunction);
{$ELSE}
INIT_CLASS_ENTRY(FClassEntry, PChar(FClassName) , nil);
{$ENDIF}
FClassObject := zend_register_internal_class(@FClassEntry, tsrmls);
{$IFDEF PHP4}
FClassObject.handle_property_get := @_class_get_property_wrapper;
FClassObject.handle_property_set := @class_set_property_handler;
FClassObject.handle_function_call := @class_call_function;
{$ENDIF}
Application.RegisterPHPClass(FClassName, FClassObject);
end;
end;
constructor TPHPClass.Create(AOwner: TComponent);
begin
inherited;
if Assigned(Owner) then
begin
if ( not (AOwner is TCustomPHPExtension)) then
raise Exception.Create('TPHPClass component can be placed only on PHPExtension module');
end;
FProperties := TClassProperties.Create(Self);
FMethods := TPHPClassMethods.Create(Self, TPHPClassMethod);
end;
destructor TPHPClass.Destroy;
begin
FProperties.Free;
FMethods.Free;
inherited;
end;
function TPHPClass.GetClassEntry: pzend_class_entry;
begin
Result := Application.GetPHPClass(FClassName);
end;
function TPHPClass.InstanceConstructor(return_value : pzval): TPHPClassInstance;
var
CI : TphpClassInstance;
rn : integer;
tsrmls : pointer;
begin
Result := nil;
if not Assigned(Application) then
Exit;
tsrmls := ts_resource_ex(0, nil);
CI := TPHPClassInstance.Create(nil);
CI.FProperties.Assign(FProperties);
CI.FProducer := Self;
Result := CI;
rn := zend_register_resource(nil, CI, le_classresource);
FClassObject := GetClassEntry;
if not assigned(FClassObject) then
Exit;
{$IFDEF PHP4}
_object_init_ex(return_value, FClassObject, nil, 0, TSRMLS);
{$ELSE}
object_init(return_value, FClassObject, TSRMLS );
{$ENDIF}
{$IFDEF PHP4}
add_property_resource_ex(return_value, 'instance', strlen('instance') +1, rn);
{$ELSE}
add_property_resource_ex(return_value, 'instance', strlen('instance') +1, rn, TSRMLS);
{$ENDIF}
{$IFDEF PHP5}
return_value.value.obj.handlers := @ClassObjectHandlers;
{$ENDIF}
end;
procedure TPHPClass.Loaded;
begin
inherited;
if ( not (csDesigning in ComponentState)) then
begin
if Assigned(Application) then
ClassRegister(Application.ModuleNumber);
end;
end;
procedure TPHPClass.ProduceInstance(AValue: pzval);
var
CI : TphpClassInstance;
rn : integer;
tsrmls : pointer;
begin
if not Assigned(Application) then
Exit;
tsrmls := ts_resource_ex(0, nil);
CI := TPHPClassInstance.Create(nil);
CI.FProperties.Assign(FProperties);
CI.FProducer := Self;
rn := zend_register_resource(nil, CI, le_classresource);
FClassObject := Application.GetPHPClass(FClassName);
if not assigned(FClassObject) then
Exit;
{$IFDEF PHP4}
_object_init_ex(AValue, FClassObject, nil, 0, TSRMLS);
{$ELSE}
object_init(AValue, FClassObject, TSRMLS );
{$ENDIF}
{$IFDEF PHP4}
add_property_resource_ex(AValue, 'instance', strlen('instance') +1, rn);
{$ELSE}
add_property_resource_ex(AValue, 'instance', strlen('instance') +1, rn, TSRMLS);
{$ENDIF}
{$IFDEF PHP5}
AValue.value.obj.handlers := @ClassObjectHandlers;
{$ENDIF}
end;
procedure TPHPClass.SetClassName(const Value: string);
begin
FClassName := LowerCase(Value);
end;
procedure TPHPClass.SetMethods(const Value: TPHPClassmethods);
begin
FMethods.Assign(Value);
end;
procedure TphpClass.SetProperties(const Value: TClassProperties);
begin
FProperties.Assign(Value);
end;
{ TPHPClassInstance }
constructor TPHPClassInstance.Create;
begin
inherited;
FProperties := TClassProperties.Create(Self);
end;
destructor TPHPClassInstance.Destroy;
begin
FProperties.Free;
inherited;
end;
procedure TPHPClassInstance.SetProperties(const Value: TClassProperties);
begin
FProperties.Assign(Value);
end;
{ TClassProperty }
procedure TClassProperty.Assign(Source: TPersistent);
begin
if (Source is TClassProperty) then
begin
FName := TClassProperty(Source).Name;
FValue := TClassProperty(Source).Value;
end
else
inherited;
end;
function TClassProperty.GetAsBoolean: boolean;
begin
if FValue = '' then
begin
Result := false;
Exit;
end;
if SameText(FValue, 'True') then
Result := true
else
Result := false;
end;
function TClassProperty.GetAsFloat: double;
begin
if FValue = '' then
begin
Result := 0;
Exit;
end;
Result := ValueToFloat(FValue);
end;
function TClassProperty.GetAsInteger: integer;
var
c : char;
begin
c := DecimalSeparator;
DecimalSeparator := '.';
Result := Round(ValueToFloat(FValue));
DecimalSeparator := c;
end;
function TClassProperty.GetDisplayName: string;
begin
if FName = '' then
result := inherited GetDisplayName
else
Result := FName;
end;
procedure TClassProperty.SetAsBoolean(const Value: boolean);
begin
if Value then
FValue := 'True'
else
FValue := 'False';
end;
procedure TClassProperty.SetAsFloat(const Value: double);
begin
FValue := FloatToValue(Value);
end;
procedure TClassProperty.SetAsInteger(const Value: integer);
var
c : char;
begin
c := DecimalSeparator;
DecimalSeparator := '.';
FValue := IntToStr(Value);
DecimalSeparator := c;
end;
{ TClassProperties }
function TClassProperties.Add: TClassProperty;
begin
result := TClassProperty(inherited Add);
end;
constructor TClassProperties.Create(AOwner: TComponent);
begin
inherited create(TClassProperty);
FOwner := AOwner;
end;
function TClassProperties.GetItem(Index: Integer): TClassProperty;
begin
Result := TClassProperty(inherited GetItem(Index));
end;
procedure TClassProperties.SetItem(Index: Integer; const Value: TClassProperty);
begin
inherited SetItem(Index, Value)
end;
function TClassProperties.GetOwner : TPersistent;
begin
Result := FOwner;
end;
function TClassProperties.GetVariables: string;
var i : integer;
begin
for i := 0 to Count - 1 do
begin
Result := Result + Items[i].FName + '=' + Items[i].FValue;
if i < Count - 1 then
Result := Result + '&';
end;
end;
function TClassProperties.IndexOf(AName: string): integer;
var
i : integer;
begin
Result := -1;
for i := 0 to Count - 1 do
begin
if SameText(Items[i].Name, AName) then
begin
Result := i;
break;
end;
end;
end;
function TClassProperties.ByName(AName: string): TClassProperty;
var
i : integer;
begin
Result := nil;
for i := 0 to Count - 1 do
begin
if SameText(Items[i].Name, AName) then
begin
Result := Items[i];
break;
end;
end;
end;
{ TPHPClassMethods }
function TPHPClassMethods.Add: TPHPClassMethod;
begin
Result := TPHPClassMethod(inherited Add);
end;
function TPHPClassMethods.ByName(AName: string): TPHPClassMethod;
var
i : integer;
begin
Result := nil;
for i := 0 to Count - 1 do
begin
if SameText(Items[i].Name, AName) then
begin
Result := Items[i];
break;
end;
end;