8000 Merge branch '3.4' into 4.1 · symfony/symfony@2130c60 · GitHub
[go: up one dir, main page]

Skip to content

Commit 2130c60

Browse files
Merge branch '3.4' into 4.1
* 3.4: [HttpFoundation] fix false-positive ConflictingHeadersException [DI] Fix false-positive circular ref leading to wrong exceptions or infinite loops at runtime
2 parents 9fae8f4 + ba31bab commit 2130c60

File tree

11 files changed

+531
-148
lines changed

11 files changed

+531
-148
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ private function getDefinitionId(string $id, ContainerBuilder $container): strin
5858
$seen = array();
5959
while ($container->hasAlias($id)) {
6060
if (isset($seen[$id])) {
61-
throw new ServiceCircularReferenceException($id, array_keys($seen));
61+
throw new ServiceCircularReferenceException($id, array_merge(array_keys($seen), array($id)));
6262
}
6363
$seen[$id] = true;
6464
$id = (string) $container->getAlias($id);

src/Symfony/Component/DependencyInjection/Container.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@ public function get($id, $invalidBehavior = /* self::EXCEPTION_ON_INVALID_REFERE
230230
private function make(string $id, int $invalidBehavior)
231231
{
232232
if (isset($this->loading[$id])) {
233-
throw new ServiceCircularReferenceException($id, array_keys($this->loading));
233+
throw new ServiceCircularReferenceException($id, array_merge(array_keys($this->loading), array($id)));
234234
}
235235

236236
$this->loading[$id] = true;

src/Symfony/Component/DependencyInjection/ContainerBuilder.php

Lines changed: 45 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,6 @@ class ContainerBuilder extends Container implements TaggedContainerInterface
121121
private $autoconfiguredInstanceof = array();
122122

123123
private $removedIds = array();
124-
private $alreadyLoading = array();
125124

126125
private static $internalTypes = array(
127126
'int' => true,
@@ -555,20 +554,30 @@ public function get($id, $invalidBehavior = ContainerInterface::EXCEPTION_ON_INV
555554
return $this->doGet($id, $invalidBehavior);
556555
}
557556

558-
private function doGet($id, $invalidBehavior = ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE, array &$inlineServices = array())
557+
private function doGet($id, $invalidBehavior = ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE, array &$inlineServices = null, $isConstructorArgument = false)
559558
{
560559
if (isset($inlineServices[$id])) {
561560
return $inlineServices[$id];
562561
}
563-
if (ContainerInterface::IGNORE_ON_UNINITIALIZED_REFERENCE === $invalidBehavior) {
564-
return parent::get($id, $invalidBehavior);
562+
if (null === $inlineServices) {
563+
$isConstructorArgument = true;
564+
$inlineServices = array();
565565
}
566-
if ($service = parent::get($id, ContainerInterface::NULL_ON_INVALID_REFERENCE)) {
567-
return $service;
566+
try {
567+
if (ContainerInterface::IGNORE_ON_UNINITIALIZED_REFERENCE === $invalidBehavior) {
568+
return parent::get($id, $invalidBehavior);
569+
}
570+
if ($service = parent::get($id, ContainerInterface::NULL_ON_INVALID_REFERENCE)) {
571+
return $service;
572+
}
573+
} catch (ServiceCircularReferenceException $e) {
574+
if ($isConstructorArgument) {
575+
throw $e;
576+
}
568577
}
569578

570579
if (!isset($this->definitions[$id]) && isset($this->aliasDefinitions[$id])) {
571-
return $this->doGet((string) $this->aliasDefinitions[$id], $invalidBehavior, $inlineServices);
580+
return $this->doGet((string) $this->aliasDefinitions[$id], $invalidBehavior, $inlineServices, $isConstructorArgument);
572581
}
573582

574583
try {
@@ -585,16 +594,17 @@ private function doGet($id, $invalidBehavior = ContainerInterface::EXCEPTION_ON_
585594
throw new RuntimeException(reset($e));
586595
}
587596

588-
$loading = isset($this->alreadyLoading[$id]) ? 'loading' : 'alreadyLoading';
589-
$this->{$loading}[$id] = true;
597+
if ($isConstructorArgument) {
598+
F438 $this->loading[$id] = true;
599+
}
590600

591601
try {
592-
$service = $this->createService($definition, $inlineServices, $id);
602+
return $this->createService($definition, $inlineServices, $isConstructorArgument, $id);
593603
} finally {
594-
unset($this->{$loading}[$id]);
604+
if ($isConstructorArgument) {
605+
unset($this->loading[$id]);
606+
}
595607
}
596-
597-
return $service;
598608
}
599609

600610
/**
@@ -1050,7 +1060,7 @@ public function findDefinition($id)
10501060
* @throws RuntimeException When the service is a synthetic service
10511061
* @throws InvalidArgumentException When configure callable is not callable
10521062
*/
1053-
private function createService(Definition $definition, array &$inlineServices, $id = null, $tryProxy = true)
1063+
private function createService(Definition $definition, array &$inlineServices, $isConstructorArgument = false, $id = null, $tryProxy = true)
10541064
{
10551065
if (null === $id && isset($inlineServices[$h = spl_object_hash($definition)])) {
10561066
return $inlineServices[$h];
@@ -1068,16 +1078,14 @@ private function createService(Definition $definition, array &$inlineServices, $
10681078
@trigger_error($definition->getDeprecationMessage($id), E_USER_DEPRECATED);
10691079
}
10701080

1071-
if ($tryProxy && $definition->isLazy()) {
1072-
$proxy = $this
1073-
->getProxyInstantiator()
1074-
->instantiateProxy(
1075-
$this,
1076-
$definition,
1077-
$id, function () use ($definition, &$inlineServices, $id) {
1078-
return $this->createService($definition, $inlineServices, $id, false);
1079-
}
1080-
);
1081+
if ($tryProxy && $definition->isLazy() && !$tryProxy = !($proxy = $this->proxyInstantiator) || $proxy instanceof RealServiceInstantiator) {
1082+
$proxy = $proxy->instantiateProxy(
1083+
$this,
1084+
$definition,
1085+
$id, function () use ($definition, &$inlineServices, $id) {
1086+
return $this->createService($definition, $inlineServices, true, $id, false);
1087+
}
1088+
);
10811089
$this->shareService($definition, $proxy, $id, $inlineServices);
10821090

10831091
return $proxy;
@@ -1089,19 +1097,21 @@ private function createService(Definition $definition, array &$inlineServices, $
10891097
require_once $parameterBag->resolveValue($definition->getFile());
10901098
}
10911099

1092-
$arguments = $this->doResolveServices($parameterBag->unescapeValue($parameterBag->resolveValue($definition->getArguments())), $inlineServices);
1093-
1094-
if (null !== $id && $definition->isShared() && isset($this->services[$id]) && ($tryProxy || !$definition->isLazy())) {
1095-
return $this->services[$id];
1096-
}
1100+
$arguments = $this->doResolveServices($parameterBag->unescapeValue($parameterBag->resolveValue($definition->getArguments())), $inlineServices, $isConstructorArgument);
10971101

10981102
if (null !== $factory = $definition->getFactory()) {
10991103
if (\is_array($factory)) {
1100-
$factory = array($this->doResolveServices($parameterBag->resolveValue($factory[0]), $inlineServices), $factory[1]);
1104+
$factory = array($this->doResolveServices($parameterBag->resolveValue($factory[0]), $inlineServices, $isConstructorArgument), $factory[1]);
11011105
} elseif (!\is_string($factory)) {
11021106
throw new RuntimeException(sprintf('Cannot create service "%s" because of invalid factory', $id));
11031107
}
1108+
}
11041109

1110+
if (null !== $id && $definition->isShared() && isset($this->services[$id]) && ($tryProxy || !$definition->isLazy())) {
1111+
return $this->services[$id];
1112+
}
1113+
1114+
if (null !== $factory) {
11051115
$service = \call_user_func_array($factory, $arguments);
11061116

11071117
if (!$definition->isDeprecated() && \is_array($factory) && \is_string($factory[0])) {
@@ -1169,11 +1179,11 @@ public function resolveServices($value)
11691179
return $this->doResolveServices($value);
11701180
}
11711181

1172-
private function doResolveServices($value, array &$inlineServices = array())
1182+
private function doResolveServices($value, array &$inlineServices = array(), $isConstructorArgument = false)
11731183
{
11741184
if (\is_array($value)) {
11751185
foreach ($value as $k => $v) {
1176-
$value[$k] = $this->doResolveServices($v, $inlineServices);
1186+
$value[$k] = $this->doResolveServices($v, $inlineServices, $isConstructorArgument);
11771187
}
11781188
} elseif ($value instanceof ServiceClosureArgument) {
11791189
$reference = $value->getValues()[0];
@@ -1216,9 +1226,9 @@ private function doResolveServices($value, array &$inlineServices = array())
12161226
return $count;
12171227
});
12181228
} elseif ($value instanceof Reference) {
1219-
$value = $this->doGet((string) $value, $value->getInvalidBehavior(), $inlineServices);
1229+
$value = $this->doGet((string) $value, $value->getInvalidBehavior(), $inlineServices, $isConstructorArgument);
12201230
} elseif ($value instanceof Definition) {
1221-
$value = $this->createService($value, $inlineServices);
1231+
$value = $this->createService($value, $inlineServices, $isConstructorArgument);
12221232
} elseif ($value instanceof Parameter) {
12231233
$value = $this->getParameter((string) $value);
12241234
} elseif ($value instanceof Expression) {
@@ -1511,18 +1521,6 @@ protected function getEnv($name)
15111521
}
15121522
}
15131523

1514-
/**
1515-
* Retrieves the currently set proxy instantiator or instantiates one.
1516-
*/
1517-
private function getProxyInstantiator(): InstantiatorInterface
1518-
{
1519-
if (!$this->proxyInstantiator) {
1520-
$this->proxyInstantiator = new RealServiceInstantiator();
1521-
}
1522-
1523-
return $this->proxyInstantiator;
1524-
}
1525-
15261524
private function callMethod($service, $call, array &$inlineServices)
15271525
{
15281526
foreach (self::getServiceConditionals($call[1]) as $s) {
@@ -1552,7 +1550,7 @@ private function shareService(Definition $definition, $service, $id, array &$inl
15521550

15531551
if (null !== $id && $definition->isShared()) {
15541552
$this->services[$id] = $service;
1555-
unset($this->loading[$id], $this->alreadyLoading[$id]);
1553+
unset($this->loading[$id]);
15561554
}
15571555
}
15581556

0 commit comments

Comments
 (0)
0