[Form] Registered FormFactory in the DIC
This commit is contained in:
parent
f2c1976da6
commit
e334c4734e
@ -8,11 +8,18 @@
|
||||
<parameter key="form.field_factory.class">Symfony\Component\Form\FieldFactory\FieldFactory</parameter>
|
||||
<parameter key="form.field_factory.validator_guesser.class">Symfony\Component\Form\FieldFactory\ValidatorFieldFactoryGuesser</parameter>
|
||||
<parameter key="form.csrf_provider.class">Symfony\Component\Form\CsrfProvider\SessionCsrfProvider</parameter>
|
||||
<parameter key="form.context.class">Symfony\Component\Form\FormContext</parameter>
|
||||
<parameter key="form.theme.class">Symfony\Component\Form\Renderer\Theme\TwigTheme</parameter>
|
||||
<parameter key="form.theme.template">TwigBundle::form.html.twig</parameter>
|
||||
<parameter key="form.factory.class">Symfony\Component\Form\FormFactory</parameter>
|
||||
<parameter key="form.csrf_protection.enabled">true</parameter>
|
||||
<parameter key="form.csrf_protection.field_name">_token</parameter>
|
||||
<parameter key="form.csrf_protection.secret">secret</parameter>
|
||||
<parameter key="form.validation_groups">Default</parameter>
|
||||
<parameter key="file.temporary_storage.class">Symfony\Component\HttpFoundation\File\SessionBasedTemporaryStorage</parameter>
|
||||
<!-- TODO: configure through Configuration! -->
|
||||
<parameter key="file.temporary_storage.secret">abcdef</parameter>
|
||||
<parameter key="file.temporary_storage.nesting_levels">3</parameter>
|
||||
<parameter key="file.temporary_storage.directory"></parameter>
|
||||
</parameters>
|
||||
|
||||
<services>
|
||||
@ -34,17 +41,34 @@
|
||||
<argument type="service" id="session" />
|
||||
<argument>%form.csrf_protection.secret%</argument>
|
||||
</service>
|
||||
|
||||
<!-- Theme -->
|
||||
<service id="form.theme" class="%form.theme.class%">
|
||||
<argument type="service" id="twig" />
|
||||
<argument>%form.theme.template%</argument>
|
||||
</service>
|
||||
|
||||
<!-- TemporaryStorage - where should we put this? -->
|
||||
<service id="file.temporary_storage" class="%file.temporary_storage.class%">
|
||||
<argument type="service" id="session" />
|
||||
<argument>%file.temporary_storage.secret%</argument>
|
||||
<argument>%file.temporary_storage.nesting_levels%</argument>
|
||||
<argument>%file.temporary_storage.directory%</argument>
|
||||
</service>
|
||||
|
||||
<!-- FormContext -->
|
||||
<service id="form.context" class="%form.context.class%">
|
||||
<argument type="collection">
|
||||
<argument key="validator" type="service" id="validator" />
|
||||
<argument key="validation_groups">%form.validation_groups%</argument>
|
||||
<argument key="field_factory" type="service" id="form.field_factory" />
|
||||
<argument key="csrf_protection">%form.csrf_protection.enabled%</argument>
|
||||
<argument key="csrf_field_name">%form.csrf_protection.field_name%</argument>
|
||||
<argument key="csrf_provider" type="service" id="form.csrf_provider" />
|
||||
</argument>
|
||||
<service id="form.factory" class="%form.factory.class%">
|
||||
<argument type="service" id="form.theme" />
|
||||
<argument type="service" id="form.csrf_provider" />
|
||||
<argument type="service" id="validator" />
|
||||
<argument type="service" id="form.field_factory" />
|
||||
<argument type="service" id="file.temporary_storage" />
|
||||
<argument type="service" id="doctrine.orm.default_entity_manager" />
|
||||
<!--
|
||||
<argument key="validation_groups">%form.validation_groups%</argument>
|
||||
<argument key="csrf_protection">%form.csrf_protection.enabled%</argument>
|
||||
<argument key="csrf_field_name">%form.csrf_protection.field_name%</argument>
|
||||
-->
|
||||
</service>
|
||||
|
||||
</services>
|
||||
|
@ -1,103 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Symfony\Component\Form;
|
||||
|
||||
/*
|
||||
* This file is part of the Symfony package.
|
||||
*
|
||||
* (c) Fabien Potencier <fabien.potencier@symfony-project.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
use Symfony\Component\Form\CsrfProvider\DefaultCsrfProvider;
|
||||
use Symfony\Component\Form\Exception\FormException;
|
||||
use Symfony\Component\Validator\ValidatorInterface;
|
||||
|
||||
/**
|
||||
* Default implementaton of FormContextInterface
|
||||
*
|
||||
* This class is immutable by design.
|
||||
*
|
||||
* @author Fabien Potencier <fabien.potencier@symfony-project.com>
|
||||
* @author Bernhard Schussek <bernhard.schussek@symfony-project.com>
|
||||
*/
|
||||
class FormContext implements FormContextInterface
|
||||
{
|
||||
/**
|
||||
* The options used in new forms
|
||||
* @var array
|
||||
*/
|
||||
protected $options = null;
|
||||
|
||||
/**
|
||||
* Builds a context with default values
|
||||
*
|
||||
* By default, CSRF protection is enabled. In this case you have to provide
|
||||
* a CSRF secret in the second parameter of this method. A recommended
|
||||
* value is a generated value with at least 32 characters and mixed
|
||||
* letters, digits and special characters.
|
||||
*
|
||||
* If you don't want to use CSRF protection, you can leave the CSRF secret
|
||||
* empty and set the third parameter to false.
|
||||
*
|
||||
* @param ValidatorInterface $validator The validator for validating
|
||||
* forms
|
||||
* @param string $csrfSecret The secret to be used for
|
||||
* generating CSRF tokens
|
||||
* @param boolean $csrfProtection Whether forms should be CSRF
|
||||
* protected
|
||||
* @throws FormException When CSRF protection is enabled,
|
||||
* but no CSRF secret is passed
|
||||
*/
|
||||
public static function buildDefault(ValidatorInterface $validator, $csrfSecret = null, $csrfProtection = true)
|
||||
{
|
||||
$options = array(
|
||||
'csrf_protection' => $csrfProtection,
|
||||
'validator' => $validator,
|
||||
);
|
||||
|
||||
if ($csrfProtection) {
|
||||
if (empty($csrfSecret)) {
|
||||
throw new FormException('Please provide a CSRF secret when CSRF protection is enabled');
|
||||
}
|
||||
|
||||
$options['csrf_provider'] = new DefaultCsrfProvider($csrfSecret);
|
||||
}
|
||||
|
||||
return new static($options);
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
*
|
||||
* Initializes the context with the settings stored in the given
|
||||
* options.
|
||||
*
|
||||
* @param array $options
|
||||
*/
|
||||
public function __construct(array $options = array())
|
||||
{
|
||||
if (isset($options['csrf_protection'])) {
|
||||
if (!$options['csrf_protection']) {
|
||||
// don't include a CSRF provider if CSRF protection is disabled
|
||||
unset($options['csrf_provider']);
|
||||
}
|
||||
|
||||
unset($options['csrf_protection']);
|
||||
}
|
||||
|
||||
$options['context'] = $this;
|
||||
|
||||
$this->options = $options;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public function getOptions()
|
||||
{
|
||||
return $this->options;
|
||||
}
|
||||
}
|
@ -1,31 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Symfony\Component\Form;
|
||||
|
||||
/*
|
||||
* This file is part of the Symfony package.
|
||||
*
|
||||
* (c) Fabien Potencier <fabien.potencier@symfony-project.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
use Symfony\Component\Form\FieldFactory\FieldFactoryInterface;
|
||||
use Symfony\Component\Form\CsrfProvider\CsrfProviderInterface;
|
||||
use Symfony\Component\Validator\ValidatorInterface;
|
||||
|
||||
/**
|
||||
* Stores options for creating new forms
|
||||
*
|
||||
* @author Bernhard Schussek <bernhard.schussek@symfony-project.com>
|
||||
*/
|
||||
interface FormContextInterface
|
||||
{
|
||||
/**
|
||||
* Returns the options used for creating a new form
|
||||
*
|
||||
* @return array The form options
|
||||
*/
|
||||
public function getOptions();
|
||||
}
|
@ -57,6 +57,7 @@ use Symfony\Component\Form\ValueTransformer\FileToStringTransformer;
|
||||
use Symfony\Component\Validator\ValidatorInterface;
|
||||
use Symfony\Component\Locale\Locale;
|
||||
use Symfony\Component\HttpFoundation\File\TemporaryStorage;
|
||||
use Doctrine\ORM\EntityManager;
|
||||
|
||||
class FormFactory
|
||||
{
|
||||
@ -70,17 +71,21 @@ class FormFactory
|
||||
|
||||
private $storage;
|
||||
|
||||
private $entityManager;
|
||||
|
||||
public function __construct(ThemeInterface $theme,
|
||||
CsrfProviderInterface $csrfProvider,
|
||||
ValidatorInterface $validator,
|
||||
FieldFactoryInterface $fieldFactory,
|
||||
TemporaryStorage $storage)
|
||||
TemporaryStorage $storage,
|
||||
EntityManager $entityManager)
|
||||
{
|
||||
$this->theme = $theme;
|
||||
$this->csrfProvider = $csrfProvider;
|
||||
$this->validator = $validator;
|
||||
$this->fieldFactory = $fieldFactory;
|
||||
$this->storage = $storage;
|
||||
$this->entityManager = $entityManager;
|
||||
}
|
||||
|
||||
protected function getTheme()
|
||||
@ -399,7 +404,7 @@ class FormFactory
|
||||
public function getEntityChoiceField($key, array $options = array())
|
||||
{
|
||||
$options = array_merge(array(
|
||||
'em' => null,
|
||||
'em' => $this->entityManager,
|
||||
'class' => null,
|
||||
'property' => null,
|
||||
'query_builder' => null,
|
||||
|
@ -20,7 +20,7 @@ use Symfony\Component\HttpFoundation\File\Exception\UnexpectedTypeException;
|
||||
*/
|
||||
class SessionBasedTemporaryStorage extends TemporaryStorage
|
||||
{
|
||||
public function __construct(Session $session, $directory, $secret, $nestingLevels = 3)
|
||||
public function __construct(Session $session, $secret, $nestingLevels = 3, $directory = null)
|
||||
{
|
||||
parent::__construct($directory, $secret, $nestingLevels);
|
||||
|
||||
|
@ -25,8 +25,12 @@ class TemporaryStorage
|
||||
|
||||
private $nestingLevels;
|
||||
|
||||
public function __construct($directory, $secret, $nestingLevels = 3)
|
||||
public function __construct($secret, $nestingLevels = 3, $directory = null)
|
||||
{
|
||||
if (empty($directory)) {
|
||||
$directory = sys_get_temp_dir();
|
||||
}
|
||||
|
||||
$this->directory = realpath($directory);
|
||||
$this->secret = $secret;
|
||||
$this->nestingLevels = $nestingLevels;
|
||||
|
@ -1,60 +0,0 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Symfony package.
|
||||
*
|
||||
* (c) Fabien Potencier <fabien.potencier@symfony-project.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Symfony\Tests\Component\Form;
|
||||
|
||||
require_once __DIR__ . '/Fixtures/Author.php';
|
||||
|
||||
use Symfony\Component\Form\FormContext;
|
||||
use Symfony\Component\Form\CsrfProvider\DefaultCsrfProvider;
|
||||
|
||||
class FormContextTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
protected $validator;
|
||||
|
||||
protected function setUp()
|
||||
{
|
||||
$this->validator = $this->getMock('Symfony\Component\Validator\ValidatorInterface');
|
||||
}
|
||||
|
||||
public function testBuildDefaultWithCsrfProtection()
|
||||
{
|
||||
$context = FormContext::buildDefault($this->validator, 'secret');
|
||||
|
||||
$expected = array(
|
||||
'validator' => $this->validator,
|
||||
'csrf_provider' => new DefaultCsrfProvider('secret'),
|
||||
'context' => $context,
|
||||
);
|
||||
|
||||
$this->assertEquals($expected, $context->getOptions());
|
||||
}
|
||||
|
||||
public function testBuildDefaultWithoutCsrfProtection()
|
||||
{
|
||||
$context = FormContext::buildDefault($this->validator, null, false);
|
||||
|
||||
$expected = array(
|
||||
'validator' => $this->validator,
|
||||
'context' => $context,
|
||||
);
|
||||
|
||||
$this->assertEquals($expected, $context->getOptions());
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException Symfony\Component\Form\Exception\FormException
|
||||
*/
|
||||
public function testBuildDefaultWithoutCsrfSecretThrowsException()
|
||||
{
|
||||
FormContext::buildDefault($this->validator, null, true);
|
||||
}
|
||||
}
|
@ -25,6 +25,8 @@ class TestCase extends \PHPUnit_Framework_TestCase
|
||||
|
||||
protected $storage;
|
||||
|
||||
private $em;
|
||||
|
||||
protected $factory;
|
||||
|
||||
protected function setUp()
|
||||
@ -36,7 +38,10 @@ class TestCase extends \PHPUnit_Framework_TestCase
|
||||
$this->storage = $this->getMockBuilder('Symfony\Component\HttpFoundation\File\TemporaryStorage')
|
||||
->disableOriginalConstructor()
|
||||
->getMock();
|
||||
$this->em = $this->getMockBuilder('Doctrine\ORM\EntityManager')
|
||||
->disableOriginalConstructor()
|
||||
->getMock();
|
||||
$this->factory = new FormFactory($this->theme, $this->csrfProvider,
|
||||
$this->validator, $this->fieldFactory, $this->storage);
|
||||
$this->validator, $this->fieldFactory, $this->storage, $this->em);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user