7 /** @var \Drush\User\UserSingleBase[] */
11 * Finds a list of user objects based on Drush arguments,
14 public function __construct($inputs) {
15 if ($this->accounts = $this->getFromOptions() + $this->getFromParameters($inputs)) {
19 throw new UserListException('Unable to find a matching user.');
24 * Iterate over each account and call the specified method.
27 * A method on a UserSingleBase object.
28 * @param array $params
29 * An array of params to pass to the method.
31 * An associate array of values keyed by account ID.
33 public function each($method, array $params = array()) {
34 foreach ($this->accounts as $account) {
35 $return[$account->id()] = call_user_func_array(array($account, $method), $params);
41 * Check common options for specifying users. If valid, return the accounts.
43 * @return \Drush\User\UserSingleBase[]
45 function getFromOptions() {
47 $userversion = drush_user_get_class();
48 if ($mails = _convert_csv_to_array(drush_get_option('mail'))) {
49 foreach ($mails as $mail) {
50 if ($account = $userversion->load_by_mail($mail)) {
51 $single = drush_usersingle_get_class($account);
52 $accounts[$single->id()] = $single;
55 throw new UserListException('Unable to find a matching user for ' . $mail . '.');
59 if ($names = _convert_csv_to_array(drush_get_option('name'))) {
60 foreach ($names as $name) {
61 if ($account = $userversion->load_by_name($name)) {
62 $single = drush_usersingle_get_class($account);
63 $accounts[$single->id()] = $single;
66 throw new UserListException('Unable to find a matching user for ' . $name . '.');
70 if ($userids = _convert_csv_to_array(drush_get_option('uid'))) {
71 foreach ($userids as $userid) {
72 if (is_numeric($userid) && $account = $userversion->load_by_uid($userid)) {
73 $single = drush_usersingle_get_class($account);
74 $accounts[$single->id()] = $single;
77 throw new UserListException('Unable to find a matching user for ' . $userid . '.');
85 * Given a comma-separated list of inputs, return accounts
86 * for users that match by uid,name or email address.
88 * @param string $inputs
89 * A comma delimited string (or array) of arguments, specifying user account(s).
91 * @throws UserListException
92 * If any input is unmatched, an exception is thrown.
94 * @return \Drush\User\UserSingleBase[]
95 * An associative array of UserSingleBase objects, keyed by user id.
97 public static function getFromParameters($inputs) {
99 $userversion = drush_user_get_class();
100 if ($inputs && $userversion) {
101 $inputs = _convert_csv_to_array($inputs);
102 foreach($inputs as $input) {
103 if (is_numeric($input) && $account = $userversion->load_by_uid($input)) {
106 elseif ($account = $userversion->load_by_name($input)) {
109 elseif ($account = $userversion->load_by_mail($input)) {
113 // Unable to load an account for the input.
114 throw new UserListException('Unable to find a matching user for ' . $input . '.');
116 // Populate $accounts with a UserSingle object. Will go into $this->accounts.
117 $single = drush_usersingle_get_class($account);
118 $accounts[$single->id()] = $single;
125 * A comma delimited list of names built from $this->accounts.
127 public function names() {
129 foreach ($this->accounts as $account) {
130 $names[] = $account->getUsername();
132 return implode(', ', $names);