Skip to content

Commit

Permalink
Resolver: fixed collision of references and autowired arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Oct 10, 2018
1 parent b1c542e commit 41deac3
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 14 deletions.
33 changes: 19 additions & 14 deletions src/DI/Resolver.php
Expand Up @@ -298,6 +298,25 @@ public function completeStatement(Statement $statement): Statement
$arguments = $statement->arguments;
$definitions = $this->builder->getDefinitions();

// @reference -> object
array_walk_recursive($arguments, function (&$val): void {
if (is_string($val) && strlen($val) > 1 && $val[0] === '@' && $val[1] !== '@') {
$pair = explode('::', substr($val, 1), 2);
if (!isset($pair[1])) { // @service
$val = new Reference($pair[0]);
} elseif (preg_match('#^[A-Z][A-Z0-9_]*\z#', $pair[1], $m)) { // @service::CONSTANT
$ref = $this->resolveServiceReference(new Reference($pair[0]));
$val = ContainerBuilder::literal($this->builder->getDefinition($ref->getName())->getType() . '::' . $pair[1]);
} else { // @service::property
$val = new Statement([new Reference($pair[0]), '$' . $pair[1]]);
}

} elseif (is_string($val) && substr($val, 0, 2) === '@@') { // escaped text @@
$val = substr($val, 1);
}
});


if (is_string($entity) && Strings::contains($entity, '?')) { // PHP literal

} elseif ($entity instanceof Reference) { // factory calling
Expand Down Expand Up @@ -371,20 +390,6 @@ public function completeStatement(Statement $statement): Statement

} elseif ($val instanceof Reference) {
$val = $this->resolveServiceReference($val);

} elseif (is_string($val) && strlen($val) > 1 && $val[0] === '@' && $val[1] !== '@') {
$pair = explode('::', $val, 2);
$serviceRef = $this->resolveServiceReference($pair[0]);
if (!isset($pair[1])) { // @service
$val = $serviceRef;
} elseif (preg_match('#^[A-Z][A-Z0-9_]*\z#', $pair[1], $m)) { // @service::CONSTANT
$val = ContainerBuilder::literal($this->builder->getDefinition($serviceRef->getName())->getType() . '::' . $pair[1]);
} else { // @service::property
$val = new Statement([$serviceRef, '$' . $pair[1]]);
}

} elseif (is_string($val) && substr($val, 0, 2) === '@@') { // escaped text @@
$val = substr($val, 1);
}
});

Expand Down
40 changes: 40 additions & 0 deletions tests/DI/Compiler.referenceBug.phpt
@@ -0,0 +1,40 @@
<?php

/**
* Test: Nette\DI\Compiler and service referencing.
*/

declare(strict_types=1);

use Nette\DI;
use Tester\Assert;


require __DIR__ . '/../bootstrap.php';


class Lorem
{
/** @array */
public $args;


public function __construct($arg1 = '@foo', $arg2 = '@@foo', $arg3 = '@\stdClass')
{
$this->args = func_get_args();
}
}


$container = createContainer(new DI\Compiler, '
services:
- stdClass
a: Lorem(3 = true)
b: Lorem(3 = Lorem(3 = true))
c: Lorem(@@test)
');


Assert::same(['@foo', '@@foo', '@\stdClass', true], $container->getService('a')->args);
Assert::equal(['@foo', '@@foo', '@\stdClass', new Lorem('@foo', '@@foo', '@\stdClass', true)], $container->getService('b')->args);
Assert::same(['@test'], $container->getService('c')->args);

0 comments on commit 41deac3

Please sign in to comment.