diff --git a/app/Commands/Sites/CreateCommand.php b/app/Commands/Sites/CreateCommand.php index 993edfb..e6e3887 100644 --- a/app/Commands/Sites/CreateCommand.php +++ b/app/Commands/Sites/CreateCommand.php @@ -100,11 +100,6 @@ protected function action(): int return self::SUCCESS; } - public function getDomainSlug(): string - { - return str_replace('.', '', $this->userInput['domain']); - } - public function questions(): array { $commonStart = [ @@ -113,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()), + ->withDefault(OptionsHelper::getDomainSlug($this->userInput['domain'], 64)), Ask::make('Database Username') ->withFlag('db-user') - ->withDefault($this->getDomainSlug()), + ->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); + } } 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'); +});