Announcement

Collapse
No announcement yet.

Global Object with registered handlers

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Global Object with registered handlers

    I need to create an service that different parts of the system can register handlers with, but I'm unsure of the approved model to do this.

    I could probably create a global variable somewhere and then have my components register against that, but it feels a bit hacky.

    Something along the lines of:

    Module A creates an Object with an array of handlers

    Module B registers 3 handlers with the object from Module A

    Module A does some processing based on the handlers registered by Module B


    How is this best done? Perhaps adding something to metadata or something?

    Or perhaps binding? (careful explaining if this is the direction as I've read the docs and barely understand it!!)

    Thanks

  • #2
    OK I think I have an approach.
    a) In each module create app metadata for the required handlers:

    Espo\Modules\ModuleA\Resources\metadata\app\module A.json
    {
    "myhandlers": ["__APPEND__",{"handler":"handlerA}]

    }
    Espo\Modules\ModuleB\Resources\metadata\app\module A.json
    {
    "myhandlers": ["__APPEND__",{"handler":"handlerB}]

    }​

    This adds new metadata to the app which can be accessed by

    $metadata->get(['app','moduleA','myhandlers']); which returns an array of the handlers. Use __APPEND__ so that the meta data is added rather than replaced

    b) Create a container service in Module A and use the metadata to do whatever needs doing.

    Comment


    • #3
      https://docs.espocrm.com/development...ainer-services.

      1. Register a container service.
      2. Bind a class name (or interface) to that service.

      Then just require a bound class name (or interface) in your classes.

      Comment

      Working...
      X