Skip to content

dependency injection

Loïc Frering edited this page Oct 8, 2010 · 1 revision

Dependency Injection

Configuration

In order to use losolib's integration of the Symfony Dependecy Injection Container into your Zend Framework application, you have to have the following to your configs/application.ini:

bootstrap.path = APPLICATION_PATH "/Bootstrap.php"
bootstrap.class = "Bootstrap"
bootstrap.container.type = "symfony"

Then you can load services in Symfony DI Container in two ways:

  • by declaring configuration files
  • by declaring a path where classes will be scanned and load by the annotation loader

Configuration files

You can define your services through the following configuration file's formats supported by Symfony DI Container:

  • XML
  • YAML
  • INI

Please refer to the corresponding documentation from Symfony to learn how to use each format.

You can load as many file as you want in the container, losolib will autodetect the file extension to use the appropriate loader. Use the configFiles array to declare files:

bootstrap.container.symfony.configFiles[] = APPLICATION_PATH "/configs/services.yml"
bootstrap.container.symfony.configFiles[] = APPLICATION_PATH "/configs/authentication.yml"

Do not forget that you can declare configuration files specific to an environment in the corresponding section of your application.ini.

Annotations

While declaring services by configuration files is supported out of the box by Symfony DI Container, losolib comes with an additional loader: AnnotationLoader. This loader allows to declare services in a more intuitive manner by directly annotating classes that will be managed by the container and configure them thanks to other annotations. Configuration of the services and their code are now located in the same place!

To let losolib scan your classes, you have to declare the paths where they are located thanks to the configPaths array:

bootstrap.container.symfony.configPaths[] = APPLICATION_PATH "/services"
bootstrap.container.symfony.configPaths[] = APPLICATION_PATH "/repositories"

The declared paths will be recursively scanned and you can also declare paths specific to your environment in the corresponding section of your application.ini file.

Caching

For a production environment, you certainly do not want all the configuration files and paths to be loaded at each request in the container. For that reason, losolib comes with a configurable caching capability that will, if activated, dump your container, once loaded after the first request, to a plain PHP cached file. Then you will have a fully configured container immediately available on the following requests.

You just have to set cache option to true and declare, if you want, the file where the container will be cached in the right section, depending on the environments where you want the cache to be activated, of your application.ini file:

[production]
bootstrap.container.symfony.cache = true
bootstrap.container.symfony.cacheFile = APPLICATION_PATH "/../data/cache/symfony/ApplicationServiceContainer.php"