Merge branch '2.7' into 2.8

* 2.7:
  Regression test for missing controller arguments
  fix a test checking for a value
  [Form][DX] FileType "multiple" fixes
  fixed CS
  [TwigBundle] Fix twig loader registered twice
  [Console] Fix wrong handling of multiline arg/opt descriptions
  [DependencyInjection] PhpDumper.php: hasReference() should not search references in lazy service arguments.
  [Form] fixed "empty_value" option deprecation
This commit is contained in:
Fabien Potencier 2016-12-05 09:41:28 +01:00
commit 1188b4d6f4
15 changed files with 197 additions and 29 deletions

View File

@ -11,6 +11,7 @@
namespace Symfony\Bundle\TwigBundle\DependencyInjection\Compiler; namespace Symfony\Bundle\TwigBundle\DependencyInjection\Compiler;
use Symfony\Component\DependencyInjection\Alias;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder; use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Reference; use Symfony\Component\DependencyInjection\Reference;
@ -73,7 +74,7 @@ class ExtensionPass implements CompilerPassInterface
$loader->addTag('twig.loader'); $loader->addTag('twig.loader');
$loader->setMethodCalls($container->getDefinition('twig.loader.filesystem')->getMethodCalls()); $loader->setMethodCalls($container->getDefinition('twig.loader.filesystem')->getMethodCalls());
$container->setDefinition('twig.loader.filesystem', $loader); $container->setAlias('twig.loader.filesystem', new Alias('twig.loader.native_filesystem', false));
} }
if ($container->has('assets.packages')) { if ($container->has('assets.packages')) {

View File

@ -38,13 +38,13 @@ class TextDescriptor extends Descriptor
} }
$totalWidth = isset($options['total_width']) ? $options['total_width'] : strlen($argument->getName()); $totalWidth = isset($options['total_width']) ? $options['total_width'] : strlen($argument->getName());
$spacingWidth = $totalWidth - strlen($argument->getName()) + 2; $spacingWidth = $totalWidth - strlen($argument->getName());
$this->writeText(sprintf(' <info>%s</info>%s%s%s', $this->writeText(sprintf(' <info>%s</info> %s%s%s',
$argument->getName(), $argument->getName(),
str_repeat(' ', $spacingWidth), str_repeat(' ', $spacingWidth),
// + 17 = 2 spaces + <info> + </info> + 2 spaces // + 4 = 2 spaces before <info>, 2 spaces after </info>
preg_replace('/\s*[\r\n]\s*/', "\n".str_repeat(' ', $totalWidth + 17), $argument->getDescription()), preg_replace('/\s*[\r\n]\s*/', "\n".str_repeat(' ', $totalWidth + 4), $argument->getDescription()),
$default $default
), $options); ), $options);
} }
@ -75,13 +75,13 @@ class TextDescriptor extends Descriptor
sprintf('--%s%s', $option->getName(), $value) sprintf('--%s%s', $option->getName(), $value)
); );
$spacingWidth = $totalWidth - strlen($synopsis) + 2; $spacingWidth = $totalWidth - strlen($synopsis);
$this->writeText(sprintf(' <info>%s</info>%s%s%s%s', $this->writeText(sprintf(' <info>%s</info> %s%s%s%s',
$synopsis, $synopsis,
str_repeat(' ', $spacingWidth), str_repeat(' ', $spacingWidth),
// + 17 = 2 spaces + <info> + </info> + 2 spaces // + 4 = 2 spaces before <info>, 2 spaces after </info>
preg_replace('/\s*[\r\n]\s*/', "\n".str_repeat(' ', $totalWidth + 17), $option->getDescription()), preg_replace('/\s*[\r\n]\s*/', "\n".str_repeat(' ', $totalWidth + 4), $option->getDescription()),
$default, $default,
$option->isArray() ? '<comment> (multiple values allowed)</comment>' : '' $option->isArray() ? '<comment> (multiple values allowed)</comment>' : ''
), $options); ), $options);

View File

@ -1,2 +1,2 @@
<info>argument_name</info> multiline <info>argument_name</info> multiline
argument description argument description

View File

@ -1,2 +1,2 @@
<info>-o, --option_name=OPTION_NAME</info> multiline <info>-o, --option_name=OPTION_NAME</info> multiline
option description option description

View File

@ -60,15 +60,19 @@ class CheckCircularReferencesPass implements CompilerPassInterface
$id = $node->getId(); $id = $node->getId();
if (empty($this->checkedNodes[$id])) { if (empty($this->checkedNodes[$id])) {
$searchKey = array_search($id, $this->currentPath);
$this->currentPath[] = $id;
if (false !== $searchKey) { // don't check circular dependencies for lazy services
throw new ServiceCircularReferenceException($id, array_slice($this->currentPath, $searchKey)); if (!$node->getValue() || !$node->getValue()->isLazy()) {
$searchKey = array_search($id, $this->currentPath);
$this->currentPath[] = $id;
if (false !== $searchKey) {
throw new ServiceCircularReferenceException($id, array_slice($this->currentPath, $searchKey));
}
$this->checkOutEdges($node->getOutEdges());
} }
$this->checkOutEdges($node->getOutEdges());
$this->checkedNodes[$id] = true; $this->checkedNodes[$id] = true;
array_pop($this->currentPath); array_pop($this->currentPath);
} }

View File

@ -1313,6 +1313,13 @@ EOF;
$visited[$argumentId] = true; $visited[$argumentId] = true;
$service = $this->container->getDefinition($argumentId); $service = $this->container->getDefinition($argumentId);
// if the proxy manager is enabled, disable searching for references in lazy services,
// as these services will be instantiated lazily and don't have direct related references.
if ($service->isLazy() && !$this->getProxyDumper() instanceof NullDumper) {
continue;
}
$arguments = array_merge($service->getMethodCalls(), $service->getArguments(), $service->getProperties()); $arguments = array_merge($service->getMethodCalls(), $service->getArguments(), $service->getProperties());
if ($this->hasReference($id, $arguments, $deep, $visited)) { if ($this->hasReference($id, $arguments, $deep, $visited)) {

View File

@ -11,6 +11,7 @@
namespace Symfony\Component\DependencyInjection\Tests\Dumper; namespace Symfony\Component\DependencyInjection\Tests\Dumper;
use DummyProxyDumper;
use Symfony\Component\DependencyInjection\ContainerBuilder; use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Dumper\PhpDumper; use Symfony\Component\DependencyInjection\Dumper\PhpDumper;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag; use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag;
@ -19,6 +20,8 @@ use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\Variable; use Symfony\Component\DependencyInjection\Variable;
use Symfony\Component\ExpressionLanguage\Expression; use Symfony\Component\ExpressionLanguage\Expression;
require_once __DIR__.'/../Fixtures/includes/classes.php';
class PhpDumperTest extends \PHPUnit_Framework_TestCase class PhpDumperTest extends \PHPUnit_Framework_TestCase
{ {
protected static $fixturesPath; protected static $fixturesPath;
@ -294,4 +297,52 @@ class PhpDumperTest extends \PHPUnit_Framework_TestCase
$container = new \Symfony_DI_PhpDumper_Test_Properties_Before_Method_Calls(); $container = new \Symfony_DI_PhpDumper_Test_Properties_Before_Method_Calls();
$this->assertTrue($container->get('bar')->callPassed(), '->dump() initializes properties before method calls'); $this->assertTrue($container->get('bar')->callPassed(), '->dump() initializes properties before method calls');
} }
public function testCircularReferenceAllowanceForLazyServices()
{
$container = new ContainerBuilder();
$container->register('foo', 'stdClass')->addArgument(new Reference('bar'));
$container->register('bar', 'stdClass')->setLazy(true)->addArgument(new Reference('foo'));
$container->compile();
$dumper = new PhpDumper($container);
$dumper->dump();
}
public function testCircularReferenceAllowanceForInlinedDefinitionsForLazyServices()
{
/*
* test graph:
* [connection] -> [event_manager] --> [entity_manager](lazy)
* |
* --(call)- addEventListener ("@lazy_service")
*
* [lazy_service](lazy) -> [entity_manager](lazy)
*
*/
$container = new ContainerBuilder();
$eventManagerDefinition = new Definition('stdClass');
$connectionDefinition = $container->register('connection', 'stdClass');
$connectionDefinition->addArgument($eventManagerDefinition);
$container->register('entity_manager', 'stdClass')
->setLazy(true)
->addArgument(new Reference('connection'));
$lazyServiceDefinition = $container->register('lazy_service', 'stdClass');
$lazyServiceDefinition->setLazy(true);
$lazyServiceDefinition->addArgument(new Reference('entity_manager'));
$eventManagerDefinition->addMethodCall('addEventListener', array(new Reference('lazy_service')));
$container->compile();
$dumper = new PhpDumper($container);
$dumper->setProxyDumper(new DummyProxyDumper());
$dumper->dump();
}
} }

View File

@ -1,5 +1,8 @@
<?php <?php
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\LazyProxy\PhpDumper\DumperInterface as ProxyDumper;
function sc_configure($instance) function sc_configure($instance)
{ {
$instance->configure(); $instance->configure();
@ -76,3 +79,21 @@ class MethodCallClass
return $this->callPassed; return $this->callPassed;
} }
} }
class DummyProxyDumper implements ProxyDumper
{
public function isProxyCandidate(Definition $definition)
{
return false;
}
public function getProxyFactoryCode(Definition $definition, $id)
{
return '';
}
public function getProxyCode(Definition $definition)
{
return '';
}
}

View File

@ -39,6 +39,11 @@ use Symfony\Component\OptionsResolver\OptionsResolver;
class ChoiceType extends AbstractType class ChoiceType extends AbstractType
{ {
/**
* @internal To be removed in 3.0
*/
const DEPRECATED_EMPTY_VALUE = '__deprecated_empty_value__';
/** /**
* Caches created choice lists. * Caches created choice lists.
* *
@ -344,7 +349,7 @@ class ChoiceType extends AbstractType
}; };
$placeholderNormalizer = function (Options $options, $placeholder) use ($that) { $placeholderNormalizer = function (Options $options, $placeholder) use ($that) {
if (!is_object($options['empty_value']) || !$options['empty_value'] instanceof \Exception) { if ($that::DEPRECATED_EMPTY_VALUE !== $options['empty_value']) {
@trigger_error(sprintf('The form option "empty_value" of the "%s" form type (%s) is deprecated since version 2.6 and will be removed in 3.0. Use "placeholder" instead.', $that->getName(), __CLASS__), E_USER_DEPRECATED); @trigger_error(sprintf('The form option "empty_value" of the "%s" form type (%s) is deprecated since version 2.6 and will be removed in 3.0. Use "placeholder" instead.', $that->getName(), __CLASS__), E_USER_DEPRECATED);
if (null === $placeholder || '' === $placeholder) { if (null === $placeholder || '' === $placeholder) {
@ -396,7 +401,7 @@ class ChoiceType extends AbstractType
'preferred_choices' => array(), 'preferred_choices' => array(),
'group_by' => null, 'group_by' => null,
'empty_data' => $emptyData, 'empty_data' => $emptyData,
'empty_value' => new \Exception(), // deprecated 'empty_value' => self::DEPRECATED_EMPTY_VALUE,
'placeholder' => $placeholder, 'placeholder' => $placeholder,
'error_bubbling' => false, 'error_bubbling' => false,
'compound' => $compound, 'compound' => $compound,

View File

@ -191,7 +191,7 @@ class DateType extends AbstractType
}; };
$placeholderNormalizer = function (Options $options, $placeholder) use ($placeholderDefault) { $placeholderNormalizer = function (Options $options, $placeholder) use ($placeholderDefault) {
if (!is_object($options['empty_value']) || !$options['empty_value'] instanceof \Exception) { if (ChoiceType::DEPRECATED_EMPTY_VALUE !== $options['empty_value']) {
@trigger_error('The form option "empty_value" is deprecated since version 2.6 and will be removed in 3.0. Use "placeholder" instead.', E_USER_DEPRECATED); @trigger_error('The form option "empty_value" is deprecated since version 2.6 and will be removed in 3.0. Use "placeholder" instead.', E_USER_DEPRECATED);
$placeholder = $options['empty_value']; $placeholder = $options['empty_value'];
@ -243,7 +243,7 @@ class DateType extends AbstractType
'format' => $format, 'format' => $format,
'model_timezone' => null, 'model_timezone' => null,
'view_timezone' => null, 'view_timezone' => null,
'empty_value' => new \Exception(), // deprecated 'empty_value' => ChoiceType::DEPRECATED_EMPTY_VALUE,
'placeholder' => $placeholder, 'placeholder' => $placeholder,
'html5' => true, 'html5' => true,
// Don't modify \DateTime classes by reference, we treat // Don't modify \DateTime classes by reference, we treat

View File

@ -12,12 +12,37 @@
namespace Symfony\Component\Form\Extension\Core\Type; namespace Symfony\Component\Form\Extension\Core\Type;
use Symfony\Component\Form\AbstractType; use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\FormEvents;
use Symfony\Component\Form\FormInterface; use Symfony\Component\Form\FormInterface;
use Symfony\Component\Form\FormView; use Symfony\Component\Form\FormView;
use Symfony\Component\OptionsResolver\Options;
use Symfony\Component\OptionsResolver\OptionsResolver; use Symfony\Component\OptionsResolver\OptionsResolver;
class FileType extends AbstractType class FileType extends AbstractType
{ {
/**
* {@inheritdoc}
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
if ($options['multiple']) {
$builder->addEventListener(FormEvents::PRE_SUBMIT, function (FormEvent $event) {
$form = $event->getForm();
$data = $event->getData();
// submitted data for an input file (not required) without choosing any file
if (array(null) === $data) {
$emptyData = $form->getConfig()->getEmptyData();
$data = is_callable($emptyData) ? call_user_func($emptyData, $form, $data) : $emptyData;
$event->setData($data);
}
});
}
}
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
@ -39,9 +64,7 @@ class FileType extends AbstractType
*/ */
public function finishView(FormView $view, FormInterface $form, array $options) public function finishView(FormView $view, FormInterface $form, array $options)
{ {
$view $view->vars['multipart'] = true;
->vars['multipart'] = true
;
} }
/** /**
@ -49,10 +72,18 @@ class FileType extends AbstractType
*/ */
public function configureOptions(OptionsResolver $resolver) public function configureOptions(OptionsResolver $resolver)
{ {
$dataClass = function (Options $options) {
return $options['multiple'] ? null : 'Symfony\Component\HttpFoundation\File\File';
};
$emptyData = function (Options $options) {
return $options['multiple'] ? array() : null;
};
$resolver->setDefaults(array( $resolver->setDefaults(array(
'compound' => false, 'compound' => false,
'data_class' => 'Symfony\Component\HttpFoundation\File\File', 'data_class' => $dataClass,
'empty_data' => null, 'empty_data' => $emptyData,
'multiple' => false, 'multiple' => false,
)); ));
} }

View File

@ -192,7 +192,7 @@ class TimeType extends AbstractType
}; };
$placeholderNormalizer = function (Options $options, $placeholder) use ($placeholderDefault) { $placeholderNormalizer = function (Options $options, $placeholder) use ($placeholderDefault) {
if (!is_object($options['empty_value']) || !$options['empty_value'] instanceof \Exception) { if (ChoiceType::DEPRECATED_EMPTY_VALUE !== $options['empty_value']) {
@trigger_error('The form option "empty_value" is deprecated since version 2.6 and will be removed in 3.0. Use "placeholder" instead.', E_USER_DEPRECATED); @trigger_error('The form option "empty_value" is deprecated since version 2.6 and will be removed in 3.0. Use "placeholder" instead.', E_USER_DEPRECATED);
$placeholder = $options['empty_value']; $placeholder = $options['empty_value'];
@ -241,7 +241,7 @@ class TimeType extends AbstractType
'with_seconds' => false, 'with_seconds' => false,
'model_timezone' => null, 'model_timezone' => null,
'view_timezone' => null, 'view_timezone' => null,
'empty_value' => new \Exception(), // deprecated 'empty_value' => ChoiceType::DEPRECATED_EMPTY_VALUE,
'placeholder' => $placeholder, 'placeholder' => $placeholder,
'html5' => true, 'html5' => true,
// Don't modify \DateTime classes by reference, we treat // Don't modify \DateTime classes by reference, we treat

View File

@ -54,6 +54,33 @@ class FileTypeTest extends \Symfony\Component\Form\Test\TypeTestCase
$this->assertNull($form->getData()); $this->assertNull($form->getData());
} }
public function testSubmitEmptyMultiple()
{
$form = $this->factory->createBuilder('file', null, array(
'multiple' => true,
))->getForm();
// submitted data when an input file is uploaded without choosing any file
$form->submit(array(null));
$this->assertSame(array(), $form->getData());
}
public function testSetDataMultiple()
{
$form = $this->factory->createBuilder('file', null, array(
'multiple' => true,
))->getForm();
$data = array(
$this->createUploadedFileMock('abcdef', 'first.jpg', true),
$this->createUploadedFileMock('zyxwvu', 'second.jpg', true),
);
$form->setData($data);
$this->assertSame($data, $form->getData());
}
public function testSubmitMultiple() public function testSubmitMultiple()
{ {
$form = $this->factory->createBuilder('Symfony\Component\Form\Extension\Core\Type\FileType', null, array( $form = $this->factory->createBuilder('Symfony\Component\Form\Extension\Core\Type\FileType', null, array(

View File

@ -36,6 +36,13 @@ class ControllerResolver implements ControllerResolverInterface
*/ */
private $supportsVariadic; private $supportsVariadic;
/**
* If scalar types exists.
*
* @var bool
*/
private $supportsScalarTypes;
/** /**
* Constructor. * Constructor.
* *
@ -46,6 +53,7 @@ class ControllerResolver implements ControllerResolverInterface
$this->logger = $logger; $this->logger = $logger;
$this->supportsVariadic = method_exists('ReflectionParameter', 'isVariadic'); $this->supportsVariadic = method_exists('ReflectionParameter', 'isVariadic');
$this->supportsScalarTypes = method_exists('ReflectionParameter', 'getType');
} }
/** /**
@ -132,7 +140,7 @@ class ControllerResolver implements ControllerResolverInterface
$arguments[] = $request; $arguments[] = $request;
} elseif ($param->isDefaultValueAvailable()) { } elseif ($param->isDefaultValueAvailable()) {
$arguments[] = $param->getDefaultValue(); $arguments[] = $param->getDefaultValue();
} elseif ($param->allowsNull()) { } elseif ($this->supportsScalarTypes && $param->hasType() && $param->allowsNull()) {
$arguments[] = null; $arguments[] = null;
} else { } else {
if (is_array($controller)) { if (is_array($controller)) {

View File

@ -223,6 +223,19 @@ class ControllerResolverTest extends \PHPUnit_Framework_TestCase
$mock->getController($request); $mock->getController($request);
} }
/**
* @expectedException \RuntimeException
*/
public function testIfExceptionIsThrownWhenMissingAnArgument()
{
$resolver = new ControllerResolver();
$request = Request::create('/');
$controller = array($this, 'controllerMethod1');
$resolver->getArguments($request, $controller);
}
/** /**
* @requires PHP 7.1 * @requires PHP 7.1
*/ */