Permalink
Browse files

[Form] Reorganized code into "form extensions"

The extension classes are now the only constructor argument of the FormFactory class. They replace the existing "type loader" classes.

    new FormFactory(array(
        new CoreExtension($validator, $storage),
        new CsrfExtension($csrfProvider),
        new DoctrineOrmExtension($em),
    ));

Together with a few upcoming commits this mechanism will make

 * extension of the form framework in bundles and
 * usage of the forms outside of Symfony2

much easier.
  • Loading branch information...
1 parent 3e321fa commit fc5c541bc5d60a0808dc15d8bbc69f5c02aa494a @webmozart webmozart committed Apr 22, 2011
Showing with 578 additions and 357 deletions.
  1. +107 −0 AbstractExtension.php
  2. +1 −6 {Type → }/AbstractType.php
  3. +1 −1 {DataTransformer → }/CallbackTransformer.php
  4. +1 −3 {Validator → }/CallbackValidator.php
  5. +1 −3 {DataMapper → }/DataMapperInterface.php
  6. +1 −1 {DataTransformer → }/DataTransformerInterface.php
  7. +1 −1 {DataTransformer → Exception}/TransformationFailedException.php
  8. +2 −2 { → Extension/Core}/ChoiceList/ArrayChoiceList.php
  9. +1 −1 { → Extension/Core}/ChoiceList/ChoiceListInterface.php
  10. +1 −1 { → Extension/Core}/ChoiceList/MonthChoiceList.php
  11. +1 −1 { → Extension/Core}/ChoiceList/PaddedChoiceList.php
  12. +1 −1 { → Extension/Core}/ChoiceList/TimezoneChoiceList.php
  13. +24 −19 Type/Loader/DefaultTypeLoader.php → Extension/Core/CoreExtension.php
  14. +10 −15 Type/Guesser/ValidatorTypeGuesser.php → Extension/Core/CoreTypeGuesser.php
  15. +2 −1 { → Extension/Core}/DataMapper/PropertyPathMapper.php
  16. +4 −2 { → Extension/Core}/DataTransformer/ArrayToBooleanChoicesTransformer.php
  17. +3 −1 { → Extension/Core}/DataTransformer/ArrayToChoicesTransformer.php
  18. +3 −1 { → Extension/Core}/DataTransformer/ArrayToPartsTransformer.php
  19. +3 −1 { → Extension/Core}/DataTransformer/BaseDateTimeTransformer.php
  20. +3 −1 { → Extension/Core}/DataTransformer/BooleanToStringTransformer.php
  21. +3 −1 { → Extension/Core}/DataTransformer/DataTransformerChain.php
  22. +2 −1 { → Extension/Core}/DataTransformer/DateTimeToArrayTransformer.php
  23. +2 −1 { → Extension/Core}/DataTransformer/DateTimeToLocalizedStringTransformer.php
  24. +2 −1 { → Extension/Core}/DataTransformer/DateTimeToStringTransformer.php
  25. +2 −1 { → Extension/Core}/DataTransformer/DateTimeToTimestampTransformer.php
  26. +3 −2 { → Extension/Core}/DataTransformer/FileToArrayTransformer.php
  27. +3 −2 { → Extension/Core}/DataTransformer/FileToStringTransformer.php
  28. +1 −3 { → Extension/Core}/DataTransformer/IntegerToLocalizedStringTransformer.php
  29. +1 −1 { → Extension/Core}/DataTransformer/MoneyToLocalizedStringTransformer.php
  30. +3 −1 { → Extension/Core}/DataTransformer/NumberToLocalizedStringTransformer.php
  31. +3 −1 { → Extension/Core}/DataTransformer/PercentToLocalizedStringTransformer.php
  32. +4 −2 { → Extension/Core}/DataTransformer/ScalarToBooleanChoicesTransformer.php
  33. +2 −1 { → Extension/Core}/DataTransformer/ScalarToChoiceTransformer.php
  34. +3 −1 { → Extension/Core}/DataTransformer/ValueToDuplicatesTransformer.php
  35. +1 −1 { → Extension/Core}/EventListener/FixFileUploadListener.php
  36. +1 −1 { → Extension/Core}/EventListener/FixRadioInputListener.php
  37. +1 −1 { → Extension/Core}/EventListener/FixUrlProtocolListener.php
  38. +1 −1 { → Extension/Core}/EventListener/ResizeFormListener.php
  39. +1 −1 { → Extension/Core}/EventListener/TrimListener.php
  40. +2 −1 { → Extension/Core}/Type/BirthdayType.php
  41. +3 −2 { → Extension/Core}/Type/CheckboxType.php
  42. +10 −9 { → Extension/Core}/Type/ChoiceType.php
  43. +3 −2 { → Extension/Core}/Type/CollectionType.php
  44. +2 −1 { → Extension/Core}/Type/CountryType.php
  45. +8 −7 { → Extension/Core}/Type/DateTimeType.php
  46. +9 −8 { → Extension/Core}/Type/DateType.php
  47. +2 −1 { → Extension/Core}/Type/EmailType.php
  48. +5 −4 { → Extension/Core}/Type/FieldType.php
  49. +7 −6 { → Extension/Core}/Type/FileType.php
  50. +4 −3 { → Extension/Core}/Type/FormType.php
  51. +2 −1 { → Extension/Core}/Type/HiddenType.php
  52. +3 −2 { → Extension/Core}/Type/IntegerType.php
  53. +2 −1 { → Extension/Core}/Type/LanguageType.php
  54. +2 −1 { → Extension/Core}/Type/LocaleType.php
  55. +3 −2 { → Extension/Core}/Type/MoneyType.php
  56. +3 −2 { → Extension/Core}/Type/NumberType.php
  57. +2 −1 { → Extension/Core}/Type/PasswordType.php
  58. +3 −2 { → Extension/Core}/Type/PercentType.php
  59. +3 −2 { → Extension/Core}/Type/RadioType.php
  60. +3 −2 { → Extension/Core}/Type/RepeatedType.php
  61. +2 −1 { → Extension/Core}/Type/TextType.php
  62. +2 −1 { → Extension/Core}/Type/TextareaType.php
  63. +7 −6 { → Extension/Core}/Type/TimeType.php
  64. +3 −2 { → Extension/Core}/Type/TimezoneType.php
  65. +3 −2 { → Extension/Core}/Type/UrlType.php
  66. +2 −1 { → Extension/Core}/Validator/DefaultValidator.php
  67. +2 −1 { → Extension/Core}/Validator/DelegatingValidator.php
  68. +37 −0 Extension/Csrf/CsrfExtension.php
  69. +1 −1 { → Extension/Csrf}/CsrfProvider/CsrfProviderInterface.php
  70. +1 −1 { → Extension/Csrf}/CsrfProvider/DefaultCsrfProvider.php
  71. +1 −1 { → Extension/Csrf}/CsrfProvider/SessionCsrfProvider.php
  72. +14 −13 { → Extension/Csrf}/Type/CsrfType.php
  73. +69 −0 Extension/DependencyInjection/DependencyInjectionExtension.php
  74. +6 −11 Form.php
  75. +1 −5 FormBuilder.php
  76. +6 −4 Type/Loader/TypeLoaderInterface.php → FormExtensionInterface.php
  77. +43 −52 FormFactory.php
  78. +76 −0 FormTypeGuesserChain.php
  79. +4 −9 Type/Guesser/TypeGuesserInterface.php → FormTypeGuesserInterface.php
  80. +1 −6 {Type → }/FormTypeInterface.php
  81. +1 −3 {Validator → }/FormValidatorInterface.php
  82. +0 −1 FormView.php
  83. +1 −1 {Type/Guesser → Guess}/Guess.php
  84. +1 −1 {Type/Guesser → Guess}/TypeGuess.php
  85. +1 −1 {Type/Guesser → Guess}/ValueGuess.php
  86. +1 −1 Resources/config/validation.xml
  87. +1 −1 {DataTransformer → }/ReversedTransformer.php
  88. +0 −41 Type/Loader/ArrayTypeLoader.php
  89. +0 −44 Type/Loader/TypeLoaderChain.php
View
@@ -0,0 +1,107 @@
+<?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;
+
+use Symfony\Component\Form\Exception\FormException;
+use Symfony\Component\Form\Exception\UnexpectedTypeException;
+
+/**
+ * @author Bernhard Schussek <bernhard.schussek@symfony.com>
+ */
+abstract class AbstractExtension implements FormExtensionInterface
+{
+ /**
+ * @var array
+ */
+ private $types;
+
+ /**
+ * @var Boolean
+ */
+ private $typesLoaded = false;
+
+ /**
+ * @var FormTypeGuesserInterface
+ */
+ private $typeGuesser;
+
+ /**
+ * @var Boolean
+ */
+ private $typeGuesserLoaded = false;
+
+ abstract protected function loadTypes();
+
+ abstract protected function loadTypeGuesser();
+
+ private function initTypes()
+ {
+ $this->typesLoaded = true;
+
+ $types = $this->loadTypes();
+ $typesByName = array();
+
+ foreach ($types as $type) {
+ if (!$type instanceof FormTypeInterface) {
+ throw new UnexpectedTypeException($type, 'Symfony\Component\Form\FormTypeInterface');
+ }
+
+ $typesByName[$type->getName()] = $type;
+ }
+
+ $this->types = $typesByName;
+ }
+
+ private function initTypeGuesser()
+ {
+ $this->typeGuesserLoaded = true;
+
+ $guesser = $this->loadTypeGuesser();
+
+ if (!$guesser instanceof FormTypeGuesserInterface) {
+ throw new UnexpectedTypeException($type, 'Symfony\Component\Form\FormTypeGuesserInterface');
+ }
+
+ $this->guesser = $guesser;
+ }
+
+ public function getType($name)
+ {
+ if (!$this->typesLoaded) {
+ $this->initTypes();
+ }
+
+ if (!isset($this->types[$name])) {
+ throw new FormException(sprintf('The type "%s" can not be typesLoaded by this extension', $name));
+ }
+
+ return $this->types[$name];
+ }
+
+ public function hasType($name)
+ {
+ if (!$this->typesLoaded) {
+ $this->initTypes();
+ }
+
+ return isset($this->types[$name]);
+ }
+
+ public function getTypeGuesser()
+ {
+ if (!$this->typeGuesserLoaded) {
+ $this->initTypeGuesser();
+ }
+
+ return $this->typeGuesser;
+ }
+}
@@ -9,12 +9,7 @@
* file that was distributed with this source code.
*/
-namespace Symfony\Component\Form\Type;
-
-use Symfony\Component\Form\FormBuilder;
-use Symfony\Component\Form\FormInterface;
-use Symfony\Component\Form\FormFactoryInterface;
-use Symfony\Component\Form\FormView;
+namespace Symfony\Component\Form;
abstract class AbstractType implements FormTypeInterface
{
@@ -9,7 +9,7 @@
* file that was distributed with this source code.
*/
-namespace Symfony\Component\Form\DataTransformer;
+namespace Symfony\Component\Form;
class CallbackTransformer implements DataTransformerInterface
{
@@ -9,9 +9,7 @@
* file that was distributed with this source code.
*/
-namespace Symfony\Component\Form\Validator;
-
-use Symfony\Component\Form\FormInterface;
+namespace Symfony\Component\Form;
class CallbackValidator implements FormValidatorInterface
{
@@ -9,9 +9,7 @@
* file that was distributed with this source code.
*/
-namespace Symfony\Component\Form\DataMapper;
-
-use Symfony\Component\Form\FormInterface;
+namespace Symfony\Component\Form;
interface DataMapperInterface
{
@@ -9,7 +9,7 @@
* file that was distributed with this source code.
*/
-namespace Symfony\Component\Form\DataTransformer;
+namespace Symfony\Component\Form;
/**
* Transforms a value between different representations.
@@ -9,7 +9,7 @@
* file that was distributed with this source code.
*/
-namespace Symfony\Component\Form\DataTransformer;
+namespace Symfony\Component\Form\Exception;
/**
* Indicates a value transformation error.
@@ -9,7 +9,7 @@
* file that was distributed with this source code.
*/
-namespace Symfony\Component\Form\ChoiceList;
+namespace Symfony\Component\Form\Extension\Core\ChoiceList;
use Symfony\Component\Form\Exception\UnexpectedTypeException;
@@ -38,7 +38,7 @@ public function getChoices()
}
/**
- * @see Symfony\Component\Form\ChoiceList\ChoiceListInterface::getChoices
+ * @see Symfony\Component\Form\Extension\Core\ChoiceList\ChoiceListInterface::getChoices
*/
protected function load()
{
@@ -9,7 +9,7 @@
* file that was distributed with this source code.
*/
-namespace Symfony\Component\Form\ChoiceList;
+namespace Symfony\Component\Form\Extension\Core\ChoiceList;
interface ChoiceListInterface
{
@@ -9,7 +9,7 @@
* file that was distributed with this source code.
*/
-namespace Symfony\Component\Form\ChoiceList;
+namespace Symfony\Component\Form\Extension\Core\ChoiceList;
class MonthChoiceList extends PaddedChoiceList
{
@@ -9,7 +9,7 @@
* file that was distributed with this source code.
*/
-namespace Symfony\Component\Form\ChoiceList;
+namespace Symfony\Component\Form\Extension\Core\ChoiceList;
class PaddedChoiceList extends ArrayChoiceList
{
@@ -9,7 +9,7 @@
* file that was distributed with this source code.
*/
-namespace Symfony\Component\Form\ChoiceList;
+namespace Symfony\Component\Form\Extension\Core\ChoiceList;
/**
* Represents a choice list where each timezone is broken down by continent.
@@ -9,21 +9,31 @@
* file that was distributed with this source code.
*/
-namespace Symfony\Component\Form\Type\Loader;
+namespace Symfony\Component\Form\Extension\Core;
-use Symfony\Component\Form\Type;
-use Symfony\Component\Form\CsrfProvider\CsrfProviderInterface;
+use Symfony\Component\Form\Extension\Core\Type;
+use Symfony\Component\Form\AbstractExtension;
use Symfony\Component\Validator\ValidatorInterface;
use Symfony\Component\HttpFoundation\File\TemporaryStorage;
-class DefaultTypeLoader extends ArrayTypeLoader
+class CoreExtension extends AbstractExtension
{
- public function __construct(
- ValidatorInterface $validator = null,
- CsrfProviderInterface $csrfProvider = null, TemporaryStorage $storage = null)
+ private $validator;
+
+ private $storage;
+
+ private $typeGuesser;
+
+ public function __construct(ValidatorInterface $validator, TemporaryStorage $storage)
+ {
+ $this->validator = $validator;
+ $this->storage = $storage;
+ }
+
+ protected function loadTypes()
{
- $types = array(
- new Type\FieldType($validator),
+ return array(
+ new Type\FieldType($this->validator),
new Type\FormType(),
new Type\BirthdayType(),
new Type\CheckboxType(),
@@ -48,17 +58,12 @@ public function __construct(
new Type\TimeType(),
new Type\TimezoneType(),
new Type\UrlType(),
+ new Type\FileType($this->storage),
);
+ }
- if ($csrfProvider) {
- // TODO Move to a Symfony\Bridge\FormSecurity
- $types[] = new Type\CsrfType($csrfProvider);
- }
-
- if ($storage) {
- $types[] = new Type\FileType($storage);
- }
-
- parent::__construct($types);
+ public function loadTypeGuesser()
+ {
+ return new CoreTypeGuesser($this->validator->getMetadataFactory());
}
}
@@ -3,29 +3,24 @@
/*
* This file is part of the Symfony package.
*
- * (c) Fabien Potencier <fabien@symfony.com>
+ * (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\Component\Form\Type\Guesser;
+namespace Symfony\Component\Form\Extension\Core;
-use Symfony\Component\Validator\Constraint;
+use Symfony\Component\Form\FormTypeGuesserInterface;
+use Symfony\Component\Form\Guess\Guess;
+use Symfony\Component\Form\Guess\TypeGuess;
+use Symfony\Component\Form\Guess\ValueGuess;
use Symfony\Component\Validator\Mapping\ClassMetadataFactoryInterface;
-/**
- * Guesses form fields from the metadata of the a Validator class
- *
- * @author Bernhard Schussek <bernhard.schussek@symfony.com>
- */
-class ValidatorTypeGuesser implements TypeGuesserInterface
+class CoreTypeGuesser implements FormTypeGuesserInterface
{
- /**
- * Constructor
- *
- * @param ClassMetadataFactoryInterface $metadataFactory
- */
+ private $metadataFactory;
+
public function __construct(ClassMetadataFactoryInterface $metadataFactory)
{
$this->metadataFactory = $metadataFactory;
@@ -301,4 +296,4 @@ public function guessMaxLengthForConstraint(Constraint $constraint)
);
}
}
-}
+}
@@ -9,9 +9,10 @@
* file that was distributed with this source code.
*/
-namespace Symfony\Component\Form\DataMapper;
+namespace Symfony\Component\Form\Extension\Core\DataMapper;
use Symfony\Component\Form\FormInterface;
+use Symfony\Component\Form\DataMapperInterface;
use Symfony\Component\Form\Util\VirtualFormAwareIterator;
use Symfony\Component\Form\Exception\FormException;
@@ -9,9 +9,11 @@
* file that was distributed with this source code.
*/
-namespace Symfony\Component\Form\DataTransformer;
+namespace Symfony\Component\Form\Extension\Core\DataTransformer;
-use Symfony\Component\Form\ChoiceList\ChoiceListInterface;
+use Symfony\Component\Form\Extension\Core\ChoiceList\ChoiceListInterface;
+use Symfony\Component\Form\DataTransformerInterface;
+use Symfony\Component\Form\Exception\TransformationFailedException;
use Symfony\Component\Form\Exception\UnexpectedTypeException;
class ArrayToBooleanChoicesTransformer implements DataTransformerInterface
@@ -9,9 +9,11 @@
* file that was distributed with this source code.
*/
-namespace Symfony\Component\Form\DataTransformer;
+namespace Symfony\Component\Form\Extension\Core\DataTransformer;
use Symfony\Component\Form\Util\FormUtil;
+use Symfony\Component\Form\DataTransformerInterface;
+use Symfony\Component\Form\Exception\TransformationFailedException;
use Symfony\Component\Form\Exception\UnexpectedTypeException;
class ArrayToChoicesTransformer implements DataTransformerInterface
@@ -9,8 +9,10 @@
* file that was distributed with this source code.
*/
-namespace Symfony\Component\Form\DataTransformer;
+namespace Symfony\Component\Form\Extension\Core\DataTransformer;
+use Symfony\Component\Form\DataTransformerInterface;
+use Symfony\Component\Form\Exception\TransformationFailedException;
use Symfony\Component\Form\Exception\UnexpectedTypeException;
/**
@@ -9,7 +9,9 @@
* file that was distributed with this source code.
*/
-namespace Symfony\Component\Form\DataTransformer;
+namespace Symfony\Component\Form\Extension\Core\DataTransformer;
+
+use Symfony\Component\Form\DataTransformerInterface;
abstract class BaseDateTimeTransformer implements DataTransformerInterface
{
Oops, something went wrong.

0 comments on commit fc5c541

Please sign in to comment.