Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature | Choose HTTP method when using saloon:request command #50

Merged
merged 6 commits into from
Dec 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,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