@elsikora/cladi
Version:
ClaDI is a library for creating and managing classes in TypeScript.
93 lines (89 loc) • 4.07 kB
JavaScript
;
var container_class = require('../class/base/container.class.js');
var factory_class = require('../class/base/factory.class.js');
var registry_class = require('../class/base/registry.class.js');
var consoleLogger_service = require('../service/console-logger.service.js');
/**
* Factory for creating infrastructure components.
* Provides methods to create instances of Registry, Factory, Container, and Logger.
* @see {@link https://elsikora.com/docs/cladi/core-concepts/factory} for more details on factories.
*/
class CoreFactory {
static instance;
LOGGER;
/**
* Creates a new infrastructure factory.
* @param {ICoreFactoryOptions} options - The options to use for the factory.
* @see {@link https://elsikora.com/docs/cladi/core-concepts/factory}
*/
constructor(options) {
this.LOGGER = options.logger ?? new consoleLogger_service.ConsoleLoggerService();
}
/**
* Gets the singleton instance of the InfrastructureFactory.
* @param {ICoreFactoryOptions} options - The options to use for the factory.
* @returns {CoreFactory} The singleton instance.
* @see {@link https://elsikora.com/docs/cladi/core-concepts/factory}
*/
static getInstance(options) {
if (!CoreFactory.instance) {
CoreFactory.instance = new CoreFactory(options);
}
return CoreFactory.instance;
}
/**
* Creates a new container instance.
* @param {IBaseContainerOptions} options - The options to use for the container.
* @returns {IContainer} A new container instance.
* @see {@link https://elsikora.com/docs/cladi/core-concepts/container}
*/
createContainer(options) {
this.LOGGER?.debug("Creating new container instance", { source: "InfrastructureFactory" });
const container = new container_class.BaseContainer(options);
this.LOGGER?.debug("Container instance created", { source: "InfrastructureFactory" });
return container;
}
/**
* Creates a new factory instance.
* @template T The type of items created by the factory.
* @param {IBaseFactoryOptions<T>} options Factory creation options.
* @returns {IFactory<T>} A new factory instance.
* @see {@link https://elsikora.com/docs/cladi/core-concepts/factory}
*/
createFactory(options) {
this.LOGGER?.debug("Creating new factory instance", { source: "InfrastructureFactory" });
const factory = new factory_class.BaseFactory(options);
this.LOGGER?.debug("Factory instance created", { source: "InfrastructureFactory" });
return factory;
}
/**
* Creates a new logger instance.
* @param {IConsoleLoggerOptions} options - The options to use for the logger.
* @returns {ILogger} A new logger instance.
* @see {@link https://elsikora.com/docs/cladi/services/logging}
*/
createLogger(options) {
this.LOGGER?.debug("Creating new logger instance", {
context: { level: options.level, loggerSource: options.source },
source: "InfrastructureFactory",
});
const logger = new consoleLogger_service.ConsoleLoggerService(options);
this.LOGGER?.debug("Logger instance created", { source: "InfrastructureFactory" });
return logger;
}
/**
* Creates a new registry instance.
* @template T The type of items stored in the registry (must have a name property).
* @param {IBaseRegistryOptions} options - The options to use for the registry.
* @returns {IRegistry<T>} A new registry instance.
* @see {@link https://elsikora.com/docs/cladi/core-concepts/registry}
*/
createRegistry(options) {
this.LOGGER?.debug("Creating new registry instance", { source: "InfrastructureFactory" });
const registry = new registry_class.BaseRegistry(options);
this.LOGGER?.debug("Registry instance created", { source: "InfrastructureFactory" });
return registry;
}
}
exports.CoreFactory = CoreFactory;
//# sourceMappingURL=core.factory.js.map