Skip to content

Commit 48fb5ce

Browse files
committed
ContainerBuilder::addDefinition(null) adds anonymous service
1 parent ceea028 commit 48fb5ce

File tree

4 files changed

+54
-27
lines changed

4 files changed

+54
-27
lines changed

src/DI/Config/Processor.php

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ public function loadDefinitions(array $services): void
175175
$this->updateDefinition($def, $config);
176176
}
177177
} catch (\Exception $e) {
178-
throw new ServiceCreationException("Service '$name': " . $e->getMessage(), 0, $e);
178+
throw new ServiceCreationException(($name ? "Service '$name': " : '') . $e->getMessage(), 0, $e);
179179
}
180180
}
181181

@@ -371,13 +371,10 @@ public function applyNamespace(array $services, string $namespace): array
371371
}
372372

373373

374-
private function createDefinitionName($name, array $config): string
374+
private function createDefinitionName($name, array $config): ?string
375375
{
376376
if (is_int($name)) {
377-
$counter = 1;
378-
do {
379-
$name = (string) $counter++;
380-
} while ($this->builder->hasDefinition($name));
377+
return null;
381378
} elseif (preg_match('#^@[\w\\\\]+\z#', $name)) {
382379
$name = $this->builder->getByType(substr($name, 1), true);
383380
}
@@ -399,13 +396,13 @@ private function prepareConfig(array $config): array
399396
}
400397

401398

402-
private function retrieveDefinition(string $name, array &$config): Definitions\Definition
399+
private function retrieveDefinition(?string $name, array &$config): Definitions\Definition
403400
{
404401
if (Helpers::takeParent($config)) {
405402
$this->builder->removeDefinition($name);
406403
}
407404

408-
if ($this->builder->hasDefinition($name)) {
405+
if ($name && $this->builder->hasDefinition($name)) {
409406
return $this->builder->getDefinition($name);
410407

411408
} elseif (isset($config['implement'], $config['references']) || isset($config['implement'], $config['tagged'])) {

src/DI/ContainerBuilder.php

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -57,25 +57,28 @@ public function __construct()
5757
* Adds new service definition.
5858
* @return Definitions\ServiceDefinition
5959
*/
60-
public function addDefinition(string $name, Definition $definition = null): Definition
60+
public function addDefinition(?string $name, Definition $definition = null): Definition
6161
{
6262
$this->needsResolve = true;
63-
if (!$name) { // builder is not ready for falsy names such as '0'
63+
if ($name === null) {
64+
for ($name = 1; isset($this->definitions[$name]); $name++);
65+
$name = (string) $name;
66+
} elseif (!$name) { // builder is not ready for falsy names such as '0'
6467
throw new Nette\InvalidArgumentException(sprintf('Service name must be a non-empty string, %s given.', gettype($name)));
65-
}
66-
$name = $this->aliases[$name] ?? $name;
67-
if (isset($this->definitions[$name])) {
68-
throw new Nette\InvalidStateException("Service '$name' has already been added.");
69-
}
70-
$lname = strtolower($name);
71-
foreach ($this->definitions as $nm => $foo) {
72-
if ($lname === strtolower((string) $nm)) {
73-
throw new Nette\InvalidStateException("Service '$name' has the same name as '$nm' in a case-insensitive manner.");
68+
} else {
69+
$name = $this->aliases[$name] ?? $name;
70+
if (isset($this->definitions[$name])) {
71+
throw new Nette\InvalidStateException("Service '$name' has already been added.");
72+
}
73+
$lname = strtolower($name);
74+
foreach ($this->definitions as $nm => $foo) {
75+
if ($lname === strtolower((string) $nm)) {
76+
throw new Nette\InvalidStateException("Service '$name' has the same name as '$nm' in a case-insensitive manner.");
77+
}
7478
}
7579
}
76-
if (!$definition) {
77-
$definition = new Definitions\ServiceDefinition;
78-
}
80+
81+
$definition = $definition ?: new Definitions\ServiceDefinition;
7982
$definition->setName($name);
8083
$definition->setNotifier(function (): void {
8184
$this->needsResolve = true;
@@ -84,25 +87,25 @@ public function addDefinition(string $name, Definition $definition = null): Defi
8487
}
8588

8689

87-
public function addAccessorDefinition(string $name): Definitions\AccessorDefinition
90+
public function addAccessorDefinition(?string $name): Definitions\AccessorDefinition
8891
{
8992
return $this->addDefinition($name, new Definitions\AccessorDefinition);
9093
}
9194

9295

93-
public function addFactoryDefinition(string $name): Definitions\FactoryDefinition
96+
public function addFactoryDefinition(?string $name): Definitions\FactoryDefinition
9497
{
9598
return $this->addDefinition($name, new Definitions\FactoryDefinition);
9699
}
97100

98101

99-
public function addLocatorDefinition(string $name): Definitions\LocatorDefinition
102+
public function addLocatorDefinition(?string $name): Definitions\LocatorDefinition
100103
{
101104
return $this->addDefinition($name, new Definitions\LocatorDefinition);
102105
}
103106

104107

105-
public function addImportedDefinition(string $name): Definitions\ImportedDefinition
108+
public function addImportedDefinition(?string $name): Definitions\ImportedDefinition
106109
{
107110
return $this->addDefinition($name, new Definitions\ImportedDefinition);
108111
}

tests/DI/Compiler.unknownDefinitionKey.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,4 @@ Assert::throws(function () {
2222
setups: []
2323
foo: bar
2424
');
25-
}, Nette\InvalidStateException::class, "Service '1': Unknown key 'autowire', 'setups', 'foo' in definition of service, did you mean 'autowired', 'setup'?");
25+
}, Nette\InvalidStateException::class, "Unknown key 'autowire', 'setups', 'foo' in definition of service, did you mean 'autowired', 'setup'?");
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use Nette\DI;
6+
use Tester\Assert;
7+
8+
9+
require __DIR__ . '/../bootstrap.php';
10+
11+
12+
$builder = new DI\ContainerBuilder;
13+
$builder->addDefinition('1')
14+
->setFactory(stdClass::class);
15+
16+
$builder->addDefinition(null)
17+
->setFactory(stdClass::class);
18+
19+
$builder->addDefinition(null)
20+
->setFactory(stdClass::class);
21+
22+
23+
$container = createContainer($builder);
24+
25+
Assert::type(stdClass::class, $container->getService('1'));
26+
Assert::type(stdClass::class, $container->getService('2'));
27+
Assert::type(stdClass::class, $container->getService('3'));

0 commit comments

Comments
 (0)