FormConfiguration.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. <?php
  2. namespace Symfony\Component\Form;
  3. /*
  4. * This file is part of the Symfony framework.
  5. *
  6. * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
  7. *
  8. * This source file is subject to the MIT license that is bundled
  9. * with this source code in the file LICENSE.
  10. */
  11. /**
  12. * FormConfiguration holds the default configuration for forms (CSRF, locale, ...).
  13. *
  14. * @author Fabien Potencier <fabien.potencier@symfony-project.com>
  15. */
  16. class FormConfiguration
  17. {
  18. protected static $defaultCsrfSecrets = array();
  19. protected static $defaultCsrfProtection = false;
  20. protected static $defaultCsrfFieldName = '_token';
  21. protected static $defaultLocale = null;
  22. /**
  23. * Sets the default locale for newly created forms.
  24. *
  25. * @param string $defaultLocale
  26. */
  27. static public function setDefaultLocale($defaultLocale)
  28. {
  29. self::$defaultLocale = $defaultLocale;
  30. }
  31. /**
  32. * Returns the default locale for newly created forms.
  33. *
  34. * @return string
  35. */
  36. static public function getDefaultLocale()
  37. {
  38. return isset(self::$defaultLocale) ? self::$defaultLocale : \Locale::getDefault();
  39. }
  40. /**
  41. * Enables CSRF protection for all new forms
  42. */
  43. static public function enableDefaultCsrfProtection()
  44. {
  45. self::$defaultCsrfProtection = true;
  46. }
  47. /**
  48. * Checks if Csrf protection for all forms is enabled.
  49. */
  50. static public function isDefaultCsrfProtectionEnabled()
  51. {
  52. return self::$defaultCsrfProtection;
  53. }
  54. /**
  55. * Disables Csrf protection for all forms.
  56. */
  57. static public function disableDefaultCsrfProtection()
  58. {
  59. self::$defaultCsrfProtection = false;
  60. }
  61. /**
  62. * Sets the CSRF field name used in all new CSRF protected forms
  63. *
  64. * @param string $name The CSRF field name
  65. */
  66. static public function setDefaultCsrfFieldName($name)
  67. {
  68. self::$defaultCsrfFieldName = $name;
  69. }
  70. /**
  71. * Returns the default CSRF field name
  72. *
  73. * @return string The CSRF field name
  74. */
  75. static public function getDefaultCsrfFieldName()
  76. {
  77. return self::$defaultCsrfFieldName;
  78. }
  79. /**
  80. * Sets the default CSRF secrets to be used in all new CSRF protected forms
  81. *
  82. * @param array $secrets
  83. */
  84. static public function setDefaultCsrfSecrets(array $secrets)
  85. {
  86. self::$defaultCsrfSecrets = $secrets;
  87. }
  88. /**
  89. * Adds CSRF secrets to be used in all new CSRF protected forms
  90. *
  91. * @param string $secret
  92. */
  93. static public function addDefaultCsrfSecret($secret)
  94. {
  95. self::$defaultCsrfSecrets[] = $secret;
  96. }
  97. /**
  98. * Returns the default CSRF secrets
  99. *
  100. * @return string
  101. */
  102. static public function getDefaultCsrfSecrets()
  103. {
  104. return self::$defaultCsrfSecrets;
  105. }
  106. }