Skip to content

Commit

Permalink
Use installer to set the site name in config
Browse files Browse the repository at this point in the history
  • Loading branch information
caendesilva committed May 4, 2022
1 parent b890eb7 commit 3f0c843
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 14 deletions.
57 changes: 43 additions & 14 deletions src/Commands/HydeInstallCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,33 +15,62 @@ class HydeInstallCommand extends Command
protected $signature = 'install';
protected $description = 'Initialize a new Hyde project.';

public ?string $siteName = null;

public function handle(): int
{
$this->title('Welcome to HydePHP!');
$this->title('Welcome to HydePHP!');

$this->info('This guided installer is optional, but can help you to get set up quickly.');
$this->info('This guided installer is optional, but can help you to get set up quickly.');

$this->warn('Please note that this installer should not be run in existing projects.');
$this->warn('Please note that this installer should not be run in existing projects.');

if (! $this->confirm('Do you want to continue?', true)) {
if (!$this->confirm('Do you want to continue?', true)) {
$this->comment('Aborting installation.');
return 130;
}

$this->info('Installing HydePHP...');
$this->newLine();

$this->call('update:configs');

$this->promptForHomepage();
$this->promptForSiteName();

$this->promptForHomepage();

return 0;
}

protected function promptForHomepage()
{
$this->info('Hyde has a few different homepage options.');
if ($this->confirm('Would you like to select one?')) {
$this->call('publish:homepage');
} else {
$this->line('Okay, leaving the default homepage.');
};
}
protected function promptForSiteName()
{
if ($this->siteName = $this->ask('What is the name of your site? (leave blank to skip)')) {
$this->updateSiteName();
$this->info('Site name set to: ' . $this->siteName);
return;
}

$this->line('Skipping site name.');
}

protected function promptForHomepage()
{
$this->info('Hyde has a few different homepage options.');
if ($this->confirm('Would you like to select one?')) {
$this->call('publish:homepage');
} else {
$this->line('Okay, leaving the default homepage.');
}
}

protected function updateSiteName(): void
{
$config = file_get_contents(Hyde::path('config/hyde.php'));
$config = str_replace(
"'name' => env('SITE_NAME', 'HydePHP'),",
"'name' => env('SITE_NAME', '".$this->siteName."'),",
$config
);
file_put_contents(Hyde::path('config/hyde.php'), $config);
}
}
28 changes: 28 additions & 0 deletions tests/Feature/Commands/HydeInstallCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public function test_command_output()
->expectsQuestion('Do you want to continue?', true)
->expectsOutput('Installing HydePHP...')
->doesntExpectOutput('Aborting installation.')
->expectsQuestion('What is the name of your site? (leave blank to skip)', null)
->expectsOutput('Hyde has a few different homepage options.')
->expectsQuestion('Would you like to select one?', false)
->assertExitCode(0);
Expand All @@ -32,13 +33,40 @@ public function test_command_exits_with_sigint_130_if_user_declines_confirmation
->assertExitCode(130);
}

public function test_prompt_for_site_name_saves_selected_site_name()
{
$this->artisan('install')
->expectsOutputToContain('Welcome to HydePHP!')
->expectsQuestion('Do you want to continue?', true)
->expectsQuestion('What is the name of your site? (leave blank to skip)', 'My Site')
->expectsQuestion('Would you like to select one?', false)
->assertExitCode(0);

$this->assertStringContainsString('My Site',
file_get_contents(Hyde::path('config/hyde.php')));
}

public function test_prompt_for_site_name_does_nothing_if_user_skips()
{
$this->artisan('install')
->expectsOutputToContain('Welcome to HydePHP!')
->expectsQuestion('Do you want to continue?', true)
->expectsQuestion('What is the name of your site? (leave blank to skip)', null)
->expectsQuestion('Would you like to select one?', false)
->assertExitCode(0);

$this->assertStringNotContainsString('My Site',
file_get_contents(Hyde::path('config/hyde.php')));
}

public function test_command_calls_publish_homepage_command()
{
$this->artisan('install')
->expectsOutputToContain('Welcome to HydePHP!')
->expectsQuestion('Do you want to continue?', true)
->expectsOutput('Installing HydePHP...')
->doesntExpectOutput('Aborting installation.')
->expectsQuestion('What is the name of your site? (leave blank to skip)', null)
->expectsOutput('Hyde has a few different homepage options.')
->expectsQuestion('Would you like to select one?', true)
->expectsQuestion('Which homepage do you want to publish?', 'default')
Expand Down

0 comments on commit 3f0c843

Please sign in to comment.