Skip to content

Commit

Permalink
LocatorDefinition: removed support for create($name) methods (BC break)
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Apr 28, 2024
1 parent 95e4b47 commit 23664f3
Show file tree
Hide file tree
Showing 4 changed files with 4 additions and 104 deletions.
8 changes: 3 additions & 5 deletions src/DI/Definitions/LocatorDefinition.php
Expand Up @@ -35,7 +35,7 @@ public function setImplement(string $interface): static

foreach ($methods as $method) {
if ($method->isStatic() || !(
(preg_match('#^(get|create)$#', $method->name) && $method->getNumberOfParameters() === 1)
($method->name === 'get' && $method->getNumberOfParameters() === 1)
|| (preg_match('#^(get|create)[A-Z]#', $method->name) && $method->getNumberOfParameters() === 0)
)) {
throw new Nette\InvalidArgumentException(sprintf(
Expand All @@ -46,15 +46,13 @@ public function setImplement(string $interface): static
));
}

if ($method->getNumberOfParameters() === 0) {
if ($method->name !== 'get') {
Nette\DI\Helpers::ensureClassType(
Nette\Utils\Type::fromReflection($method),
"return type of $interface::$method->name()",
$this->getDescriptor(),
allowNullable: true,
);
} elseif (str_starts_with($method->name, 'create')) {
trigger_error(sprintf("Service '%s': Method %s::create(\$name) is deprecated, use createFoo().", $this->getName(), $interface), E_USER_DEPRECATED);
}
}

Expand Down Expand Up @@ -155,7 +153,7 @@ public function generateMethod(Nette\PhpGenerator\Method $method, Nette\DI\PhpGe
$methodInner->setBody('if (!isset($this->mapping[$name])) {
' . ($nullable ? 'return null;' : 'throw new Nette\DI\MissingServiceException("Service \'$name\' is not defined.");') . '
}
return $this->container->' . $m[1] . 'Service($this->mapping[$name]);')
return $this->container->getService($this->mapping[$name]);')
->addParameter('name');

} elseif (isset($this->references[$name])) {
Expand Down
23 changes: 1 addition & 22 deletions tests/DI/Compiler.generatedLocator.phpt
Expand Up @@ -42,8 +42,7 @@ interface LocatorFactoryN
}


// create($name) is deprecated
$container = @createContainer(new DI\Compiler, '
$container = createContainer(new DI\Compiler, '
services:
- LoremChild
Expand All @@ -64,10 +63,7 @@ services:
one: Locator(a: @lorem1, b: LoremChild)
two: Locator(tagged: a)
three: LocatorFactory(a: @lorem1, b: LoremChild)
four: LocatorFactory(tagged: b)
five: LocatorN(tagged: a)
six: LocatorFactoryN(tagged: a)
seven: Locator(a: @lorem1)
eight: Locator(a: LoremChild())
');
Expand Down Expand Up @@ -96,29 +92,12 @@ Assert::exception(
"Service '3' is not defined.",
);

// factory
$three = $container->getService('three');
Assert::type(Lorem::class, $three->create('a'));
Assert::type(LoremChild::class, $three->create('b'));
Assert::notSame($three->create('a'), $three->create('a'));

// tagged factory
$four = $container->getService('four');
Assert::type(Lorem::class, $four->create('3'));
Assert::notSame($container->getService('lorem3'), $four->create('3'));

// nullable accessor
$five = $container->getService('five');
Assert::type(Lorem::class, $five->get('1'));
Assert::type(Lorem::class, $five->get('2'));
Assert::null($five->get('3'));

// nullable factory
$six = $container->getService('six');
Assert::type(Lorem::class, $six->create('1'));
Assert::type(Lorem::class, $six->create('2'));
Assert::null($six->create('3'));

// accessor with one service
$one = $container->getService('seven');
Assert::type(Lorem::class, $one->get('a'));
Expand Down
13 changes: 0 additions & 13 deletions tests/DI/Definitions.LocatorDefinition.api.phpt
Expand Up @@ -50,11 +50,6 @@ interface Good1
public function get($name);
}

interface Good2
{
public function create($name);
}

interface Good3
{
public function createA(): stdClass;
Expand Down Expand Up @@ -127,14 +122,6 @@ Assert::noError(function () {
});


Assert::noError(function () {
$def = new LocatorDefinition;
@$def->setImplement(Good2::class); // create($name) is deprecated
Assert::same(Good2::class, $def->getImplement());
Assert::same(Good2::class, $def->getType());
});


Assert::noError(function () {
$def = new LocatorDefinition;
$def->setImplement(Good3::class);
Expand Down
64 changes: 0 additions & 64 deletions tests/DI/Definitions.LocatorDefinition.render.create.phpt

This file was deleted.

0 comments on commit 23664f3

Please sign in to comment.