Skip to content

Commit

Permalink
added unit tests, renamed OptionClassInterface to OptionsClassInterfa…
Browse files Browse the repository at this point in the history
…ce, updated docs
  • Loading branch information
Sandro Keil committed Nov 18, 2013
1 parent 72aaea5 commit 41c75e8
Show file tree
Hide file tree
Showing 19 changed files with 534 additions and 34 deletions.
41 changes: 39 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,44 @@ return array(
);
```
So `doctrine` is the module, `connection` is the scope and `orm_default` is the name. After that follows the specified instance options.
With [AbstractConfigurableFactory](https://github.com/sandrokeil/EasyConfig/tree/master/docs/Configurable.md) we have access to these options easily.

```php
use Sake\EasyConfig\Service\AbstractConfigurableFactory;
use Zend\ServiceManager\FactoryInterface;
use Zend\ServiceManager\ServiceLocatorInterface;

class MyDBALConnectionFactory extends AbstractConfigurableFactory implements FactoryInterface
{
public function createService(ServiceLocatorInterface $serviceLocator)
{
// get options for doctrine.connection.orm_default
$options = $this->getOptions($serviceLocator);

$driverClass = $options['driverClass'];
$params = $options['params'];

// create your instance and set options

return $instance;
}

public function getModule()
{
return 'doctrine';
}

public function getScope()
{
return 'connection';
}

public function getName()
{
return 'orm_default';
}
}
```

## Installation

Expand All @@ -35,11 +72,11 @@ Put the following into your composer.json

{
"require": {
"sandrokeil/easy-config": "*"
"sandrokeil/easy-config": "dev-master"
}
}

Then add `Sake\EasyConfig` to your `config/application.config.php`.
Then add `Sake\EasyConfig` to your `config/application.config.php` at the first module.

## Documentation

Expand Down
30 changes: 19 additions & 11 deletions docs/Configurable.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# AbstractConfigurableFactory

Use this class if you want to retrieve the configuration options and setup your instance manually.
Use this class if you want to retrieve the configuration options and setup your instance manually.

Let's assume we have the following configuration:
Let's assume we have the following module configuration:

```php
return array(
Expand All @@ -14,16 +14,19 @@ return array(
)
)
)
...
);
```

## Array Options
Then you have easily access to the `orm_default` options in your createService() method with this factory.
Then you have easily access to the `orm_default` options in your `createService()` method with this factory.

```php
use Sake\EasyConfig\Service\AbstractConfigurableFactory;
use Zend\ServiceManager\FactoryInterface;
use Zend\ServiceManager\ServiceLocatorInterface;

class MyDBALConnectionFactory extends AbstractConfigurableFactory
class MyDBALConnectionFactory extends AbstractConfigurableFactory implements FactoryInterface
{
public function createService(ServiceLocatorInterface $serviceLocator)
{
Expand Down Expand Up @@ -57,9 +60,12 @@ class MyDBALConnectionFactory extends AbstractConfigurableFactory
## Option Class
If you implement `OptionClassInterface` then you get a option class. Your options class should extend from `\Zend\Stdlib\AbstractOptions`.
```
use \Sake\EasyConfig\Service\AbstractConfigurableFactory;
use \Sake\EasyConfig\Service\OptionClassInterface;
use Zend\ServiceManager\FactoryInterface;
use Zend\ServiceManager\ServiceLocatorInterface;
class MyDBALConnectionFactory extends AbstractConfigurableFactory implements OptionClassInterface
class MyDBALConnectionFactory extends AbstractConfigurableFactory implements FactoryInterface, OptionClassInterface
{
public function createService(ServiceLocatorInterface $serviceLocator)
{
Expand All @@ -72,6 +78,13 @@ class MyDBALConnectionFactory extends AbstractConfigurableFactory implements Opt
$wrapperClass = $options->getWrapperClass();
// create your instance
return $instance;
}
public function getOptionClass()
{
return '\DoctrineORMModule\Options\DBALConnection';
}
public function getModule()
Expand All @@ -87,11 +100,6 @@ class MyDBALConnectionFactory extends AbstractConfigurableFactory implements Opt
public function getName()
{
return 'orm_default';
}
public function getOptionClass()
{
return '\DoctrineORMModule\Options\DBALConnection';
}
}
}
```
114 changes: 112 additions & 2 deletions docs/ConstructorOptionConfig.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,114 @@
# AbstractConstructorOptionConfigFactory
Use this factory for easy dependency injection in your instance via constructor.
Use this factory for easy dependency injection to your instance via constructor.

TODO
## Single constructor argument
Let's assume we have the following module configuration:

```php
return array(
'cache_module' => array(
'result_config' => array(
'default' => array(
'lifetime' => 50,
'foo' => 'bar',
...
)
)
)
...
);
```

We have also a class `ResultCache` with a single constructor argument like `public function __construct(array $config)`. So you can easily inject the above config to this class with this factory.


```php
use \Sake\EasyConfig\Service\AbstractConstructorOptionConfigFactory;

class ResultCacheFactory extends AbstractConstructorOptionConfigFactory
{
public function getModule()
{
return 'cache_module';
}

public function getScope()
{
return 'result_config';
}

public function getName()
{
return 'default';
}

protected function getInjectionType()
{
return self::INJECTION_TYPE_SINGLE;
}

public function getClassName()
{
return 'MyModule\Service\ResultCache';
}
}
```

## Multiple constructor arguments
Let's assume we have the following module configuration:

```php
return array(
'cache_module' => array(
'result_config' => array(
'default' => array(
// first constructor argument
'first' => array(
'lifetime' => 50,
'foo' => 'bar',
),
// second constructor argument
'second' => array(
'bar' => 'foo',
),
...
)
)
)
...
);
```

We have also a class `ResultCache` with two constructor arguments like `public function __construct(array $config, array $other)`. So you can easily inject the above config to this class with this factory.

```php
use \Sake\EasyConfig\Service\AbstractConstructorOptionConfigFactory;

class ResultCacheFactory extends AbstractConstructorOptionConfigFactory
{
public function getModule()
{
return 'cache_module';
}

public function getScope()
{
return 'result_config';
}

public function getName()
{
return 'default';
}

protected function getInjectionType()
{
return self::INJECTION_TYPE_MULTI;
}

public function getClassName()
{
return 'MyModule\Service\ResultCache';
}
}
```
53 changes: 52 additions & 1 deletion docs/OptionHydratorConfig.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,55 @@
# AbstractOptionHydratorConfigFactory
Use this factory for easy dependency injection in your instance via hydrator.

TODO
Let's assume we have the following module configuration:

```php
return array(
'mymodule' => array(
'myscope' => array(
'default' => array(
// setter/getter available for foo, time and date_format
'foo' => 'bar',
'time' => 5,
'date_format' => 'Y-m-d'
)
)
)
...
);
```

You can easily inject the above options to an instance with this factory.

```php
use \Sake\EasyConfig\Service\AbstractOptionHydratorConfigFactory;

class ResultCacheFactory extends AbstractOptionHydratorConfigFactory
{
public function getModule()
{
return 'mymodule';
}

public function getScope()
{
return 'myscope';
}

public function getName()
{
return 'default';
}

public function getClassName()
{
return 'MyModule\Service\ClassWithSetter';
}

public function getHydrator(ServiceLocatorInterface $serviceLocator)
{
// use service locator to retrieve other hydrators
return new \Zend\Stdlib\Hydrator\ClassMethods();
}
}
```
48 changes: 47 additions & 1 deletion docs/ServiceManagerConfig.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,50 @@
# AbstractServiceManagerConfigFactory
This factory injects options to a service manager instance via a service manager config class. This is useful for plugin manager.
Let's assume we have the following configuration:

TODO
```php
return array(
'mymodule' => array(
'orm_manager' => array(
'orm_default' => array(
'invokables' => array(
// '[short name]' => 'FCQN of doctrine service'
),
),
),
),
);
```
You can easily create a configured plugin manager with these options easily. The repository is injected via constructor. See test assets for details.

```php
use Sake\EasyConfig\Service\AbstractServiceManagerConfigFactory;

class MyOrmManagerFactory extends AbstractServiceManagerConfigFactory
{
public function getClassName()
{
return 'MyModule\Service\OrmManager';
}

public function getModule()
{
return 'mymodule';
}

public function getScope()
{
return 'orm_manager';
}

public function getName()
{
return 'orm_default';
}

public function getOptionClass()
{
return 'MyModule\Service\Options\OrmManagerOptions';
}
}
```
Loading

0 comments on commit 41c75e8

Please sign in to comment.