Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

[Form] Moved FormHelper creation to TemplatingExtension

  • Loading branch information...
commit ed8736140f8240969dde1a2d9544082bb93d3daf 1 parent 87ccb6a
@webmozart webmozart authored
View
32 src/Symfony/Component/Form/Extension/Templating/TemplatingExtension.php
@@ -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))
+ ));
+ }
+}
View
48 src/Symfony/Component/Form/Forms.php
@@ -102,6 +102,54 @@
* ->getFormFactory();
* </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>
*/
final class Forms
View
45 src/Symfony/Component/Form/Tests/Extension/Templating/FormHelperDivLayoutTest.php
@@ -12,9 +12,7 @@
namespace Symfony\Component\Form\Tests\Extension\Templating;
use Symfony\Component\Form\FormView;
-use Symfony\Component\Form\FormRenderer;
-use Symfony\Component\Form\Extension\Templating\FormHelper;
-use Symfony\Component\Form\Extension\Templating\TemplatingRendererEngine;
+use Symfony\Component\Form\Extension\Templating\TemplatingExtension;
use Symfony\Component\Form\Tests\AbstractDivLayoutTest;
use Symfony\Component\Form\Tests\Extension\Templating\Fixtures\StubTemplateNameParser;
use Symfony\Component\Form\Tests\Extension\Templating\Fixtures\StubTranslator;
@@ -26,7 +24,10 @@
class FormHelperDivLayoutTest extends AbstractDivLayoutTest
{
- protected $helper;
+ /**
+ * @var PhpEngine
+ */
+ protected $engine;
protected function setUp()
{
@@ -39,7 +40,10 @@ protected function setUp()
}
parent::setUp();
+ }
+ protected function getExtensions()
+ {
// should be moved to the Form component once absolute file paths are supported
// by the default name parser in the Templating component
$reflClass = new \ReflectionClass('Symfony\Bundle\FrameworkBundle\FrameworkBundle');
@@ -47,57 +51,58 @@ protected function setUp()
$rootTheme = realpath(__DIR__.'/Resources');
$templateNameParser = new StubTemplateNameParser($root, $rootTheme);
$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);
- $engine->setHelpers(array(
- $this->helper,
+ $this->engine = new PhpEngine($templateNameParser, $loader);
+ $this->engine->addGlobal('global', '');
+ $this->engine->setHelpers(array(
new TranslatorHelper(new StubTranslator()),
));
+
+ return array_merge(parent::getExtensions(), array(
+ new TemplatingExtension($this->engine, $this->csrfProvider, array(
+ 'FrameworkBundle:Form',
+ )),
+ ));
}
protected function tearDown()
{
- $this->helper = null;
+ $this->engine = null;
}
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())
{
- return (string) $this->helper->label($view, $label, $vars);
+ return (string) $this->engine->get('form')->label($view, $label, $vars);
}
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())
{
- return (string) $this->helper->widget($view, $vars);
+ return (string) $this->engine->get('form')->widget($view, $vars);
}
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())
{
- return (string) $this->helper->rest($view, $vars);
+ return (string) $this->engine->get('form')->rest($view, $vars);
}
protected function setTheme(FormView $view, array $themes)
{
- $this->helper->setTheme($view, $themes);
+ $this->engine->get('form')->setTheme($view, $themes);
}
public static function themeBlockInheritanceProvider()
View
49 src/Symfony/Component/Form/Tests/Extension/Templating/FormHelperTableLayoutTest.php
@@ -12,9 +12,7 @@
namespace Symfony\Component\Form\Tests\Extension\Templating;
use Symfony\Component\Form\FormView;
-use Symfony\Component\Form\FormRenderer;
-use Symfony\Component\Form\Extension\Templating\FormHelper;
-use Symfony\Component\Form\Extension\Templating\TemplatingRendererEngine;
+use Symfony\Component\Form\Extension\Templating\TemplatingExtension;
use Symfony\Component\Form\Tests\AbstractTableLayoutTest;
use Symfony\Component\Form\Tests\Extension\Templating\Fixtures\StubTemplateNameParser;
use Symfony\Component\Form\Tests\Extension\Templating\Fixtures\StubTranslator;
@@ -26,7 +24,10 @@
class FormHelperTableLayoutTest extends AbstractTableLayoutTest
{
- protected $helper;
+ /**
+ * @var PhpEngine
+ */
+ protected $engine;
protected function setUp()
{
@@ -39,7 +40,10 @@ protected function setUp()
}
parent::setUp();
+ }
+ protected function getExtensions()
+ {
// should be moved to the Form component once absolute file paths are supported
// by the default name parser in the Templating component
$reflClass = new \ReflectionClass('Symfony\Bundle\FrameworkBundle\FrameworkBundle');
@@ -47,59 +51,58 @@ protected function setUp()
$rootTheme = realpath(__DIR__.'/Resources');
$templateNameParser = new StubTemplateNameParser($root, $rootTheme);
$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);
-
- $engine->setHelpers(array(
- $this->helper,
+ $this->engine = new PhpEngine($templateNameParser, $loader);
+ $this->engine->addGlobal('global', '');
+ $this->engine->setHelpers(array(
new TranslatorHelper(new StubTranslator()),
));
+
+ return array_merge(parent::getExtensions(), array(
+ new TemplatingExtension($this->engine, $this->csrfProvider, array(
+ 'FrameworkBundle:Form',
+ 'FrameworkBundle:FormTable',
+ )),
+ ));
}
protected function tearDown()
{
- $this->helper = null;
+ $this->engine = null;
}
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())
{
- return (string) $this->helper->label($view, $label, $vars);
+ return (string) $this->engine->get('form')->label($view, $label, $vars);
}
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())
{
- return (string) $this->helper->widget($view, $vars);
+ return (string) $this->engine->get('form')->widget($view, $vars);
}
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())
{
- return (string) $this->helper->rest($view, $vars);
+ return (string) $this->engine->get('form')->rest($view, $vars);
}
protected function setTheme(FormView $view, array $themes)
{
- $this->helper->setTheme($view, $themes);
+ $this->engine->get('form')->setTheme($view, $themes);
}
}
Please sign in to comment.
Something went wrong with that request. Please try again.