Skip to content

Commit

Permalink
Add sub-providers
Browse files Browse the repository at this point in the history
  • Loading branch information
tomphp committed Sep 23, 2015
1 parent ec729f3 commit 642aba8
Show file tree
Hide file tree
Showing 11 changed files with 333 additions and 99 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
### 0.3.0 (2015-09-23)

* Added: `ConfigServiceProvider::fromConfig()` factory method
* Added: Sub providers
* Updated: `TomPHP\ConfigServiceProvider\InflectorConfigServiceProvider` is
now a sub provider
* Removed: `TomPHP\ConfigServiceProvider\Config` static factory

### 0.2.1 (2015-09-21)

* Added: Support to set up inflectors via configuration
Expand Down
38 changes: 15 additions & 23 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ $ composer require tomphp/config-service-provider

use League\Container\Container;
use League\Container\ServiceProvider\AbstractServiceProvider;
use TomPHP\ConfigServiceProvider\Config;
use TomPHP\ConfigServiceProvider\ConfigServiceProvider;

class DatabaseConnectionProvider extends AbstractServiceProvider
{
Expand Down Expand Up @@ -55,7 +55,7 @@ $appConfig = [

$container = new Container();

Config::addToContainer($container, $appConfig);
$container->addServiceProvider(ConfigServiceProvider::fromConfig($appConfig));
$container->addServiceProvider(new DatabaseConnectionProvider());

$db = $container->get('database_connection');
Expand Down Expand Up @@ -113,31 +113,23 @@ $appConfig = [
];
```

## Advanced Usage
### Extra Settings

`Config::addToContainer` makes some assumptions about how your config is
arranged. If you need to customise this then you can create the service
providers directly.
You can provide an array of extra settings as a second parameter to
`TomPHP\ConfigServiceProvider\ConfigServiceProvider::fromConfig()`.

### The Config Service Provider
Current valid keys are:

You can change the config root name and the separator by creating an instance
of `TomPHP\ConfigServiceProvider\ConfigServiceProvider` directly.
| Name | Effect |
|-------------|-----------------------------------------------|
| `prefix` | Changes `config` prefix given to config keys. |
| `separator` | Changes `.` separator in config keys. |

```php
ConfigServiceProvider::__construct(array $config, $prefix = 'config', $separator = '.')
```

### Inflector Config Service Provider

The configuring of inflectors is done using a separate service provider:
Example:

```php
$inflectorConfig = [
LoggerAwareInterface::class => [
'setLogger' => ['Some\Logger']
]
];

$container->addServiceProvider(new InflectorConfigServiceProvider($inflectorConfig));
$provider = ConfigServiceProvider::fromConfig($appConfig, [
'prefix' => 'settings',
'separator' => '/'
]);
```
4 changes: 2 additions & 2 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<phpunit colors="true">
<testsuites>
<testsuite name="Tests">
<directory>tests/</directory>
<testsuite name="">
<directory>tests</directory>
</testsuite>
</testsuites>
</phpunit>
24 changes: 0 additions & 24 deletions src/Config.php

This file was deleted.

100 changes: 93 additions & 7 deletions src/ConfigServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,19 @@
namespace TomPHP\ConfigServiceProvider;

use League\Container\ServiceProvider\AbstractServiceProvider;
use League\Container\ServiceProvider\BootableServiceProviderInterface;
use TomPHP\ConfigServiceProvider\InflectorConfigServiceProvider;

class ConfigServiceProvider extends AbstractServiceProvider
class ConfigServiceProvider extends AbstractServiceProvider implements
BootableServiceProviderInterface
{
const DEFAULT_PREFIX = 'config';
const DEFAULT_SEPARATOR = '.';
const DEFAULT_INFLECTORS_KEY = 'inflectors';

const SETTING_PREFIX = 'prefix';
const SETTING_SEPARATOR = 'separator';

/**
* @var array
*/
Expand All @@ -21,30 +31,80 @@ class ConfigServiceProvider extends AbstractServiceProvider
*/
private $separator;

/**
* @var ConfigurableServiceProvider[]
*/
private $subProviders;

/**
* @api
*
* @param array $config
* @param string $prefix
* @param string $separator
* @param array $config
* @param array $settings
*
* @return ConfigServiceProvider
*/
public function __construct(array $config, $prefix = 'config', $separator = '.')
public static function fromConfig(array $config, array $settings = [])
{
$this->prefix = $prefix;
$this->separator = $separator;
return new self(
$config,
self::getSettingOrDefault(self::SETTING_PREFIX, $settings, self::DEFAULT_PREFIX),
self::getSettingOrDefault(self::SETTING_SEPARATOR, $settings, self::DEFAULT_SEPARATOR),
[self::DEFAULT_INFLECTORS_KEY => new InflectorConfigServiceProvider([])]
);
}

/**
* @api
*
* @param array $config
* @param string $prefix
* @param string $separator
* @param ConfigurableServiceProvider[] $subProviders
*/
public function __construct(
array $config,
$prefix = self::DEFAULT_PREFIX,
$separator = self::DEFAULT_SEPARATOR,
array $subProviders = []
) {
$this->prefix = $prefix;
$this->separator = $separator;
$this->subProviders = $subProviders;

$config = $this->expandSubGroups($config);

$this->provides = $this->getKeys($config);

$this->config = array_combine($this->provides, array_values($config));

foreach ($this->subProviders as $key => $provider) {
$this->configureSubProvider($key, $config, $provider);
}
}

public function register()
{
foreach ($this->config as $key => $value) {
$this->getContainer()->add($key, $value);
}

foreach ($this->subProviders as $provider) {
$provider->setContainer($this->getContainer());
$provider->register();
}
}

public function boot()
{
foreach ($this->subProviders as $provider) {
if (!$provider instanceof BootableServiceProviderInterface) {
continue;
}

$provider->setContainer($this->getContainer());
$provider->boot();
}
}

/**
Expand Down Expand Up @@ -113,4 +173,30 @@ function ($key) {
$keys
);
}

/**
* @param string $key
*/
private function configureSubProvider($key, $config, ConfigurableServiceProvider $provider)
{
if (!array_key_exists($key, $config)) {
return;
}

$provider->configure($config[$key]);

$this->provides = array_merge($this->provides, $provider->provides());
}

/**
* @param string $name
* @param array $settings
* @param mixed $default
*
* @return mixed
*/
private static function getSettingOrDefault($name, array $settings, $default)
{
return isset($settings[$name]) ? $settings[$name] : $default;
}
}
13 changes: 13 additions & 0 deletions src/ConfigurableServiceProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace TomPHP\ConfigServiceProvider;

use League\Container\ServiceProvider\ServiceProviderInterface;

interface ConfigurableServiceProvider extends ServiceProviderInterface
{
/**
* @param array $config
*/
public function configure(array $config);
}
11 changes: 10 additions & 1 deletion src/InflectorConfigServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
use League\Container\ServiceProvider\BootableServiceProviderInterface;

class InflectorConfigServiceProvider extends AbstractServiceProvider implements
BootableServiceProviderInterface
BootableServiceProviderInterface,
ConfigurableServiceProvider
{
/**
* @var array
Expand All @@ -23,6 +24,14 @@ public function __construct(array $config)
$this->config = $config;
}

/**
* @param array $config
*/
public function configure(array $config)
{
$this->config = $config;
}

public function register()
{
}
Expand Down
Loading

0 comments on commit 642aba8

Please sign in to comment.