redis.admin.inc 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. <?php
  2. /**
  3. * @file
  4. * Redis module administration pages.
  5. */
  6. /**
  7. * Main settings and review administration screen.
  8. */
  9. function redis_settings_form($form, &$form_state) {
  10. $form['connection'] = array(
  11. '#type' => 'fieldset',
  12. '#title' => t("Connection information"),
  13. '#collapsible' => TRUE,
  14. '#collapsed' => TRUE,
  15. );
  16. $form['connection']['scheme'] = array(
  17. '#type' => 'textfield',
  18. '#title' => t("Scheme"),
  19. '#default_value' => 'tcp',
  20. '#disabled' => TRUE,
  21. '#description' => t("Connection scheme.") . " " . t("Only <em>tcp</em> is currently supported. This is ignored when using a UNIX socket."),
  22. );
  23. $form['connection']['redis_client_host'] = array(
  24. '#type' => 'textfield',
  25. '#title' => t("Host"),
  26. '#default_value' => variable_get('redis_client_host', NULL),
  27. '#description' => t("Redis server host. Default is <em>@default</em>.", array('@default' => Redis_Client_Manager::REDIS_DEFAULT_HOST)),
  28. );
  29. $form['connection']['redis_client_port'] = array(
  30. '#type' => 'textfield',
  31. '#title' => t("Port"),
  32. '#default_value' => variable_get('redis_client_port', NULL),
  33. '#description' => t("Redis server port. Default is <em>@default</em>.", array('@default' => Redis_Client_Manager::REDIS_DEFAULT_PORT)),
  34. );
  35. $form['connection']['redis_client_socket'] = array(
  36. '#type' => 'textfield',
  37. '#title' => t("UNIX socket"),
  38. '#default_value' => variable_get('redis_client_socket', NULL),
  39. '#description' => t("Redis UNIX socket for connection. If set remote server host and port will be ignored."),
  40. );
  41. $form['connection']['redis_client_base'] = array(
  42. '#type' => 'textfield',
  43. '#title' => t("Database"),
  44. '#default_value' => variable_get('redis_client_base', NULL),
  45. '#description' => t("Redis server database. Default is none, Redis server will autoselect the database 0."),
  46. );
  47. $form['connection']['redis_client_interface'] = array(
  48. '#type' => 'radios',
  49. '#title' => t("Client"),
  50. '#options' => array(
  51. NULL => t("None or automatic"),
  52. 'PhpRedis' => t("PhpRedis PHP extension"),
  53. 'Predis' => t("Predis PHP library"),
  54. ),
  55. '#default_value' => variable_get('redis_client_interface', NULL),
  56. '#description' => t("Redis low level backend."),
  57. );
  58. $form = system_settings_form($form);
  59. // Enforce empty values drop from the $form_state in order to avoid empty
  60. // values saving. Empty values would cause the isset() checks in client
  61. // options to see false positives and fail upon connection.
  62. array_unshift($form['#submit'], 'redis_settings_form_submit_clean_values');
  63. return $form;
  64. }
  65. /**
  66. * Deep clean of $form_state values.
  67. */
  68. function redis_settings_form_submit_clean_values($form, &$form_state) {
  69. $string_values = array('redis_client_host', 'redis_client_interface');
  70. foreach ($string_values as $name) {
  71. // Empty check is sufficient to verify that the field is indeed empty.
  72. if (empty($form_state['values'][$name])) {
  73. // Using unset() will keep the key in the array, with an associated NULL
  74. // value. While this wouldn't really matter, it's safer to remove it so
  75. // that system_settings_form_submit() won't find it and attempt to save
  76. // it.
  77. $form_state['values'] = array_diff_key($form_state['values'], array($name => NULL));
  78. variable_del($name);
  79. }
  80. }
  81. $numeric_values = array('redis_client_base', 'redis_client_port');
  82. foreach ($numeric_values as $name) {
  83. // Numeric values can be both of NULL or 0 (NULL meaning the value is not
  84. // not set and the client will use the default, while 0 has a business
  85. // meaning and should be kept as is).
  86. if ('0' !== $form_state['values'][$name] && empty($form_state['values'][$name])) {
  87. $form_state['values'] = array_diff_key($form_state['values'], array($name => NULL));
  88. variable_del($name);
  89. } else {
  90. $form_state['values'][$name] = (int)$form_state['values'][$name];
  91. }
  92. }
  93. }