[Form] Moved FormHelper creation to TemplatingExtension
This commit is contained in:
parent
87ccb6adb9
commit
ed8736140f
@ -0,0 +1,32 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
/*
|
||||||
|
* This file is part of the Symfony package.
|
||||||
|
*
|
||||||
|
* (c) Fabien Potencier <fabien@symfony.com>
|
||||||
|
*
|
||||||
|
* For the full copyright and license information, please view the LICENSE
|
||||||
|
* file that was distributed with this source code.
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace Symfony\Component\Form\Extension\Templating;
|
||||||
|
|
||||||
|
use Symfony\Component\Form\AbstractExtension;
|
||||||
|
use Symfony\Component\Form\FormRenderer;
|
||||||
|
use Symfony\Component\Form\Extension\Csrf\CsrfProvider\CsrfProviderInterface;
|
||||||
|
use Symfony\Component\Templating\PhpEngine;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Integrates the Templating component with the Form library.
|
||||||
|
*
|
||||||
|
* @author Bernhard Schussek <bschussek@gmail.com>
|
||||||
|
*/
|
||||||
|
class TemplatingExtension extends AbstractExtension
|
||||||
|
{
|
||||||
|
public function __construct(PhpEngine $engine, CsrfProviderInterface $csrfProvider = null, array $defaultThemes = array())
|
||||||
|
{
|
||||||
|
$engine->addHelpers(array(
|
||||||
|
new FormHelper(new FormRenderer(new TemplatingRendererEngine($engine, $defaultThemes), $csrfProvider))
|
||||||
|
));
|
||||||
|
}
|
||||||
|
}
|
@ -102,6 +102,54 @@ use Symfony\Component\Form\Extension\HttpFoundation\HttpFoundationExtension;
|
|||||||
* ->getFormFactory();
|
* ->getFormFactory();
|
||||||
* </code>
|
* </code>
|
||||||
*
|
*
|
||||||
|
* Support for the Templating component is provided by TemplatingExtension.
|
||||||
|
* This extension needs a PhpEngine object for rendering forms. As second
|
||||||
|
* argument you should pass the names of the default themes. Here is an
|
||||||
|
* example for using the default layout with "<div>" tags:
|
||||||
|
*
|
||||||
|
* <code>
|
||||||
|
* use Symfony\Component\Form\Extension\Templating\TemplatingExtension;
|
||||||
|
*
|
||||||
|
* $formFactory = Forms::createFormFactoryBuilder()
|
||||||
|
* ->addExtension(new TemplatingExtension($engine, null, array(
|
||||||
|
* 'FrameworkBundle:Form',
|
||||||
|
* )))
|
||||||
|
* ->getFormFactory();
|
||||||
|
* </code>
|
||||||
|
*
|
||||||
|
* The next example shows how to include the "<table>" layout:
|
||||||
|
*
|
||||||
|
* <code>
|
||||||
|
* use Symfony\Component\Form\Extension\Templating\TemplatingExtension;
|
||||||
|
*
|
||||||
|
* $formFactory = Forms::createFormFactoryBuilder()
|
||||||
|
* ->addExtension(new TemplatingExtension($engine, null, array(
|
||||||
|
* 'FrameworkBundle:Form',
|
||||||
|
* 'FrameworkBundle:FormTable',
|
||||||
|
* )))
|
||||||
|
* ->getFormFactory();
|
||||||
|
* </code>
|
||||||
|
*
|
||||||
|
* If you also loaded the CsrfExtension, you should pass the CSRF provider
|
||||||
|
* to the extension so that you can render CSRF tokens in your templates
|
||||||
|
* more easily:
|
||||||
|
*
|
||||||
|
* <code>
|
||||||
|
* use Symfony\Component\Form\Extension\Csrf\CsrfExtension;
|
||||||
|
* use Symfony\Component\Form\Extension\Csrf\CsrfProvider\DefaultCsrfProvider;
|
||||||
|
* use Symfony\Component\Form\Extension\Templating\TemplatingExtension;
|
||||||
|
*
|
||||||
|
*
|
||||||
|
* $secret = 'V8a5Z97e...';
|
||||||
|
* $csrfProvider = new DefaultCsrfProvider($secret);
|
||||||
|
* $formFactory = Forms::createFormFactoryBuilder()
|
||||||
|
* ->addExtension(new CsrfExtension($csrfProvider))
|
||||||
|
* ->addExtension(new TemplatingExtension($engine, $csrfProvider, array(
|
||||||
|
* 'FrameworkBundle:Form',
|
||||||
|
* )))
|
||||||
|
* ->getFormFactory();
|
||||||
|
* </code>
|
||||||
|
*
|
||||||
* @author Bernhard Schussek <bschussek@gmail.com>
|
* @author Bernhard Schussek <bschussek@gmail.com>
|
||||||
*/
|
*/
|
||||||
final class Forms
|
final class Forms
|
||||||
|
@ -12,9 +12,7 @@
|
|||||||
namespace Symfony\Component\Form\Tests\Extension\Templating;
|
namespace Symfony\Component\Form\Tests\Extension\Templating;
|
||||||
|
|
||||||
use Symfony\Component\Form\FormView;
|
use Symfony\Component\Form\FormView;
|
||||||
use Symfony\Component\Form\FormRenderer;
|
use Symfony\Component\Form\Extension\Templating\TemplatingExtension;
|
||||||
use Symfony\Component\Form\Extension\Templating\FormHelper;
|
|
||||||
use Symfony\Component\Form\Extension\Templating\TemplatingRendererEngine;
|
|
||||||
use Symfony\Component\Form\Tests\AbstractDivLayoutTest;
|
use Symfony\Component\Form\Tests\AbstractDivLayoutTest;
|
||||||
use Symfony\Component\Form\Tests\Extension\Templating\Fixtures\StubTemplateNameParser;
|
use Symfony\Component\Form\Tests\Extension\Templating\Fixtures\StubTemplateNameParser;
|
||||||
use Symfony\Component\Form\Tests\Extension\Templating\Fixtures\StubTranslator;
|
use Symfony\Component\Form\Tests\Extension\Templating\Fixtures\StubTranslator;
|
||||||
@ -26,7 +24,10 @@ use Symfony\Bundle\FrameworkBundle\Templating\Helper\TranslatorHelper;
|
|||||||
|
|
||||||
class FormHelperDivLayoutTest extends AbstractDivLayoutTest
|
class FormHelperDivLayoutTest extends AbstractDivLayoutTest
|
||||||
{
|
{
|
||||||
protected $helper;
|
/**
|
||||||
|
* @var PhpEngine
|
||||||
|
*/
|
||||||
|
protected $engine;
|
||||||
|
|
||||||
protected function setUp()
|
protected function setUp()
|
||||||
{
|
{
|
||||||
@ -39,7 +40,10 @@ class FormHelperDivLayoutTest extends AbstractDivLayoutTest
|
|||||||
}
|
}
|
||||||
|
|
||||||
parent::setUp();
|
parent::setUp();
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function getExtensions()
|
||||||
|
{
|
||||||
// should be moved to the Form component once absolute file paths are supported
|
// should be moved to the Form component once absolute file paths are supported
|
||||||
// by the default name parser in the Templating component
|
// by the default name parser in the Templating component
|
||||||
$reflClass = new \ReflectionClass('Symfony\Bundle\FrameworkBundle\FrameworkBundle');
|
$reflClass = new \ReflectionClass('Symfony\Bundle\FrameworkBundle\FrameworkBundle');
|
||||||
@ -47,57 +51,58 @@ class FormHelperDivLayoutTest extends AbstractDivLayoutTest
|
|||||||
$rootTheme = realpath(__DIR__.'/Resources');
|
$rootTheme = realpath(__DIR__.'/Resources');
|
||||||
$templateNameParser = new StubTemplateNameParser($root, $rootTheme);
|
$templateNameParser = new StubTemplateNameParser($root, $rootTheme);
|
||||||
$loader = new FilesystemLoader(array());
|
$loader = new FilesystemLoader(array());
|
||||||
$engine = new PhpEngine($templateNameParser, $loader);
|
|
||||||
$engine->addGlobal('global', '');
|
|
||||||
$rendererEngine = new TemplatingRendererEngine($engine, array('FrameworkBundle:Form'));
|
|
||||||
$renderer = new FormRenderer($rendererEngine, $this->getMock('Symfony\Component\Form\Extension\Csrf\CsrfProvider\CsrfProviderInterface'));
|
|
||||||
|
|
||||||
$this->helper = new FormHelper($renderer);
|
$this->engine = new PhpEngine($templateNameParser, $loader);
|
||||||
|
$this->engine->addGlobal('global', '');
|
||||||
$engine->setHelpers(array(
|
$this->engine->setHelpers(array(
|
||||||
$this->helper,
|
|
||||||
new TranslatorHelper(new StubTranslator()),
|
new TranslatorHelper(new StubTranslator()),
|
||||||
));
|
));
|
||||||
|
|
||||||
|
return array_merge(parent::getExtensions(), array(
|
||||||
|
new TemplatingExtension($this->engine, $this->csrfProvider, array(
|
||||||
|
'FrameworkBundle:Form',
|
||||||
|
)),
|
||||||
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function tearDown()
|
protected function tearDown()
|
||||||
{
|
{
|
||||||
$this->helper = null;
|
$this->engine = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function renderEnctype(FormView $view)
|
protected function renderEnctype(FormView $view)
|
||||||
{
|
{
|
||||||
return (string) $this->helper->enctype($view);
|
return (string) $this->engine->get('form')->enctype($view);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function renderLabel(FormView $view, $label = null, array $vars = array())
|
protected function renderLabel(FormView $view, $label = null, array $vars = array())
|
||||||
{
|
{
|
||||||
return (string) $this->helper->label($view, $label, $vars);
|
return (string) $this->engine->get('form')->label($view, $label, $vars);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function renderErrors(FormView $view)
|
protected function renderErrors(FormView $view)
|
||||||
{
|
{
|
||||||
return (string) $this->helper->errors($view);
|
return (string) $this->engine->get('form')->errors($view);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function renderWidget(FormView $view, array $vars = array())
|
protected function renderWidget(FormView $view, array $vars = array())
|
||||||
{
|
{
|
||||||
return (string) $this->helper->widget($view, $vars);
|
return (string) $this->engine->get('form')->widget($view, $vars);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function renderRow(FormView $view, array $vars = array())
|
protected function renderRow(FormView $view, array $vars = array())
|
||||||
{
|
{
|
||||||
return (string) $this->helper->row($view, $vars);
|
return (string) $this->engine->get('form')->row($view, $vars);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function renderRest(FormView $view, array $vars = array())
|
protected function renderRest(FormView $view, array $vars = array())
|
||||||
{
|
{
|
||||||
return (string) $this->helper->rest($view, $vars);
|
return (string) $this->engine->get('form')->rest($view, $vars);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function setTheme(FormView $view, array $themes)
|
protected function setTheme(FormView $view, array $themes)
|
||||||
{
|
{
|
||||||
$this->helper->setTheme($view, $themes);
|
$this->engine->get('form')->setTheme($view, $themes);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function themeBlockInheritanceProvider()
|
public static function themeBlockInheritanceProvider()
|
||||||
|
@ -12,9 +12,7 @@
|
|||||||
namespace Symfony\Component\Form\Tests\Extension\Templating;
|
namespace Symfony\Component\Form\Tests\Extension\Templating;
|
||||||
|
|
||||||
use Symfony\Component\Form\FormView;
|
use Symfony\Component\Form\FormView;
|
||||||
use Symfony\Component\Form\FormRenderer;
|
use Symfony\Component\Form\Extension\Templating\TemplatingExtension;
|
||||||
use Symfony\Component\Form\Extension\Templating\FormHelper;
|
|
||||||
use Symfony\Component\Form\Extension\Templating\TemplatingRendererEngine;
|
|
||||||
use Symfony\Component\Form\Tests\AbstractTableLayoutTest;
|
use Symfony\Component\Form\Tests\AbstractTableLayoutTest;
|
||||||
use Symfony\Component\Form\Tests\Extension\Templating\Fixtures\StubTemplateNameParser;
|
use Symfony\Component\Form\Tests\Extension\Templating\Fixtures\StubTemplateNameParser;
|
||||||
use Symfony\Component\Form\Tests\Extension\Templating\Fixtures\StubTranslator;
|
use Symfony\Component\Form\Tests\Extension\Templating\Fixtures\StubTranslator;
|
||||||
@ -26,7 +24,10 @@ use Symfony\Bundle\FrameworkBundle\Templating\Helper\TranslatorHelper;
|
|||||||
|
|
||||||
class FormHelperTableLayoutTest extends AbstractTableLayoutTest
|
class FormHelperTableLayoutTest extends AbstractTableLayoutTest
|
||||||
{
|
{
|
||||||
protected $helper;
|
/**
|
||||||
|
* @var PhpEngine
|
||||||
|
*/
|
||||||
|
protected $engine;
|
||||||
|
|
||||||
protected function setUp()
|
protected function setUp()
|
||||||
{
|
{
|
||||||
@ -39,7 +40,10 @@ class FormHelperTableLayoutTest extends AbstractTableLayoutTest
|
|||||||
}
|
}
|
||||||
|
|
||||||
parent::setUp();
|
parent::setUp();
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function getExtensions()
|
||||||
|
{
|
||||||
// should be moved to the Form component once absolute file paths are supported
|
// should be moved to the Form component once absolute file paths are supported
|
||||||
// by the default name parser in the Templating component
|
// by the default name parser in the Templating component
|
||||||
$reflClass = new \ReflectionClass('Symfony\Bundle\FrameworkBundle\FrameworkBundle');
|
$reflClass = new \ReflectionClass('Symfony\Bundle\FrameworkBundle\FrameworkBundle');
|
||||||
@ -47,59 +51,58 @@ class FormHelperTableLayoutTest extends AbstractTableLayoutTest
|
|||||||
$rootTheme = realpath(__DIR__.'/Resources');
|
$rootTheme = realpath(__DIR__.'/Resources');
|
||||||
$templateNameParser = new StubTemplateNameParser($root, $rootTheme);
|
$templateNameParser = new StubTemplateNameParser($root, $rootTheme);
|
||||||
$loader = new FilesystemLoader(array());
|
$loader = new FilesystemLoader(array());
|
||||||
$engine = new PhpEngine($templateNameParser, $loader);
|
|
||||||
$engine->addGlobal('global', '');
|
|
||||||
$rendererEngine = new TemplatingRendererEngine($engine, array(
|
|
||||||
'FrameworkBundle:Form',
|
|
||||||
'FrameworkBundle:FormTable'
|
|
||||||
));
|
|
||||||
$renderer = new FormRenderer($rendererEngine, $this->getMock('Symfony\Component\Form\Extension\Csrf\CsrfProvider\CsrfProviderInterface'));
|
|
||||||
|
|
||||||
$this->helper = new FormHelper($renderer);
|
$this->engine = new PhpEngine($templateNameParser, $loader);
|
||||||
|
$this->engine->addGlobal('global', '');
|
||||||
$engine->setHelpers(array(
|
$this->engine->setHelpers(array(
|
||||||
$this->helper,
|
|
||||||
new TranslatorHelper(new StubTranslator()),
|
new TranslatorHelper(new StubTranslator()),
|
||||||
));
|
));
|
||||||
|
|
||||||
|
return array_merge(parent::getExtensions(), array(
|
||||||
|
new TemplatingExtension($this->engine, $this->csrfProvider, array(
|
||||||
|
'FrameworkBundle:Form',
|
||||||
|
'FrameworkBundle:FormTable',
|
||||||
|
)),
|
||||||
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function tearDown()
|
protected function tearDown()
|
||||||
{
|
{
|
||||||
$this->helper = null;
|
$this->engine = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function renderEnctype(FormView $view)
|
protected function renderEnctype(FormView $view)
|
||||||
{
|
{
|
||||||
return (string) $this->helper->enctype($view);
|
return (string) $this->engine->get('form')->enctype($view);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function renderLabel(FormView $view, $label = null, array $vars = array())
|
protected function renderLabel(FormView $view, $label = null, array $vars = array())
|
||||||
{
|
{
|
||||||
return (string) $this->helper->label($view, $label, $vars);
|
return (string) $this->engine->get('form')->label($view, $label, $vars);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function renderErrors(FormView $view)
|
protected function renderErrors(FormView $view)
|
||||||
{
|
{
|
||||||
return (string) $this->helper->errors($view);
|
return (string) $this->engine->get('form')->errors($view);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function renderWidget(FormView $view, array $vars = array())
|
protected function renderWidget(FormView $view, array $vars = array())
|
||||||
{
|
{
|
||||||
return (string) $this->helper->widget($view, $vars);
|
return (string) $this->engine->get('form')->widget($view, $vars);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function renderRow(FormView $view, array $vars = array())
|
protected function renderRow(FormView $view, array $vars = array())
|
||||||
{
|
{
|
||||||
return (string) $this->helper->row($view, $vars);
|
return (string) $this->engine->get('form')->row($view, $vars);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function renderRest(FormView $view, array $vars = array())
|
protected function renderRest(FormView $view, array $vars = array())
|
||||||
{
|
{
|
||||||
return (string) $this->helper->rest($view, $vars);
|
return (string) $this->engine->get('form')->rest($view, $vars);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function setTheme(FormView $view, array $themes)
|
protected function setTheme(FormView $view, array $themes)
|
||||||
{
|
{
|
||||||
$this->helper->setTheme($view, $themes);
|
$this->engine->get('form')->setTheme($view, $themes);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user