Zend Framework module to leverage the symfony dependency injection container
- Install the module via composer by running:
composer require adlogix/zf-symfony-container:^0.4
- Add the
Adlogix\ZfSymfonyContainer
module to the module section of yourconfig/application.config.php
Symfony Container parameters can be defined in the application configurations:
<?php
return [
'zf_symfony_container' => [
// directory where to look for the service container configurations (e.g. config/services.yaml)
'config_dir' => 'config',
// Caching options
'cache' => [
// directory where the cached container will be stored
'dir' => 'data/ZfSymfonyContainer',
// name of the file to generate the cached container class
'filename' => 'zf_symfony_container_cache',
// name of the class of the generated cached container
'classname' => 'CachedContainer',
// the namespace of the generated cached container
'namespace' => 'Adlogix\ZfSymfonyContainer\DependencyInjection',
// enable in dev mode
'debug' => false
],
],
];
Refer to the documentation of the Symfony DI Component for information on how to use the DI container.
Any existing symfony service will directly be available through the Service Manager of Zend:
<?php
$service = $this->getServiceLocator()->get(\My\Public\Service::class);
It will happen that you have to use a dependency which is already loaded into the Zend Service Manager. For example, if you are using the Doctrine Module and you need to have the instance of the Entity Manager, you can leverage the ZendServiceProxyFactory class:
services:
_defaults:
autowire: true # Automatically injects dependencies in your services.
autoconfigure: true # Automatically registers your services as commands, event subscribers, etc.
public: false # Allows optimizing the container by removing unused services; this also means
# fetching services directly from the container via $container->get() won't work.
# The best practice is to be explicit about your dependencies anyway.
#
# Define service that needs to be retrieved via the Zend Service Manager
#
Doctrine\ORM\EntityManagerInterface:
factory: ['Adlogix\ZfSymfonyContainer\Service\Factory\ZendServiceProxyFactory', getService]
arguments: ['@zend.container', 'doctrine.entitymanager.orm_default']
class: Doctrine\ORM\EntityManagerInterface
With this configuration, and service defined with the symfony container requiring an instance of Doctrine\ORM\EntityManagerInterface will receive this from the Zend Service Manager.
<?php
$container = $this->getServiceLocator()->get('zf_symfony_container');
The module takes care of transforming all zend configurations into Symfony parameters allowing you to immediately use the parameters in the service definition file:
<?php
// global.php
return [
'myconfig' => 'hello world'
];
services:
Some\Service:
arguments: ['%myconfig%']
When the Zend configuration contains deep level keys, the "keys" will be concatenated with a dot.
<?php
// global.php
return [
'deep' => [
'level' => [
'config' => 'hello world'
]
]
];
services:
Some\Service:
arguments: ['%deep.level.config%']
Note: zend configurations using callables will be ignored and cannot be used!