Skip to content

Commit

Permalink
Allow adding PHP configuration as array directly
Browse files Browse the repository at this point in the history
  • Loading branch information
mnapoli committed Jun 18, 2016
1 parent e720f33 commit 52998e3
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 3 deletions.
26 changes: 23 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,18 @@ $kernel = new Kernel([
$container = $kernel->createContainer();
```

If you want to register configuration on the container, you can:

- create a module - this is the recommended solution, read the next sections to learn more
- or set the configuration directly - this is useful in micro-frameworks or micro-applications:

```php
$kernel = new Kernel();
$kernel->addConfig([
'db.host' => 'localhost',
]);
```

### Installing a module

To install a 3rd party module:
Expand All @@ -49,9 +61,9 @@ To install a 3rd party module:

### Creating a module

1. choose a module name, for example `blogpress`
1. choose a module name, usually `app` when writing an application, or anything else when writing a reusable module
1. create a resource directory in your package, usually `res/`
1. map it with Puli, for example `puli map /blogpress res`
1. map it with Puli, for example `puli map /app res`
1. create as many PHP-DI configuration files as needed in `res/config/`

That's it. Here is what your package should look like:
Expand All @@ -67,7 +79,15 @@ composer.json
puli.json
```

When users install your package and tell the kernel to load the `blogpress` module, it will load all the files matching the Puli path `/blogpress/config/*.php` (i.e. `vendor/johndoe/blogpress/res/config/*.php` on the filesystem).
When the module is registered in the kernel like this:

```php
$kernel = new Kernel([
'app',
]);
```

all the files matching the Puli path `/blogpress/config/*.php` (i.e. `vendor/johndoe/blogpress/res/config/*.php` on the filesystem) will be loaded.

### Environments

Expand Down
23 changes: 23 additions & 0 deletions src/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ class Kernel
*/
private $environment;

/**
* @var array
*/
private $config = [];

/**
* @param array $modules The name of the modules to load.
* @param string $environment Environment of the application (prod, dev, test, ...).
Expand All @@ -44,6 +49,20 @@ public function __construct(array $modules = [], $environment = 'prod')
$this->environment = $environment;
}

/**
* Add container configuration.
*
* Use this method to define config easily when writing a micro-application.
* In bigger applications you are encouraged to define configuration in
* files using modules.
*
* @see http://php-di.org/doc/php-definitions.html
*/
public function addConfig(array $config)
{
$this->config = array_merge($this->config, $config);
}

/**
* Configure and create a container using all configuration files registered under
* the `php-di/configuration` binding type in Puli.
Expand Down Expand Up @@ -81,6 +100,10 @@ public function createContainer()
$this->loadModule($containerBuilder, $repository, $module);
}

if (!empty($this->config)) {
$containerBuilder->addDefinitions($this->config);
}

$this->configureContainerBuilder($containerBuilder);

return $containerBuilder->build();
Expand Down
19 changes: 19 additions & 0 deletions tests/KernelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,4 +84,23 @@ public function loads_module_environment_config()

$this->assertEquals('biz', $container->get('foo'));
}

/**
* @test
*/
public function uses_provided_config()
{
$this->kernel = new Kernel;
$this->kernel->addConfig([
'foo' => 'bar',
'bar' => 'bar',
]);
$this->kernel->addConfig([
'foo' => 'biz',
]);
$container = $this->kernel->createContainer();

$this->assertEquals('biz', $container->get('foo'));
$this->assertEquals('bar', $container->get('bar'));
}
}

0 comments on commit 52998e3

Please sign in to comment.