Skip to content

Commit

Permalink
Merge branch '1.x' into develop
Browse files Browse the repository at this point in the history
Signed-off-by: Mior Muhammad Zaki <[email protected]>
  • Loading branch information
crynobone committed Apr 15, 2024
2 parents 0eb8571 + e63c597 commit 4f4a304
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 15 deletions.
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
"require": {
"php": "^7.3 || ^8.0",
"composer-runtime-api": "^2.2",
"illuminate/support": "^8.83.4 || ^9.51 || ^10.0 || ^11.0",
"illuminate/support": "^9.51 || ^10.0 || ^11.0",
"laravel/vapor-core": "^2.37",
"nova-kit/nova-queued-export-as-csv": "^1.4"
},
Expand All @@ -35,7 +35,7 @@
"league/flysystem-aws-s3-v3": "^1.0 || ^3.0",
"nova-kit/nova-devtool": "^1.3",
"orchestra/testbench": "^7.40 || ^8.21 || ^9.0",
"phpunit/phpunit": "^9.6 || ^10.1"
"phpunit/phpunit": "^10.1"
},
"repositories": [
{
Expand Down
29 changes: 24 additions & 5 deletions src/Console/UserCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@
use Illuminate\Support\Facades\Hash;
use Laravel\Nova\Nova;
use Laravel\Nova\Util;
use Laravel\Prompts\Prompt;
use NovaKit\NovaOnVapor\Console\Util\CreateUserOptions;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

class UserCommand extends Command
{
Expand Down Expand Up @@ -38,19 +41,35 @@ class UserCommand extends Command
*/
protected $originalCreateUserCommandCallback;

/**
* Configure the command options.
*
* @return void
*/
/** {@inheritDoc} */
#[\Override]
protected function configure()
{
$this->ignoreValidationErrors();

$this->setName($this->name)
->setDescription($this->description);
}

/** {@inheritDoc} */
#[\Override]
protected function initialize(InputInterface $input, OutputInterface $output)
{
$input->setInteractive(false);

if (class_exists(Prompt::class)) {
Prompt::fallbackWhen(true);

if (method_exists(Prompt::class, 'interactive')) {
Prompt::interactive(false);
}
}
}

/** {@inheritDoc} */
#[\Override]
protected function specifyParameters()
{
$this->originalCreateUserCommandCallback = Nova::$createUserCommandCallback;

$this->createUserOptions = new CreateUserOptions(
Expand Down
40 changes: 32 additions & 8 deletions src/Console/Util/CreateUserOptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,33 @@
use Illuminate\Support\Collection;
use Illuminate\Support\Str;
use InvalidArgumentException;
use Laravel\Prompts\Prompt;
use Symfony\Component\Console\Input\InputOption;

class CreateUserOptions
{
/**
* List of create user options.
*
* @var \Illuminate\Support\Collection<int, array>
* @var \Illuminate\Support\Collection<int, array>|null
*/
protected $questions;
protected $questions = null;

/**
* The questions callback.
*
* @var callable(\NovaKit\NovaOnVapor\Console\Util\CreateUserOptions):array
*/
protected $questionsCallback;

/**
* Construct a new Create User Options.
*
* @param callable(\NovaKit\NovaOnVapor\Console\Util\CreateUserOptions):array $callback
* @param callable(\NovaKit\NovaOnVapor\Console\Util\CreateUserOptions):array $questionsCallback
*/
public function __construct(callable $callback)
public function __construct(callable $questionsCallback)
{
$this->questions = new Collection(call_user_func($callback, $this));
$this->questionsCallback = $questionsCallback;
}

/**
Expand Down Expand Up @@ -72,8 +80,10 @@ public function secret(string $question, bool $fallback = true): array
*/
public function toCommandOptions(Command $command): void
{
$this->questions->each(function ($question) use ($command) {
$command->addOption(...$question);
$this->resolveQuestions()->each(function ($question) use ($command) {
if (is_array($question)) {
$command->addOption(...$question);
}
});
}

Expand All @@ -85,7 +95,7 @@ public function toCommandOptions(Command $command): void
public function toCommandCallback(Command $command): Closure
{
return function () use ($command) {
return $this->questions->transform(function ($question) use ($command) {
return $this->resolveQuestions()->transform(function ($question) use ($command) {
$key = $question[0];
$variant = $question[2];
$value = $command->hasOption($key) ? $command->option($key) : null;
Expand Down Expand Up @@ -114,4 +124,18 @@ protected function parseQuestion(string $question): string

return Str::slug($question);
}

/**
* Resolve the available questions.
*
* @return \Illuminate\Support\Collection<int, array>
*/
protected function resolveQuestions(): Collection
{
if (is_null($this->questions)) {
$this->questions = Collection::make(call_user_func($this->questionsCallback, $this));
}

return $this->questions;
}
}

0 comments on commit 4f4a304

Please sign in to comment.