Skip to content

Commit e10cee9

Browse files
committed
DI config: add spread operator
1 parent 2274c57 commit e10cee9

File tree

2 files changed

+61
-1
lines changed

2 files changed

+61
-1
lines changed

src/DI/Helpers.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,11 @@ public static function expand($var, array $params, $recursive = false)
3434
if (is_array($var)) {
3535
$res = [];
3636
foreach ($var as $key => $val) {
37-
$res[$key] = self::expand($val, $params, $recursive);
37+
if (is_int($key) && is_string($val) && preg_match('#^\.\.\.(%[\w.-]*%)\z#i', $val, $matches)) {
38+
$res = array_merge($res, self::expand($matches[1], $params, $recursive));
39+
} else {
40+
$res[$key] = self::expand($val, $params, $recursive);
41+
}
3842
}
3943
return $res;
4044

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?php
2+
3+
/**
4+
* Test: Nette\DI\Compiler: spread operator
5+
*/
6+
7+
declare(strict_types = 1);
8+
9+
use Nette\DI;
10+
use Tester\Assert;
11+
12+
13+
require __DIR__ . '/../bootstrap.php';
14+
15+
16+
class DbConnection
17+
{
18+
19+
/** @var array */
20+
public $config;
21+
22+
23+
public function __construct(array $config)
24+
{
25+
$this->config = $config;
26+
}
27+
}
28+
29+
30+
$container = createContainer(new DI\Compiler, '
31+
parameters:
32+
connection:
33+
username: root
34+
password: 123
35+
connection2:
36+
- ...%connection%
37+
database: app
38+
services:
39+
connection: DbConnection([timezone: utc, ...%connection2%])
40+
');
41+
42+
43+
Assert::same([
44+
'username' => 'root',
45+
'password' => 123,
46+
'database' => 'app',
47+
], $container->getParameters()['connection2']);
48+
49+
/** @var DbConnection $connection */
50+
$connection = $container->getService('connection');
51+
Assert::same([
52+
'timezone' => 'utc',
53+
'username' => 'root',
54+
'password' => 123,
55+
'database' => 'app',
56+
], $connection->config);

0 commit comments

Comments
 (0)