alliage-service-loader v1.3.1
Alliage Service Loader
Automatic import and registration of services.
Dependencies
Installation
yarn add alliage-service-loader
With npm
npm install alliage-service-loader
Registration
If you have already installed alliage-module-installer you just have to run the following command:
$(npm bin)/alliage-scripts install alliage-service-loader
Otherwise, update your alliage-modules.json
file to add this at the bottom:
{
// ... other modules
"alliage-service-loader": {
"module": "alliage-service-loader",
"deps": [
"alliage-lifecycle",
"alliage-di",
"alliage-module-installer",
"alliage-config-loader"
],
"envs": [],
}
}
Usage
The goal of this module is to allow us to import and register services without having to create an alliage module.
Configuration
Once installed, a new file located in config/services.yaml
should be available that should look like this.
basePath: 'src'
paths: ['services/**/*']
exclude: ['services/**/NotAService.*']
- The
basePath
parameters just tells the module where is located the source code of our application. - The
paths
parameters will define which files must be considered as services and thus must be automatically imported and registered. - The
exclude
parameter will allow to exclude files that could match one of thepaths
.
Define a service
To define a service, the module must follow the following rules:
- Its path must match with what's configured in the
config/services.yaml
file - It must export the service as
default
- It must declare the service using the
Service
decorator
Like in the following example:
import { Service } from 'alliage-service-loader/decorators';
import { service, parameter } from 'alliage-di/dependencies';
class MyService {
constructor(otherService, dummyParameter) {
super();
this.otherService = otherService;
this.dummyParameter = dummyParameter;
}
// ...
}
export default Service('my_service', [
service('other_service'),
parameter('parameters.dummy_parameter'),
])(MyService);
The Service
decorator takes following parameters:
- The service's name (which must be unique)
- The list of dependencies
Once done, the service will be automatically loaded !
Events
Service loader events
import { SERVICE_LOADER_EVENTS } from 'alliage-service-loader/events';
Type | Event object | Description |
---|---|---|
SERVICE_LOADER_EVENTS.BEFORE_ALL | ServiceLoaderBeforeAllEvent | Before loading all services |
SERVICE_LOADER_EVENTS.BEFORE_ONE | ServiceLoaderBeforeOneEvent | Before loading one service |
SERVICE_LOADER_EVENTS.AFTER_ONE | ServiceLoaderAfterOneEvent | After having loaded on service |
SERVICE_LOADER_EVENTS.AFTER_ALL | ServiceLoaderAfterAllEvent | After having loaded all services |
ServiceLoaderBeforeAllEvent
This is the instance of the event object received in any SERVICE_LOADER_EVENTS.BEFORE_ALL
listener.
getBasePath(): string
: Returns the base path as configured in the configuration filegetPaths(): string[]
: Returns the paths as configured in the configuration filegetExclude(): string[]
: Returns the exclusions as configured in the configuration filesetPaths(paths: string[]): ServiceLoaderBeforeAllEvent
: Allows to re-define the pathssetExclude(exclude: string[]): ServiceLoaderBeforeAllEvent
: Allows to re-define the exclusions
ServiceLoaderBeforeOneEvent
This is the instance of the event object received in any SERVICE_LOADER_EVENTS.BEFORE_ONE
listener.
getModulePath(): string
: Returns the path of the service about to be loadedgetName(): string
: Returns the unique name of the service about to be loadedgetConstructor(): string
: Returns the constructor of the service about to be loadedgetDependencies(): Dependency[]
: Returns the list of dependencies of the service about to be loadedsetConstructor(constructor: any): ServiceLoaderBeforeOneEvent
: Allows to re-define the constructor of the service about to be loadedsetDependencies(dependencies: Dependency[]): ServiceLoaderBeforeOneEvent
: Allows to re-define the dependencies of the service about to be loaded
ServiceLoaderAfterOneEvent
This is the instance of the event object received in any SERVICE_LOADER_EVENTS.AFTER_ONE
listener.
getModulePath(): string
: Returns the path of the loaded servicegetName(): string
: Returns the unique name of the loaded servicegetConstructor(): string
: Returns the constructor of the loaded servicegetDependencies(): Dependency[]
: Returns the list of dependencies of the loaded service
ServiceLoaderAfterAllEvent
This is the instance of the event object received in any SERVICE_LOADER_EVENTS.AFTER_ALL
listener.
getBasePath(): string
: Returns the base path as configured in the configuration filegetPaths(): string[]
: Returns the paths as configured in the configuration filegetExclude(): string[]
: Returns the exclusions as configured in the configuration file
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
3 years ago
4 years ago
4 years ago
4 years ago
4 years ago