From 90a33f92803111e55d33412a6bbd082aa276b308 Mon Sep 17 00:00:00 2001 From: Matthew Grasmick Date: Wed, 3 May 2017 15:56:51 -0400 Subject: [PATCH 1/2] Making random username generation more efficient. --- src/Robo/Commands/Drupal/DrupalCommand.php | 3 ++- src/Robo/Common/RandomString.php | 14 ++++++++++++-- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/src/Robo/Commands/Drupal/DrupalCommand.php b/src/Robo/Commands/Drupal/DrupalCommand.php index 93da89a1a..1c33c0324 100644 --- a/src/Robo/Commands/Drupal/DrupalCommand.php +++ b/src/Robo/Commands/Drupal/DrupalCommand.php @@ -27,7 +27,8 @@ public function install() { $username = RandomString::string(10, FALSE, function ($string) { return !preg_match('/[^\x{80}-\x{F7} a-z0-9@+_.\'-]/i', $string); - } + }, + 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890!#%^&*()_-?/.,+=><' ); $task = $this->taskDrush() diff --git a/src/Robo/Common/RandomString.php b/src/Robo/Common/RandomString.php index ca13340c2..689dde728 100644 --- a/src/Robo/Common/RandomString.php +++ b/src/Robo/Common/RandomString.php @@ -31,15 +31,19 @@ class RandomString { * Defaults to FALSE. * @param callable $validator * (optional) A callable to validate the string. Defaults to NULL. + * @param string $characters + * (optional) A string containing all possible characters that may be used + * to generate the random string. * * @return string * Randomly generated string. * * @see \Drupal\Component\Utility\Random::name() */ - public static function string($length = 8, $unique = FALSE, callable $validator = NULL) { + public static function string($length = 8, $unique = FALSE, callable $validator = NULL, $characters = '') { $counter = 0; $strings = []; + $characters_array = str_split($characters); // Continue to loop if $unique is TRUE and the generated string is not // unique or if $validator is a callable that returns FALSE. To generate a @@ -50,7 +54,13 @@ public static function string($length = 8, $unique = FALSE, callable $validator } $str = ''; for ($i = 0; $i < $length; $i++) { - $str .= chr(mt_rand(32, 126)); + if ($characters_array) { + $position = mt_rand(0, count($characters_array)); + $str .= $characters_array[$position]; + } + else { + $str .= chr(mt_rand(32, 126)); + } } $counter++; From df2958c3f44e7e706aaf68972cb9b048ac322ac7 Mon Sep 17 00:00:00 2001 From: Matthew Grasmick Date: Wed, 3 May 2017 16:18:44 -0400 Subject: [PATCH 2/2] Fixing random for random character selection. --- src/Robo/Common/RandomString.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Robo/Common/RandomString.php b/src/Robo/Common/RandomString.php index 689dde728..7bf08476c 100644 --- a/src/Robo/Common/RandomString.php +++ b/src/Robo/Common/RandomString.php @@ -43,7 +43,7 @@ class RandomString { public static function string($length = 8, $unique = FALSE, callable $validator = NULL, $characters = '') { $counter = 0; $strings = []; - $characters_array = str_split($characters); + $characters_array = $characters ? str_split($characters) : []; // Continue to loop if $unique is TRUE and the generated string is not // unique or if $validator is a callable that returns FALSE. To generate a @@ -55,7 +55,7 @@ public static function string($length = 8, $unique = FALSE, callable $validator $str = ''; for ($i = 0; $i < $length; $i++) { if ($characters_array) { - $position = mt_rand(0, count($characters_array)); + $position = mt_rand(0, count($characters_array) - 1); $str .= $characters_array[$position]; } else {