FormContext.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. <?php
  2. namespace Symfony\Component\Form;
  3. /*
  4. * This file is part of the Symfony package.
  5. *
  6. * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
  7. *
  8. * For the full copyright and license information, please view the LICENSE
  9. * file that was distributed with this source code.
  10. */
  11. use Symfony\Component\Form\CsrfProvider\DefaultCsrfProvider;
  12. use Symfony\Component\Form\Exception\FormException;
  13. use Symfony\Component\Validator\ValidatorInterface;
  14. /**
  15. * Default implementaton of FormContextInterface
  16. *
  17. * This class is immutable by design.
  18. *
  19. * @author Fabien Potencier <fabien.potencier@symfony-project.com>
  20. * @author Bernhard Schussek <bernhard.schussek@symfony-project.com>
  21. */
  22. class FormContext implements FormContextInterface
  23. {
  24. /**
  25. * The options used in new forms
  26. * @var array
  27. */
  28. protected $options = null;
  29. /**
  30. * Builds a context with default values
  31. *
  32. * By default, CSRF protection is enabled. In this case you have to provide
  33. * a CSRF secret in the second parameter of this method. A recommended
  34. * value is a generated value with at least 32 characters and mixed
  35. * letters, digits and special characters.
  36. *
  37. * If you don't want to use CSRF protection, you can leave the CSRF secret
  38. * empty and set the third parameter to false.
  39. *
  40. * @param ValidatorInterface $validator The validator for validating
  41. * forms
  42. * @param string $csrfSecret The secret to be used for
  43. * generating CSRF tokens
  44. * @param boolean $csrfProtection Whether forms should be CSRF
  45. * protected
  46. * @throws FormException When CSRF protection is enabled,
  47. * but no CSRF secret is passed
  48. */
  49. public static function buildDefault(ValidatorInterface $validator, $csrfSecret = null, $csrfProtection = true)
  50. {
  51. $options = array(
  52. 'csrf_protection' => $csrfProtection,
  53. 'validator' => $validator,
  54. );
  55. if ($csrfProtection) {
  56. if (empty($csrfSecret)) {
  57. throw new FormException('Please provide a CSRF secret when CSRF protection is enabled');
  58. }
  59. $options['csrf_provider'] = new DefaultCsrfProvider($csrfSecret);
  60. }
  61. return new static($options);
  62. }
  63. /**
  64. * Constructor
  65. *
  66. * Initializes the context with the settings stored in the given
  67. * options.
  68. *
  69. * @param array $options
  70. */
  71. public function __construct(array $options = array())
  72. {
  73. if (isset($options['csrf_protection'])) {
  74. if (!$options['csrf_protection']) {
  75. // don't include a CSRF provider if CSRF protection is disabled
  76. unset($options['csrf_provider']);
  77. }
  78. unset($options['csrf_protection']);
  79. }
  80. $options['context'] = $this;
  81. $this->options = $options;
  82. }
  83. /**
  84. * {@inheritDoc}
  85. */
  86. public function getOptions()
  87. {
  88. return $this->options;
  89. }
  90. }