Skip to content

Commit 86e70cd

Browse files
committed
Compiler: added addConfig() & loadConfig() as future replacement for parameter $config in compile()
1 parent 408cb36 commit 86e70cd

File tree

2 files changed

+71
-3
lines changed

2 files changed

+71
-3
lines changed

src/DI/Compiler.php

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ class Compiler extends Nette\Object
2525
private $builder;
2626

2727
/** @var array */
28-
private $config;
28+
private $config = array();
2929

3030
/** @var string[] of file names */
3131
private $dependencies = array();
@@ -74,6 +74,30 @@ public function getContainerBuilder()
7474
}
7575

7676

77+
/**
78+
* Adds new configuration.
79+
* @return self
80+
*/
81+
public function addConfig(array $config)
82+
{
83+
$this->config = Config\Helpers::merge($config, $this->config);
84+
return $this;
85+
}
86+
87+
88+
/**
89+
* Adds new configuration from file.
90+
* @return self
91+
*/
92+
public function loadConfig($file)
93+
{
94+
$loader = new Config\Loader;
95+
$this->addConfig($loader->load($file));
96+
$this->addDependencies($loader->getDependencies());
97+
return $this;
98+
}
99+
100+
77101
/**
78102
* Returns configuration.
79103
* @return array
@@ -108,9 +132,9 @@ public function getDependencies()
108132
/**
109133
* @return string
110134
*/
111-
public function compile(array $config, $className, $parentName = NULL)
135+
public function compile(array $config = NULL, $className = NULL, $parentName = NULL)
112136
{
113-
$this->config = $config;
137+
$this->config = $config ?: $this->config;
114138
$this->processParameters();
115139
$this->processExtensions();
116140
$this->processServices();

tests/DI/Compiler.config.phpt

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
3+
/**
4+
* Test: Nette\DI\Compiler and config.
5+
*/
6+
7+
use Nette\DI,
8+
Tester\Assert;
9+
10+
11+
require __DIR__ . '/../bootstrap.php';
12+
13+
$compiler = new DI\Compiler;
14+
15+
Assert::same(
16+
array(),
17+
$compiler->getConfig()
18+
);
19+
20+
21+
$compiler->addConfig(array(
22+
'item1' => 1,
23+
));
24+
25+
Assert::same(
26+
array(
27+
'item1' => 1,
28+
),
29+
$compiler->getConfig()
30+
);
31+
32+
33+
$compiler->loadConfig(Tester\FileMock::create('
34+
item1: 11
35+
item2: 2
36+
', 'neon'));
37+
38+
Assert::same(
39+
array(
40+
'item1' => 11,
41+
'item2' => 2,
42+
),
43+
$compiler->getConfig()
44+
);

0 commit comments

Comments
 (0)