From 52998e30a9724bf9fc26dac01001607031f8f8e2 Mon Sep 17 00:00:00 2001 From: Matthieu Napoli Date: Sat, 18 Jun 2016 10:34:37 +0200 Subject: [PATCH] Allow adding PHP configuration as array directly --- README.md | 26 +++++++++++++++++++++++--- src/Kernel.php | 23 +++++++++++++++++++++++ tests/KernelTest.php | 19 +++++++++++++++++++ 3 files changed, 65 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index ede48f2..8ddf701 100644 --- a/README.md +++ b/README.md @@ -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: @@ -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: @@ -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 diff --git a/src/Kernel.php b/src/Kernel.php index b3afa44..7ab6e7e 100644 --- a/src/Kernel.php +++ b/src/Kernel.php @@ -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, ...). @@ -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. @@ -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(); diff --git a/tests/KernelTest.php b/tests/KernelTest.php index fae25f4..c61552c 100644 --- a/tests/KernelTest.php +++ b/tests/KernelTest.php @@ -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')); + } }