diff --git a/src/Symfony/Component/DependencyInjection/ContainerBuilder.php b/src/Symfony/Component/DependencyInjection/ContainerBuilder.php index 0fd333aedcf23..f09850e928401 100644 --- a/src/Symfony/Component/DependencyInjection/ContainerBuilder.php +++ b/src/Symfony/Component/DependencyInjection/ContainerBuilder.php @@ -759,8 +759,8 @@ private function createService(Definition $definition, $id) $service = call_user_func_array(array($factory, $definition->getFactoryMethod()), $arguments); } else { - $r = new \ReflectionClass($parameterBag->resolveValue($definition->getClass())); - + $r = new \ReflectionClass(trim($parameterBag->resolveValue($definition->getClass()))); + $service = null === $r->getConstructor() ? $r->newInstance() : $r->newInstanceArgs($arguments); } diff --git a/src/Symfony/Component/DependencyInjection/Tests/ContainerBuilderTest.php b/src/Symfony/Component/DependencyInjection/Tests/ContainerBuilderTest.php index c9e6b07847dc0..77c3b84d0525b 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/ContainerBuilderTest.php +++ b/src/Symfony/Component/DependencyInjection/Tests/ContainerBuilderTest.php @@ -319,6 +319,70 @@ public function testCreateServiceConfigurator() } } + /** + * @covers Symfony\Component\DependencyInjection\ContainerBuilder::createService + */ + public function testCreateServiceWithNewLineWrapping() + { + $builder = new ContainerBuilder(); + $builder->setParameter('foo1class', "\nFooClass\n"); + $builder->register('foo1', '%foo1class%')->setFile(__DIR__ . '/Fixtures/includes/foo.php'); + try { + $this->assertInstanceOf( + '\FooClass', + $builder->get('foo1'), + 'New Line Wrapping \n in Parameter should be stripped out.' + ); + } catch (\ReflectionException $e) { + $this->fail('FooClass is not callable due to the new lines not being stripped'); + } + $builder->setParameter('foo2class', "\rFooClass\r"); + $builder->register('foo2', '%foo2class%')->setFile(__DIR__ . '/Fixtures/includes/foo.php'); + try { + $this->assertInstanceOf( + '\FooClass', + $builder->get('foo2'), + 'New Line Wrapping \r in Parameter should be stripped out.' + ); + } catch (\ReflectionException $e) { + $this->fail('FooClass is not callable due to the new lines not being stripped'); + } + } + + /** + * @covers Symfony\Component\DependencyInjection\ContainerBuilder::createService + */ + public function testCreateServiceWithNewLineWrappingWithWhiteSpace() + { + $builder = new ContainerBuilder(); + $builder->setParameter('foo1class', "\n FooClass \n"); + $builder->register('foo1', '%foo1class%')->setFile(__DIR__ . '/Fixtures/includes/foo.php'); + try { + $this->assertInstanceOf( + '\FooClass', + $builder->get('foo1'), + 'New Line Wrapping \n in Parameter should be stripped out along with whitespace.' + ); + } catch (\ReflectionException $e) { + $this->fail( + 'FooClass is not callable due to the new lines not being stripped and/or whitespacing not being trimmed' + ); + } + $builder->setParameter('foo2class', "\r FooClass \r"); + $builder->register('foo2', '%foo2class%')->setFile(__DIR__ . '/Fixtures/includes/foo.php'); + try { + $this->assertInstanceOf( + '\FooClass', + $builder->get('foo2'), + 'New Line Wrapping \r in Parameter should be stripped out along with whitespace.' + ); + } catch (\ReflectionException $e) { + $this->fail( + 'FooClass is not callable due to the new lines not being stripped and/or whitespacing not being trimmed.' + ); + } + } + /** * @covers Symfony\Component\DependencyInjection\ContainerBuilder::resolveServices */