UserList.php

  1. 8.0.x lib/Drush/User/UserList.php
  2. 7.x lib/Drush/User/UserList.php
  3. master lib/Drush/User/UserList.php

Namespace

Drush\User

Classes

Namesort descending Description
UserList

File

lib/Drush/User/UserList.php
View source
  1. <?php
  2. namespace Drush\User;
  3. class UserList {
  4. /** @var \Drush\User\UserSingleBase[] */
  5. public $accounts;
  6. /**
  7. * Finds a list of user objects based on Drush arguments,
  8. * or options.
  9. */
  10. public function __construct($inputs) {
  11. if ($this->accounts = $this->getFromOptions() + $this->getFromParameters($inputs)) {
  12. return $this;
  13. }
  14. else {
  15. throw new UserListException('Unable to find a matching user.');
  16. }
  17. }
  18. /**
  19. * Iterate over each account and call the specified method.
  20. *
  21. * @param $method
  22. * A method on a UserSingleBase object.
  23. * @param array $params
  24. * An array of params to pass to the method.
  25. * @return array
  26. * An associate array of values keyed by account ID.
  27. */
  28. public function each($method, array $params = array()) {
  29. foreach ($this->accounts as $account) {
  30. $return[$account->id()] = call_user_func_array(array($account, $method), $params);
  31. }
  32. return $return;
  33. }
  34. /*
  35. * Check common options for specifying users. If valid, return the accounts.
  36. *
  37. * @return \Drush\User\UserSingleBase[]
  38. */
  39. function getFromOptions() {
  40. $accounts = array();
  41. $userversion = drush_user_get_class();
  42. if ($mails = _convert_csv_to_array(drush_get_option('mail'))) {
  43. foreach ($mails as $mail) {
  44. if ($account = $userversion->load_by_mail($mail)) {
  45. $single = drush_usersingle_get_class($account);
  46. $accounts[$single->id()] = $single;
  47. }
  48. else {
  49. throw new UserListException('Unable to find a matching user for ' . $mail . '.');
  50. }
  51. }
  52. }
  53. if ($names = _convert_csv_to_array(drush_get_option('name'))) {
  54. foreach ($names as $name) {
  55. if ($account = $userversion->load_by_name($name)) {
  56. $single = drush_usersingle_get_class($account);
  57. $accounts[$single->id()] = $single;
  58. }
  59. else {
  60. throw new UserListException('Unable to find a matching user for ' . $name . '.');
  61. }
  62. }
  63. }
  64. if ($userids = _convert_csv_to_array(drush_get_option('uid'))) {
  65. foreach ($userids as $userid) {
  66. if (is_numeric($userid) && $account = $userversion->load_by_uid($userid)) {
  67. $single = drush_usersingle_get_class($account);
  68. $accounts[$single->id()] = $single;
  69. }
  70. else {
  71. throw new UserListException('Unable to find a matching user for ' . $userid . '.');
  72. }
  73. }
  74. }
  75. return $accounts;
  76. }
  77. /**
  78. * Given a comma-separated list of inputs, return accounts
  79. * for users that match by uid,name or email address.
  80. *
  81. * @param string $inputs
  82. * A comma delimited string (or array) of arguments, specifying user account(s).
  83. *
  84. * @throws UserListException
  85. * If any input is unmatched, an exception is thrown.
  86. *
  87. * @return \Drush\User\UserSingleBase[]
  88. * An associative array of UserSingleBase objects, keyed by user id.
  89. */
  90. public static function getFromParameters($inputs) {
  91. $accounts = array();
  92. $userversion = drush_user_get_class();
  93. if ($inputs && $userversion) {
  94. $inputs = _convert_csv_to_array($inputs);
  95. foreach($inputs as $input) {
  96. if (is_numeric($input) && $account = $userversion->load_by_uid($input)) {
  97. }
  98. elseif ($account = $userversion->load_by_name($input)) {
  99. }
  100. elseif ($account = $userversion->load_by_mail($input)) {
  101. }
  102. else {
  103. // Unable to load an account for the input.
  104. throw new UserListException('Unable to find a matching user for ' . $input . '.');
  105. }
  106. // Populate $accounts with a UserSingle object. Will go into $this->accounts.
  107. $single = drush_usersingle_get_class($account);
  108. $accounts[$single->id()] = $single;
  109. }
  110. }
  111. return $accounts;
  112. }
  113. /*
  114. * A comma delimited list of names built from $this->accounts.
  115. */
  116. public function names() {
  117. $names = array();
  118. foreach ($this->accounts as $account) {
  119. $names[] = $account->getUsername();
  120. }
  121. return implode(', ', $names);
  122. }
  123. }