1.3.1 • Published 1 year ago

alliage-service-loader v1.3.1

Weekly downloads
4
License
GNU
Repository
github
Last release
1 year ago

Alliage Service Loader

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 the paths.

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';
TypeEvent objectDescription
SERVICE_LOADER_EVENTS.BEFORE_ALLServiceLoaderBeforeAllEventBefore loading all services
SERVICE_LOADER_EVENTS.BEFORE_ONEServiceLoaderBeforeOneEventBefore loading one service
SERVICE_LOADER_EVENTS.AFTER_ONEServiceLoaderAfterOneEventAfter having loaded on service
SERVICE_LOADER_EVENTS.AFTER_ALLServiceLoaderAfterAllEventAfter 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 file
  • getPaths(): string[]: Returns the paths as configured in the configuration file
  • getExclude(): string[]: Returns the exclusions as configured in the configuration file
  • setPaths(paths: string[]): ServiceLoaderBeforeAllEvent: Allows to re-define the paths
  • setExclude(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 loaded
  • getName(): string: Returns the unique name of the service about to be loaded
  • getConstructor(): string: Returns the constructor of the service about to be loaded
  • getDependencies(): Dependency[]: Returns the list of dependencies of the service about to be loaded
  • setConstructor(constructor: any): ServiceLoaderBeforeOneEvent: Allows to re-define the constructor of the service about to be loaded
  • setDependencies(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 service
  • getName(): string: Returns the unique name of the loaded service
  • getConstructor(): string: Returns the constructor of the loaded service
  • getDependencies(): 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 file
  • getPaths(): string[]: Returns the paths as configured in the configuration file
  • getExclude(): string[]: Returns the exclusions as configured in the configuration file
1.3.1-beta.0

1 year ago

1.3.0-beta.2

1 year ago

1.3.0-beta.5

1 year ago

1.3.1

1 year ago

1.3.0

1 year ago

1.2.0-pre1

3 years ago

1.1.1

4 years ago

1.1.0

4 years ago

1.0.0

4 years ago

0.0.1

4 years ago