Merge branch '3.3' into 3.4

* 3.3:
  [DI] Fix deep-inlining of non-shared refs
  parse newlines in quoted multiline strings
  Fix collision between view properties and form fields
This commit is contained in:
Fabien Potencier 2017-12-04 10:15:22 -08:00
commit 9dff16479a
9 changed files with 150 additions and 14 deletions

View File

@ -15,6 +15,7 @@ use Symfony\Bridge\Twig\TokenParser\FormThemeTokenParser;
use Symfony\Bridge\Twig\Form\TwigRendererInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\Form\ChoiceList\View\ChoiceView;
use Symfony\Component\Form\FormView;
use Twig\Environment;
use Twig\Extension\AbstractExtension;
use Twig\Extension\InitRuntimeInterface;
@ -105,6 +106,7 @@ class FormExtension extends AbstractExtension implements InitRuntimeInterface
{
return array(
new TwigTest('selectedchoice', 'Symfony\Bridge\Twig\Extension\twig_is_selected_choice'),
new TwigTest('rootform', array($this, 'isRootForm')),
);
}
@ -164,6 +166,11 @@ class FormExtension extends AbstractExtension implements InitRuntimeInterface
unset($this->$name);
}
public function isRootForm(FormView $formView)
{
return null === $formView->parent;
}
/**
* {@inheritdoc}
*/

View File

@ -140,12 +140,12 @@
{% block form_errors -%}
{% if errors|length > 0 -%}
{% if form.parent %}<span class="help-block">{% else %}<div class="alert alert-danger">{% endif %}
{% if form is not rootform %}<span class="help-block">{% else %}<div class="alert alert-danger">{% endif %}
<ul class="list-unstyled">
{%- for error in errors -%}
<li><span class="glyphicon glyphicon-exclamation-sign"></span> {{ error.message }}</li>
{%- endfor -%}
</ul>
{% if form.parent %}</span>{% else %}</div>{% endif %}
{% if form is not rootform %}</span>{% else %}</div>{% endif %}
{%- endif %}
{%- endblock form_errors %}

View File

@ -15,7 +15,7 @@
{%- block form_widget_compound -%}
<div {{ block('widget_container_attributes') }}>
{%- if form.parent is empty -%}
{%- if form is rootform -%}
{{ form_errors(form) }}
{%- endif -%}
{{- block('form_rows') -}}
@ -349,7 +349,7 @@
{% endif %}
{%- endfor %}
{% if not form.methodRendered and form.parent is null %}
{% if not form.methodRendered and form is rootform %}
{%- do form.setMethodRendered() -%}
{% set method = method|upper %}
{%- if method in ["GET", "POST"] -%}

View File

@ -31,7 +31,7 @@
{%- block form_widget_compound -%}
<table {{ block('widget_container_attributes') }}>
{%- if form.parent is empty and errors|length > 0 -%}
{%- if form is rootform and errors|length > 0 -%}
<tr>
<td colspan="2">
{{- form_errors(form) -}}

View File

@ -149,6 +149,22 @@ class FormExtensionDivLayoutTest extends AbstractDivLayoutTest
$this->assertSame('<form name="form" method="get" action="0">', $html);
}
public function isRootFormProvider()
{
return array(
array(true, new FormView()),
array(false, new FormView(new FormView())),
);
}
/**
* @dataProvider isRootFormProvider
*/
public function testIsRootForm($expected, FormView $formView)
{
$this->assertSame($expected, $this->extension->isRootForm($formView));
}
protected function renderForm(FormView $view, array $vars = array())
{
return (string) $this->renderer->renderBlock($view, 'form', $vars);

View File

@ -13,6 +13,7 @@ namespace Symfony\Component\DependencyInjection\Compiler;
use Symfony\Component\DependencyInjection\Argument\ArgumentInterface;
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\Exception\ServiceCircularReferenceException;
use Symfony\Component\DependencyInjection\Reference;
/**
@ -23,6 +24,7 @@ use Symfony\Component\DependencyInjection\Reference;
class InlineServiceDefinitionsPass extends AbstractRecursivePass implements RepeatablePassInterface
{
private $repeatedPass;
private $cloningIds = array();
private $inlinedServiceIds = array();
/**
@ -58,18 +60,44 @@ class InlineServiceDefinitionsPass extends AbstractRecursivePass implements Repe
// Reference found in ArgumentInterface::getValues() are not inlineable
return $value;
}
if ($value instanceof Reference && $this->container->hasDefinition($id = (string) $value)) {
$definition = $this->container->getDefinition($id);
if ($this->isInlineableDefinition($id, $definition, $this->container->getCompiler()->getServiceReferenceGraph())) {
$this->container->log($this, sprintf('Inlined service "%s" to "%s".', $id, $this->currentId));
$this->inlinedServiceIds[$id][] = $this->currentId;
return $definition->isShared() ? $definition : clone $definition;
if ($value instanceof Definition && $this->cloningIds) {
if ($value->isShared()) {
return $value;
}
$value = clone $value;
}
return parent::processValue($value, $isRoot);
if (!$value instanceof Reference || !$this->container->hasDefinition($id = (string) $value)) {
return parent::processValue($value, $isRoot);
}
$definition = $this->container->getDefinition($id);
if (!$this->isInlineableDefinition($id, $definition, $this->container->getCompiler()->getServiceReferenceGraph())) {
return $value;
}
$this->container->log($this, sprintf('Inlined service "%s" to "%s".', $id, $this->currentId));
$this->inlinedServiceIds[$id][] = $this->currentId;
if ($definition->isShared()) {
return $definition;
}
if (isset($this->cloningIds[$id])) {
$ids = array_keys($this->cloningIds);
$ids[] = $id;
throw new ServiceCircularReferenceException($id, array_slice($ids, array_search($id, $ids)));
}
$this->cloningIds[$id] = true;
try {
return $this->processValue($definition);
} finally {
unset($this->cloningIds[$id]);
}
}
/**

View File

@ -111,6 +111,60 @@ class InlineServiceDefinitionsPassTest extends TestCase
$this->assertEquals($container->getDefinition('foo')->getArgument(0), $container->getDefinition('bar'));
}
/**
* @expectedException \Symfony\Component\DependencyInjection\Exception\ServiceCircularReferenceException
* @expectedExceptionMessage Circular reference detected for service "bar", path: "bar -> foo -> bar".
*/
public function testProcessThrowsOnNonSharedLoops()
{
$container = new ContainerBuilder();
$container
->register('foo')
->addArgument(new Reference('bar'))
->setShared(false)
;
$container
->register('bar')
->setShared(false)
->addMethodCall('setFoo', array(new Reference('foo')))
;
$this->process($container);
}
public function testProcessNestedNonSharedServices()
{
$container = new ContainerBuilder();
$container
->register('foo')
->addArgument(new Reference('bar1'))
->addArgument(new Reference('bar2'))
;
$container
->register('bar1')
->setShared(false)
->addArgument(new Reference('baz'))
;
$container
->register('bar2')
->setShared(false)
->addArgument(new Reference('baz'))
;
$container
->register('baz')
->setShared(false)
;
$this->process($container);
$baz1 = $container->getDefinition('foo')->getArgument(0)->getArgument(0);
$baz2 = $container->getDefinition('foo')->getArgument(1)->getArgument(0);
$this->assertEquals($container->getDefinition('baz'), $baz1);
$this->assertEquals($container->getDefinition('baz'), $baz2);
$this->assertNotSame($baz1, $baz2);
}
public function testProcessInlinesIfMultipleReferencesButAllFromTheSameDefinition()
{
$container = new ContainerBuilder();

View File

@ -738,6 +738,8 @@ class Parser
return Inline::parse($value, $flags, $this->refs);
}
$lines = array();
while ($this->moveToNextLine()) {
// unquoted strings end before the first unindented line
if (null === $quotation && 0 === $this->getCurrentLineIndentation()) {
@ -746,7 +748,7 @@ class Parser
break;
}
$value .= ' '.trim($this->currentLine);
$lines[] = trim($this->currentLine);
// quoted string values end with a line that is terminated with the quotation character
if ('' !== $this->currentLine && substr($this->currentLine, -1) === $quotation) {
@ -754,6 +756,21 @@ class Parser
}
}
for ($i = 0, $linesCount = count($lines), $previousLineBlank = false; $i < $linesCount; ++$i) {
if ('' === $lines[$i]) {
$value .= "\n";
$previousLineBlank = true;
} elseif ($previousLineBlank) {
$value .= $lines[$i];
$previousLineBlank = false;
} else {
$value .= ' '.$lines[$i];
$previousLineBlank = false;
}
}
Inline::$parsedLineNumber = $this->getRealCurrentLineNb();
$parsedValue = Inline::parse($value, $flags, $this->refs);
if ('mapping' === $context && is_string($parsedValue) && '"' !== $value[0] && "'" !== $value[0] && '[' !== $value[0] && '{' !== $value[0] && '!' !== $value[0] && false !== strpos($parsedValue, ': ')) {

View File

@ -1579,6 +1579,20 @@ YAML;
$this->assertSame($expected, $this->parser->parse($yaml));
}
public function testBlankLinesInQuotedMultiLineString()
{
$yaml = <<<YAML
foobar: 'foo
bar'
YAML;
$expected = array(
'foobar' => "foo\nbar",
);
$this->assertSame($expected, $this->parser->parse($yaml));
}
public function testParseMultiLineUnquotedString()
{
$yaml = <<<EOT