Skip to content

Commit 7582b4f

Browse files
committed
Add environment support
1 parent 9a284a2 commit 7582b4f

File tree

5 files changed

+65
-2
lines changed

5 files changed

+65
-2
lines changed

README.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,3 +68,28 @@ puli.json
6868
```
6969
7070
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).
71+
72+
### Environments
73+
74+
Applications often need to behave differently according to the environment: `dev`, `prod`, etc.
75+
76+
PHP-DI's Kernel let you write config for specific environments through a simple convention:
77+
78+
```
79+
res/
80+
config/
81+
config.php
82+
env/
83+
dev.php
84+
prod.php
85+
...
86+
```
87+
88+
You can then instruct the environment to load:
89+
90+
```php
91+
$kernel = new Kernel($modules, 'dev'); // dev environment
92+
$kernel = new Kernel($modules, 'prod'); // prod environment
93+
```
94+
95+
Note that **environments are optional**.

src/Kernel.php

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,19 @@ class Kernel
2929
*/
3030
private $modules;
3131

32+
/**
33+
* @var string
34+
*/
35+
private $environment;
36+
3237
/**
3338
* @param array $modules The name of the modules to load.
39+
* @param string $environment Environment of the application (prod, dev, test, ...).
3440
*/
35-
public function __construct(array $modules = [])
41+
public function __construct(array $modules = [], $environment = 'prod')
3642
{
3743
$this->modules = $modules;
44+
$this->environment = $environment;
3845
}
3946

4047
/**
@@ -112,5 +119,14 @@ private function loadModule(ContainerBuilder $builder, ResourceRepository $resou
112119
$builder->addDefinitions($resource->getFilesystemPath());
113120
}
114121
}
122+
123+
// Load the environment-specific config if it exists
124+
$envConfig = '/' . $module . '/config/env/' . $this->environment . '.php';
125+
if ($resources->contains($envConfig)) {
126+
$resource = $resources->get($envConfig);
127+
if ($resource instanceof FilesystemResource) {
128+
$builder->addDefinitions($resource->getFilesystemPath());
129+
}
130+
}
115131
}
116132
}

tests/KernelTest.php

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public function registers_puli_discovery()
5858
*/
5959
public function loads_module_configs()
6060
{
61-
PuliFactoryClass::$repository->add('/blog/config/config.php', new FileResource(__DIR__.'/Fixture/config.php'));
61+
PuliFactoryClass::$repository->add('/blog/config/config.php', new FileResource(__DIR__.'/test-module/config.php'));
6262

6363
$this->kernel = new Kernel([
6464
'blog',
@@ -68,4 +68,21 @@ public function loads_module_configs()
6868

6969
$this->assertEquals('bar', $container->get('foo'));
7070
}
71+
72+
/**
73+
* @test
74+
*/
75+
public function loads_module_environment_config()
76+
{
77+
PuliFactoryClass::$repository->add('/blog/config/config.php', new FileResource(__DIR__.'/test-module/config.php'));
78+
PuliFactoryClass::$repository->add('/blog/config/env/dev.php', new FileResource(__DIR__.'/test-module/env/dev.php'));
79+
80+
$this->kernel = new Kernel([
81+
'blog',
82+
], 'dev');
83+
$this->kernel->setPuliFactoryClass(PuliFactoryClass::class);
84+
$container = $this->kernel->createContainer();
85+
86+
$this->assertEquals('biz', $container->get('foo'));
87+
}
7188
}
File renamed without changes.

tests/test-module/env/dev.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?php
2+
3+
return [
4+
'foo' => 'biz',
5+
];

0 commit comments

Comments
 (0)