-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #28 from deliciousbrains/26-add-create-site-command
CLI can create (blank and wp) sites
- Loading branch information
Showing
19 changed files
with
603 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,188 @@ | ||
<?php | ||
|
||
namespace App\Commands\Sites; | ||
|
||
use App\Commands\BaseCommand; | ||
use App\Commands\Concerns\HasServerIdParameter; | ||
use App\Helpers\OptionsHelper; | ||
use App\Questions\Ask; | ||
use App\Questions\Choice; | ||
use App\Questions\Confirm; | ||
use App\Questions\HasQuestions; | ||
use Illuminate\Support\Str; | ||
|
||
class CreateCommand extends BaseCommand | ||
{ | ||
use HasQuestions; | ||
use HasServerIdParameter; | ||
|
||
protected $signature = 'sites:create | ||
{server_id? : Server ID} | ||
{--installation-method= : Type of installation (wp or blank)} | ||
{--domain= : Domain name} | ||
{--site-user= : Name for unique system user who will have ownership permission of all the site files} | ||
{--db-name= : Name of a database to be created. Must be unique for the server} | ||
{--db-user= : Database username to use when accessing the database} | ||
{--db-pass= : Database password to use when accessing the database} | ||
{--wp-title= : The title of your WordPress site} | ||
{--wp-admin-user= : For a WordPress site, the admin user\'s username} | ||
{--wp-admin-email= : For a WordPress site, the admin user\'s email} | ||
{--wp-admin-pass= : For a Wordpress site, the admin user\'s password} | ||
{--php-version= : PHP version the site will run under} | ||
{--page-cache-enabled : Enabling this option will configure page caching that is optimized for WordPress} | ||
{--https-enabled : Enabling secures your site by serving traffic over HTTPS} | ||
{--profile=} | ||
{--f|force}'; | ||
|
||
protected $description = 'Create a site'; | ||
|
||
protected array $userInput; | ||
|
||
protected array $validationLabels = [ | ||
'domain' => 'Primary Domain', | ||
'page_cache.enabled' => 'Enable Page Cache', | ||
'https.enabled' => 'Enable HTTPS', | ||
'database.name' => 'Database Name', | ||
'database.username' => 'Database Username', | ||
'database.password' => 'Database Password', | ||
'wordpress.title' => 'WordPress Title', | ||
'wordpress.admin_user' => 'WordPress Admin Username', | ||
'wordpress.admin_email' => 'WordPress Admin Email', | ||
'wordpress.admin_password' => 'WordPress Admin Password', | ||
]; | ||
|
||
protected function action(): int | ||
{ | ||
$server = $this->selectServer('deploy to')->first(); | ||
|
||
if (is_null($server)) { | ||
return self::INVALID; | ||
} | ||
|
||
$this->userInput['installation-method'] = Choice::make('What files would you like SpinupWP to install?') | ||
->withFlag('installation-method') | ||
->withChoices(OptionsHelper::INSTALLATION_METHODS) | ||
->withDefault(array_key_first(OptionsHelper::INSTALLATION_METHODS)) | ||
->nonInteractive($this->nonInteractive()) | ||
->resolveAnswer($this); | ||
|
||
if (!array_key_exists($this->userInput['installation-method'], OptionsHelper::INSTALLATION_METHODS)) { | ||
$this->error('Invalid installation method.'); | ||
$this->newLine(1); | ||
return self::INVALID; | ||
} | ||
|
||
$this->userInput['domain'] = Ask::make('Primary Domain') | ||
->withFlag('domain') | ||
->nonInteractive($this->nonInteractive()) | ||
->resolveAnswer($this); | ||
|
||
$this->userInput += $this->askQuestions($this->nonInteractive()); | ||
|
||
$this->userInput = array_merge($this->arguments(), $this->options(), $this->userInput); | ||
|
||
$site = $this->spinupwp->createSite($server->id, $this->userInput); | ||
|
||
$this->displaySuccess($site->eventId()); | ||
|
||
return self::SUCCESS; | ||
} | ||
|
||
public function getDomainSlug(): string | ||
{ | ||
return str_replace('.', '', $this->userInput['domain']); | ||
} | ||
|
||
public function questions(): array | ||
{ | ||
$commonStart = [ | ||
Confirm::make('Enable HTTPS') | ||
->withFlag('https-enabled') | ||
->withDefault((bool) !$this->nonInteractive()), | ||
|
||
Ask::make('Site User') | ||
->withDefault($this->getDomainSlug()), | ||
]; | ||
|
||
$db = [ | ||
Ask::make('Database Name') | ||
->withFlag('db-name') | ||
->withDefault($this->getDomainSlug()), | ||
|
||
Ask::make('Database Username') | ||
->withFlag('db-user') | ||
->withDefault($this->getDomainSlug()), | ||
|
||
Ask::make('Database Password') | ||
->withFlag('db-pass') | ||
->withDefault(Str::random(12)), | ||
]; | ||
|
||
$wp = [ | ||
Ask::make('WordPress Title') | ||
->withFlag('wp-title'), | ||
|
||
Ask::make('WordPress Admin Email') | ||
->withFlag('wp-admin-email'), | ||
|
||
Ask::make('WordPress Admin Username') | ||
->withFlag('wp-admin-user'), | ||
|
||
Ask::make('WordPress Admin Password') | ||
->withFlag('wp-admin-pass') | ||
->withDefault(Str::random(12)), | ||
]; | ||
|
||
$commonEnd = [ | ||
Choice::make('PHP Version') | ||
->withFlag('php-version') | ||
->withChoices(OptionsHelper::PHP_VERSIONS) | ||
->withDefault('8.0'), | ||
|
||
Confirm::make('Enable Page Cache') | ||
->withFlag('page-cache-enabled') | ||
->withDefault((bool) !$this->nonInteractive()), | ||
]; | ||
|
||
switch ($this->userInput['installation-method']) { | ||
case 'blank': | ||
return array_merge($commonStart, $commonEnd); | ||
default: | ||
return array_merge( | ||
$commonStart, | ||
$db, | ||
$wp, | ||
$commonEnd | ||
); | ||
} | ||
} | ||
|
||
protected function displaySuccess($eventId): void | ||
{ | ||
$tableHeadings = [ | ||
'Event ID', | ||
'Site', | ||
]; | ||
|
||
$tableRow = [ | ||
$eventId, | ||
$this->userInput['domain'], | ||
]; | ||
|
||
if ($this->userInput['installation-method'] === 'wp') { | ||
$tableHeadings = array_merge($tableHeadings, [ | ||
'Database Password', | ||
'WordPress Admin Password', | ||
]); | ||
|
||
$tableRow = array_merge($tableRow, [ | ||
$this->userInput['db-pass'], | ||
$this->userInput['wp-admin-pass'], | ||
]); | ||
} | ||
|
||
$this->successfulStep('Site queued for creation.'); | ||
|
||
$this->stepTable($tableHeadings, [$tableRow]); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<?php | ||
|
||
namespace App\Helpers; | ||
|
||
class OptionsHelper | ||
{ | ||
// SpinupWP CLI only supports a subset of installation methods available via the REST API | ||
public const INSTALLATION_METHODS = [ | ||
'wp' => 'WordPress', | ||
'blank' => 'Don\'t Install Any Files', | ||
]; | ||
|
||
public const PHP_VERSIONS = ['8.0', '7.4']; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<?php | ||
|
||
namespace App\Questions; | ||
|
||
class Ask extends Question | ||
{ | ||
protected function question() | ||
{ | ||
return $this->command->ask($this->prompt, $this->default); | ||
} | ||
} |
Oops, something went wrong.