Skip to content

Commit 52998e3

Browse files
committed
Allow adding PHP configuration as array directly
1 parent e720f33 commit 52998e3

File tree

3 files changed

+65
-3
lines changed

3 files changed

+65
-3
lines changed

README.md

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,18 @@ $kernel = new Kernel([
3434
$container = $kernel->createContainer();
3535
```
3636

37+
If you want to register configuration on the container, you can:
38+
39+
- create a module - this is the recommended solution, read the next sections to learn more
40+
- or set the configuration directly - this is useful in micro-frameworks or micro-applications:
41+
42+
```php
43+
$kernel = new Kernel();
44+
$kernel->addConfig([
45+
'db.host' => 'localhost',
46+
]);
47+
```
48+
3749
### Installing a module
3850

3951
To install a 3rd party module:
@@ -49,9 +61,9 @@ To install a 3rd party module:
4961

5062
### Creating a module
5163

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

5769
That's it. Here is what your package should look like:
@@ -67,7 +79,15 @@ composer.json
6779
puli.json
6880
```
6981
70-
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).
82+
When the module is registered in the kernel like this:
83+
84+
```php
85+
$kernel = new Kernel([
86+
'app',
87+
]);
88+
```
89+
90+
all the files matching the Puli path `/blogpress/config/*.php` (i.e. `vendor/johndoe/blogpress/res/config/*.php` on the filesystem) will be loaded.
7191

7292
### Environments
7393

src/Kernel.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,11 @@ class Kernel
3434
*/
3535
private $environment;
3636

37+
/**
38+
* @var array
39+
*/
40+
private $config = [];
41+
3742
/**
3843
* @param array $modules The name of the modules to load.
3944
* @param string $environment Environment of the application (prod, dev, test, ...).
@@ -44,6 +49,20 @@ public function __construct(array $modules = [], $environment = 'prod')
4449
$this->environment = $environment;
4550
}
4651

52+
/**
53+
* Add container configuration.
54+
*
55+
* Use this method to define config easily when writing a micro-application.
56+
* In bigger applications you are encouraged to define configuration in
57+
* files using modules.
58+
*
59+
* @see http://php-di.org/doc/php-definitions.html
60+
*/
61+
public function addConfig(array $config)
62+
{
63+
$this->config = array_merge($this->config, $config);
64+
}
65+
4766
/**
4867
* Configure and create a container using all configuration files registered under
4968
* the `php-di/configuration` binding type in Puli.
@@ -81,6 +100,10 @@ public function createContainer()
81100
$this->loadModule($containerBuilder, $repository, $module);
82101
}
83102

103+
if (!empty($this->config)) {
104+
$containerBuilder->addDefinitions($this->config);
105+
}
106+
84107
$this->configureContainerBuilder($containerBuilder);
85108

86109
return $containerBuilder->build();

tests/KernelTest.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,4 +84,23 @@ public function loads_module_environment_config()
8484

8585
$this->assertEquals('biz', $container->get('foo'));
8686
}
87+
88+
/**
89+
* @test
90+
*/
91+
public function uses_provided_config()
92+
{
93+
$this->kernel = new Kernel;
94+
$this->kernel->addConfig([
95+
'foo' => 'bar',
96+
'bar' => 'bar',
97+
]);
98+
$this->kernel->addConfig([
99+
'foo' => 'biz',
100+
]);
101+
$container = $this->kernel->createContainer();
102+
103+
$this->assertEquals('biz', $container->get('foo'));
104+
$this->assertEquals('bar', $container->get('bar'));
105+
}
87106
}

0 commit comments

Comments
 (0)