8000 [DI] Fix inlining conflict by restricting IteratorArgument to Referen… · symfony/symfony@4d3dce1 · GitHub
[go: up one dir, main page]

Skip to content

Commit 4d3dce1

Browse files
[DI] Fix inlining conflict by restricting IteratorArgument to Reference[]
1 parent 9c0067b commit 4d3dce1

File tree

14 files changed

+86
-91
lines changed

14 files changed

+86
-91
lines changed

src/Symfony/Component/DependencyInjection/Argument/IteratorArgument.php

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@
1111

1212
namespace Symfony\Component\DependencyInjection\Argument;
1313

14+
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
15+
use Symfony\Component\DependencyInjection\Reference;
16+
1417
/**
1518
* Represents a collection of values to lazily iterate over.
1619
*
@@ -20,9 +23,12 @@ class IteratorArgument implements ArgumentInterface
2023
{
2124
private $values;
2225

26+
/**
27+
* @param Reference[] $values
28+
*/
2329
public function __construct(array $values)
2430
{
25-
$this->values = $values;
31+
$this->setValues($values);
2632
}
2733

2834
/**
@@ -34,10 +40,16 @@ public function getValues()
3440
}
3541

3642
/**
37-
* @param array $values The values to lazily iterate over
43+
* @param Reference[] $values The service references to lazily iterate over
3844
*/
3945
public function setValues(array $values)
4046
{
47+
foreach ($values as $k => $v) {
48+
if (null !== $v && !$v instanceof Reference) {
49+
throw new InvalidArgumentException(sprintf('An IteratorArgument must hold only Reference instances, "%s" given.', is_object($v) ? get_class($v) : gettype($v)));
50+
}
51+
}
52+
4153
$this->values = $values;
4254
}
4355
}

src/Symfony/Component/DependencyInjection/Compiler/InlineServiceDefinitionsPass.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,7 @@ public function setRepeatedPass(RepeatedPass $repeatedPass)
3838
protected function processValue($value, $isRoot = false)
3939
{
4040
if ($value instanceof ArgumentInterface) {
41-
$this->processValue($value->getValues());
42-
41+
// Reference found in ArgumentInterface::getValues() are not inlineable
4342
return $value;
4443
}
4544
if ($value instanceof Reference && $this->container->hasDefinition($id = (string) $value)) {

src/Symfony/Component/DependencyInjection/ContainerBuilder.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1125,16 +1125,15 @@ public function resolveServices($value)
11251125
return $this->resolveServices($reference);
11261126
};
11271127
} elseif ($value instanceof IteratorArgument) {
1128-
$parameterBag = $this->getParameterBag();
1129-
$value = new RewindableGenerator(function () use ($value, $parameterBag) {
1128+
$value = new RewindableGenerator(function () use ($value) {
11301129
foreach ($value->getValues() as $k => $v) {
11311130
foreach (self::getServiceConditionals($v) as $s) {
11321131
if (!$this->has($s)) {
11331132
continue 2;
11341133
}
11351134
}
11361135

1137-
yield $k => $this->resolveServices($parameterBag->unescapeValue($parameterBag->resolveValue($v)));
1136+
yield $k => $this->resolveServices($v);
11381137
}
11391138
}, function () use ($value) {
11401139
$count = 0;
@@ -1409,7 +1408,7 @@ private function callMethod($service, $call)
14091408
* Shares a given service in the container.
14101409
*
14111410
* @param Definition $definition
1412-
* @param mixed $service
1411+
* @param object $service
14131412
* @param string|null $id
14141413
*/
14151414
private function shareService(Definition $definition, $service, $id)

src/Symfony/Component/DependencyInjection/Dumper/PhpDumper.php

Lines changed: 38 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -202,22 +202,20 @@ private function getProxyDumper()
202202
/**
203203
* Generates Service local temp variables.
204204
*
205-
* @param string $cId
206-
* @param string $definition
205+
* @param string $cId
206+
* @param Definition $definition
207+
* @param array $inlinedDefinitions
207208
*
208209
* @return string
209210
*/
210-
private function addServiceLocalTempVariables($cId, $definition)
211+
private function addServiceLocalTempVariables($cId, Definition $definition, array $inlinedDefinitions)
211212
{
212213
static $template = " \$%s = %s;\n";
213214

214-
$localDefinitions = array_merge(
215-
array($definition),
216-
$this->getInlinedDefinitions($definition)
217-
);
215+
array_unshift($inlinedDefinitions, $definition);
218216

219217
$calls = $behavior = array();
220-
foreach ($localDefinitions as $iDefinition) {
218+
foreach ($inlinedDefinitions as $iDefinition) {
221219
$this->getServiceCallsFromArguments($iDefinition->getArguments(), $calls, $behavior);
222220
$this->getServiceCallsFromArguments($iDefinition->getMethodCalls(), $calls, $behavior);
223221
$this->getServiceCallsFromArguments($iDefinition->getProperties(), $calls, $behavior);
@@ -280,12 +278,13 @@ private function addProxyClasses()
280278
/**
281279
* Generates the require_once statement for service includes.
282280
*
283-
* @param string $id The service id
281+
* @param string $id
284282
* @param Definition $definition
283+
* @param array $inlinedDefinitions
285284
*
286285
* @return string
287286
*/
288-
private function addServiceInclude($id, $definition)
287+
private function addServiceInclude($id, Definition $definition, array $inlinedDefinitions)
289288
{
290289
$template = " require_once %s;\n";
291290
$code = '';
@@ -294,7 +293,7 @@ private function addServiceInclude($id, $definition)
294293
$code .= sprintf($template, $this->dumpValue($file));
295294
}
296295

297-
foreach ($this->getInlinedDefinitions($definition) as $definition) {
296+
foreach ($inlinedDefinitions as $definition) {
298297
if (null !== $file = $definition->getFile()) {
299298
$code .= sprintf($template, $this->dumpValue($file));
300299
}
@@ -310,21 +309,20 @@ private function addServiceInclude($id, $definition)
310309
/**
311310
* Generates the inline definition of a service.
312311
*
313-
* @param string $id
314-
* @param Definition $definition
312+
* @param string $id
313+
* @param array $inlinedDefinitions
315314
*
316315
* @return string
317316
*
318317
* @throws RuntimeException When the factory definition is incomplete
319318
* @throws ServiceCircularReferenceException When a circular reference is detected
320319
*/
321-
private function addServiceInlinedDefinitions($id, $definition)
320+
private function addServiceInlinedDefinitions($id, array $inlinedDefinitions)
322321
{
323322
$code = '';
324323
$variableMap = $this->definitionVariables;
325324
$nbOccurrences = new \SplObjectStorage();
326325
$processed = new \SplObjectStorage();
327-
$inlinedDefinitions = $this->getInlinedDefinitions($definition);
328326

329327
foreach ($inlinedDefinitions as $definition) {
330328
if (false === $nbOccurrences->contains($definition)) {
@@ -375,14 +373,14 @@ private function addServiceInlinedDefinitions($id, $definition)
375373
/**
376374
* Adds the service return statement.
377375
*
378-
* @param string $id Service id
379-
* @param Definition $definition
376+
* @param string $id
377+
* @param bool $isSimpleInstance
380378
*
381379
* @return string
382380
*/
383-
private function addServiceReturn($id, $definition)
381+
private function addServiceReturn($id, $isSimpleInstance)
384382
{
385-
if ($this->isSimpleInstance($id, $definition)) {
383+
if ($isSimpleInstance) {
386384
return " }\n";
387385
}
388386

@@ -394,13 +392,14 @@ private function addServiceReturn($id, $definition)
394392
*
395393
* @param string $id
396394
* @param Definition $definition
395+
* @param bool $isSimpleInstance
397396
*
398397
* @return string
399398
*
400399
* @throws InvalidArgumentException
401400
* @throws RuntimeException
402401
*/
403-
private function addServiceInstance($id, Definition $definition)
402+
private function addServiceInstance($id, Definition $definition, $isSimpleInstance)
404403
{
405404
$class = $definition->getClass();
406405

@@ -414,26 +413,25 @@ private function addServiceInstance($id, Definition $definition)
414413
throw new InvalidArgumentException(sprintf('"%s" is not a valid class name for the "%s" service.', $class, $id));
415414
}
416415

417-
$simple = $this->isSimpleInstance($id, $definition);
418416
$isProxyCandidate = $this->getProxyDumper()->isProxyCandidate($definition);
419417
$instantiation = '';
420418

421419
if (!$isProxyCandidate && $definition->isShared()) {
422-
$instantiation = "\$this->services['$id'] = ".($simple ? '' : '$instance');
423-
} elseif (!$simple) {
420+
$instantiation = "\$this->services['$id'] = ".($isSimpleInstance ? '' : '$instance');
421+
} elseif (!$isSimpleInstance) {
424422
$instantiation = '$instance';
425423
}
426424

427425
$return = '';
428-
if ($simple) {
426+
if ($isSimpleInstance) {
429427
$return = 'return ';
430428
} else {
431429
$instantiation .= ' = ';
432430
}
433431

434432
$code = $this->addNewInstance($definition, $return, $instantiation, $id);
435433

436-
if (!$simple) {
434+
if (!$isSimpleInstance) {
437435
$code .= "\n";
438436
}
439437

@@ -500,20 +498,21 @@ private function addServiceProperties($id, Definition $definition, $variableName
500498
/**
501499
* Generates the inline definition setup.
502500
*
503-
* @param string $id
504-
* @param Definition $definition
501+
* @param string $id
502+
* @param array $inlinedDefinitions
503+
* @param bool $isSimpleInstance
505504
*
506505
* @return string
507506
*
508507
* @throws ServiceCircularReferenceException when the container contains a circular reference
509508
*/
510-
private function addServiceInlinedDefinitionsSetup($id, Definition $definition)
509+
private function addServiceInlinedDefinitionsSetup($id, array $inlinedDefinitions, $isSimpleInstance)
511510
{
512511
$this->referenceVariables[$id] = new Variable('instance');
513512

514513
$code = '';
515514
$processed = new \SplObjectStorage();
516-
foreach ($this->getInlinedDefinitions($definition) as $iDefinition) {
515+
foreach ($inlinedDefinitions as $iDefinition) {
517516
if ($processed->contains($iDefinition)) {
518517
continue;
519518
}
@@ -525,7 +524,7 @@ private function addServiceInlinedDefinitionsSetup($id, Definition $definition)
525524

526525
// if the instance is simple, the return statement has already been generated
527526
// so, the only possible way to get there is because of a circular reference
528-
if ($this->isSimpleInstance($id, $definition)) {
527+
if ($isSimpleInstance) {
529528
throw new ServiceCircularReferenceException($id, array($id));
530529
}
531530

@@ -683,16 +682,19 @@ private function addService($id, Definition $definition)
683682
$code .= sprintf(" @trigger_error(%s, E_USER_DEPRECATED);\n\n", $this->export($definition->getDeprecationMessage($id)));
684683
}
685684

685+
$inlinedDefinitions = $this->getInlinedDefinitions($definition);
686+
$isSimpleInstance = $this->isSimpleInstance($id, $definition, $inlinedDefinitions);
687+
686688
$code .=
687-
$this->addServiceInclude($id, $definition).
688-
$this->addServiceLocalTempVariables($id, $definition).
689-
$this->addServiceInlinedDefinitions($id, $definition).
690-
$this->addServiceInstance($id, $definition).
691-
$this->addServiceInlinedDefinitionsSetup($id, $definition).
689+
$this->addServiceInclude($id, $definition, $inlinedDefinitions).
690+
$this->addServiceLocalTempVariables($id, $definition, $inlinedDefinitions).
691+
$this->addServiceInlinedDefinitions($id, $inlinedDefinitions).
692+
$this->addServiceInstance($id, $definition, $isSimpleInstance).
693+
$this->addServiceInlinedDefinitionsSetup($id, $inlinedDefinitions, $isSimpleInstance).
692694
$this->addServiceProperties($id, $definition).
693695
$this->addServiceMethodCalls($id, $definition).
694696
$this->addServiceConfigurator($id, $definition).
695-
$this->addServiceReturn($id, $definition)
697+
$this->addServiceReturn($id, $isSimpleInstance)
696698
;
697699

698700
$this->definitionVariables = null;
@@ -1332,8 +1334,6 @@ private function getDefinitionsFromArguments(array $arguments)
13321334
foreach ($arguments as $argument) {
13331335
if (is_array($argument)) {
13341336
$definitions = array_merge($definitions, $this->getDefinitionsFromArguments($argument));
1335-
} elseif ($argument instanceof ArgumentInterface) {
1336-
$definitions = array_merge($definitions, $this->getDefinitionsFromArguments($argument->getValues()));
13371337
} elseif ($argument instanceof Definition) {
13381338
$definitions = array_merge(
13391339
$definitions,

src/Symfony/Component/DependencyInjection/Loader/XmlFileLoader.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -480,7 +480,12 @@ private function getArgumentsAsPhp(\DOMElement $node, $name, $lowercase = true,
480480
$arguments[$key] = $this->getArgumentsAsPhp($arg, $name, false);
481481
break;
482482
case 'iterator':
483-
$arguments[$key] = new IteratorArgument($this->getArgumentsAsPhp($arg, $name, false));
483+
$arg = $this->getArgumentsAsPhp($arg, $name, false);
484+
try {
485+
$arguments[$key] = new IteratorArgument($arg);
486+
} catch (InvalidArgumentException $e) {
487+
throw new InvalidArgumentException(sprintf('Tag "<%s>" with type="iterator" only accepts collections of type="service" references.', $name));
488+
}
484489
break;
485490
case 'string':
486491
$arguments[$key] = $arg->nodeValue;

src/Symfony/Component/DependencyInjection/Loader/YamlFileLoader.php

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -654,14 +654,18 @@ private function resolveServices($value, $file, $isParameter = false)
654654
$argument = $value->getValue();
655655
if ('iterator' === $value->getTag()) {
656656
if (!is_array($argument)) {
657-
throw new InvalidArgumentException('"!iterator" tag only accepts sequences.');
657+
throw new InvalidArgumentException(sprintf('"!iterator" tag only accepts sequences in "%s".', $file));
658+
}
659+
$argument = $this->resolveServices($argument, $file, $isParameter);
660+
try {
661+
return new IteratorArgument($argument);
662+
} catch (InvalidArgumentException $e) {
663+
throw new InvalidArgumentException(sprintf('"!iterator" tag only accepts arrays of "@service" references in "%s".', $file));
658664
}
659-
660-
return new IteratorArgument($this->resolveServices($argument, $file, $isParameter));
661665
}
662666
if ('closure_proxy' === $value->getTag()) {
663667
if (!is_array($argument) || array(0, 1) !== array_keys($argument) || !is_string($argument[0]) || !is_string($argument[1]) || 0 !== strpos($argument[0], '@') || 0 === strpos($argument[0], '@@')) {
664-
throw new InvalidArgumentException('"!closure_proxy" tagged values must be arrays of [@service, method].');
668+
throw new InvalidArgumentException(sprintf('"!closure_proxy" tagged values must be arrays of [@service, method] in "%s".', $file));
665669
}
666670

667671
if (0 === strpos($argument[0], '@?')) {

src/Symfony/Component/DependencyInjection/Tests/Dumper/PhpDumperTest.php

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -434,7 +434,7 @@ public function testLazyArgumentProvideGenerator()
434434
$container->register('lazy_referenced', 'stdClass');
435435
$container
436436
->register('lazy_context', 'LazyContext')
437-
->setArguments(array(new IteratorArgument(array('foo', new Reference('lazy_referenced'), 'k1' => array('foo' => 'bar'), true, 'k2' => new Reference('service_container')))))
437+
->setArguments(array(new IteratorArgument(array('k1' => new Reference('lazy_referenced'), 'k2' => new Reference('service_container')))))
438438
;
439439
$container->compile();
440440

@@ -450,24 +450,12 @@ public function testLazyArgumentProvideGenerator()
450450
foreach ($lazyContext->lazyValues as $k => $v) {
451451
switch (++$i) {
452452
case 0:
453-
$this->assertEquals(0, $k);
454-
$this->assertEquals('foo', $v);
455-
break;
456-
case 1:
457-
$this->assertEquals(1, $k);
458-
$this->assertInstanceOf('stdClass', $v);
459-
break;
460-
case 2:
461453
$this->assertEquals('k1', $k);
462-
$this->assertEquals(array('foo' => 'bar'), $v);
454+
$this->assertInstanceOf('stdCLass', $v);
463455
break;
464-
case 3:
465-
$this->assertEquals(2, $k);
466-
$this->assertTrue($v);
467-
break;
468-
case 4:
456+
case 1:
469457
$this->assertEquals('k2', $k);
470-
$this->assertInstanceOf('\Symfony_DI_PhpDumper_Test_Lazy_Argument_Provide_Generator', $v);
458+
$this->assertInstanceOf('Symfony_DI_PhpDumper_Test_Lazy_Argument_Provide_Generator', $v);
471459
break;
472460
}
473461
}

src/Symfony/Component/DependencyInjection/Tests/Fixtures/containers/container9.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@
134134
;
135135
$container
136136
->register('lazy_context', 'LazyContext')
137-
->setArguments(array(new IteratorArgument(array('foo', new Reference('foo.baz'), array('%foo%' => 'foo is %foo%', 'foobar' => '%foo%'), true, new Reference('service_container')))))
137+
->setArguments(array(new IteratorArgument(array('k1' => new Reference('foo.baz'), 'k2' => new Reference('service_container')))))
138138
;
139139
$container
140140
->register('lazy_context_ignore_invalid_ref', 'LazyContext')

src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services9.php

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -321,12 +321,9 @@ protected function getFooWithInlineService()
321321
protected function getLazyContextService()
322322
{
323323
return $this->services['lazy_context'] = new \LazyContext(new RewindableGenerator(function () {
324-
yield 0 => 'foo';
325-
yield 1 => ${($_ = isset($this->services['foo.baz']) ? $this->services['foo.baz'] : $this->get('foo.baz')) && false ?: '_'};
326-
yield 2 => array($this->getParameter('foo') => 'foo is '.$this->getParameter('foo').'', 'foobar' => $this->getParameter('foo'));
327-
yield 3 => true;
328-
yield 4 => $this;
329-
}, 5));
324+
yield 'k1' => ${($_ = isset($this->services['foo.baz']) ? $this->services['foo.baz'] : $this->get('foo.baz')) && false ?: '_'};
325+
yield 'k2' => $this;
326+
}, 2));
330327
}
331328

332329
/**

0 commit comments

Comments
 (0)
0