@aedart/support
Version:
The Ion support package
263 lines (258 loc) • 6.11 kB
TypeScript
/**
* @aedart/support
*
* BSD-3-Clause, Copyright (c) 2023-present Alin Eugen Deac <aedart@gmail.com>.
*/
import { Container as Container$1, Identifier } from '@aedart/contracts/container';
import { SpyFactoryCallback } from '@aedart/contracts/support/facades';
/**
* Facade
*
* Adaptation of Laravel's Facade abstraction.
*
* @see https://github.com/laravel/framework/blob/master/src/Illuminate/Support/Facades/Facade.php
*
* @abstract
*/
declare abstract class Facade {
/**
* The facade's service container instance
*
* @type {Container|undefined}
*
* @protected
* @static
*/
protected static container: Container$1 | undefined;
/**
* The "type" of the resolved object instance.
*
* **Note**: _This property is not used for anything other
* than to provide a TypeScript return type for the `obtain()`
* method._
*
* @protected
* @static
*/
protected static type: any;
/**
* Resolved instances
*
* @type {Map<Identifier, any>}
*
* @protected
* @static
*/
protected static resolved: Map<Identifier, any>;
/**
* Registered object spies
*
* @type {Set<Identifier>}
*
* @protected
* @static
*/
protected static spies: Set<Identifier>;
/**
* Facade Constructor
*
* @protected
*/
protected constructor();
/**
* Returns identifier to be used for resolving facade's underlying object instance
*
* @return {Identifier}
*
* @abstract
*
* @static
*/
static getIdentifier(): Identifier;
/**
* Obtain the underlying object instance, or a "spy" (for testing)
*
* @see spy
*
* @return {any}
*
* @throws {NotFoundException}
* @throws {ContainerException}
* @throws {LogicalError}
*
* @abstract
*
* @static
*/
static obtain(): any;
/**
* Register a "spy" (e.g. object mock) for this facade's identifier
*
* @template T = any
*
* @param {SpyFactoryCallback} callback Callback to be used for creating some kind of object spy
* or mock, with appropriate configuration and expectations for testing
* purposes.
*
* @return {T} Object instance (spy / object mock) to be registered in service container.
*
* @static
*/
static spy<T = any>(callback: SpyFactoryCallback<T>): T;
/**
* Determine if a spy has been registered for this facade's identifier
*
* @return {boolean}
*
* @static
*/
static isSpy(): boolean;
/**
* Removes registered spy for this facade's identifier
*
* **Warning**: _Method does NOT perform any actual "spy" cleanup logic.
* It only removes the reference to the "spy" object._
*
* @return {boolean}
*
* @static
*/
static forgetSpy(): boolean;
/**
* Removes all registered spies
*
* @return {void}
*
* @static
*/
static forgetAllSpies(): void;
/**
* Swap the facade's underlying instance
*
* @param {any} instance
*
* @return {void}
*
* @static
*/
static swap(instance: any): void;
/**
* Set this facade's service container instance
*
* @param {Container | undefined} container
*
* @return {this}
*
* @static
*/
static setContainer(container: Container$1 | undefined): typeof Facade;
/**
* Get this facade's service container instance
*
* @return {Container | undefined}
*
* @static
*/
static getContainer(): Container$1 | undefined;
/**
* Determine if facade's has a service container instance set
*
* @return {boolean}
*
* @static
*/
static hasContainer(): boolean;
/**
* Removes this facade's service container instance
*
* @return {this}
*
* @static
*/
static forgetContainer(): typeof Facade;
/**
* Determine if resolved facade instance exists
*
* @param {Identifier} identifier
*
* @reurn {boolean}
*
* @static
*/
static hasResolved(identifier: Identifier): boolean;
/**
* Forget resolved facade instance
*
* @param {Identifier} identifier
*
* @return {boolean}
*
* @static
*/
static forgetResolved(identifier: Identifier): boolean;
/**
* Forget all resolved facade instances
*
* @return {void}
*
* @static
*/
static forgetAllResolved(): void;
/**
* Clears all resolved instances, service container and evt. spies.
*
* @return {void}
*/
static destroy(): void;
/**
* Resolves the facade's underlying object instance from the service container,
* which matches given identifier.
*
* **Note**: _If a "spy" has been registered for given identifier, then that spy
* object is returned instead._
*
* @template T = any
*
* @param {Identifier} identifier
*
* @return {T}
*
* @throws {NotFoundException}
* @throws {ContainerException}
* @throws {LogicalError}
*
* @protected
*
* @static
*/
protected static resolve<T = any>(identifier: Identifier): T;
}
/**
* Container Facade
*/
declare class Container extends Facade {
/**
* The "type" of the resolved object instance.
*
* **Note**: _This property is not used for anything other
* than to provide a TypeScript return type for the `obtain()`
* method._
*
* @type {import('@aedart/contracts/container').Container}
*
* @protected
* @static
*/
protected static type: Container$1;
/**
* Returns identifier to be used for resolving facade's underlying object instance
*
* @return {Identifier}
*
* @abstract
*
* @static
*/
static getIdentifier(): Identifier;
}
export { Container, Facade };