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

Skip to content

Commit 568e3a4

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

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
@@ -231,7 +231,7 @@ public function get($id, $invalidBehavior = /* self::EXCEPTION_ON_INVALID_REFERE
231231
private function make(string $id, int $invalidBehavior)
232232
{
233233
if (isset($this->loading[$id])) {
234-
throw new ServiceCircularReferenceException($id, array_keys($this->loading));
234+
throw new ServiceCircularReferenceException($id, array_merge(array_keys($this->loading), array($id)));
235235
}
236236

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

src/Symfony/Component/DependencyInjection/ContainerBuilder.php

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

125125
private $removedIds = array();
126-
private $alreadyLoading = array();
127126

128127
private static $internalTypes = array(
129128
'int' => true,
@@ -557,20 +556,30 @@ public function get($id, $invalidBehavior = ContainerInterface::EXCEPTION_ON_INV
557556
return $this->doGet($id, $invalidBehavior);
558557
}
559558

560-
private function doGet($id, $invalidBehavior = ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE, array &$inlineServices = array())
559+
private function doGet($id, $invalidBehavior = ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE, array &$inlineServices = null, $isConstructorArgument = false)
561560
{
562561
if (isset($inlineServices[$id])) {
563562
return $inlineServices[$id];
564563
}
565-
if (ContainerInterface::IGNORE_ON_UNINITIALIZED_REFERENCE === $invalidBehavior) {
566-
return parent::get($id, $invalidBehavior);
564+
if (null === $inlineServices) {
565+
$isConstructorArgument = true;
566+
$inlineServices = array();
567567
}
568-
if ($service = parent::get($id, ContainerInterface::NULL_ON_INVALID_REFERENCE)) {
569-
return $service;
568+
try {
569+
if (ContainerInterface::IGNORE_ON_UNINITIALIZED_REFERENCE === $invalidBehavior) {
570+
return parent::get($id, $invalidBehavior);
571+
}
572+
if ($service = parent::get($id, ContainerInterface::NULL_ON_INVALID_REFERENCE)) {
573+
return $service;
574+
}
575+
} catch (ServiceCircularReferenceException $e) {
576+
if ($isConstructorArgument) {
577+
throw $e;
578+
}
570579
}
571580

572581
if (!isset($this->definitions[$id]) && isset($this->aliasDefinitions[$id])) {
573-
return $this->doGet((string) $this->aliasDefinitions[$id], $invalidBehavior, $inlineServices);
582+
return $this->doGet((string) $this->aliasDefinitions[$id], $invalidBehavior, $inlineServices, $isConstructorArgument);
574583
}
575584

576585
try {
@@ -587,16 +596,17 @@ private function doGet($id, $invalidBehavior = ContainerInterface::EXCEPTION_ON_
587596
throw new RuntimeException(reset($e));
588597
}
589598

590-
$loading = isset($this->alreadyLoading[$id]) ? 'loading' : 'alreadyLoading';
591-
$this->{$loading}[$id] = true;
599+
if ($isConstructorArgument) {
600+
$this->loading[$id] = true;
601+
}
592602

593603
try {
594-
$service = $this->createService($definition, $inlineServices, $id);
604+
return $this->createService($definition, $inlineServices, $isConstructorArgument, $id);
595605
} finally {
596-
unset($this->{$loading}[$id]);
606+
if ($isConstructorArgument) {
607+
unset($this->loading[$id]);
608+
}
597609
}
598-
599-
return $service;
600610
}
601611

602612
/**
@@ -1052,7 +1062,7 @@ public function findDefinition($id)
10521062
* @throws RuntimeException When the service is a synthetic service
10531063
* @throws InvalidArgumentException When configure callable is not callable
10541064
*/
1055-
private function createService(Definition $definition, array &$inlineServices, $id = null, $tryProxy = true)
1065+
private function createService(Definition $definition, array &$inlineServices, $isConstructorArgument = false, $id = null, $tryProxy = true)
10561066
{
10571067
if (null === $id && isset($inlineServices[$h = spl_object_hash($definition)])) {
10581068
return $inlineServices[$h];
@@ -1070,16 +1080,14 @@ private function createService(Definition $definition, array &$inlineServices, $
10701080
@trigger_error($definition->getDeprecationMessage($id), E_USER_DEPRECATED);
10711081
}
10721082

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

10851093
return $proxy;
@@ -1091,19 +1099,21 @@ private function createService(Definition $definition, array &$inlineServices, $
10911099
require_once $parameterBag->resolveValue($definition->getFile());
10921100
}
10931101

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

11001104
if (null !== $factory = $definition->getFactory()) {
11011105
if (\is_array($factory)) {
1102-
$factory = array($this->doResolveServices($parameterBag->resolveValue($factory[0]), $inlineServices), $factory[1]);
1106+
$factory = array($this->doResolveServices($parameterBag->resolveValue($factory[0]), $inlineServices, $isConstructorArgument), $factory[1]);
11031107
} elseif (!\is_string($factory)) {
11041108
throw new RuntimeException(sprintf('Cannot create service "%s" because of invalid factory', $id));
11051109
}
1110+
}
11061111

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

11091119
if (!$definition->isDeprecated() && \is_array($factory) && \is_string($factory[0])) {
@@ -1171,11 +1181,11 @@ public function resolveServices($value)
11711181
return $this->doResolveServices($value);
11721182
}
11731183

1174-
private function doResolveServices($value, array &$inlineServices = array())
1184+
private function doResolveServices($value, array &$inlineServices = array(), $isConstructorArgument = false)
11751185
{
11761186
if (\is_array($value)) {
11771187
foreach ($value as $k => $v) {
1178-
$value[$k] = $this->doResolveServices($v, $inlineServices);
1188+
$value[$k] = $this->doResolveServices($v, $inlineServices, $isConstructorArgument);
11791189
}
11801190
} elseif ($value instanceof ServiceClosureArgument) {
11811191
$reference = $value->getValues()[0];
@@ -1226,9 +1236,9 @@ private function doResolveServices($value, array &$inlineServices = array())
12261236
}
12271237
$value = new ServiceLocator(\Closure::fromCallable(array($this, 'resolveServices')), $refs);
12281238
} elseif ($value instanceof Reference) {
1229-
$value = $this->doGet((string) $value, $value->getInvalidBehavior(), $inlineServices);
1239+
$value = $this->doGet((string) $value, $value->getInvalidBehavior(), $inlineServices, $isConstructorArgument);
12301240
} elseif ($value instanceof Definition) {
1231-
$value = $this->createService($value, $inlineServices);
1241+
$value = $this->createService($value, $inlineServices, $isConstructorArgument);
12321242
} elseif ($value instanceof Parameter) {
12331243
$value = $this->getParameter((string) $value);
12341244
} elseif ($value instanceof Expression) {
@@ -1521,18 +1531,6 @@ protected function getEnv($name)
15211531
}
15221532
}
15231533

1524-
/**
1525-
* Retrieves the currently set proxy instantiator or instantiates one.
1526-
*/
1527-
private function getProxyInstantiator(): InstantiatorInterface
1528-
{
1529-
if (!$this->proxyInstantiator) {
1530-
$this->proxyInstantiator = new RealServiceInstantiator();
1531-
}
1532-
1533-
return $this->proxyInstantiator;
1534-
}
1535-
15361534
private function callMethod($service, $call, array &$inlineServices)
15371535
{
15381536
foreach (self::getServiceConditionals($call[1]) as $s) {
@@ -1562,7 +1560,7 @@ private function shareService(Definition $definition, $service, $id, array &$inl
15621560

15631561
if (null !== $id && $definition->isShared()) {
15641562
$this->services[$id] = $service;
1565-
unset($this->loading[$id], $this->alreadyLoading[$id]);
1563+
unset($this->loading[$id]);
15661564
}
15671565
}
15681566

0 commit comments

Comments
 (0)