8000 Merge branch '3.3' into 3.4 · weaverryan/symfony@6fc5967 · GitHub
[go: up one dir, main page]

Skip to content

Commit 6fc5967

Browse files
Merge branch '3.3' into 3.4
* 3.3: [Form] Fixed ContextErrorException in FileType [DI] Fix handling of inlined definitions by ContainerBuilder
2 parents 70c634f + ae62e56 commit 6fc5967

File tree

5 files changed

+78
-19
lines changed

5 files changed

+78
-19
lines changed

src/Symfony/Component/DependencyInjection/ContainerBuilder.php

Lines changed: 31 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -599,7 +599,7 @@ private function doGet($id, $invalidBehavior = ContainerInterface::EXCEPTION_ON_
599599
$this->{$loading}[$id] = true;
600600

601601
try {
602-
$service = $this->createService($definition, $id);
602+
$service = $this->createService($definition, new \SplObjectStorage(), $id);
603603
} finally {
604604
unset($this->{$loading}[$id]);
605605
}
@@ -1054,8 +1054,12 @@ public function findDefinition($id)
10541054
* @throws RuntimeException When the service is a synthetic service
10551055
* @throws InvalidArgumentException When configure callable is not callable
10561056
*/
1057-
private function createService(Definition $definition, $id, $tryProxy = true)
1057+
private function createService(Definition $definition, \SplObjectStorage $inlinedDefinitions, $id = null, $tryProxy = true)
10581058
{
1059+
if (null === $id && isset($inlinedDefinitions[$definition])) {
1060+
return $inlinedDefinitions[$definition];
1061+
}
1062+
10591063
if ($definition instanceof ChildDefinition) {
10601064
throw new RuntimeException(sprintf('Constructing service "%s" from a parent definition is not supported at build time.', $id));
10611065
}
@@ -1074,11 +1078,11 @@ private function createService(Definition $definition, $id, $tryProxy = true)
10741078
->instantiateProxy(
10751079
$this,
10761080
$definition,
1077-
$id, function () use ($definition, $id) {
1078-
return $this->createService($definition, $id, false);
1081+
$id, function () use ($definition, $inlinedDefinitions, $id) {
1082+
return $this->createService($definition, $inlinedDefinitions, $id, false);
10791083
}
10801084
);
1081-
$this->shareService($definition, $proxy, $id);
1085+
$this->shareService($definition, $proxy, $id, $inlinedDefinitions);
10821086

10831087
return $proxy;
10841088
}
@@ -1089,15 +1093,15 @@ private function createService(Definition $definition, $id, $tryProxy = true)
10891093
require_once $parameterBag->resolveValue($definition->getFile());
10901094
}
10911095

1092-
$arguments = $this->resolveServices($parameterBag->unescapeValue($parameterBag->resolveValue($definition->getArguments())));
1096+
$arguments = $this->doResolveServices($parameterBag->unescapeValue($parameterBag->resolveValue($definition->getArguments())), $inlinedDefinitions);
10931097

10941098
if (null !== $id && $definition->isShared() && isset($this->services[$id]) && ($tryProxy || !$definition->isLazy())) {
10951099
return $this->services[$id];
10961100
}
10971101

< 57AE /td>
10981102
if (null !== $factory = $definition->getFactory()) {
10991103
if (is_array($factory)) {
1100-
$factory = array($this->resolveServices($parameterBag->resolveValue($factory[0])), $factory[1]);
1104+
$factory = array($this->doResolveServices($parameterBag->resolveValue($factory[0]), $inlinedDefinitions), $factory[1]);
11011105
} elseif (!is_string($factory)) {
11021106
throw new RuntimeException(sprintf('Cannot create service "%s" because of invalid factory', $id));
11031107
}
@@ -1126,16 +1130,16 @@ private function createService(Definition $definition, $id, $tryProxy = true)
11261130

11271131
if ($tryProxy || !$definition->isLazy()) {
11281132
// share only if proxying failed, or if not a proxy
1129-
$this->shareService($definition, $service, $id);
1133+
$this->shareService($definition, $service, $id, $inlinedDefinitions);
11301134
}
11311135

1132-
$properties = $this->resolveServices($parameterBag->unescapeValue($parameterBag->resolveValue($definition->getProperties())));
1136+
$properties = $this->doResolveServices($parameterBag->unescapeValue($parameterBag->resolveValue($definition->getProperties())), $inlinedDefinitions);
11331137
foreach ($properties as $name => $value) {
11341138
$service->$name = $value;
11351139
}
11361140

11371141
foreach ($definition->getMethodCalls() as $call) {
1138-
$this->callMethod($service, $call);
1142+
$this->callMethod($service, $call, $inlinedDefinitions);
11391143
}
11401144

11411145
if ($callable = $definition->getConfigurator()) {
@@ -1145,7 +1149,7 @@ private function createService(Definition $definition, $id, $tryProxy = true)
11451149
if ($callable[0] instanceof Reference) {
11461150
$callable[0] = $this->doGet((string) $callable[0], $callable[0]->getInvalidBehavior());
11471151
} elseif ($callable[0] instanceof Definition) {
1148-
$callable[0] = $this->createService($callable[0], null);
1152+
$callable[0] = $this->createService($callable[0], $inlinedDefinitions);
11491153
}
11501154
}
11511155

@@ -1168,10 +1172,15 @@ private function createService(Definition $definition, $id, $tryProxy = true)
11681172
* the real service instances and all expressions evaluated
11691173
*/
11701174
public function resolveServices($value)
1175+
{
1176+
return $this->doResolveServices($value, new \SplObjectStorage());
1177+
}
1178+
1179+
private function doResolveServices($value, \SplObjectStorage $inlinedDefinitions)
11711180
{
11721181
if (is_array($value)) {
11731182
foreach ($value as $k => $v) {
1174-
$value[$k] = $this->resolveServices($v);
1183+
$value[$k] = $this->doResolveServices($v, $inlinedDefinitions);
11751184
}
11761185
} elseif ($value instanceof ServiceClosureArgument) {
11771186
$reference = $value->getValues()[0];
@@ -1216,7 +1225,7 @@ public function resolveServices($value)
12161225
} elseif ($value instanceof Reference) {
12171226
$value = $this->doGet((string) $value, $value->getInvalidBehavior());
12181227
} elseif ($value instanceof Definition) {
1219-
$value = $this->createService($value, null);
1228+
$value = $this->createService($value, $inlinedDefinitions);
12201229
} elseif ($value instanceof Expression) {
12211230
$value = $this->getExpressionLanguage()->evaluate($value, array('container' => $this));
12221231
}
@@ -1531,7 +1540,7 @@ private function getProxyInstantiator()
15311540
return $this->proxyInstantiator;
15321541
}
15331542

1534-
private function callMethod($service, $call)
1543+
private function callMethod($service, $call, \SplObjectStorage $inlinedDefinitions)
15351544
{
15361545
foreach (self::getServiceConditionals($call[1]) as $s) {
15371546
if (!$this->has($s)) {
@@ -1544,7 +1553,7 @@ private function callMethod($service, $call)
15441553
}
15451554
}
15461555

1547-
call_user_func_array(array($service, $call[0]), $this->resolveServices($this->getParameterBag()->unescapeValue($this->getParameterBag()->resolveValue($call[1]))));
1556+
call_user_func_array(array($service, $call[0]), $this->doResolveServices($this->getParameterBag()->unescapeValue($this->getParameterBag()->resolveValue($call[1])), $inlinedDefinitions));
15481557
}
15491558

15501559
/**
@@ -1554,9 +1563,14 @@ private function callMethod($service, $call)
15541563
* @param object $service
15551564
* @param string|null $id
15561565
*/
1557-
private function shareService(Definition $definition, $service, $id)
1566+
private function shareService(Definition $definition, $service, $id, \SplObjectStorage $inlinedDefinitions)
15581567
{
1559-
if (null !== $id && $definition->isShared()) {
1568+
if (!$definition->isShared()) {
1569+
return;
1570+
}
1571+
if (null === $id) {
1572+
$inlinedDefinitions[$definition] = $service;
1573+
} else {
15601574
$this->services[$id] = $service;
15611575
unset($this->loading[$id], $this->alreadyLoading[$id]);
15621576
}

src/Symfony/Component/DependencyInjection/Tests/ContainerBuilderTest.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1077,6 +1077,28 @@ public function testLazyLoadedService()
10771077
$this->assertTrue($classInList);
10781078
}
10791079

1080+
public function testInlinedDefinitions()
1081+
{
1082+
$container = new ContainerBuilder();
1083+
1084+
$definition = new Definition('BarClass');
1085+
1086+
$container->register('bar_user', 'BarUserClass')
1087+
->addArgument($definition)
1088+
->setProperty('foo', $definition);
1089+
1090+
$container->register('bar', 'BarClass')
1091+
->setProperty('foo', $definition)
1092+
->addMethodCall('setBaz', array($definition));
1093+
1094+
$barUser = $container->get('bar_user');
1095+
$bar = $container->get('bar');
1096+
1097+
$this->assertSame($barUser->foo, $barUser->bar);
1098+
$this->assertSame($bar->foo, $bar->getBaz());
1099+
$this->assertNotSame($bar->foo, $barUser->foo);
1100+
}
1101+
10801102
public function testInitializePropertiesBeforeMethodCalls()
10811103
{
10821104
$container = new ContainerBuilder();

src/Symfony/Component/DependencyInjection/Tests/Fixtures/includes/classes.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ function sc_configure($instance)
99
$instance->configure();
1010
}
1111

12-
class BarClass
12+
class BarClass extends BazClass
1313
{
1414
protected $baz;
1515
public $foo = 'foo';

src/Symfony/Component/Form/Extension/Core/Type/FileType.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,13 @@ public function buildForm(FormBuilderInterface $builder, array $options)
3434

3535
if ($options['multiple']) {
3636
$data = array();
37+
$files = $event->getData();
3738

38-
foreach ($event->getData() as $file) {
39+
if (!is_array($files)) {
40+
$files = array();
41+
}
42+
43+
foreach ($files as $file) {
3944
if ($requestHandler->isFileUpload($file)) {
4045
$data[] = $file;
4146
}

src/Symfony/Component/Form/Tests/Extension/Core/Type/FileTypeTest.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,24 @@ public function testMultipleSubmittedFilePathsAreDropped(RequestHandlerInterface
159159
$this->assertCount(1, $form->getData());
160160
}
161161

162+
/**
163+
* @dataProvider requestHandlerProvider
164+
*/
165+
public function testSubmitNonArrayValueWhenMultiple(RequestHandlerInterface $requestHandler)
166+
{
167+
$form = $this->factory
168+
->createBuilder(static::TESTED_TYPE, null, array(
169+
'multiple' => true,
170+
))
171+
->setRequestHandler($requestHandler)
172+
->getForm();
173+
$form->submit(null);
174+
175+
$this->assertSame(array(), $form->getData());
176+
$this->assertSame(array(), $form->getNormData());
177+
$this->assertSame(array(), $form->getViewData());
178+
}
179+
162180
public function requestHandlerProvider()
163181
{
164182
return array(

0 commit comments

Comments
 (0)
0