From bd3c5f9e14218a4733cf0fb0087e2e174319c572 Mon Sep 17 00:00:00 2001 From: realdanielmlozano Date: Mon, 25 Jul 2022 09:56:19 -0500 Subject: [PATCH 1/3] Change slug generator --- app/Commands/Sites/CreateCommand.php | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/app/Commands/Sites/CreateCommand.php b/app/Commands/Sites/CreateCommand.php index 993edfb..a226904 100644 --- a/app/Commands/Sites/CreateCommand.php +++ b/app/Commands/Sites/CreateCommand.php @@ -100,9 +100,11 @@ protected function action(): int return self::SUCCESS; } - public function getDomainSlug(): string + public function getDomainSlug($maxLength = 32): string { - return str_replace('.', '', $this->userInput['domain']); + $name = explode('.', $this->userInput['domain'])[0]; + $name = str_replace(['-', '_'], '', $name); + return substr(Str::slug($name), 0, $maxLength); } public function questions(): array @@ -119,11 +121,11 @@ public function questions(): array $db = [ Ask::make('Database Name') ->withFlag('db-name') - ->withDefault($this->getDomainSlug()), + ->withDefault($this->getDomainSlug(48)), Ask::make('Database Username') ->withFlag('db-user') - ->withDefault($this->getDomainSlug()), + ->withDefault($this->getDomainSlug(16)), Ask::make('Database Password') ->withFlag('db-pass') From 241e36c3ec62c6753b27767f471ea29c757e81a8 Mon Sep 17 00:00:00 2001 From: realdanielmlozano Date: Mon, 25 Jul 2022 19:06:14 -0500 Subject: [PATCH 2/3] More robust slug generator --- app/Commands/Sites/CreateCommand.php | 13 +++---------- app/Helpers/OptionsHelper.php | 21 +++++++++++++++++++++ 2 files changed, 24 insertions(+), 10 deletions(-) diff --git a/app/Commands/Sites/CreateCommand.php b/app/Commands/Sites/CreateCommand.php index a226904..e6e3887 100644 --- a/app/Commands/Sites/CreateCommand.php +++ b/app/Commands/Sites/CreateCommand.php @@ -100,13 +100,6 @@ protected function action(): int return self::SUCCESS; } - public function getDomainSlug($maxLength = 32): string - { - $name = explode('.', $this->userInput['domain'])[0]; - $name = str_replace(['-', '_'], '', $name); - return substr(Str::slug($name), 0, $maxLength); - } - public function questions(): array { $commonStart = [ @@ -115,17 +108,17 @@ public function questions(): array ->withDefault((bool) !$this->nonInteractive()), Ask::make('Site User') - ->withDefault($this->getDomainSlug()), + ->withDefault(OptionsHelper::getDomainSlug($this->userInput['domain'])), ]; $db = [ Ask::make('Database Name') ->withFlag('db-name') - ->withDefault($this->getDomainSlug(48)), + ->withDefault(OptionsHelper::getDomainSlug($this->userInput['domain'], 64)), Ask::make('Database Username') ->withFlag('db-user') - ->withDefault($this->getDomainSlug(16)), + ->withDefault(OptionsHelper::getDomainSlug($this->userInput['domain'], 16)), Ask::make('Database Password') ->withFlag('db-pass') diff --git a/app/Helpers/OptionsHelper.php b/app/Helpers/OptionsHelper.php index 58eb91b..052a4b5 100644 --- a/app/Helpers/OptionsHelper.php +++ b/app/Helpers/OptionsHelper.php @@ -2,6 +2,8 @@ namespace App\Helpers; +use Illuminate\Support\Str; + class OptionsHelper { // SpinupWP CLI only supports a subset of installation methods available via the REST API @@ -11,4 +13,23 @@ class OptionsHelper ]; public const PHP_VERSIONS = ['8.0', '7.4']; + + public static function getDomainSlug(string $domain, int $maxLength = 32): string + { + $parsedDomain = parse_url($domain); + + $domain = data_get($parsedDomain, 'host', data_get($parsedDomain, 'path')); + + $names = explode('.', $domain); + + $name = array_shift($names); + + if (strtolower($name) === 'www') { + $name = array_shift($names); + } + + $name = str_replace(['-', '_'], '', $name); + + return substr(Str::slug($name), 0, $maxLength); + } } From f78d529810efa67003ac5ed0ed3b0933bf630ac6 Mon Sep 17 00:00:00 2001 From: realdanielmlozano Date: Mon, 25 Jul 2022 19:06:26 -0500 Subject: [PATCH 3/3] Tests --- tests/Unit/Helpers/OptionsHelperTest.php | 11 +++++++++++ 1 file changed, 11 insertions(+) create mode 100644 tests/Unit/Helpers/OptionsHelperTest.php diff --git a/tests/Unit/Helpers/OptionsHelperTest.php b/tests/Unit/Helpers/OptionsHelperTest.php new file mode 100644 index 0000000..5be22fb --- /dev/null +++ b/tests/Unit/Helpers/OptionsHelperTest.php @@ -0,0 +1,11 @@ +toEqual('thisisaverylongs'); + + expect(OptionsHelper::getDomainSlug('www.this-is-a-very-long-subdomain.yoursite.com', 16))->toEqual('thisisaverylongs'); + + expect(OptionsHelper::getDomainSlug('https://www.this-is-a-very-long-subdomain.yoursite.com', 64))->toEqual('thisisaverylongsubdomain'); +});