From 7582b4ff0a4cbdecf5558cfa47e63416daa43943 Mon Sep 17 00:00:00 2001 From: Matthieu Napoli Date: Sat, 18 Jun 2016 09:59:28 +0200 Subject: [PATCH] Add environment support --- README.md | 25 +++++++++++++++++++++++ src/Kernel.php | 18 +++++++++++++++- tests/KernelTest.php | 19 ++++++++++++++++- tests/{Fixture => test-module}/config.php | 0 tests/test-module/env/dev.php | 5 +++++ 5 files changed, 65 insertions(+), 2 deletions(-) rename tests/{Fixture => test-module}/config.php (100%) create mode 100644 tests/test-module/env/dev.php diff --git a/README.md b/README.md index 66fa76e..ede48f2 100644 --- a/README.md +++ b/README.md @@ -68,3 +68,28 @@ 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). + +### Environments + +Applications often need to behave differently according to the environment: `dev`, `prod`, etc. + +PHP-DI's Kernel let you write config for specific environments through a simple convention: + +``` +res/ + config/ + config.php + env/ + dev.php + prod.php + ... +``` + +You can then instruct the environment to load: + +```php +$kernel = new Kernel($modules, 'dev'); // dev environment +$kernel = new Kernel($modules, 'prod'); // prod environment +``` + +Note that **environments are optional**. diff --git a/src/Kernel.php b/src/Kernel.php index 3f1a3d2..c48b75b 100644 --- a/src/Kernel.php +++ b/src/Kernel.php @@ -29,12 +29,19 @@ class Kernel */ private $modules; + /** + * @var string + */ + private $environment; + /** * @param array $modules The name of the modules to load. + * @param string $environment Environment of the application (prod, dev, test, ...). */ - public function __construct(array $modules = []) + public function __construct(array $modules = [], $environment = 'prod') { $this->modules = $modules; + $this->environment = $environment; } /** @@ -112,5 +119,14 @@ private function loadModule(ContainerBuilder $builder, ResourceRepository $resou $builder->addDefinitions($resource->getFilesystemPath()); } } + + // Load the environment-specific config if it exists + $envConfig = '/' . $module . '/config/env/' . $this->environment . '.php'; + if ($resources->contains($envConfig)) { + $resource = $resources->get($envConfig); + if ($resource instanceof FilesystemResource) { + $builder->addDefinitions($resource->getFilesystemPath()); + } + } } } diff --git a/tests/KernelTest.php b/tests/KernelTest.php index 0496dc0..672157c 100644 --- a/tests/KernelTest.php +++ b/tests/KernelTest.php @@ -58,7 +58,7 @@ public function registers_puli_discovery() */ public function loads_module_configs() { - PuliFactoryClass::$repository->add('/blog/config/config.php', new FileResource(__DIR__.'/Fixture/config.php')); + PuliFactoryClass::$repository->add('/blog/config/config.php', new FileResource(__DIR__.'/test-module/config.php')); $this->kernel = new Kernel([ 'blog', @@ -68,4 +68,21 @@ public function loads_module_configs() $this->assertEquals('bar', $container->get('foo')); } + + /** + * @test + */ + public function loads_module_environment_config() + { + PuliFactoryClass::$repository->add('/blog/config/config.php', new FileResource(__DIR__.'/test-module/config.php')); + PuliFactoryClass::$repository->add('/blog/config/env/dev.php', new FileResource(__DIR__.'/test-module/env/dev.php')); + + $this->kernel = new Kernel([ + 'blog', + ], 'dev'); + $this->kernel->setPuliFactoryClass(PuliFactoryClass::class); + $container = $this->kernel->createContainer(); + + $this->assertEquals('biz', $container->get('foo')); + } } diff --git a/tests/Fixture/config.php b/tests/test-module/config.php similarity index 100% rename from tests/Fixture/config.php rename to tests/test-module/config.php diff --git a/tests/test-module/env/dev.php b/tests/test-module/env/dev.php new file mode 100644 index 0000000..63d464c --- /dev/null +++ b/tests/test-module/env/dev.php @@ -0,0 +1,5 @@ + 'biz', +];