The responsibility of the Factory is to orchestrate the creation of different classes, and its dependencies (through DependencyProvider or Config).
- The Factory creates the classes of your module and its dependencies
- The Factory is accessible to the Facade (with
Creating your objects
The Factory is the place where you create your domain services and objects. It is accessible only by the Facade.
Full code snippet: gacela-example/comment-spam-score/factory
Auto-wiring dependencies into the Factory
Gacela can resolve automatically the dependencies for the Factory. If the dependency is a concrete class it will create a new instance of it - recursively with their inner dependencies as well. But, if the dependency is an interface, then the way to tell Gacela which instance do you want to create you need to create a map between the interface and the concrete class or object that you want to use. For example:
The major difference between these two are:
InterfaceToConcreteClasswill be resolved by creating an instance of that
ConcreteClasson the fly (even using auto-wiring for its dependencies recursively if needed).
InterfaceToCallablewon't create a new instance, but instead it will use the instance that you might want to.
- using a callable as value (the
fn () => ...) is also a "lazy loading", so it will delay the execution of that code till its needed.
Real example: symfony-gacela-example/gacela.php
Injecting external services to Gacela config
You can let know Gacela the external services that you want to have access in your
gacela.php config file
by passing them in the entry point of your app:
You now have access to the external services (in this case the symfony kernel), so you can map the
EntityManagerInterface to the one that the
symfony.kernel.container itself already created:
To see the complete example, please check out this repository.