Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion src/DI/Helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,11 @@ public static function expand($var, array $params, $recursive = false)
if (is_array($var)) {
$res = [];
foreach ($var as $key => $val) {
$res[$key] = self::expand($val, $params, $recursive);
if (is_int($key) && is_string($val) && preg_match('#^\.\.\.(%[\w.-]*%)\z#i', $val, $matches)) {
$res = array_merge($res, self::expand($matches[1], $params, $recursive));
} else {
$res[$key] = self::expand($val, $params, $recursive);
}
}
return $res;

Expand Down
56 changes: 56 additions & 0 deletions tests/DI/Compiler.parameters.spread.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

/**
* Test: Nette\DI\Compiler: spread operator
*/

declare(strict_types = 1);

use Nette\DI;
use Tester\Assert;


require __DIR__ . '/../bootstrap.php';


class DbConnection
{

/** @var array */
public $config;


public function __construct(array $config)
{
$this->config = $config;
}
}


$container = createContainer(new DI\Compiler, '
parameters:
connection:
username: root
password: 123
connection2:
- ...%connection%
database: app
services:
connection: DbConnection([timezone: utc, ...%connection2%])
');


Assert::same([
'username' => 'root',
'password' => 123,
'database' => 'app',
], $container->getParameters()['connection2']);

/** @var DbConnection $connection */
$connection = $container->getService('connection');
Assert::same([
'timezone' => 'utc',
'username' => 'root',
'password' => 123,
'database' => 'app',
], $connection->config);