Skip to content

Commit 1a04219

Browse files
committedSep 29, 2021
FactoryDefinition: added support for syntax $params in definition
1 parent f36567e commit 1a04219

File tree

2 files changed

+19
-4
lines changed

2 files changed

+19
-4
lines changed
 

‎src/DI/Definitions/FactoryDefinition.php

+18-3
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use Nette;
1313
use Nette\DI\Helpers;
1414
use Nette\DI\ServiceCreationException;
15+
use Nette\PhpGenerator as Php;
1516
use Nette\Utils\Reflection;
1617
use Nette\Utils\Type;
1718

@@ -206,6 +207,10 @@ public function complete(Nette\DI\Resolver $resolver): void
206207
if (!$this->parameters) {
207208
$this->completeParameters($resolver);
208209
}
210+
$this->convertArguments($resultDef->getFactory()->arguments);
211+
foreach ($resultDef->getSetup() as $setup) {
212+
$this->convertArguments($setup->arguments);
213+
}
209214

210215
if ($resultDef->getEntity() instanceof Reference && !$resultDef->getFactory()->arguments) {
211216
$resultDef->setFactory([ // render as $container->createMethod()
@@ -247,7 +252,7 @@ private function completeParameters(Nette\DI\Resolver $resolver): void
247252
$class
248253
));
249254
}
250-
$this->resultDefinition->getFactory()->arguments[$ctorParam->getPosition()] = Nette\DI\ContainerBuilder::literal('$' . $ctorParam->name);
255+
$this->resultDefinition->getFactory()->arguments[$ctorParam->getPosition()] = new Php\Literal('$' . $ctorParam->name);
251256

252257
} elseif (!$this->resultDefinition->getSetup()) {
253258
$hint = Nette\Utils\Helpers::getSuggestion(array_keys($ctorParams), $param->name);
@@ -268,9 +273,19 @@ private function completeParameters(Nette\DI\Resolver $resolver): void
268273
}
269274

270275

271-
public function generateMethod(Nette\PhpGenerator\Method $method, Nette\DI\PhpGenerator $generator): void
276+
public function convertArguments(array &$args): void
277+
{
278+
foreach ($args as &$v) {
279+
if (is_string($v) && $v[0] === '$') {
280+
$v = new Php\Literal($v);
281+
}
282+
}
283+
}
284+
285+
286+
public function generateMethod(Php\Method $method, Nette\DI\PhpGenerator $generator): void
272287
{
273-
$class = (new Nette\PhpGenerator\ClassType)
288+
$class = (new Php\ClassType)
274289
->addImplement($this->getType());
275290

276291
$class->addProperty('container')

‎tests/DI/files/compiler.generatedFactory.neon

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ services:
1717

1818
article2:
1919
implement: IArticleFactory
20-
arguments: [%title%]
20+
arguments: [$title]
2121
parameters: [title]
2222

2323
fooFactory1:

0 commit comments

Comments
 (0)
Please sign in to comment.