-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[DI] Allow to count on lazy collection arguments #21455
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1000,6 +1000,19 @@ public function resolveServices($value) | |
|
||
yield $k => $this->resolveServices($parameterBag->unescapeValue($parameterBag->resolveValue($v))); | ||
} | ||
}, function () use ($value) { | ||
$count = 0; | ||
foreach ($value->getValues() as $v) { | ||
foreach (self::getServiceConditionals($v) as $s) { | ||
if (!$this->has($s)) { | ||
continue 2; | ||
} | ||
} | ||
|
||
++$count; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. missing return statement? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Grumpf. Good catch. 😅 I need to add a test case. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
} | ||
|
||
return $count; | ||
}); | ||
} elseif ($value instanceof ClosureProxyArgument) { | ||
$parameterBag = $this->getParameterBag(); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\DependencyInjection\Tests\Argument; | ||
|
||
use Symfony\Component\DependencyInjection\Argument\RewindableGenerator; | ||
|
||
class RewindableGeneratorTest extends \PHPUnit_Framework_TestCase | ||
{ | ||
public function testImplementsCountable() | ||
{ | ||
$this->assertInstanceOf(\Countable::class, new RewindableGenerator(function () { | ||
yield 1; | ||
}, 1)); | ||
} | ||
|
||
public function testCountUsesProvidedValue() | ||
{ | ||
$generator = new RewindableGenerator(function () { | ||
yield 1; | ||
}, 3); | ||
|
||
$this->assertCount(3, $generator); | ||
} | ||
|
||
public function testCountUsesProvidedValueAsCallback() | ||
{ | ||
$called = 0; | ||
$generator = new RewindableGenerator(function () { | ||
yield 1; | ||
}, function () use (&$called) { | ||
++$called; | ||
|
||
return 3; | ||
}); | ||
|
||
$this->assertSame(0, $called, 'Count callback is called lazily'); | ||
$this->assertCount(3, $generator); | ||
|
||
count($generator); | ||
|
||
$this->assertSame(1, $called, 'Count callback is called only once'); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we need the local
$count
variable here?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we need it as calling
$this->count()
would fail below, but could be declared inside the if indeedThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No thinking about this again I would keep it as is.