From 68772de010091db7be23f6b6d88e6bcafd61d9f3 Mon Sep 17 00:00:00 2001 From: JonPurvis Date: Sat, 30 Sep 2023 16:04:43 +0100 Subject: [PATCH 1/6] add option for method type --- src/Console/Commands/MakeRequest.php | 46 ++++++++++++++++++++++++++-- stubs/saloon.request.connect.stub | 22 +++++++++++++ stubs/saloon.request.delete.stub | 22 +++++++++++++ stubs/saloon.request.head.stub | 22 +++++++++++++ stubs/saloon.request.options.stub | 22 +++++++++++++ stubs/saloon.request.patch.stub | 22 +++++++++++++ stubs/saloon.request.post.stub | 22 +++++++++++++ stubs/saloon.request.put.stub | 22 +++++++++++++ stubs/saloon.request.trace.stub | 22 +++++++++++++ 9 files changed, 219 insertions(+), 3 deletions(-) create mode 100644 stubs/saloon.request.connect.stub create mode 100644 stubs/saloon.request.delete.stub create mode 100644 stubs/saloon.request.head.stub create mode 100644 stubs/saloon.request.options.stub create mode 100644 stubs/saloon.request.patch.stub create mode 100644 stubs/saloon.request.post.stub create mode 100644 stubs/saloon.request.put.stub create mode 100644 stubs/saloon.request.trace.stub diff --git a/src/Console/Commands/MakeRequest.php b/src/Console/Commands/MakeRequest.php index 37a5736..271e47f 100644 --- a/src/Console/Commands/MakeRequest.php +++ b/src/Console/Commands/MakeRequest.php @@ -4,6 +4,13 @@ namespace Saloon\Laravel\Console\Commands; +use Illuminate\Support\Arr; +use Saloon\Enums\Method; +use Symfony\Component\Console\Input\InputInterface; +use Symfony\Component\Console\Input\InputOption; +use Symfony\Component\Console\Output\OutputInterface; +use function Laravel\Prompts\select; + class MakeRequest extends MakeCommand { /** @@ -34,10 +41,43 @@ class MakeRequest extends MakeCommand */ protected $namespace = '\Http\Integrations\{integration}\Requests'; + protected function resolveStubName(): string + { + return match ($this->option('method')) { + 'HEAD' => 'saloon.request.head.stub', + 'POST' => 'saloon.request.post.stub', + 'PUT' => 'saloon.request.put.stub', + 'PATCH' => 'saloon.request.patch.stub', + 'DELETE' => 'saloon.request.delete.stub', + 'OPTIONS' => 'saloon.request.options.stub', + 'CONNECT' => 'saloon.request.connect.stub', + 'TRACE' => 'saloon.request.trace.stub', + default => 'saloon.request.stub' + }; + } + + protected function getOptions(): array + { + return [ + ['method', null, InputOption::VALUE_REQUIRED, 'the method of the request'], + ]; + } + /** - * The default stub + * Interact further with the user if they were prompted for missing arguments. * - * @var string */ - protected $stub = 'saloon.request.stub'; + protected function afterPromptingForMissingArguments(InputInterface $input, OutputInterface $output): void + { + if ($this->didReceiveOptions($input)) { + return; + } + + $methodType = select( + 'What method type should the saloon request be?', + Arr::pluck(Method::cases(), 'name') + ); + + $input->setOption('method', $methodType); + } } diff --git a/stubs/saloon.request.connect.stub b/stubs/saloon.request.connect.stub new file mode 100644 index 0000000..3c258e3 --- /dev/null +++ b/stubs/saloon.request.connect.stub @@ -0,0 +1,22 @@ + Date: Sat, 30 Sep 2023 16:32:04 +0100 Subject: [PATCH 2/6] wording change --- src/Console/Commands/MakeRequest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Console/Commands/MakeRequest.php b/src/Console/Commands/MakeRequest.php index 271e47f..bce9ba5 100644 --- a/src/Console/Commands/MakeRequest.php +++ b/src/Console/Commands/MakeRequest.php @@ -74,7 +74,7 @@ protected function afterPromptingForMissingArguments(InputInterface $input, Outp } $methodType = select( - 'What method type should the saloon request be?', + 'What method should the saloon request send?', Arr::pluck(Method::cases(), 'name') ); From e2e6815fdeb62d1d9d37389916f856f322bfb517 Mon Sep 17 00:00:00 2001 From: JonPurvis Date: Sun, 1 Oct 2023 00:46:25 +0100 Subject: [PATCH 3/6] remove stub files --- src/Console/Commands/MakeRequest.php | 43 +++++++++++++++++++--------- stubs/saloon.request.connect.stub | 22 -------------- stubs/saloon.request.delete.stub | 22 -------------- stubs/saloon.request.head.stub | 22 -------------- stubs/saloon.request.options.stub | 22 -------------- stubs/saloon.request.patch.stub | 22 -------------- stubs/saloon.request.post.stub | 22 -------------- stubs/saloon.request.put.stub | 22 -------------- stubs/saloon.request.stub | 2 +- stubs/saloon.request.trace.stub | 22 -------------- 10 files changed, 30 insertions(+), 191 deletions(-) delete mode 100644 stubs/saloon.request.connect.stub delete mode 100644 stubs/saloon.request.delete.stub delete mode 100644 stubs/saloon.request.head.stub delete mode 100644 stubs/saloon.request.options.stub delete mode 100644 stubs/saloon.request.patch.stub delete mode 100644 stubs/saloon.request.post.stub delete mode 100644 stubs/saloon.request.put.stub delete mode 100644 stubs/saloon.request.trace.stub diff --git a/src/Console/Commands/MakeRequest.php b/src/Console/Commands/MakeRequest.php index bce9ba5..a96908c 100644 --- a/src/Console/Commands/MakeRequest.php +++ b/src/Console/Commands/MakeRequest.php @@ -4,6 +4,7 @@ namespace Saloon\Laravel\Console\Commands; +use Illuminate\Contracts\Filesystem\FileNotFoundException; use Illuminate\Support\Arr; use Saloon\Enums\Method; use Symfony\Component\Console\Input\InputInterface; @@ -41,20 +42,7 @@ class MakeRequest extends MakeCommand */ protected $namespace = '\Http\Integrations\{integration}\Requests'; - protected function resolveStubName(): string - { - return match ($this->option('method')) { - 'HEAD' => 'saloon.request.head.stub', - 'POST' => 'saloon.request.post.stub', - 'PUT' => 'saloon.request.put.stub', - 'PATCH' => 'saloon.request.patch.stub', - 'DELETE' => 'saloon.request.delete.stub', - 'OPTIONS' => 'saloon.request.options.stub', - 'CONNECT' => 'saloon.request.connect.stub', - 'TRACE' => 'saloon.request.trace.stub', - default => 'saloon.request.stub' - }; - } + protected $stub = 'saloon.request.stub'; protected function getOptions(): array { @@ -80,4 +68,31 @@ protected function afterPromptingForMissingArguments(InputInterface $input, Outp $input->setOption('method', $methodType); } + + /** + * Build the class with the given name. + * + * @param string $name + * @return MakeRequest|string + * + * @throws FileNotFoundException + */ + protected function buildClass($name): MakeRequest|string + { + $stub = $this->files->get($this->getStub()); + $stub = $this->replaceMethod($stub, $this->option('method')); + + return $this->replaceNamespace($stub, $name)->replaceClass($stub, $name); + } + + /** + * Replace the method for the stub + * @param $stub + * @param $name + * @return string + */ + protected function replaceMethod($stub, $name): string + { + return str_replace('{{ method }}', $name, $stub); + } } diff --git a/stubs/saloon.request.connect.stub b/stubs/saloon.request.connect.stub deleted file mode 100644 index 3c258e3..0000000 --- a/stubs/saloon.request.connect.stub +++ /dev/null @@ -1,22 +0,0 @@ - Date: Sun, 1 Oct 2023 00:48:38 +0100 Subject: [PATCH 4/6] add docblock back --- src/Console/Commands/MakeRequest.php | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/Console/Commands/MakeRequest.php b/src/Console/Commands/MakeRequest.php index a96908c..879f22a 100644 --- a/src/Console/Commands/MakeRequest.php +++ b/src/Console/Commands/MakeRequest.php @@ -42,6 +42,11 @@ class MakeRequest extends MakeCommand */ protected $namespace = '\Http\Integrations\{integration}\Requests'; + /** + * The default stub + * + * @var string + */ protected $stub = 'saloon.request.stub'; protected function getOptions(): array From 2d8ca33b687d5702aeb73ce1093bab415845fd22 Mon Sep 17 00:00:00 2001 From: JonPurvis Date: Sat, 2 Dec 2023 23:00:19 +0000 Subject: [PATCH 5/6] run composer fix code --- src/Console/Commands/MakeRequest.php | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) diff --git a/src/Console/Commands/MakeRequest.php b/src/Console/Commands/MakeRequest.php index 879f22a..68c6242 100644 --- a/src/Console/Commands/MakeRequest.php +++ b/src/Console/Commands/MakeRequest.php @@ -4,13 +4,13 @@ namespace Saloon\Laravel\Console\Commands; -use Illuminate\Contracts\Filesystem\FileNotFoundException; -use Illuminate\Support\Arr; use Saloon\Enums\Method; -use Symfony\Component\Console\Input\InputInterface; +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 function Laravel\Prompts\select; +use Illuminate\Contracts\Filesystem\FileNotFoundException; class MakeRequest extends MakeCommand { @@ -58,7 +58,6 @@ protected function getOptions(): array /** * Interact further with the user if they were prompted for missing arguments. - * */ protected function afterPromptingForMissingArguments(InputInterface $input, OutputInterface $output): void { @@ -78,7 +77,6 @@ protected function afterPromptingForMissingArguments(InputInterface $input, Outp * Build the class with the given name. * * @param string $name - * @return MakeRequest|string * * @throws FileNotFoundException */ @@ -92,9 +90,6 @@ protected function buildClass($name): MakeRequest|string /** * Replace the method for the stub - * @param $stub - * @param $name - * @return string */ protected function replaceMethod($stub, $name): string { From 0e1f3f4ba27910b1b9311a0e8e952b5f6321995f Mon Sep 17 00:00:00 2001 From: Sammyjo20 <29132017+Sammyjo20@users.noreply.github.com> Date: Sat, 2 Dec 2023 23:13:13 +0000 Subject: [PATCH 6/6] Fixed missing option --- .github/workflows/tests.yml | 2 +- src/Console/Commands/MakeRequest.php | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 739eaa3..6608991 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -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 diff --git a/src/Console/Commands/MakeRequest.php b/src/Console/Commands/MakeRequest.php index 68c6242..74020fd 100644 --- a/src/Console/Commands/MakeRequest.php +++ b/src/Console/Commands/MakeRequest.php @@ -52,7 +52,7 @@ class MakeRequest extends MakeCommand protected function getOptions(): array { return [ - ['method', null, InputOption::VALUE_REQUIRED, 'the method of the request'], + ['method', 'm', InputOption::VALUE_REQUIRED, 'the method of the request'], ]; } @@ -83,7 +83,7 @@ protected function afterPromptingForMissingArguments(InputInterface $input, Outp protected function buildClass($name): MakeRequest|string { $stub = $this->files->get($this->getStub()); - $stub = $this->replaceMethod($stub, $this->option('method')); + $stub = $this->replaceMethod($stub, $this->option('method') ?? 'GET'); return $this->replaceNamespace($stub, $name)->replaceClass($stub, $name); } @@ -91,7 +91,7 @@ protected function buildClass($name): MakeRequest|string /** * Replace the method for the stub */ - protected function replaceMethod($stub, $name): string + protected function replaceMethod(string $stub, string $name): string { return str_replace('{{ method }}', $name, $stub); }