Skip to content

Commit

Permalink
removed option 'parameters' used by generated factories (BC break)
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Dec 15, 2021
1 parent 038da8e commit 086748f
Show file tree
Hide file tree
Showing 11 changed files with 7 additions and 254 deletions.
49 changes: 3 additions & 46 deletions src/DI/Definitions/FactoryDefinition.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
use Nette\DI\Helpers;
use Nette\DI\ServiceCreationException;
use Nette\PhpGenerator as Php;
use Nette\Utils\Reflection;
use Nette\Utils\Type;


Expand All @@ -24,7 +23,6 @@ final class FactoryDefinition extends Definition
{
private const METHOD_CREATE = 'create';

public array $parameters = [];
private Definition $resultDefinition;


Expand Down Expand Up @@ -90,37 +88,6 @@ public function getResultDefinition(): Definition
}


/** @deprecated */
public function setParameters(array $params): static
{
if ($params) {
$old = $new = [];
foreach ($params as $k => $v) {
$tmp = explode(' ', is_int($k) ? $v : $k);
$old[] = '%' . end($tmp) . '%';
$new[] = '$' . end($tmp);
}

trigger_error(sprintf(
"[%s]\nOption 'parameters' is deprecated and should be removed. The %s should be replaced with %s in configuration.",
$this->getDescriptor(),
implode(', ', $old),
implode(', ', $new),
), E_USER_DEPRECATED);
}

$this->parameters = $params;
return $this;
}


/** @deprecated */
public function getParameters(): array
{
return $this->parameters;
}


public function resolveType(Nette\DI\Resolver $resolver): void
{
$interface = $this->getType();
Expand Down Expand Up @@ -159,9 +126,7 @@ public function complete(Nette\DI\Resolver $resolver): void
$resultDef = $this->resultDefinition;

if ($resultDef instanceof ServiceDefinition) {
if (!$this->parameters) {
$this->completeParameters($resolver);
}
$this->completeParameters($resolver);

$this->convertArguments($resultDef->getCreator()->arguments);
foreach ($resultDef->getSetup() as $setup) {
Expand Down Expand Up @@ -196,11 +161,10 @@ private function completeParameters(Nette\DI\Resolver $resolver): void
}

foreach ($method->getParameters() as $param) {
$methodType = Type::fromReflection($param);
if (isset($ctorParams[$param->name])) {
$ctorParam = $ctorParams[$param->name];
$ctorType = Type::fromReflection($ctorParam);
if ($ctorType && !$ctorType->allows((string) $methodType)) {
if ($ctorType && !$ctorType->allows((string) Type::fromReflection($param))) {
throw new ServiceCreationException(sprintf(
"Type of \$%s in %s::create() doesn't match type in %s constructor.",
$param->name,
Expand All @@ -219,13 +183,6 @@ private function completeParameters(Nette\DI\Resolver $resolver): void
$interface,
) . ($hint ? ", did you mean \${$hint}?" : '.'));
}

$paramDef = $methodType . ' ' . $param->name;
if ($param->isDefaultValueAvailable()) {
$this->parameters[$paramDef] = Reflection::getParameterDefaultValue($param);
} else {
$this->parameters[] = $paramDef;
}
}
}

Expand Down Expand Up @@ -261,7 +218,7 @@ public function generateMethod(Php\Method $method, Nette\DI\PhpGenerator $genera

$rm = new \ReflectionMethod($this->getType(), self::METHOD_CREATE);
$methodCreate
->setParameters($generator->convertParameters($this->parameters))
->setParameters(array_map([new Php\Factory, 'fromParameterReflection'], $rm->getParameters()))
->setReturnType((string) (Type::fromReflection($rm) ?? $this->getResultType()))
->setBody($body);

Expand Down
7 changes: 0 additions & 7 deletions src/DI/Extensions/DefinitionSchema.php
Original file line number Diff line number Diff line change
Expand Up @@ -179,12 +179,6 @@ private function sniffType($key, array $def): string
private function expandParameters(array $config): array
{
$params = $this->builder->parameters;
if (isset($config['parameters'])) {
foreach ((array) $config['parameters'] as $k => $v) {
$v = explode(' ', is_int($k) ? $v : $k);
$params[end($v)] = $this->builder::literal('$' . end($v));
}
}

return Nette\DI\Helpers::expand($config, $params);
}
Expand Down Expand Up @@ -240,7 +234,6 @@ private static function getFactorySchema(): Schema
'implement' => Expect::string(),
'arguments' => Expect::array(),
'setup' => Expect::listOf('callable|Nette\DI\Definitions\Statement|array:1'),
'parameters' => Expect::array(),
'references' => Expect::array(),
'tagged' => Expect::string(),
'inject' => Expect::bool(),
Expand Down
4 changes: 0 additions & 4 deletions src/DI/Extensions/ServicesExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -175,10 +175,6 @@ private function updateFactoryDefinition(Definitions\FactoryDefinition $definiti
}
}

if (isset($config->parameters)) {
$definition->setParameters($config->parameters);
}

if (isset($config->inject)) {
$definition->addTag(InjectExtension::TAG_INJECT, $config->inject);
}
Expand Down
23 changes: 0 additions & 23 deletions src/DI/PhpGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -187,29 +187,6 @@ public function formatPhp(string $statement, array $args): string
}


/**
* Converts parameters from Definition to PhpGenerator.
* @return Php\Parameter[]
*/
public function convertParameters(array $parameters): array
{
$res = [];
foreach ($parameters as $k => $v) {
$tmp = explode(' ', is_int($k) ? $v : $k);
$param = $res[] = new Php\Parameter(end($tmp));
if (!is_int($k)) {
$param->setDefaultValue($v);
}

if (isset($tmp[1])) {
$param->setType($tmp[0]);
}
}

return $res;
}


public function getClassName(): ?string
{
return $this->className;
Expand Down
75 changes: 0 additions & 75 deletions tests/DI/Compiler.generatedFactory.nullableParameters.phpt

This file was deleted.

1 change: 0 additions & 1 deletion tests/DI/Compiler.generatedFactory.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,6 @@ class TestExtension extends DI\CompilerExtension
{
$builder = $this->getContainerBuilder();
$builder->addFactoryDefinition('fooFactory2')
->setParameters(['Baz baz' => null])
->setImplement(IFooFactory::class)
->getResultDefinition()
->setCreator(Foo::class)
Expand Down
8 changes: 2 additions & 6 deletions tests/DI/Compiler.generatedFactory.returnTypes.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -34,18 +34,14 @@ class FooArticle extends Article
}

$compiler = new DI\Compiler;
// parameters are deprecated
$container = @createContainer($compiler, '
$container = createContainer($compiler, '
services:
article:
factory: Article(%title%)
factory: Article
implement: IArticleFactory
parameters: [title]
article2:
implement: IArticleFactory
arguments: [%title%]
parameters: [title]
article3:
implement: IArticleFactory
Expand Down
63 changes: 0 additions & 63 deletions tests/DI/Compiler.generatedFactory.scalarParameters.phpt

This file was deleted.

15 changes: 0 additions & 15 deletions tests/DI/ContainerBuilder.factory.params.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -26,32 +26,17 @@ $builder->addFactoryDefinition('one')
->setCreator(stdClass::class)
->addSetup('$a', [$builder::literal('$a')]);

@$builder->addFactoryDefinition('two')
->setParameters(['stdClass foo', 'array bar', 'foobar' => null]) // parameters is deprecated
->setImplement(StdClassFactory::class)
->getResultDefinition()
->setCreator(stdClass::class)
->addSetup('$a', [$builder::literal('$foo')]);

$builder->addDefinition('three')
->setType(stdClass::class);

$builder->addDefinition('four')
->setCreator('@one::create', [1 => [1]])
->setAutowired(false);

$builder->addDefinition('five')
->setCreator('@two::create', [1 => [1]])
->setAutowired(false);


$container = createContainer($builder);

Assert::type(StdClassFactory::class, $container->getService('one'));
Assert::type(StdClassFactory::class, $container->getService('two'));

Assert::type(stdClass::class, $container->getService('four'));
Assert::same($container->getService('four')->a, $container->getService('three'));

Assert::type(stdClass::class, $container->getService('five'));
Assert::same($container->getService('five')->a, $container->getService('three'));
9 changes: 0 additions & 9 deletions tests/DI/Definitions.FactoryDefinition.api.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -96,15 +96,6 @@ Assert::noError(function () {
});


test('', function () {
$def = new FactoryDefinition;
$def->setImplement(Good1::class);

@$def->setParameters(['a' => 1]); // parameters are deprecated
Assert::same(['a' => 1], $def->getParameters());
});


test('', function () {
$def = new FactoryDefinition;
$def->setImplement(Good1::class);
Expand Down
Loading

0 comments on commit 086748f

Please sign in to comment.