Merge branch '3.4'

* 3.4:
  Fix phpunit bridge
  [Form] [TwigBridge] Added option to disable usage of default themes when rendering a form
This commit is contained in:
Nicolas Grekas 2017-10-13 18:40:22 +02:00
commit e69f9dbc25
24 changed files with 109 additions and 44 deletions

View File

@ -19,5 +19,6 @@ class ProcessIsolationTest extends TestCase
public function testIsolation() public function testIsolation()
{ {
@trigger_error('Test abc', E_USER_DEPRECATED); @trigger_error('Test abc', E_USER_DEPRECATED);
$test->addToAssertionCount(1);
} }
} }

View File

@ -68,7 +68,7 @@ if (!file_exists("$PHPUNIT_DIR/phpunit-$PHPUNIT_VERSION/phpunit") || md5_file(__
if (5.1 <= $PHPUNIT_VERSION && $PHPUNIT_VERSION < 5.4) { if (5.1 <= $PHPUNIT_VERSION && $PHPUNIT_VERSION < 5.4) {
passthru("$COMPOSER require --no-update phpunit/phpunit-mock-objects \"~3.1.0\""); passthru("$COMPOSER require --no-update phpunit/phpunit-mock-objects \"~3.1.0\"");
} }
passthru("$COMPOSER require --no-update symfony/phpunit-bridge \">=3.2@dev\""); passthru("$COMPOSER require --no-update symfony/phpunit-bridge \">=3.3.11@dev\"");
$prevRoot = getenv('COMPOSER_ROOT_VERSION'); $prevRoot = getenv('COMPOSER_ROOT_VERSION');
putenv("COMPOSER_ROOT_VERSION=$PHPUNIT_VERSION"); putenv("COMPOSER_ROOT_VERSION=$PHPUNIT_VERSION");
$exit = proc_close(proc_open("$COMPOSER install --no-dev --prefer-dist --no-progress --ansi", array(), $p, getcwd(), null, array('bypass_shell' => true))); $exit = proc_close(proc_open("$COMPOSER install --no-dev --prefer-dist --no-progress --ansi", array(), $p, getcwd(), null, array('bypass_shell' => true)));

View File

@ -4,6 +4,7 @@ CHANGELOG
3.4.0 3.4.0
----- -----
* added an `only` keyword to `form_theme` tag to disable usage of default themes when rendering a form
* deprecated `Symfony\Bridge\Twig\Form\TwigRenderer` * deprecated `Symfony\Bridge\Twig\Form\TwigRenderer`
* deprecated `DebugCommand::set/getTwigEnvironment`. Pass an instance of * deprecated `DebugCommand::set/getTwigEnvironment`. Pass an instance of
`Twig\Environment` as first argument of the constructor instead `Twig\Environment` as first argument of the constructor instead

View File

@ -106,9 +106,11 @@ class TwigRendererEngine extends AbstractRendererEngine
// Check the default themes once we reach the root view without success // Check the default themes once we reach the root view without success
if (!$view->parent) { if (!$view->parent) {
for ($i = count($this->defaultThemes) - 1; $i >= 0; --$i) { if (!isset($this->useDefaultThemes[$cacheKey]) || $this->useDefaultThemes[$cacheKey]) {
$this->loadResourcesFromTheme($cacheKey, $this->defaultThemes[$i]); for ($i = count($this->defaultThemes) - 1; $i >= 0; --$i) {
// CONTINUE LOADING (see doc comment) $this->loadResourcesFromTheme($cacheKey, $this->defaultThemes[$i]);
// CONTINUE LOADING (see doc comment)
}
} }
} }

View File

@ -20,9 +20,9 @@ use Twig\Node\Node;
*/ */
class FormThemeNode extends Node class FormThemeNode extends Node
{ {
public function __construct(Node $form, Node $resources, $lineno, $tag = null) public function __construct(Node $form, Node $resources, $lineno, $tag = null, $only = false)
{ {
parent::__construct(array('form' => $form, 'resources' => $resources), array(), $lineno, $tag); parent::__construct(array('form' => $form, 'resources' => $resources), array('only' => (bool) $only), $lineno, $tag);
} }
public function compile(Compiler $compiler) public function compile(Compiler $compiler)
@ -35,6 +35,8 @@ class FormThemeNode extends Node
->subcompile($this->getNode('form')) ->subcompile($this->getNode('form'))
->raw(', ') ->raw(', ')
->subcompile($this->getNode('resources')) ->subcompile($this->getNode('resources'))
->raw(', ')
->raw(false === $this->getAttribute('only') ? 'true' : 'false')
->raw(");\n"); ->raw(");\n");
} }
} }

View File

@ -99,8 +99,8 @@ class FormExtensionBootstrap3HorizontalLayoutTest extends AbstractBootstrap3Hori
return (string) $this->renderer->renderBlock($view, 'form_end', $vars); return (string) $this->renderer->renderBlock($view, 'form_end', $vars);
} }
protected function setTheme(FormView $view, array $themes) protected function setTheme(FormView $view, array $themes, $useDefaultThemes = true)
{ {
$this->renderer->setTheme($view, $themes); $this->renderer->setTheme($view, $themes, $useDefaultThemes);
} }
} }

View File

@ -119,8 +119,8 @@ class FormExtensionBootstrap3LayoutTest extends AbstractBootstrap3LayoutTest
return (string) $this->renderer->renderBlock($view, 'form_end', $vars); return (string) $this->renderer->renderBlock($view, 'form_end', $vars);
} }
protected function setTheme(FormView $view, array $themes) protected function setTheme(FormView $view, array $themes, $useDefaultThemes = true)
{ {
$this->renderer->setTheme($view, $themes); $this->renderer->setTheme($view, $themes, $useDefaultThemes);
} }
} }

View File

@ -100,8 +100,8 @@ class FormExtensionBootstrap4HorizontalLayoutTest extends AbstractBootstrap4Hori
return (string) $this->renderer->renderBlock($view, 'form_end', $vars); return (string) $this->renderer->renderBlock($view, 'form_end', $vars);
} }
protected function setTheme(FormView $view, array $themes) protected function setTheme(FormView $view, array $themes, $useDefaultThemes = true)
{ {
$this->renderer->setTheme($view, $themes); $this->renderer->setTheme($view, $themes, $useDefaultThemes);
} }
} }

View File

@ -122,8 +122,8 @@ class FormExtensionBootstrap4LayoutTest extends AbstractBootstrap4LayoutTest
return (string) $this->renderer->renderBlock($view, 'form_end', $vars); return (string) $this->renderer->renderBlock($view, 'form_end', $vars);
} }
protected function setTheme(FormView $view, array $themes) protected function setTheme(FormView $view, array $themes, $useDefaultThemes = true)
{ {
$this->renderer->setTheme($view, $themes); $this->renderer->setTheme($view, $themes, $useDefaultThemes);
} }
} }

View File

@ -193,9 +193,9 @@ class FormExtensionDivLayoutTest extends AbstractDivLayoutTest
return (string) $this->renderer->renderBlock($view, 'form_end', $vars); return (string) $this->renderer->renderBlock($view, 'form_end', $vars);
} }
protected function setTheme(FormView $view, array $themes) protected function setTheme(FormView $view, array $themes, $useDefaultThemes = true)
{ {
$this->renderer->setTheme($view, $themes); $this->renderer->setTheme($view, $themes, $useDefaultThemes);
} }
public static function themeBlockInheritanceProvider() public static function themeBlockInheritanceProvider()

View File

@ -120,8 +120,8 @@ class FormExtensionTableLayoutTest extends AbstractTableLayoutTest
return (string) $this->renderer->renderBlock($view, 'form_end', $vars); return (string) $this->renderer->renderBlock($view, 'form_end', $vars);
} }
protected function setTheme(FormView $view, array $themes) protected function setTheme(FormView $view, array $themes, $useDefaultThemes = true)
{ {
$this->renderer->setTheme($view, $themes); $this->renderer->setTheme($view, $themes, $useDefaultThemes);
} }
} }

View File

@ -39,6 +39,7 @@ class FormThemeTest extends TestCase
$this->assertEquals($form, $node->getNode('form')); $this->assertEquals($form, $node->getNode('form'));
$this->assertEquals($resources, $node->getNode('resources')); $this->assertEquals($resources, $node->getNode('resources'));
$this->assertFalse($node->getAttribute('only'));
} }
public function testCompile() public function testCompile()
@ -60,7 +61,17 @@ class FormThemeTest extends TestCase
$this->assertEquals( $this->assertEquals(
sprintf( sprintf(
'$this->env->getRuntime("Symfony\\\\Component\\\\Form\\\\FormRenderer")->setTheme(%s, array(0 => "tpl1", 1 => "tpl2"));', '$this->env->getRuntime("Symfony\\\\Component\\\\Form\\\\FormRenderer")->setTheme(%s, array(0 => "tpl1", 1 => "tpl2"), true);',
$this->getVariableGetter('form')
),
trim($compiler->compile($node)->getSource())
);
$node = new FormThemeNode($form, $resources, 0, null, true);
$this->assertEquals(
sprintf(
'$this->env->getRuntime("Symfony\\\\Component\\\\Form\\\\FormRenderer")->setTheme(%s, array(0 => "tpl1", 1 => "tpl2"), false);',
$this->getVariableGetter('form') $this->getVariableGetter('form')
), ),
trim($compiler->compile($node)->getSource()) trim($compiler->compile($node)->getSource())
@ -72,7 +83,17 @@ class FormThemeTest extends TestCase
$this->assertEquals( $this->assertEquals(
sprintf( sprintf(
'$this->env->getRuntime("Symfony\\\\Component\\\\Form\\\\FormRenderer")->setTheme(%s, "tpl1");', '$this->env->getRuntime("Symfony\\\\Component\\\\Form\\\\FormRenderer")->setTheme(%s, "tpl1", true);',
$this->getVariableGetter('form')
),
trim($compiler->compile($node)->getSource())
);
$node = new FormThemeNode($form, $resources, 0, null, true);
$this->assertEquals(
sprintf(
'$this->env->getRuntime("Symfony\\\\Component\\\\Form\\\\FormRenderer")->setTheme(%s, "tpl1", false);',
$this->getVariableGetter('form') $this->getVariableGetter('form')
), ),
trim($compiler->compile($node)->getSource()) trim($compiler->compile($node)->getSource())

View File

@ -100,6 +100,21 @@ class FormThemeTokenParserTest extends TestCase
'form_theme' 'form_theme'
), ),
), ),
array(
'{% form_theme form with ["tpl1", "tpl2"] only %}',
new FormThemeNode(
new NameExpression('form', 1),
new ArrayExpression(array(
new ConstantExpression(0, 1),
new ConstantExpression('tpl1', 1),
new ConstantExpression(1, 1),
new ConstantExpression('tpl2', 1),
), 1),
1,
'form_theme',
true
),
),
); );
} }
} }

View File

@ -37,10 +37,15 @@ class FormThemeTokenParser extends AbstractTokenParser
$stream = $this->parser->getStream(); $stream = $this->parser->getStream();
$form = $this->parser->getExpressionParser()->parseExpression(); $form = $this->parser->getExpressionParser()->parseExpression();
$only = false;
if ($this->parser->getStream()->test(Token::NAME_TYPE, 'with')) { if ($this->parser->getStream()->test(Token::NAME_TYPE, 'with')) {
$this->parser->getStream()->next(); $this->parser->getStream()->next();
$resources = $this->parser->getExpressionParser()->parseExpression(); $resources = $this->parser->getExpressionParser()->parseExpression();
if ($this->parser->getStream()->nextIf(Token::NAME_TYPE, 'only')) {
$only = true;
}
} else { } else {
$resources = new ArrayExpression(array(), $stream->getCurrent()->getLine()); $resources = new ArrayExpression(array(), $stream->getCurrent()->getLine());
do { do {
@ -50,7 +55,7 @@ class FormThemeTokenParser extends AbstractTokenParser
$stream->expect(Token::BLOCK_END_TYPE); $stream->expect(Token::BLOCK_END_TYPE);
return new FormThemeNode($form, $resources, $lineno, $this->getTag()); return new FormThemeNode($form, $resources, $lineno, $this->getTag(), $only);
} }
/** /**

View File

@ -49,12 +49,13 @@ class FormHelper extends Helper
* *
* The theme format is "<Bundle>:<Controller>". * The theme format is "<Bundle>:<Controller>".
* *
* @param FormView $view A FormView instance * @param FormView $view A FormView instance
* @param string|array $themes A theme or an array of theme * @param string|array $themes A theme or an array of theme
* @param bool $useDefaultThemes If true, will use default themes defined in the renderer
*/ */
public function setTheme(FormView $view, $themes) public function setTheme(FormView $view, $themes, $useDefaultThemes = true)
{ {
$this->renderer->setTheme($view, $themes); $this->renderer->setTheme($view, $themes, $useDefaultThemes);
} }
/** /**

View File

@ -121,9 +121,9 @@ class FormHelperDivLayoutTest extends AbstractDivLayoutTest
return (string) $this->engine->get('form')->end($view, $vars); return (string) $this->engine->get('form')->end($view, $vars);
} }
protected function setTheme(FormView $view, array $themes) protected function setTheme(FormView $view, array $themes, $useDefaultThemes = true)
{ {
$this->engine->get('form')->setTheme($view, $themes); $this->engine->get('form')->setTheme($view, $themes, $useDefaultThemes);
} }
public static function themeBlockInheritanceProvider() public static function themeBlockInheritanceProvider()

View File

@ -122,8 +122,8 @@ class FormHelperTableLayoutTest extends AbstractTableLayoutTest
return (string) $this->engine->get('form')->end($view, $vars); return (string) $this->engine->get('form')->end($view, $vars);
} }
protected function setTheme(FormView $view, array $themes) protected function setTheme(FormView $view, array $themes, $useDefaultThemes = true)
{ {
$this->engine->get('form')->setTheme($view, $themes); $this->engine->get('form')->setTheme($view, $themes, $useDefaultThemes);
} }
} }

View File

@ -33,6 +33,11 @@ abstract class AbstractRendererEngine implements FormRendererEngineInterface
*/ */
protected $themes = array(); protected $themes = array();
/**
* @var array
*/
protected $useDefaultThemes = array();
/** /**
* @var array * @var array
*/ */
@ -57,13 +62,16 @@ abstract class AbstractRendererEngine implements FormRendererEngineInterface
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function setTheme(FormView $view, $themes) public function setTheme(FormView $view, $themes /*, $useDefaultThemes = true */)
{ {
$cacheKey = $view->vars[self::CACHE_KEY_VAR]; $cacheKey = $view->vars[self::CACHE_KEY_VAR];
// Do not cast, as casting turns objects into arrays of properties // Do not cast, as casting turns objects into arrays of properties
$this->themes[$cacheKey] = is_array($themes) ? $themes : array($themes); $this->themes[$cacheKey] = is_array($themes) ? $themes : array($themes);
$args = func_get_args();
$this->useDefaultThemes[$cacheKey] = isset($args[2]) ? (bool) $args[2] : true;
// Unset instead of resetting to an empty array, in order to allow // Unset instead of resetting to an empty array, in order to allow
// implementations (like TwigRendererEngine) to check whether $cacheKey // implementations (like TwigRendererEngine) to check whether $cacheKey
// is set at all. // is set at all.

View File

@ -26,6 +26,8 @@ CHANGELOG
* added `DebugCommand` * added `DebugCommand`
* deprecated `ChoiceLoaderInterface` implementation in `TimezoneType` * deprecated `ChoiceLoaderInterface` implementation in `TimezoneType`
* added options "input" and "regions" to `TimezoneType` * added options "input" and "regions" to `TimezoneType`
* added an option to ``Symfony\Component\Form\FormRendererEngineInterface::setTheme()`` and
``Symfony\Component\Form\FormRendererInterface::setTheme()`` to disable usage of default themes when rendering a form
3.3.0 3.3.0
----- -----

View File

@ -72,9 +72,11 @@ class TemplatingRendererEngine extends AbstractRendererEngine
// Check the default themes once we reach the root form without success // Check the default themes once we reach the root form without success
if (!$view->parent) { if (!$view->parent) {
for ($i = count($this->defaultThemes) - 1; $i >= 0; --$i) { if (!isset($this->useDefaultThemes[$cacheKey]) || $this->useDefaultThemes[$cacheKey]) {
if ($this->loadResourceFromTheme($cacheKey, $blockName, $this->defaultThemes[$i])) { for ($i = count($this->defaultThemes) - 1; $i >= 0; --$i) {
return true; if ($this->loadResourceFromTheme($cacheKey, $blockName, $this->defaultThemes[$i])) {
return true;
}
} }
} }
} }

View File

@ -70,9 +70,10 @@ class FormRenderer implements FormRendererInterface
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function setTheme(FormView $view, $themes) public function setTheme(FormView $view, $themes /*, $useDefaultThemes = true */)
{ {
$this->engine->setTheme($view, $themes); $args = func_get_args();
$this->engine->setTheme($view, $themes, isset($args[2]) ? (bool) $args[2] : true);
} }
/** /**

View File

@ -21,11 +21,13 @@ interface FormRendererEngineInterface
/** /**
* Sets the theme(s) to be used for rendering a view and its children. * Sets the theme(s) to be used for rendering a view and its children.
* *
* @param FormView $view The view to assign the theme(s) to * @param FormView $view The view to assign the theme(s) to
* @param mixed $themes The theme(s). The type of these themes * @param mixed $themes The theme(s). The type of these themes
* is open to the implementation. * is open to the implementation.
* @param bool $useDefaultThemes If true, will use default themes specified
* in the engine, will be added to the interface in 4.0
*/ */
public function setTheme(FormView $view, $themes); public function setTheme(FormView $view, $themes /*, $useDefaultThemes = true */);
/** /**
* Returns the resource for a block name. * Returns the resource for a block name.

View File

@ -28,11 +28,13 @@ interface FormRendererInterface
/** /**
* Sets the theme(s) to be used for rendering a view and its children. * Sets the theme(s) to be used for rendering a view and its children.
* *
* @param FormView $view The view to assign the theme(s) to * @param FormView $view The view to assign the theme(s) to
* @param mixed $themes The theme(s). The type of these themes * @param mixed $themes The theme(s). The type of these themes
* is open to the implementation. * is open to the implementation.
* @param bool $useDefaultThemes If true, will use default themes specified
* in the renderer, will be added to the interface in 4.0
*/ */
public function setTheme(FormView $view, $themes); public function setTheme(FormView $view, $themes /*, $useDefaultThemes = true */);
/** /**
* Renders a named block of the form theme. * Renders a named block of the form theme.

View File

@ -125,7 +125,7 @@ abstract class AbstractLayoutTest extends FormIntegrationTestCase
abstract protected function renderEnd(FormView $view, array $vars = array()); abstract protected function renderEnd(FormView $view, array $vars = array());
abstract protected function setTheme(FormView $view, array $themes); abstract protected function setTheme(FormView $view, array $themes, $useDefaultThemes = true);
public function testLabel() public function testLabel()
{ {