8000 Merge branch '2.8' into 3.3 · symfony/symfony@ae62e56 · GitHub
[go: up one dir, main page]

Skip to content

Commit ae62e56

Browse files
Merge branch '2.8' into 3.3
* 2.8: [Form] Fixed ContextErrorException in FileType [DI] Fix handling of inlined definitions by ContainerBuilder
2 parents f0270a8 + 10ec39e commit ae62e56

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
@@ -573,7 +573,7 @@ public function get($id, $invalidBehavior = ContainerInterface::EXCEPTION_ON_INV
573573
$this->loading[$id] = true;
574574

575575
try {
576-
$service = $this->createService($definition, $id);
576+
$service = $this->createService($definition, new \SplObjectStorage(), $id);
577577
} finally {
578578
unset($this->loading[$id]);
579579
}
@@ -1013,8 +1013,12 @@ public function findDefinition($id)
10131013
* @throws RuntimeException When the service is a synthetic service
10141014
* @throws InvalidArgumentException When configure callable is not callable
10151015
*/
1016-
private function createService(Definition $definition, $id, $tryProxy = true)
1016+
private function createService(Definition $definition, \SplObjectStorage $inlinedDefinitions, $id = null, $tryProxy = true)
10171017
{
1018+
if (null === $id && isset($inlinedDefinitions[$definition])) {
1019+
return $inlinedDefinitions[$definition];
1020+
}
1021+
10181022
if ($definition instanceof ChildDefinition) {
10191023
throw new RuntimeException(sprintf('Constructing service "%s" from a parent definition is not supported at build time.', $id));
10201024
}
@@ -1033,11 +1037,11 @@ private function createService(Definition $definition, $id, $tryProxy = true)
10331037
->instantiateProxy(
10341038
$this,
10351039
$definition,
1036-
$id, function () use ($definition, $id) {
1037-
return $this->createService($definition, $id, false);
1040+
$id, function () use ($definition, $inlinedDefinitions, $id) {
1041+
return $this->createService($definition, $inlinedDefinitions, $id, false);
10381042
}
10391043
);
1040-
$this->shareService($definition, $proxy, $id);
1044+
$this->shareService($definition, $proxy, $id, $inlinedDefinitions);
10411045

10421046
return $proxy;
10431047
}
@@ -1048,11 +1052,11 @@ private function createService(Definition $definition, $id, $tryProxy = true)
10481052
require_once $parameterBag->resolveValue($definition->getFile());
10491053
}
10501054

1051-
$arguments = $this->resolveServices($parameterBag->unescapeValue($parameterBag->resolveValue($definition->getArguments())));
1055+
$arguments = $this->doResolveServices($parameterBag->unescapeValue($parameterBag->resolveValue($definition->getArguments())), $inlinedDefinitions);
10521056

10531057
if (null !== $factory = $definition->getFactory()) {
10541058
if (is_array($factory)) {
1055-
$factory = array($this->resolveServices($parameterBag->resolveValue($factory[0])), $factory[1]);
1059+
$factory = array($this->doResolveServices($parameterBag->resolveValue($factory[0]), $inlinedDefinitions), $factory[1]);
10561060
} elseif (!is_string($factory)) {
10571061
throw new RuntimeException(sprintf('Cannot create service "%s" because of invalid factory', $id));
10581062
}
@@ -1081,16 +1085,16 @@ private function createService(Definition $definition, $id, $tryProxy = true)
10811085

10821086
if ($tryProxy || !$definition->isLazy()) {
10831087
// share only if proxying failed, or if not a proxy
1084-
$this->shareService($definition, $service, $id);
1088+
$this->shareService($definition, $service, $id, $inlinedDefinitions);
10851089
}
10861090

1087-
$properties = $this->resolveServices($parameterBag->unescapeValue($parameterBag->resolveValue($definition->getProperties())));
1091+
$properties = $this->doResolveServices($parameterBag->unescapeValue($parameterBag->resolveValue($definition->getProperties())), $inlinedDefinitions);
10881092
foreach ($properties as $name => $value) {
10891093
$service->$name = $value;
10901094
}
10911095

10921096
foreach ($definition->getMethodCalls() as $call) {
1093-
$this->callMethod($service, $call);
1097+
$this->callMethod($service, $call, $inlinedDefinitions);
10941098
}
10951099

10961100
if ($callable = $definition->getConfigurator()) {
@@ -1100,7 +1104,7 @@ private function createService(Definition $definition, $id, $tryProxy = true)
11001104
if ($callable[0] instanceof Reference) {
11011105
$callable[0] = $this->get((string) $callable[0], $callable[0]->getInvalidBehavior());
11021106
} elseif ($callable[0] instanceof Definition) {
1103-
$callable[0] = $this->createService($callable[0], null);
1107+
$callable[0] = $this->createService($callable[0], $inlinedDefinitions);
11041108
}
11051109
}
11061110

@@ -1123,10 +1127,15 @@ private function createService(Definition $definition, $id, $tryProxy = true)
11231127
* the real service instances and all expressions evaluated
11241128
*/
11251129
public function resolveServices($value)
1130+
{
1131+
return $this->doResolveServices($value, new \SplObjectStorage());
1132+
}
1133+
1134+
private function doResolveServices($value, \SplObjectStorage $inlinedDefinitions)
11261135
{
11271136
if (is_array($value)) {
11281137
foreach ($value as $k => $v) {
1129-
$value[$k] = $this->resolveServices($v);
1138+
$value[$k] = $this->doResolveServices($v, $inlinedDefinitions);
11301139
}
11311140
} elseif ($value instanceof ServiceClosureArgument) {
11321141
$reference = $value->getValues()[0];
@@ -1161,7 +1170,7 @@ public function resolveServices($value)
11611170
} elseif ($value instanceof Reference) {
11621171
$value = $this->get((string) $value, $value->getInvalidBehavior());
11631172
} elseif ($value instanceof Definition) {
1164-
$value = $this->createService($value, null);
1173+
$value = $this->createService($value, $inlinedDefinitions);
11651174
} elseif ($value instanceof Expression) {
11661175
$value = $this->getExpressionLanguage()->evaluate($value, array('container' => $this));
11671176
}
@@ -1430,7 +1439,7 @@ private function getProxyInstantiator()
14301439
return $this->proxyInstantiator;
14311440
}
14321441

1433-
private function callMethod($service, $call)
1442+
private function callMethod($service, $call, \SplObjectStorage $inlinedDefinitions)
14341443
{
14351444
$services = self::getServiceConditionals($call[1]);
14361445

@@ -1440,7 +1449,7 @@ private function callMethod($service, $call)
14401449
}
14411450
}
14421451

1443-
call_user_func_array(array($service, $call[0]), $this->resolveServices($this->getParameterBag()->unescapeValue($this->getParameterBag()->resolveValue($call[1]))));
1452+
call_user_func_array(array($service, $call[0]), $this->doResolveServices($this->getParameterBag()->unescapeValue($this->getParameterBag()->resolveValue($call[1])), $inlinedDefinitions));
14441453
}
14451454

14461455
/**
@@ -1450,9 +1459,14 @@ private function callMethod($service, $call)
14501459
* @param object $service
14511460
* @param string|null $id
14521461
*/
1453-
private function shareService(Definition $definition, $service, $id)
1462+
private function shareService(Definition $definition, $service, $id, \SplObjectStorage $inlinedDefinitions)
14541463
{
1455-
if (null !== $id && $definition->isShared()) {
1464+
if (!$definition->isShared()) {
1465+
return;
1466+
}
1467+
if (null === $id) {
1468+
$inlinedDefinitions[$definition] = $service;
1469+
} else {
14561470
$this->services[$this->normalizeId($id)] = $service;
14571471
}
14581472
}

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

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1006,6 +1006,28 @@ public function testLazyLoadedService()
10061006
$this->assertTrue($classInList);
10071007
}
10081008

1009+
public function testInlinedDefinitions()
1010+
{
1011+
$container = new ContainerBuilder();
1012+
1013+
$definition = new Definition('BarClass');
1014+
1015+
$container->register('bar_user', 'BarUserClass')
1016+
->addArgument($definition)
1017+
->setProperty('foo', $definition);
1018+
1019+
$container->register('bar', 'BarClass')
1020+
->setProperty('foo', $definition)
1021+
->addMethodCall('setBaz', array($definition));
1022+
1023+
$barUser = $container->get('bar_user');
1024+
$bar = $container->get('bar');
1025+
1026+
$this->assertSame($barUser->foo, $barUser->bar);
1027+
$this->assertSame($bar->foo, $bar->getBaz());
1028+
$this->assertNotSame($bar->foo, $barUser->foo);
1029+
}
1030+
10091031
public function testInitializePropertiesBeforeMethodCalls()
10101032
{
10111033
$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