Skip to content

Commit

Permalink
DI: support ENV variables
Browse files Browse the repository at this point in the history
  • Loading branch information
f3l1x committed Nov 4, 2021
1 parent 854edf4 commit 0fc1881
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 2 deletions.
17 changes: 17 additions & 0 deletions .docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,23 @@ This package provides several serializers:
- IgbinarySerializer
- SnappySerializer

### ENV variables

In the need of passing down ENV variables to the configuration you can use `nette/configurator` and [**dynamic parameters**](https://doc.nette.org/en/3.1/bootstrap#toc-dynamic-parameters).

```php
$configurator->addDynamicParameters([
'env' => getenv(),
]);
```

```neon
redis:
connection:
default:
uri: %env.REDIS_HOST%
```

### Sessions and cache

When using sessions and cache make sure you use **2 different databases**. One for cache and one for sessions. In case you will use only 1 database for both **you will loose sessions when clearing cache.**
Expand Down
2 changes: 1 addition & 1 deletion src/DI/RedisExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public function getConfigSchema(): Schema
'debug' => Expect::bool(false),
'serializer' => Expect::anyOf(Expect::string()),
'connection' => Expect::arrayOf(Expect::structure([
'uri' => Expect::anyOf(Expect::string(), Expect::listOf(Expect::string()))->default('tcp://127.0.0.1:6379'),
'uri' => Expect::anyOf(Expect::string(), Expect::listOf(Expect::string()))->default('tcp://127.0.0.1:6379')->dynamic(),
'options' => Expect::array(),
'storage' => Expect::bool(false),
'sessions' => Expect::anyOf(
Expand Down
28 changes: 28 additions & 0 deletions tests/cases/DI/RedisExtension.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -131,3 +131,31 @@ Toolkit::test(function (): void {
Assert::notSame(Liberator::of($storage1)->client, Liberator::of($storage2)->client);
Assert::notSame(Liberator::of($storage1)->journal, Liberator::of($storage2)->journal);
});

// Dynamic parameters
Toolkit::test(function (): void {
$container = Container::of()
->withCompiler(function (Compiler $compiler): void {
$compiler->addExtension('redis', new RedisExtension());
$compiler->addConfig(Helpers::neon('
redis:
connection:
default:
uri: %env.REDIS_URI%
'));
})
->withDynamicParameters([
'env' => [
'REDIS_URI' => 'tcp://1.2.3.4:1234',
],
])
->build();

/** @var Client $client */
$client = $container->getService('redis.connection.default.client');

$parameters = Liberator::of($client->getConnection())->parameters->toArray();

Assert::equal('1.2.3.4', $parameters['host']);
Assert::equal(1234, $parameters['port']);
});
14 changes: 13 additions & 1 deletion tests/toolkit/Container.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ final class Container
/** @var callable[] */
private $onCompile = [];

/** @var mixed[] */
private $parameters = [];

public function __construct(string $key)
{
$this->key = $key;
Expand Down Expand Up @@ -63,6 +66,13 @@ public function withDefaultParameters(): Container
return $this;
}

public function withDynamicParameters(array $parameters): Container
{
$this->parameters = $parameters;

return $this;
}

public function withCompiler(callable $cb): Container
{
$this->onCompile[] = function (Compiler $compiler) use ($cb): void {
Expand All @@ -79,9 +89,11 @@ public function build(): NetteContainer
foreach ($this->onCompile as $cb) {
$cb($compiler);
}

$compiler->setDynamicParameterNames(array_keys($this->parameters));
}, $this->key);

return new $class();
return new $class($this->parameters);
}

}

0 comments on commit 0fc1881

Please sign in to comment.