UserData.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. <?php
  2. namespace Drupal\user;
  3. use Drupal\Core\Database\Connection;
  4. /**
  5. * Defines the user data service.
  6. */
  7. class UserData implements UserDataInterface {
  8. /**
  9. * The database connection to use.
  10. *
  11. * @var \Drupal\Core\Database\Connection
  12. */
  13. protected $connection;
  14. /**
  15. * Constructs a new user data service.
  16. *
  17. * @param \Drupal\Core\Database\Connection $connection
  18. * The database connection to use.
  19. */
  20. public function __construct(Connection $connection) {
  21. $this->connection = $connection;
  22. }
  23. /**
  24. * {@inheritdoc}
  25. */
  26. public function get($module, $uid = NULL, $name = NULL) {
  27. $query = $this->connection->select('users_data', 'ud')
  28. ->fields('ud')
  29. ->condition('module', $module);
  30. if (isset($uid)) {
  31. $query->condition('uid', $uid);
  32. }
  33. if (isset($name)) {
  34. $query->condition('name', $name);
  35. }
  36. $result = $query->execute();
  37. // If $module, $uid, and $name were passed, return the value.
  38. if (isset($name) && isset($uid)) {
  39. $result = $result->fetchAllAssoc('uid');
  40. if (isset($result[$uid])) {
  41. return $result[$uid]->serialized ? unserialize($result[$uid]->value) : $result[$uid]->value;
  42. }
  43. return NULL;
  44. }
  45. $return = [];
  46. // If $module and $uid were passed, return data keyed by name.
  47. if (isset($uid)) {
  48. foreach ($result as $record) {
  49. $return[$record->name] = ($record->serialized ? unserialize($record->value) : $record->value);
  50. }
  51. return $return;
  52. }
  53. // If $module and $name were passed, return data keyed by uid.
  54. if (isset($name)) {
  55. foreach ($result as $record) {
  56. $return[$record->uid] = ($record->serialized ? unserialize($record->value) : $record->value);
  57. }
  58. return $return;
  59. }
  60. // If only $module was passed, return data keyed by uid and name.
  61. foreach ($result as $record) {
  62. $return[$record->uid][$record->name] = ($record->serialized ? unserialize($record->value) : $record->value);
  63. }
  64. return $return;
  65. }
  66. /**
  67. * {@inheritdoc}
  68. */
  69. public function set($module, $uid, $name, $value) {
  70. $serialized = (int) !is_scalar($value);
  71. if ($serialized) {
  72. $value = serialize($value);
  73. }
  74. $this->connection->merge('users_data')
  75. ->keys([
  76. 'uid' => $uid,
  77. 'module' => $module,
  78. 'name' => $name,
  79. ])
  80. ->fields([
  81. 'value' => $value,
  82. 'serialized' => $serialized,
  83. ])
  84. ->execute();
  85. }
  86. /**
  87. * {@inheritdoc}
  88. */
  89. public function delete($module = NULL, $uid = NULL, $name = NULL) {
  90. $query = $this->connection->delete('users_data');
  91. // Cast scalars to array so we can consistently use an IN condition.
  92. if (isset($module)) {
  93. $query->condition('module', (array) $module, 'IN');
  94. }
  95. if (isset($uid)) {
  96. $query->condition('uid', (array) $uid, 'IN');
  97. }
  98. if (isset($name)) {
  99. $query->condition('name', (array) $name, 'IN');
  100. }
  101. $query->execute();
  102. }
  103. }