You want to configure your factories?
You want to reduce your factory boilerplate code?
You want to check automatically for mandatory options or merge default options?
You want to have a valid config structure?
You want to generate your configuration files from factory classes?
This library comes to the rescue!
intop-config
provides interfaces and a concrete implementation to create instances depending on configuration via
factory classes and ensures a valid config structure. It can also be used to auto discover factories and to create
configuration files.
You should have coding conventions and you should have config conventions. If not, you should think about that.
interop-config
is universally applicable! See further documentation for more details.
The suggested installation method is via composer. For composer documentation, please refer to getcomposer.org.
Run composer require sandrokeil/interop-config
to install interop-config.
It is recommended to use psr-11 to retrieve the configuration in your factories.
The following example is a common practice for libraries. You are free to use another config structure. See examples.
The config keys should have the following structure vendor.package.container_id
. The container_id
is optional and is
only necessary if you have different instances of the same class e.g. database connection.
A common configuration looks like that:
// interop config example
return [
// vendor name
'doctrine' => [
// package name
'connection' => [
// container id
'orm_default' => [
// mandatory options
'driverClass' => 'Doctrine\DBAL\Driver\PDOMySql\Driver',
'params' => [
'host' => 'localhost',
'port' => '3306',
'user' => 'username',
'password' => 'password',
'dbname' => 'database',
],
],
],
],
];
So doctrine
is the vendor, connection
is the package and orm_default
is the container id. After that the specified
instance options follow. The following example uses ConfigurationTrait
which implements the logic to retrieve the
options from a configuration. RequiresConfigId
interface ensures support for more than one instance.
Note that the configuration above is injected as
$config
inoptions()
and psr-11 is used to retrieve the application configuration.
use Interop\Config\ConfigurationTrait;
use Interop\Config\RequiresConfigId;
use Interop\Config\RequiresMandatoryOptions;
use Psr\Container\ContainerInterface;
class MyDBALConnectionFactory implements RequiresConfigId, RequiresMandatoryOptions
{
use ConfigurationTrait;
public function __invoke(ContainerInterface $container)
{
// get options for doctrine.connection.orm_default
$options = $this->options($container->get('config'), 'orm_default');
// mandatory options check is automatically done by RequiresMandatoryOptions
$driverClass = $options['driverClass'];
$params = $options['params'];
// create your instance and set options
return $instance;
}
/**
* Is used to retrieve options from the configuration array ['doctrine' => ['connection' => []]].
*
* @return iterable
*/
public function dimensions() : iterable
{
return ['doctrine', 'connection'];
}
/**
* Returns a list of mandatory options which must be available
*
* @return iterable List with mandatory options
*/
public function mandatoryOptions() : iterable
{
return ['params' => ['user', 'password', 'dbname']];
}
}