@rnaga/wp-node
Version:
👉 **[View Full Documentation at rnaga.github.io/wp-node →](https://rnaga.github.io/wp-node/)**
133 lines (132 loc) • 5.3 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
require("reflect-metadata");
const context_1 = require("./core/context");
const database_1 = __importDefault(require("./database"));
const hooks_1 = require("./core/hooks/hooks");
const default_hooks_1 = require("./core/hooks/default-hooks");
/**
* The Application class serves as a central helper for managing the lifecycle of the application.
* It provides methods to initialize and retrieve application contexts, register hooks, and manage configurations.
*
* Key Responsibilities:
* - **Context Initialization**: The `getContext` method initializes and retrieves the application context for a given environment.
* It ensures that hooks and configurations are properly set up and passed to the context.
* - **Hook Registration**: The `registerHooks` method allows registering custom hooks for specific environments.
* Hooks are used to extend or modify the application's behavior dynamically.
* - **Configuration Management**: The class holds global and environment-specific configurations, which are passed to the context during initialization.
*
* Usage:
* - Use `Application.getContext(env)` to retrieve the application context for a specific environment.
* - Use `Application.registerHooks(clazzes, env)` to register hooks for an environment.
* - Access `Application.configs` or `Application.config` to manage configurations.
*
* This class is designed to be used as a static utility and cannot be instantiated directly.
*/
class Application {
/**
* A flag to indicate if the application is currently installing.
*/
static installing = false;
/**
* Stores the global configurations accessible across different parts of the application.
*/
static configs;
/**
* A singular configuration object if only one environment configuration is needed.
*/
static config;
/**
* A map storing instances of Hooks by environment keys.
*/
static hooksInstanceMap = new Map();
/**
* Private constructor to prevent instantiation.
*/
constructor() { }
/**
* Returns the map of hooks instances.
*/
static get hooks() {
return Application.hooksInstanceMap;
}
/**
* Registers hooks for a given environment.
*
* @param clazzes - Array of hook class constructors to be registered.
* @param env - Optional environment name, defaults to "default".
*/
static registerHooks(clazzes, env = "default") {
const hookMap = hooks_1.Hooks.get(env);
let clazz;
for (clazz of clazzes) {
// __name is set by @hook decorator
if (!clazz.__name) {
throw new Error("Hook name is not defined. Use @hook decorator to define a unique name for the hook.");
}
hookMap.set(clazz.__name, clazz);
}
hooks_1.Hooks.set(env, hookMap);
}
/**
* Retrieves the application context for a given environment,
* initializing hooks and configurations.
*
* This method initializes the hooks based on the environment
* and hooks registered via the `registerHooks` method.
*
* It ensures the application configuration is valid
* and throws errors if not properly set.
*
* @param env - The environment identifier for which to get the context, defaults to "default".
* @returns Promise<Context> - A promise that resolves to the instance
* of Context configured with appropriate hooks and settings.
* @throws Error - If the configuration is invalid or undefined.
*/
static async getContext(env = "default") {
// Load and add default hooks
default_hooks_1.DefaultHooks.load(env);
if (!Application.config && !Application.configs[env]) {
throw new Error("Invalid Config");
}
let config = undefined;
if (Application.configs && Application.configs[env]) {
config = Application.configs[env];
}
else if (Application.config) {
config = Application.config;
}
if (!config) {
throw new Error("Empty config");
}
let hooks = Application.hooksInstanceMap.get(env);
if (!hooks) {
// Merge default hooks to non-default hooks \}
if (env !== "default") {
const hookMap = hooks_1.Hooks.get(env);
const defaultHookSet = hooks_1.Hooks.get("default");
if (defaultHookSet) {
defaultHookSet.forEach((hook) => hookMap.set(hook.__name, hook));
}
}
hooks = new hooks_1.Hooks(hooks_1.Hooks.get(env));
Application.hooksInstanceMap.set(env, hooks);
}
hooks.init();
return await context_1.Context.getInstance(config, {
env,
hooks,
installing: Application.installing,
});
}
/**
* Closes all database connections gracefully.
*/
static terminate() {
database_1.default.closeAll();
}
}
exports.default = Application;