Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[DI] Reintroduce !service_locator in favor of !iterator #23542

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +11,43 @@

namespace Symfony\Component\DependencyInjection\Argument;

use Psr\Container\ContainerInterface;

/**
* @internal
*/
class RewindableGenerator implements \IteratorAggregate, \Countable
class RewindableGenerator implements ContainerInterface, \IteratorAggregate, \Countable
{
private $factory;
private $generator;
private $count;

/**
* @param callable $factory
* @param callable $generator
* @param int|callable $count
*/
public function __construct(callable $generator, $count)
public function __construct(callable $factory, callable $generator, $count)
{
$this->factory = $factory;
$this->generator = $generator;
$this->count = $count;
}

public function has($id)
{
$factory = $this->factory;

return $factory($id, true);
}

public function get($id)
{
$factory = $this->factory;

return $factory($id);
}

public function getIterator()
{
$g = $this->generator;
Expand Down
23 changes: 22 additions & 1 deletion src/Symfony/Component/DependencyInjection/ContainerBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -1132,7 +1132,28 @@ public function resolveServices($value)
return $this->resolveServices($reference);
};
} elseif ($value instanceof IteratorArgument) {
$value = new RewindableGenerator(function () use ($value) {
$value = new RewindableGenerator(function ($k, $bool = false) use ($value) {
$values = $value->getValues();
if (!isset($values[$k])) {
if ($bool) {
return false;
}

throw new ServiceNotFoundException($k, null, null, array_keys($values));
}
if ($bool) {
return true;
}
if (true === $v = $values[$k]) {
throw new ServiceCircularReferenceException($k, array($k, $k));
}
$values[$k] = true;
try {
return $this->resolveServices($v);
} finally {
$values[$k] = $v;
}
}, function () use ($value) {
foreach ($value->getValues() as $k => $v) {
foreach (self::getServiceConditionals($v) as $s) {
if (!$this->has($s)) {
Expand Down
23 changes: 18 additions & 5 deletions src/Symfony/Component/DependencyInjection/Dumper/PhpDumper.php
Original file line number Diff line number Diff line change
Expand Up @@ -784,6 +784,7 @@ private function startClass($class, $baseClass, $namespace)
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
use Symfony\Component\DependencyInjection\Exception\LogicException;
use Symfony\Component\DependencyInjection\Exception\RuntimeException;
use Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException;
$bagClass

/*{$this->docStar}
Expand Down Expand Up @@ -1430,26 +1431,38 @@ private function dumpValue($value, $interpolate = true)
if ($value instanceof IteratorArgument) {
$operands = array(0);
$code = array();
$code[] = 'new RewindableGenerator(function () {';
$code[] = 'new RewindableGenerator(function ($k, $b = false) {';

if (!$values = $value->getValues()) {
$code[] = ' if ($b) { return false; }';
$code[] = ' throw new ServiceNotFoundException($k);';
$code[] = ' }, function() {';
$code[] = ' return new \EmptyIterator();';
} else {
$countCode = array();
$countCode[] = 'function () {';
$code[] = ' switch ($k) {';
$genCode = array();
$countCode = array('function () {');

foreach ($values as $k => $v) {
($c = $this->getServiceConditionals($v)) ? $operands[] = "(int) ($c)" : ++$operands[0];
$v = $this->wrapServiceConditionals($v, sprintf(" yield %s => %s;\n", $this->dumpValue($k, $interpolate), $this->dumpValue($v, $interpolate)));
$v = $this->wrapServiceConditionals($v, sprintf(" yield %s => %s;\n", $this->dumpValue($k, $interpolate), $vDump = $this->dumpValue($v, $interpolate)));
$code[] = sprintf(' case %s: $o = %s; break;', $this->dumpValue($k, false), $vDump);
foreach (explode("\n", $v) as $v) {
if ($v) {
$code[] = ' '.$v;
$genCode[] = ' '.$v;
}
}
}

$countCode[] = sprintf(' return %s;', implode(' + ', $operands));
$countCode[] = ' }';

$code[] = ' }';
$code[] = ' if ($b) { return isset($o); }';
$code[] = ' if (isset($o)) { return $o; }';
$code[] = sprintf(' throw new ServiceNotFoundException($k, null, null, %s);', $this->dumpValue(array_keys($values), false));
$code[] = ' }, function() {';
$code[] = implode("\n", $genCode);
}

$code[] = sprintf(' }, %s)', count($operands) > 1 ? implode("\n", $countCode) : $operands[0]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,16 @@ class RewindableGeneratorTest extends TestCase
{
public function testImplementsCountable()
{
$this->assertInstanceOf(\Countable::class, new RewindableGenerator(function () {
$this->assertInstanceOf(\Countable::class, new RewindableGenerator(function ($k, $b = false) {
}, function () {
yield 1;
}, 1));
}

public function testCountUsesProvidedValue()
{
$generator = new RewindableGenerator(function () {
$generator = new RewindableGenerator(function ($k, $b = false) {
}, function () {
yield 1;
}, 3);

Expand All @@ -35,7 +37,8 @@ public function testCountUsesProvidedValue()
public function testCountUsesProvidedValueAsCallback()
{
$called = 0;
$generator = new RewindableGenerator(function () {
$generator = new RewindableGenerator(function ($k, $b = false) {
}, function () {
yield 1;
}, function () use (&$called) {
++$called;
Expand All @@ -50,4 +53,18 @@ public function testCountUsesProvidedValueAsCallback()

$this->assertSame(1, $called, 'Count callback is called only once');
}

public function testPsrContainer()
{
$generator = new RewindableGenerator(function ($k, $b = false) {
return $b ? 'a' === $k : ('a' === $k ? 0 : 1);
}, function () {
yield 'a' => 0;
}, 1);

$this->assertTrue($generator->has('a'));
$this->assertFalse($generator->has('b'));
$this->assertSame(0, $generator->get('a'));
$this->assertSame(1, $generator->get('b'));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
use Symfony\Component\DependencyInjection\Exception\LogicException;
use Symfony\Component\DependencyInjection\Exception\RuntimeException;
use Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException;
use Symfony\Component\DependencyInjection\ParameterBag\FrozenParameterBag;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
use Symfony\Component\DependencyInjection\Exception\LogicException;
use Symfony\Component\DependencyInjection\Exception\RuntimeException;
use Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException;
use Symfony\Component\DependencyInjection\ParameterBag\FrozenParameterBag;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
use Symfony\Component\DependencyInjection\Exception\LogicException;
use Symfony\Component\DependencyInjection\Exception\RuntimeException;
use Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException;
use Symfony\Component\DependencyInjection\ParameterBag\FrozenParameterBag;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
use Symfony\Component\DependencyInjection\Exception\LogicException;
use Symfony\Component\DependencyInjection\Exception\RuntimeException;
use Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException;
use Symfony\Component\DependencyInjection\ParameterBag\FrozenParameterBag;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
use Symfony\Component\DependencyInjection\Exception\LogicException;
use Symfony\Component\DependencyInjection\Exception\RuntimeException;
use Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException;
use Symfony\Component\DependencyInjection\ParameterBag\FrozenParameterBag;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
use Symfony\Component\DependencyInjection\Exception\LogicException;
use Symfony\Component\DependencyInjection\Exception\RuntimeException;
use Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException;
use Symfony\Component\DependencyInjection\ParameterBag\FrozenParameterBag;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
use Symfony\Component\DependencyInjection\Exception\LogicException;
use Symfony\Component\DependencyInjection\Exception\RuntimeException;
use Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException;
use Symfony\Component\DependencyInjection\ParameterBag\FrozenParameterBag;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
use Symfony\Component\DependencyInjection\Exception\LogicException;
use Symfony\Component\DependencyInjection\Exception\RuntimeException;
use Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException;
use Symfony\Component\DependencyInjection\ParameterBag\FrozenParameterBag;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
use Symfony\Component\DependencyInjection\Exception\LogicException;
use Symfony\Component\DependencyInjection\Exception\RuntimeException;
use Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException;
use Symfony\Component\DependencyInjection\ParameterBag\FrozenParameterBag;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
use Symfony\Component\DependencyInjection\Exception\LogicException;
use Symfony\Component\DependencyInjection\Exception\RuntimeException;
use Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException;
use Symfony\Component\DependencyInjection\ParameterBag\FrozenParameterBag;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
use Symfony\Component\DependencyInjection\Exception\LogicException;
use Symfony\Component\DependencyInjection\Exception\RuntimeException;
use Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException;
use Symfony\Component\DependencyInjection\ParameterBag\FrozenParameterBag;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
use Symfony\Component\DependencyInjection\Exception\LogicException;
use Symfony\Component\DependencyInjection\Exception\RuntimeException;
use Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag;

/**
Expand Down Expand Up @@ -304,10 +305,21 @@ protected function getFooWithInlineService()
*/
protected function getLazyContextService()
{
return $this->services['lazy_context'] = new \LazyContext(new RewindableGenerator(function () {
return $this->services['lazy_context'] = new \LazyContext(new RewindableGenerator(function ($k, $b = false) {
switch ($k) {
case 'k1': $o = ${($_ = isset($this->services['foo.baz']) ? $this->services['foo.baz'] : $this->get('foo.baz')) && false ?: '_'}; break;
case 'k2': $o = $this; break;
}
if ($b) { return isset($o); }
if (isset($o)) { return $o; }
throw new ServiceNotFoundException($k, null, null, array(0 => 'k1', 1 => 'k2'));
}, function() {
yield 'k1' => ${($_ = isset($this->services['foo.baz']) ? $this->services['foo.baz'] : $this->get('foo.baz')) && false ?: '_'};
yield 'k2' => $this;
}, 2), new RewindableGenerator(function () {
}, 2), new RewindableGenerator(function ($k, $b = false) {
if ($b) { return false; }
throw new ServiceNotFoundException($k);
}, function() {
return new \EmptyIterator();
}, 0));
}
Expand All @@ -322,14 +334,25 @@ protected function getLazyContextService()
*/
protected function getLazyContextIgnoreInvalidRefService()
{
return $this->services['lazy_context_ignore_invalid_ref'] = new \LazyContext(new RewindableGenerator(function () {
return $this->services['lazy_context_ignore_invalid_ref'] = new \LazyContext(new RewindableGenerator(function ($k, $b = false) {
switch ($k) {
case 0: $o = ${($_ = isset($this->services['foo.baz']) ? $this->services['foo.baz'] : $this->get('foo.baz')) && false ?: '_'}; break;
case 1: $o = $this->get('invalid', ContainerInterface::NULL_ON_INVALID_REFERENCE); break;
}
if ($b) { return isset($o); }
if (isset($o)) { return $o; }
throw new ServiceNotFoundException($k, null, null, array(0 => 0, 1 => 1));
}, function() {
yield 0 => ${($_ = isset($this->services['foo.baz']) ? $this->services['foo.baz'] : $this->get('foo.baz')) && false ?: '_'};
if ($this->has('invalid')) {
yield 1 => $this->get('invalid', ContainerInterface::NULL_ON_INVALID_REFERENCE);
}
}, function () {
return 1 + (int) ($this->has('invalid'));
}), new RewindableGenerator(function () {
}), new RewindableGenerator(function ($k, $b = false) {
if ($b) { return false; }
throw new ServiceNotFoundException($k);
}, function() {
return new \EmptyIterator();
}, 0));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
use Symfony\Component\DependencyInjection\Exception\LogicException;
use Symfony\Component\DependencyInjection\Exception\RuntimeException;
use Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException;
use Symfony\Component\DependencyInjection\ParameterBag\FrozenParameterBag;

/**
Expand Down Expand Up @@ -307,10 +308,21 @@ protected function getFooWithInlineService()
*/
protected function getLazyContextService()
{
return $this->services['lazy_context'] = new \LazyContext(new RewindableGenerator(function () {
return $this->services['lazy_context'] = new \LazyContext(new RewindableGenerator(function ($k, $b = false) {
switch ($k) {
case 'k1': $o = ${($_ = isset($this->services['foo.baz']) ? $this->services['foo.baz'] : $this->get('foo.baz')) && false ?: '_'}; break;
case 'k2': $o = $this; break;
}
if ($b) { return isset($o); }
if (isset($o)) { return $o; }
throw new ServiceNotFoundException($k, null, null, array(0 => 'k1', 1 => 'k2'));
}, function() {
yield 'k1' => ${($_ = isset($this->services['foo.baz']) ? $this->services['foo.baz'] : $this->get('foo.baz')) && false ?: '_'};
yield 'k2' => $this;
}, 2), new RewindableGenerator(function () {
}, 2), new RewindableGenerator(function ($k, $b = false) {
if ($b) { return false; }
throw new ServiceNotFoundException($k);
}, function() {
return new \EmptyIterator();
}, 0));
}
Expand All @@ -325,9 +337,19 @@ protected function getLazyContextService()
*/
protected function getLazyContextIgnoreInvalidRefService()
{
return $this->services['lazy_context_ignore_invalid_ref'] = new \LazyContext(new RewindableGenerator(function () {
return $this->services['lazy_context_ignore_invalid_ref'] = new \LazyContext(new RewindableGenerator(function ($k, $b = false) {
switch ($k) {
case 0: $o = ${($_ = isset($this->services['foo.baz']) ? $this->services['foo.baz'] : $this->get('foo.baz')) && false ?: '_'}; break;
}
if ($b) { return isset($o); }
if (isset($o)) { return $o; }
throw new ServiceNotFoundException($k, null, null, array(0 => 0));
}, function() {
yield 0 => ${($_ = isset($this->services['foo.baz']) ? $this->services['foo.baz'] : $this->get('foo.baz')) && false ?: '_'};
}, 1), new RewindableGenerator(function () {
}, 1), new RewindableGenerator(function ($k, $b = false) {
if ($b) { return false; }
throw new ServiceNotFoundException($k);
}, function() {
return new \EmptyIterator();
}, 0));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
use Symfony\Component\DependencyInjection\Exception\LogicException;
use Symfony\Component\DependencyInjection\Exception\RuntimeException;
use Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException;
use Symfony\Component\DependencyInjection\ParameterBag\FrozenParameterBag;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
use Symfony\Component\DependencyInjection\Exception\LogicException;
use Symfony\Component\DependencyInjection\Exception\RuntimeException;
use Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException;
use Symfony\Component\DependencyInjection\ParameterBag\FrozenParameterBag;

/**
Expand Down
Loading