@uisap/core
Version:
A modular Fastify-based framework inspired by Laravel
36 lines (32 loc) • 836 B
JavaScript
// src/container.js
export class Container {
constructor() {
this.bindings = new Map();
this.instances = new Map();
}
// Bir sınıfı bağla
bind(key, resolver) {
this.bindings.set(key, resolver);
}
// Singleton olarak bağla
singleton(key, resolver) {
this.bind(key, () => {
if (!this.instances.has(key)) {
this.instances.set(key, resolver(this));
}
return this.instances.get(key);
});
}
// Bağlantıyı çöz (resolve)
make(key) {
const resolver = this.bindings.get(key);
if (!resolver) {
throw new Error(`No binding found for ${key}`);
}
return resolver(this);
}
// Instance'ı doğrudan kaydet
instance(key, instance) {
this.instances.set(key, instance);
}
}