Skip to content

Commit

Permalink
Merge pull request #50 from JonPurvis/add-option-for-method-type
Browse files Browse the repository at this point in the history
Feature | Choose HTTP method when using `saloon:request` command
  • Loading branch information
Sammyjo20 authored Dec 2, 2023
2 parents 6fb2711 + 0e1f3f4 commit 29f653b
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 2 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
55 changes: 55 additions & 0 deletions src/Console/Commands/MakeRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
/**
Expand Down Expand Up @@ -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);
}
}
2 changes: 1 addition & 1 deletion stubs/saloon.request.stub
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 29f653b

Please sign in to comment.