You want to configure your factories?
You want to reduce your factory boilerplate code?
You want to check automatically for mandatory options?
You want to have an uniform config structure?
This library comes to the rescue!
interop-config provides interfaces and a concrete implementation to create instances depending on configuration via factory classes and ensures a uniform config structure. It can also be used to auto discover factories and to create configuration files.
Please join the discussion about the PSR config proposal.
- Well tested. Besides unit test and continuous integration/inspection this solution is also ready for production use.
- Framework agnostic This PHP library does not depends on any framework but you can use it with your favourite framework.
- 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. See CONTRIBUTING.md
You should have coding conventions and you should have config conventions. If not, you should think about that.
The config keys should have the following structure vendor.package.container_id
. 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. See documentation for more details.
Note that the configuration above is injected as
$config
inoptions()
use Interop\Config\ConfigurationTrait;
use Interop\Config\RequiresContainerId;
use Interop\Config\RequiresMandatoryOptions;
use Interop\Container\ContainerInterface;
class MyDBALConnectionFactory implements RequiresContainerId, RequiresMandatoryOptions
{
use ConfigurationTrait;
public function __invoke(ContainerInterface $container)
{
// get options for doctrine.connection.orm_default
$options = $this->options($container->get('config'));
// mandatory options check is automatically done by RequiresMandatoryOptions
$driverClass = $options['driverClass'];
$params = $options['params'];
// create your instance and set options
return $instance;
}
/**
* Returns the vendor name
*
* @return string
*/
public function vendorName()
{
return 'doctrine';
}
/**
* Returns the package name
*
* @return string
*/
public function packageName()
{
return 'connection';
}
/**
* Returns the container identifier
*
* @return string
*/
public function containerId()
{
return 'orm_default';
}
/**
* Returns a list of mandatory options which must be available
*
* @return string[] List with mandatory options
*/
public function mandatoryOptions()
{
return ['params' => ['user', 'password', 'dbname']];
}
}
Installation of this module uses composer. For composer documentation, please refer to getcomposer.org.
Put the following into your composer.json
{
"require": {
"sandrokeil/interop-config": "^0.3"
}
}
For the latest online documentation you can visit http://sandrokeil.github.io/interop-config/.
Documentation is in the doc tree, and can be compiled using bookdown and Docker
$ docker run -it --rm -v $(pwd):/app sandrokeil/bookdown doc/bookdown.json
$ docker run -it --rm -p 8080:8080 -v $(pwd):/app php:5.6-cli php -S 0.0.0.0:8080 -t /app/doc/html
or make sure bookdown is installed globally via composer and $HOME/.composer/vendor/bin
is on your $PATH
.
$ bookdown doc/bookdown.json
$ php -S 0.0.0.0:8080 -t doc/html/
Then browse to http://localhost:8080/
The benchmarks uses PHPBench and can be started by the following command:
$ ./vendor/bin/phpbench run --report=aggregate
or with Docker
$ docker run --rm -it --volume $(pwd):/app sandrokeil/php:5.6-cli php ./vendor/bin/phpbench run --report=aggregate