import { DiCache } from "../di/di-cache.ts" ; import { LOGGING_FILTER , LOGGING_FORMATTER , LOGGING_PROVIDER , PLATFORM , } from "./constants.ts" ; import { ActionMetadata , ControllerClass , ControllerMetadata , } from "./controller-metadata.ts" ; import { DiContainer , Scope , Type , TypeIdentifier } from "./deps.ts" ; import { OnPlatformBootstrap } from "./lifecycle-events.ts" ; import { Logger } from "./logger.ts" ; import { LoggingFilter } from "./logging-filter.ts" ; import { LoggingFormatter } from "./logging-formatter.ts" ; import { LoggingProvider } from "./logging-provider.ts" ; import { ModuleCatalog } from "./module-catalog.ts" ; import { ModuleClass } from "./module-metadata.ts" ; import { ModuleRef } from "./module-ref.ts" ; import { MvFilter } from "./mv-filter.ts" ; import { MvMiddleware } from "./mv-middleware.ts" ; import { MvTransformer } from "./mv-transformer.ts" ; import { ErrorHandler , ServerController } from "./server-controller.ts" ; returns { MomentumPlatform } remarks export function platformMomentum ( ) { return new MomentumPlatform ( DiContainer . root ( ) . createChild ( "platform" ) ) ; } /**
 * The Momentum platform is the entry point for every Momentum application.
 * Each application has exactly one platform and services are bound the scope of the platform.
 */ export abstract class Platform { readonly #diCache = new DiCache ( ) . beginScope ( Scope . Singleton ) ; readonly #container : DiContainer ; #module ? : ModuleRef ; protected logger ! : Logger ; constructor ( container ) { this . #container = container ; } returns { ModuleRef } get module ( ) { this . ensureInitialized ( ) ; return this . #module as ModuleRef ; } returns { DiContainer } get container ( ) { this . ensureInitialized ( ) ; return this . module . diContainer ; } returns { DiCache } get diCache ( ) { return this . #diCache ; } param identifier typeParam returns { Promise < TReturn > } resolve < TReturn = unknown > ( identifier ) { this . ensureInitialized ( ) ; return this . module . resolve < TReturn > ( identifier , this . diCache ) ; } param moduleType remarks async bootstrapModule ( moduleType ) { await this . preBootstrap ( ) ; this . #container . registerValue ( PLATFORM , this ) ; this . #module = await ModuleRef . createModuleRef ( ModuleCatalog . getMetadata ( moduleType ) , this . #container . createChild ( moduleType . name ) , this . #diCache ) ; this . #module . diContainer . preCompileDependencyGraph ( true ) ; this . logger = await this . resolve ( Logger ) ; this . logger . namespace = "Momentum" ; this . logger . loggerName = "Internal" ; this . logger . info ( `Initializing platform` ) ; await this . postBootstrap ( ) ; this . logger . info ( `Completed initializing platform` ) ; return this ; } /**
     * Register a global logging provider.
     */ registerGlobalLoggingProvider ( loggingProvider ) { if ( typeof loggingProvider === "function" ) { this . #container . registerAlias ( loggingProvider , LOGGING_PROVIDER ) ; } else { this . #container . registerValue ( LOGGING_PROVIDER , loggingProvider ) ; } return this ; } /**
     * Register a global logging filter.
     */ registerGlobalLoggingFilter ( loggingFilter ) { if ( typeof loggingFilter === "function" ) { this . #container . registerAlias ( loggingFilter , LOGGING_FILTER ) ; } else { this . #container . registerValue ( LOGGING_FILTER , loggingFilter ) ; } return this ; } /**
     * Register a global logging formatter.
     */ registerGlobalLoggingFormatter ( loggingFormatter ) { if ( typeof loggingFormatter === "function" ) { this . #container . registerAlias ( loggingFormatter , LOGGING_FORMATTER ) ; } else { this . #container . registerValue ( LOGGING_FORMATTER , loggingFormatter ) ; } return this ; } async preBootstrap ( ) { } async postBootstrap ( ) { for ( const item of new Set ( this . #diCache . items ) ) { await ( item as Partial < OnPlatformBootstrap > ) ?. mvOnPlatformBootstrap ?. ( ) ; } } private ensureInitialized ( ) { if ( ! this . #module ) { throw new Error ( "Platform has not been bootstrapped" ) ; } } } /**
 * Momentum platform for server applications such as APIs and web applications.
 */ export abstract class ServerPlatform extends Platform { #serverController : ServerController ; constructor ( container ) { super ( container ) ; this . #serverController = new ServerController ( this ) ; } async postBootstrap ( ) { await this . #serverController . initialize ( this . logger ) ; await super . postBootstrap ( ) ; } abstract addRouteHandler ( controller , action , route , controllerMetadata , actionMetadata , handler ) ; abstract addMiddlewareHandler ( handler ) ; abstract getContextItem ( kind , context , identifier ? ) ; abstract setContextItem ( kind , context , value , identifier ? , options ? ) ; abstract sendFile ( context , path ) ; remarks use ( middleware ) { this . #serverController . registerMiddleware ( middleware ) ; return this ; } remarks registerGlobalFilter ( filter ) { this . #serverController . registerGlobalFilter ( filter ) ; return this ; } /**
     * Register a transformer globally. The transformer can either be an instance
     * of @see MvFilter or a type that implements @see MvFilter which will be resolved by the request scoped resolver.    *
     */ registerGlobalTransformer ( transformer ) { this . #serverController . registerGlobalTransformer ( transformer ) ; return this ; } param errorHandler param priority remarks registerGlobalErrorHandler ( errorHandler , priority ? ) { this . #serverController . registerGlobalErrorHandler ( errorHandler , priority ) ; } } export class MomentumPlatform extends Platform { }