import {DependencyKey, Instantiable} from './types' import NamedFactory from './factory/NamedFactory' import {AbstractFactory} from './factory/AbstractFactory' import {Factory} from './factory/Factory' import {ClosureFactory} from './factory/ClosureFactory' export class ContainerBlueprint { private static instance?: ContainerBlueprint public static getContainerBlueprint(): ContainerBlueprint { if ( !this.instance ) { this.instance = new ContainerBlueprint() } return this.instance } protected factories: (() => AbstractFactory)[] = [] /** * Register a basic instantiable class as a standard Factory with this container, * identified by a string name rather than static class. * @param {string} name - unique name to identify the factory in the container * @param {Instantiable} dependency */ registerNamed(name: string, dependency: Instantiable): this { this.factories.push(() => new NamedFactory(name, dependency)) return this } /** * Register a basic instantiable class as a standard Factory with this container. * @param {Instantiable} dependency */ register(dependency: Instantiable): this { this.factories.push(() => new Factory(dependency)) return this } /** * Register a producer function as a ClosureFactory with this container. * @param key * @param producer */ registerProducer(key: DependencyKey, producer: () => any): this { this.factories.push(() => new ClosureFactory(key, producer)) return this } resolve(): AbstractFactory[] { return this.factories.map(x => x()) } }