adlogix / zf-symfony-container
Zend Framework module to leverage the symfony dependency injection container
Installs: 3 839
Dependents: 0
Suggesters: 0
Security: 0
Stars: 2
Watchers: 3
Forks: 0
Open Issues: 0
Requires
- php: >=5.6
- symfony/config: ^3.4
- symfony/dependency-injection: ^3.4
- zendframework/zend-modulemanager: ^2.8
- zendframework/zend-servicemanager: ^2.7
Requires (Dev)
- friendsofphp/php-cs-fixer: ^2.13
- phpunit/phpunit: ^5.0
- zendframework/zend-i18n: ^2.8
- zendframework/zend-log: ^2.10
- zendframework/zend-mvc: ^2.7
- zendframework/zend-serializer: ^2.9
- zendframework/zend-test: ^2.6
This package is auto-updated.
Last update: 2024-11-10 05:40:43 UTC
README
Zend Framework module to leverage the symfony dependency injection container
Installation
- 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
Configuration
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 ], ], ];
Usage
Refer to the documentation of the Symfony DI Component for information on how to use the DI container.
Obtain a symfony defined service
Any existing symfony service will directly be available through the Service Manager of Zend:
<?php $service = $this->getServiceLocator()->get(\My\Public\Service::class);
Build a symfony defined service with a Zend service dependency
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.
Obtain the symfony container instance
<?php $container = $this->getServiceLocator()->get('zf_symfony_container');
Use the Zend config as Symfony parameters
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!