Repository navigation
Expand file tree
/
Copy pathPHPObject.ts
More file actions
171 lines (159 loc) · 5.91 KB
/
Copy pathPHPObject.ts
File metadata and controls
171 lines (159 loc) · 5.91 KB
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
import type { PHPContext } from "../PHPContext.js";
import { PHPFatalError } from "./PHPError.js";
import { PHPReference } from "./PHPVariable.js";
import { normalizePhpRegistryName } from "./Reflection.js";
export interface PHPParameterMetadata {
name: string;
position: number;
isOptional: boolean;
hasDefault: boolean;
defaultValue?: any;
type?: string;
byref?: boolean;
}
export interface PHPPropertyMetadata {
name: string;
visibility: "public" | "protected" | "private";
isStatic: boolean;
isReadOnly: boolean;
defaultValue?: any;
}
export interface PHPMethodMetadata {
name: string;
visibility: "public" | "protected" | "private";
isStatic: boolean;
isAbstract: boolean;
isFinal: boolean;
numberOfParameters: number;
numberOfRequiredParameters: number;
parameters: PHPParameterMetadata[];
fn: Function;
}
export class PHPClass {
public readonly name: string;
public readonly parentClass?: PHPClass;
public nativeConstructor?: Function;
public readonly interfaces: PHPClass[] = [];
public readonly traits: any[] = [];
public constants: Record<string, any> = Object.create(null);
public staticProperties: Record<string, any> = Object.create(null);
public properties: Record<string, PHPPropertyMetadata> = Object.create(null);
public methods: Record<string, PHPMethodMetadata> = Object.create(null);
public isAbstract: boolean = false;
public isFinal: boolean = false;
constructor(name: string, parentClass?: PHPClass | Function) {
this.name = name;
if (typeof parentClass === "function") {
const nativeParent = new PHPClass((parentClass as any).phpName || parentClass.name);
nativeParent.nativeConstructor = parentClass;
parentClass = nativeParent;
}
this.parentClass = parentClass;
if (parentClass instanceof PHPClass) {
this.nativeConstructor = parentClass.nativeConstructor;
this.properties = Object.assign(Object.create(null), parentClass.properties);
this.methods = Object.assign(Object.create(null), parentClass.methods);
this.constants = Object.assign(Object.create(null), parentClass.constants);
}
}
public isSubclassOf(className: string): boolean {
if (normalizePhpRegistryName(this.name) === normalizePhpRegistryName(className)) return true;
if (this.parentClass && this.parentClass.isSubclassOf(className)) return true;
return this.interfaces.some((iface) => iface.isSubclassOf(className));
}
}
export class PHPObject {
public readonly phpClass: PHPClass;
public properties: Record<string, any> = Object.create(null);
private settingProperties: Set<string> = new Set();
private gettingProperties: Set<string> = new Set();
constructor(phpClass: PHPClass) {
this.phpClass = phpClass;
for (const [name, metadata] of Object.entries(phpClass.properties)) {
if (metadata.isStatic) continue;
const value = metadata.defaultValue;
this.properties[name] = Array.isArray(value) ? [...value] : value && typeof value === "object" ? { ...value } : value;
}
}
public async getProperty(ctx: PHPContext, name: string): Promise<any> {
if (!(this.gettingProperties instanceof Set)) {
this.gettingProperties = new Set();
}
if (Object.hasOwn(this.properties, name)) {
return this.properties[name];
}
const __getMeta = this.phpClass.methods["__get"];
if (__getMeta?.fn && !this.gettingProperties.has(name)) {
this.gettingProperties.add(name);
try {
return await __getMeta.fn.call(this, ctx, name);
} finally {
this.gettingProperties.delete(name);
}
}
return undefined;
}
public async setProperty(ctx: PHPContext, name: string, value: any): Promise<void> {
if (!(this.settingProperties instanceof Set)) {
this.settingProperties = new Set();
}
const __setMeta = this.phpClass.methods["__set"];
if (__setMeta?.fn && !this.settingProperties.has(name)) {
this.settingProperties.add(name);
try {
await __setMeta.fn.call(this, ctx, name, value);
} finally {
this.settingProperties.delete(name);
}
} else {
this.properties[name] = value;
}
}
public async callMethod(ctx: PHPContext, name: string, args: any[]): Promise<any> {
const lowerName = name.toLowerCase();
ctx.currentClassStack.push(this.phpClass);
try {
let cls: PHPClass | undefined = this.phpClass;
while (cls) {
const methodMeta = cls.methods[lowerName];
if (methodMeta?.fn) {
let res = await methodMeta.fn.apply(this, [ctx, ...args]);
if (res && typeof res === "object" && typeof (res as any).get === "function") res = await (res as any).get();
return res;
}
cls = cls.parentClass;
}
cls = this.phpClass;
while (cls) {
const __callMeta = cls.methods["__call"];
if (__callMeta?.fn) {
let res = await __callMeta.fn.call(this, ctx, name, args);
if (res instanceof PHPReference) res = await res.get();
return res;
}
cls = cls.parentClass;
}
let target: any = this;
while (target && target !== Object.prototype) {
for (const propName of Object.getOwnPropertyNames(target)) {
if (propName.toLowerCase() === lowerName && typeof (this as any)[propName] === "function") {
let res = await (this as any)[propName].apply(this, args);
if (res instanceof PHPReference) res = await res.get();
return res;
}
}
target = Object.getPrototypeOf(target);
}
throw new PHPFatalError(`Call to undefined method ${this.phpClass.name}::${name}()`);
} finally {
ctx.currentClassStack.pop();
}
}
public async toString(ctx: PHPContext): Promise<string> {
const __toStringMeta = this.phpClass.methods["__tostring"];
if (__toStringMeta?.fn) {
return String(await __toStringMeta.fn.call(this, ctx));
}
return `Object(${this.phpClass.name})`;
}
}