Documentation

AutoService
in package
implements AutoServiceInterface uses AutoServiceTrait

AbstractYes

AutoService is a base-class for defining a service (in Civi's service-container).

Child classes are scanned automatically for various annotations and interfaces.

= ANNOTATIONS =

Annotations are used to declare and initialize services:

  • Declare a service. This can be done on a class or static factory-method. Supported annotations:
    • @service NAME: Set the name of the new service.
    • @serviceTags TAG-1,TAG-2: Declare additional tags for the service.
    • @internal: Generate a hidden/automatic name. Limit discovery of the service.
  • Initialize the service by calling methods. This works with factory-, constructor-, and setter-methods.
    • @inject SERVICE-1,SERVICE-2: Call the method automatically. Pass a list of other services as parameters.
  • Initialize the service by assigning properties. Any property may have these annotations:
    • @inject SERVICE: Lookup the SERVICE and set the property. If the SERVICE is blank, then it loads an eponymous service.

For examples of using these annotations, consult the tests or the Developer Guide:

Tags
see
AutoDefinitionTest
see
HookInterface
see
SpecProviderInterface
see
EventSubscriberInterface

= REQUIREMENTS / LIMITATIONS =

  • To scan an extension, one must use <mixin>scan-classes@1.0.0</mixin> or hook_scanClasses.
  • AutoServices are part of the container. They cannot be executed until the container has started. Consequently, the services cannot subscribe to some early/boot-time events (eg hook_entityTypes or hook_container).
link

= INTERFACES =

Additionally, some interfaces will be detected automatically. If an AutoService implements any of these interfaces, then it will be registered with appropriate parties:

Table of Contents

Interfaces

AutoServiceInterface
The CiviCRM container will automatically load classes that implement AutoServiceInterface.

        
On this page
  • Table Of Contents

Search results