diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index f4f068d..1883a67 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -37,7 +37,7 @@ jobs: uses: shivammathur/setup-php@v2 with: php-version: ${{ matrix.php }} - extensions: dom, curl, libxml, mbstring, zip, pcntl, pdo, sqlite, pdo_sqlite, bcmath, soap, intl, gd, exif, iconv, imagick, fileinfo + extensions: dom, curl, libxml, mbstring, zip, pcntl, pdo, bcmath, intl, exif, iconv, fileinfo coverage: none - name: Setup problem matchers diff --git a/src/Console/Commands/MakeRequest.php b/src/Console/Commands/MakeRequest.php index 37a5736..74020fd 100644 --- a/src/Console/Commands/MakeRequest.php +++ b/src/Console/Commands/MakeRequest.php @@ -4,6 +4,14 @@ namespace Saloon\Laravel\Console\Commands; +use Saloon\Enums\Method; +use Illuminate\Support\Arr; +use function Laravel\Prompts\select; +use Symfony\Component\Console\Input\InputOption; +use Symfony\Component\Console\Input\InputInterface; +use Symfony\Component\Console\Output\OutputInterface; +use Illuminate\Contracts\Filesystem\FileNotFoundException; + class MakeRequest extends MakeCommand { /** @@ -40,4 +48,51 @@ class MakeRequest extends MakeCommand * @var string */ protected $stub = 'saloon.request.stub'; + + protected function getOptions(): array + { + return [ + ['method', 'm', InputOption::VALUE_REQUIRED, 'the method of the request'], + ]; + } + + /** + * Interact further with the user if they were prompted for missing arguments. + */ + protected function afterPromptingForMissingArguments(InputInterface $input, OutputInterface $output): void + { + if ($this->didReceiveOptions($input)) { + return; + } + + $methodType = select( + 'What method should the saloon request send?', + Arr::pluck(Method::cases(), 'name') + ); + + $input->setOption('method', $methodType); + } + + /** + * Build the class with the given name. + * + * @param string $name + * + * @throws FileNotFoundException + */ + protected function buildClass($name): MakeRequest|string + { + $stub = $this->files->get($this->getStub()); + $stub = $this->replaceMethod($stub, $this->option('method') ?? 'GET'); + + return $this->replaceNamespace($stub, $name)->replaceClass($stub, $name); + } + + /** + * Replace the method for the stub + */ + protected function replaceMethod(string $stub, string $name): string + { + return str_replace('{{ method }}', $name, $stub); + } } diff --git a/stubs/saloon.request.stub b/stubs/saloon.request.stub index 8852aa8..547bc49 100644 --- a/stubs/saloon.request.stub +++ b/stubs/saloon.request.stub @@ -10,7 +10,7 @@ class {{ class }} extends Request /** * The HTTP method of the request */ - protected Method $method = Method::GET; + protected Method $method = Method::{{ method }}; /** * The endpoint for the request