Skip to content

Commit

Permalink
Add environment support
Browse files Browse the repository at this point in the history
  • Loading branch information
mnapoli committed Jun 18, 2016
1 parent 9a284a2 commit 7582b4f
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 2 deletions.
25 changes: 25 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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**.
18 changes: 17 additions & 1 deletion src/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand Down Expand Up @@ -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());
}
}
}
}
19 changes: 18 additions & 1 deletion tests/KernelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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'));
}
}
File renamed without changes.
5 changes: 5 additions & 0 deletions tests/test-module/env/dev.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php

return [
'foo' => 'biz',
];

0 comments on commit 7582b4f

Please sign in to comment.