sandrokeil / easy-config
Zend Framework 2 (zf2) module to retrieve specific module options and provides some abstract factories to create easily instances depending on configuration
Installs: 3 132
Dependents: 1
Suggesters: 0
Security: 0
Stars: 11
Watchers: 2
Forks: 1
Open Issues: 0
Requires
- php: ~5.3
- zendframework/zend-servicemanager: ~2.2
Requires (Dev)
Suggests
- zendframework/zend-stdlib: Zend\StdLib for AbstractOptionHydratorConfigFactory
This package is auto-updated.
Last update: 2024-11-28 23:08:24 UTC
README
You want to configure your factories via your module config?
You want to configure option classes via module config for your plugin manager?
You want to add other services via module config to a factory?
This module comes to the rescue!
EasyConfig provides some abstract factories and some interfaces to easily create instances depending on configuration or retrieve specified module options.
- Well tested. Besides unit test and continuous integration/inspection this solution is also ready for production use.
- Great foundations. Based on Zend Framework 2
- Every change is tracked. Want to know whats new? Take a look at CHANGELOG.md
- Listen to your ideas. Have a great idea? Bring your tested pull request or open a new issue.
You should have coding conventions and you should have config conventions. If not, you should think about that.
The module config keys should have the following structure module.scope.name
. A common configuration looks like that:
return array( 'doctrine' => array( 'connection' => array( 'orm_default' => array( 'driverClass' => 'Doctrine\DBAL\Driver\PDOMySql\Driver', 'params' => array(...) ) ) ) );
So doctrine
is the module, connection
is the scope and orm_default
is the name. After that the specified instance options follow.
With AbstractConfigurableFactory we can easily access to these options also with an option class and mandatory options check. See docs for a detailed explanation.
use Sake\EasyConfig\Service\AbstractConfigurableFactory; use Sake\EasyConfig\Service\OptionsClassInterface; use Sake\EasyConfig\Service\MandatoryOptionsInterface; use Zend\ServiceManager\FactoryInterface; use Zend\ServiceManager\ServiceLocatorInterface; class MyDBALConnectionFactory extends AbstractConfigurableFactory implements FactoryInterface, OptionsClassInterface, MandatoryOptionsInterface { public function createService(ServiceLocatorInterface $serviceLocator) { // get option class for doctrine.connection.orm_default // dont implement OptionsClassInterface to get an array of options $options = $this->getOptions($serviceLocator); // so you can do $pdo = $options->getPdo(); $driverClass = $options->getDriverClass(); $wrapperClass = $options->getWrapperClass(); // create your instance return $instance; } /** * Returns a list of mandatory options which must be available * * @return array */ public function getMandatoryOptions() { return array( 'driverClass', 'params', ); } /** * Return the option class name (fcqn) where options are injected via constructor * * @return string */ public function getOptionsClass() { return '\DoctrineORMModule\Options\DBALConnection'; } /** * Module name * * @return string */ public function getModule() { return 'doctrine'; } /** * Config scope * * @return string */ public function getScope() { return 'connection'; } /** * Config name * * @return string */ public function getName() { return 'orm_default'; } }
Installation
Installation of this module uses composer. For composer documentation, please refer to getcomposer.org.
Put the following into your composer.json
{
"require": {
"sandrokeil/easy-config": "~1.0"
}
}
It is not necessary to add this module to your config/application.config.php
.
Documentation
You can find documentation about the usages of factories at the following links:
- Configurable - Get an options class or an array of options and with mandatory options check
- ConstructorOptionConfig - Inject options via constructor
- OptionHydratorConfig - Inject options with a hydrator
- ServiceConfig - Inject an other service to instance
- ServiceManagerConfig - Inject Options to a service plugin manager
- ServiceOptionConfig - Inject one or more services