Repository navigation
Expand file tree
/
Copy pathPHPObject.js
More file actions
142 lines (142 loc) · 5.35 KB
/
Copy pathPHPObject.js
File metadata and controls
142 lines (142 loc) · 5.35 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
import { PHPFatalError } from "./PHPError.js";
import { PHPReference } from "./PHPVariable.js";
import { normalizePhpRegistryName } from "./Reflection.js";
export class PHPClass {
name;
parentClass;
nativeConstructor;
interfaces = [];
traits = [];
constants = Object.create(null);
staticProperties = Object.create(null);
properties = Object.create(null);
methods = Object.create(null);
isAbstract = false;
isFinal = false;
constructor(name, parentClass) {
this.name = name;
if (typeof parentClass === "function") {
const nativeParent = new PHPClass(parentClass.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);
}
}
isSubclassOf(className) {
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 {
phpClass;
properties = Object.create(null);
settingProperties = new Set();
gettingProperties = new Set();
constructor(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;
}
}
async getProperty(ctx, name) {
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;
}
async setProperty(ctx, name, value) {
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;
}
}
async callMethod(ctx, name, args) {
const lowerName = name.toLowerCase();
ctx.currentClassStack.push(this.phpClass);
try {
let cls = 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.get === "function")
res = await res.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 = this;
while (target && target !== Object.prototype) {
for (const propName of Object.getOwnPropertyNames(target)) {
if (propName.toLowerCase() === lowerName && typeof this[propName] === "function") {
let res = await this[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();
}
}
async toString(ctx) {
const __toStringMeta = this.phpClass.methods["__tostring"];
if (__toStringMeta?.fn) {
return String(await __toStringMeta.fn.call(this, ctx));
}
return `Object(${this.phpClass.name})`;
}
}
//# sourceMappingURL=PHPObject.js.map