Skip to content

Commit

Permalink
Added better support for timezone configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
ghermans authored May 14, 2024
1 parent 85e8b03 commit 1b76edc
Showing 1 changed file with 52 additions and 3 deletions.
55 changes: 52 additions & 3 deletions packages/Webkul/Installer/src/Console/Commands/Installer.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Webkul\Installer\Console\Commands;

use DateTimeZone;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Event;
Expand All @@ -12,6 +13,7 @@
use function Laravel\Prompts\multiselect;
use function Laravel\Prompts\password;
use function Laravel\Prompts\select;
use function Laravel\Prompts\suggest;
use function Laravel\Prompts\text;

class Installer extends Command
Expand Down Expand Up @@ -175,12 +177,14 @@ protected function askForApplicationDetails()
env('APP_URL', 'http://localhost:8000')
);

$this->envUpdate(
$timezones = $this->getTimezones();
$defaultLocale = $this->updateEnvSuggest(
'APP_TIMEZONE',
date_default_timezone_get()
'Please select the application timezone',
$timezones
);

$this->info('Your Default Timezone is '.date_default_timezone_get());
// $this->info('Your Default Timezone is '.date_default_timezone_get());

$defaultLocale = $this->updateEnvChoice(
'APP_LOCALE',
Expand Down Expand Up @@ -419,6 +423,25 @@ protected function updateEnvChoice(string $key, string $question, array $choices
return $choice;
}

/**
* Method for suggesting choice based on the list of options.
*
* @return string
*/
protected function updateEnvSuggest(string $key, string $question, array $choices)
{
$choice = suggest(
label: $question,
options: $choices,
default: env($key, ''),
required: true,
);

$this->envUpdate($key, $choice);

return $choice;
}

/**
* Function for getting allowed choices based on the list of options.
*/
Expand Down Expand Up @@ -482,4 +505,30 @@ protected static function getEnvAtRuntime(string $key): string|bool

return false;
}

/**
* Get sorted list of timezone abbreviations.
*
* Retrieve a list of timezone abbreviations available in the PHP DateTimeZone class,
* and sorts them alphabetically.
*
* @return array Sorted array of timezone abbreviations.
*/
protected static function getTimezones()
{
$timezoneAbbreviations = DateTimeZone::listAbbreviations();
$timezones = [];

foreach ($timezoneAbbreviations as $zones) {
foreach ($zones as $zone) {
if (! empty($zone['timezone_id'])) {
$timezones[$zone['timezone_id']] = $zone['timezone_id'];
}
}
}

asort($timezones);

return $timezones;
}
}

0 comments on commit 1b76edc

Please sign in to comment.