@xrystal/core 3.13.5 → 3.13.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json
CHANGED
|
@@ -2,6 +2,7 @@ import { createContainer, asClass, asValue, InjectionMode, listModules } from 'a
|
|
|
2
2
|
import path from 'node:path';
|
|
3
3
|
export class X {
|
|
4
4
|
container;
|
|
5
|
+
initializedNames = new Set();
|
|
5
6
|
constructor() {
|
|
6
7
|
this.container = createContainer({
|
|
7
8
|
injectionMode: InjectionMode.PROXY,
|
|
@@ -9,21 +10,39 @@ export class X {
|
|
|
9
10
|
});
|
|
10
11
|
}
|
|
11
12
|
load(patterns, verbose = false) {
|
|
12
|
-
if (!patterns
|
|
13
|
+
if (!patterns) {
|
|
14
|
+
if (verbose)
|
|
15
|
+
console.warn('[DI] load() çağrıldı ama patterns undefined.');
|
|
13
16
|
return this;
|
|
17
|
+
}
|
|
14
18
|
const cwd = process.cwd();
|
|
15
|
-
|
|
16
|
-
.
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
19
|
+
if (!cwd) {
|
|
20
|
+
console.error('[DI] Critical: process.cwd() undefined döndü!');
|
|
21
|
+
return this;
|
|
22
|
+
}
|
|
23
|
+
const input = Array.isArray(patterns) ? patterns : [patterns];
|
|
24
|
+
const resolvedPatterns = [];
|
|
25
|
+
for (const p of input) {
|
|
26
|
+
if (typeof p !== 'string') {
|
|
27
|
+
console.error(`[DI] Critical: load() metoduna geçersiz path gönderildi. Beklenen: string, Gelen: ${typeof p}`, p);
|
|
28
|
+
continue;
|
|
29
|
+
}
|
|
30
|
+
try {
|
|
31
|
+
const resolved = path.isAbsolute(p) ? p : path.resolve(cwd, p);
|
|
32
|
+
resolvedPatterns.push(resolved.replace(/\\/g, '/'));
|
|
33
|
+
}
|
|
34
|
+
catch (err) {
|
|
35
|
+
console.error(`[DI] Path resolve hatası (${p}):`, err.message);
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
if (resolvedPatterns.length === 0)
|
|
39
|
+
return this;
|
|
21
40
|
let modules = [];
|
|
22
41
|
try {
|
|
23
42
|
modules = listModules(resolvedPatterns);
|
|
24
43
|
}
|
|
25
44
|
catch (err) {
|
|
26
|
-
console.error(`[DI] Critical:
|
|
45
|
+
console.error(`[DI] Critical: listModules patladı. Patternlar:`, resolvedPatterns, err.message);
|
|
27
46
|
return this;
|
|
28
47
|
}
|
|
29
48
|
for (const m of modules) {
|
|
@@ -47,14 +66,16 @@ export class X {
|
|
|
47
66
|
}
|
|
48
67
|
}
|
|
49
68
|
catch (err) {
|
|
50
|
-
console.error(`[DI] Critical:
|
|
69
|
+
console.error(`[DI] Critical: Modül yüklenemedi (${m.path}):`, err.message);
|
|
51
70
|
}
|
|
52
71
|
}
|
|
53
72
|
return this;
|
|
54
73
|
}
|
|
55
74
|
register(Dependency) {
|
|
56
|
-
if (!Dependency
|
|
75
|
+
if (!Dependency || !Dependency.name) {
|
|
76
|
+
console.error('[DI] register() başarısız: Geçersiz sınıf.', Dependency);
|
|
57
77
|
return this;
|
|
78
|
+
}
|
|
58
79
|
const name = Dependency.name.charAt(0).toLowerCase() + Dependency.name.slice(1);
|
|
59
80
|
if (this.isRegistered(name))
|
|
60
81
|
return this;
|
|
@@ -68,27 +89,32 @@ export class X {
|
|
|
68
89
|
return this;
|
|
69
90
|
}
|
|
70
91
|
registerInstance(name, instance) {
|
|
71
|
-
if (!name
|
|
92
|
+
if (!name) {
|
|
93
|
+
console.error('[DI] registerInstance() başarısız: İsim boş.');
|
|
72
94
|
return this;
|
|
95
|
+
}
|
|
73
96
|
const formattedName = name.charAt(0).toLowerCase() + name.slice(1);
|
|
97
|
+
if (this.isRegistered(formattedName))
|
|
98
|
+
return this;
|
|
74
99
|
this.container.register({ [formattedName]: asValue(instance) });
|
|
75
100
|
return this;
|
|
76
101
|
}
|
|
77
102
|
async initialize(input, verbose = false) {
|
|
78
103
|
const cradle = this.container.cradle;
|
|
79
104
|
const inputList = input ? (Array.isArray(input) ? input : [input]) : [];
|
|
80
|
-
const initializedNames = new Set();
|
|
81
105
|
for (const item of inputList) {
|
|
82
|
-
if (!item
|
|
106
|
+
if (!item || !item.service)
|
|
83
107
|
continue;
|
|
84
108
|
const name = typeof item.service === 'function'
|
|
85
109
|
? item.service.name.charAt(0).toLowerCase() + item.service.name.slice(1)
|
|
86
110
|
: item.service;
|
|
111
|
+
if (!name || this.initializedNames.has(name))
|
|
112
|
+
continue;
|
|
87
113
|
const instance = cradle[name];
|
|
88
114
|
if (instance && typeof instance.load === 'function') {
|
|
89
115
|
try {
|
|
90
116
|
await instance.load(item.props || {});
|
|
91
|
-
initializedNames.add(name);
|
|
117
|
+
this.initializedNames.add(name);
|
|
92
118
|
if (verbose)
|
|
93
119
|
console.log(`[DI] Initialized (Priority): ${name}`);
|
|
94
120
|
}
|
|
@@ -98,13 +124,13 @@ export class X {
|
|
|
98
124
|
}
|
|
99
125
|
}
|
|
100
126
|
for (const key of Object.keys(this.container.registrations)) {
|
|
101
|
-
if (initializedNames.has(key))
|
|
127
|
+
if (this.initializedNames.has(key))
|
|
102
128
|
continue;
|
|
103
129
|
const instance = cradle[key];
|
|
104
130
|
if (instance && typeof instance.load === 'function') {
|
|
105
131
|
try {
|
|
106
132
|
await instance.load({});
|
|
107
|
-
initializedNames.add(key);
|
|
133
|
+
this.initializedNames.add(key);
|
|
108
134
|
if (verbose)
|
|
109
135
|
console.log(`[DI] Initialized (Auto): ${key}`);
|
|
110
136
|
}
|
|
@@ -117,13 +143,15 @@ export class X {
|
|
|
117
143
|
}
|
|
118
144
|
get(target) {
|
|
119
145
|
if (!target)
|
|
120
|
-
throw new Error('[DI]
|
|
146
|
+
throw new Error('[DI] get() çağrıldı ama hedef (target) undefined.');
|
|
121
147
|
const resolveName = typeof target === 'function'
|
|
122
148
|
? target.name.charAt(0).toLowerCase() + target.name.slice(1)
|
|
123
149
|
: target;
|
|
124
150
|
return this.container.resolve(resolveName);
|
|
125
151
|
}
|
|
126
152
|
get cradle() { return this.container.cradle; }
|
|
127
|
-
isRegistered(name) {
|
|
153
|
+
isRegistered(name) {
|
|
154
|
+
return !!this.container.registrations[name] && this.container.registrations[name].resolve !== undefined;
|
|
155
|
+
}
|
|
128
156
|
}
|
|
129
157
|
export default new X();
|