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

1.3.0 2014-09-24 14:05 UTC

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!

Build Status Scrutinizer Quality Score Coverage Status HHVM Status SensioLabsInsight Latest Stable Version Dependency Status Total Downloads License

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: