In the section describing the Container for the HLEB2 framework, this documentation already provides a simple example of adding a demo service. Next, we'll look at an example of adding a real library for mutexes as a Service.
The library github.com/phphleb/conductor contains a mutex mechanism. If you plan to use this library, you need to install it first.
It is perfectly possible to assign a key in the container as a class from the library, but this may cause issues later as the application's code will be tied to a specific class or library interface, making it impossible to change it.
It is better to connect external libraries to the project using the Adapter pattern, the class of which will be the key of the service in the container.
This wrapper class for the service is created in the /app/Bootstrap/Services/ folder. Although this is a convenient directory for examples, structurally the Services folder should be located next to the project logic.
Now let's add the library to the container by the created class:
As seen in the example, the rollback() method has been added to reset the state for the connected mutex library that supports asynchrony.
After adding, the new service is available from the container as a singleton through this class.
The method of using the added service in controllers, commands, and events (in all classes inherited from Hleb\Base\Container):
You can simplify the example call to the service by adding a new method with the same name mutex() to the App\Bootstrap\BaseContainer class and its interface:
Now the call will look like this: