Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create Site recommended values now match those from web UI #50

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 3 additions & 8 deletions app/Commands/Sites/CreateCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
Expand All @@ -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')
Expand Down
21 changes: 21 additions & 0 deletions app/Helpers/OptionsHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);
}
}
11 changes: 11 additions & 0 deletions tests/Unit/Helpers/OptionsHelperTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

use App\Helpers\OptionsHelper;

test('domain slug ', function () {
expect(OptionsHelper::getDomainSlug('this-is-a-very-long-subdomain.yoursite.com', 16))->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');
});