diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..7925832 --- /dev/null +++ b/.gitattributes @@ -0,0 +1,8 @@ +/.github export-ignore +/art export-ignore +/docs export-ignore +/tests export-ignore +/.gitattributes export-ignore +/.gitignore export-ignore +/AGENTS.md export-ignore +/phpunit.xml.dist export-ignore diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 63834f5..ed51d95 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -19,16 +19,6 @@ jobs: fail-fast: false matrix: include: - - php: "8.3" - laravel: "10" - testbench: "^8.0" - phpunit: "^10.5" - allow_insecure: true - - php: "8.3" - laravel: "11" - testbench: "^9.0" - phpunit: "^11.5" - allow_insecure: true - php: "8.3" laravel: "12" testbench: "^10.0" diff --git a/README.md b/README.md index 8a4ee63..eb2c3e6 100644 --- a/README.md +++ b/README.md @@ -8,7 +8,9 @@ [![PHP](https://img.shields.io/badge/PHP-%5E8.3-777BB4?logo=php&logoColor=white)](https://packagist.org/packages/php-skir/client) [![License](https://img.shields.io/github/license/php-skir/client)](LICENSE) -Laravel package for calling SkirRPC services through generated typed clients and Saloon. +Call [SkirRPC](https://skir.build/docs/skirrpc) services from Laravel through generated, typed PHP clients. Requests go out over [Saloon](https://docs.saloon.dev/) using the standard SkirRPC protocol, so the server can be [`php-skir/server`](https://github.com/php-skir/server) or a Skir service written in any other language. + +**Requirements:** PHP 8.3+, Laravel 12 or 13, and Node.js for the Skir compiler. ## Features @@ -56,7 +58,7 @@ Skir owns the output directory and may replace its contents during generation. Generate the client, configure Composer autoloading, and publish the Laravel config: ```bash -npx skir gen +npx skir gen # or: php artisan skir:generate-client npx skir-php-generator configure-composer composer dump-autoload php artisan vendor:publish --tag=skir-client-config diff --git a/composer.json b/composer.json index 00f5e66..d4b9992 100644 --- a/composer.json +++ b/composer.json @@ -24,16 +24,16 @@ ], "require": { "php": "^8.3", - "illuminate/contracts": "^10.0|^11.0|^12.0|^13.0", - "illuminate/support": "^10.0|^11.0|^12.0|^13.0", + "illuminate/console": "^12.0|^13.0", + "illuminate/support": "^12.0|^13.0", "php-skir/runtime": "^0.1@dev|^0.2@dev", "saloonphp/saloon": "^4.0", "symfony/process": "^6.4|^7.0|^8.0" }, "require-dev": { "laravel/pint": "^1.29", - "orchestra/testbench": "^8.0|^9.0|^10.0|^11.0", - "phpunit/phpunit": "^10.5|^11.5|^12.5" + "orchestra/testbench": "^10.0|^11.0", + "phpunit/phpunit": "^11.5|^12.5" }, "autoload": { "psr-4": { diff --git a/docs/error-handling-and-testing.md b/docs/error-handling-and-testing.md index b420213..f84fd57 100644 --- a/docs/error-handling-and-testing.md +++ b/docs/error-handling-and-testing.md @@ -1,20 +1,22 @@ # Error handling and testing -The client reports failed HTTP responses and response-decoding failures through `SkirClientException`, while keeping the method descriptor available for application logging and policy decisions. +The client reports failed HTTP responses and response-decoding failures through `SkirClientException`, with the invoked method descriptor attached. ## Handle client failures A response that Saloon considers failed throws `SkirClientException` with the message `Skir RPC request failed with status {status}.`. By default, Saloon treats 4xx and 5xx responses as failed. The invoked `MethodDescriptor` is available through `$exception->method`, and the Saloon `Response` is available through `$exception->response`. Inspect the response when your application needs its status or body; the exception does not expose separate server error fields. -Any response Saloon does not consider failed proceeds to response decoding. Redirects are therefore not automatically handled by the failed-response branch. +Redirects are followed by the underlying Guzzle client; any other response Saloon does not consider failed is decoded. A 2xx response that cannot be decoded also throws `SkirClientException`, but with the message `Skir RPC response for [{method}] could not be decoded.`. In this case, `$exception->method` contains the method descriptor, `$exception->response` is `null`, and `$exception->getPrevious()` returns the underlying `SkirRuntimeException`. -The exception boundary is deliberately narrow: +Only these cases throw `SkirClientException`: -- Only a `SkirRuntimeException` thrown while decoding the response is wrapped as an invalid-response `SkirClientException`. -- A `SkirRuntimeException` thrown while encoding the request propagates directly. -- DNS, connection, and TLS failures propagate as `Saloon\Exceptions\Request\FatalRequestException` and do not have a response. +- a failed HTTP response; +- a successful response that cannot be decoded (the `SkirRuntimeException` is available as the previous exception); +- resolving the client from the container without a configured base URL. + +A `SkirRuntimeException` thrown while encoding the request propagates directly. DNS, connection, and TLS failures propagate as `Saloon\Exceptions\Request\FatalRequestException` and have no response. The following handler is specifically for the `SkirClientException` paths. It does not catch direct request-encoding or Saloon fatal-request exceptions: @@ -35,8 +37,6 @@ try { } ``` -Rethrowing preserves the application's exception policy after adding contextual logging; Laravel's exception handler can report the exception once. - ## Test a generated client The following feature test belongs in a consuming Laravel application. It uses the application's `Tests\TestCase`, resolves the configured transport from the container, and exercises the generated standard-PHP client without making a network call. diff --git a/docs/laravel-data.md b/docs/laravel-data.md index 935d855..392decd 100644 --- a/docs/laravel-data.md +++ b/docs/laravel-data.md @@ -86,6 +86,6 @@ generators: - max:100 ``` -Validation failures occur when a generated Data class hydrates a raw request or response through `makeFromSkirPayload()`. In this journey, a response whose `name` exceeds 100 characters fails while `getUser()` hydrates `UserData`; the failure is not deferred until the property is read. A raw request passed through `GetUserRequestData::makeFromSkirPayload()` follows the same rule, while the generated client does not rehydrate an already constructed request object. +Validation failures occur when a generated Data class hydrates a raw request or response through `makeFromSkirPayload()`. A response whose `name` exceeds 100 characters fails while `getUser()` hydrates `UserData`; the failure is not deferred until the property is read. A raw request passed through `GetUserRequestData::makeFromSkirPayload()` follows the same rule, while the generated client does not rehydrate an already constructed request object. See the [Skir Laravel Data Generator](https://github.com/php-skir/skir-laravel-data-generator) for its full validation and generated-API reference. diff --git a/docs/simple-data-objects.md b/docs/simple-data-objects.md index ad5e898..368c738 100644 --- a/docs/simple-data-objects.md +++ b/docs/simple-data-objects.md @@ -67,7 +67,7 @@ $user = $client->getUser(new GetUserRequestData(userId: 42)); echo $user->name; ``` -The direct `new GetUserRequestData(...)` call is a trusted construction path. The generated client serializes that existing request with `toSkirArray()` without revalidating it, then hydrates the response as `UserData` before returning it. When request data starts as an untrusted raw array, use `makeFromSkirPayload()` as shown below. +The direct `new GetUserRequestData(...)` call is not validated. The generated client serializes that existing request with `toSkirArray()` without revalidating it, then hydrates the response as `UserData` before returning it. When request data starts as an untrusted raw array, use `makeFromSkirPayload()` as shown below. ## Hydrate raw client data safely diff --git a/resources/boost/skills/skir-client-development/references/generation-and-configuration.md b/resources/boost/skills/skir-client-development/references/generation-and-configuration.md index 2ceadda..488c602 100644 --- a/resources/boost/skills/skir-client-development/references/generation-and-configuration.md +++ b/resources/boost/skills/skir-client-development/references/generation-and-configuration.md @@ -16,7 +16,9 @@ npx skir-php-generator configure-composer composer dump-autoload ``` -`configure-composer` adds the generated PSR-4 mapping but does not refresh Composer autoloading. +Run `configure-composer` from the generator you use (`skir-php-generator`, `skir-laravel-data-generator`, or +`skir-simple-data-objects-generator`). It adds the generated PSR-4 mapping but does not refresh Composer +autoloading. Laravel applications may replace only `npx skir gen` with: @@ -57,7 +59,3 @@ SKIR_CLIENT_ENDPOINT=/api/skir `SKIR_CLIENT_BASE_URL` is required. `SKIR_CLIENT_ENDPOINT` defaults to `/`; do not duplicate the endpoint path in the base URL. - -Do not substitute repository-local wrapper commands or machine-specific absolute paths in reusable -package guidance. Use the consuming application's documented runtime surface when executing these -commands. diff --git a/resources/boost/skills/skir-client-development/references/invocation-errors-and-testing.md b/resources/boost/skills/skir-client-development/references/invocation-errors-and-testing.md index 4d10c9c..18bf374 100644 --- a/resources/boost/skills/skir-client-development/references/invocation-errors-and-testing.md +++ b/resources/boost/skills/skir-client-development/references/invocation-errors-and-testing.md @@ -19,7 +19,7 @@ decodes a dense JSON response, so it works against any SkirRPC server. | Request encoding fails | `SkirRuntimeException` propagates directly | Verify generated request values | | DNS, connection, or TLS fails | Saloon `FatalRequestException` propagates with no response | Inspect URL, DNS, TLS, and connectivity | -Responses Saloon does not classify as failed, including redirects by default, continue to decoding. +Redirects are followed by Guzzle; other responses Saloon does not classify as failed are decoded. Catching only `SkirClientException` does not catch encoding or network failures. ## Test without network access diff --git a/src/Commands/GenerateClientCommand.php b/src/Commands/GenerateClientCommand.php index 2fe3f7a..fde7f96 100644 --- a/src/Commands/GenerateClientCommand.php +++ b/src/Commands/GenerateClientCommand.php @@ -45,7 +45,7 @@ public function handle(): int $this->output->write($buffer); }); } catch (ProcessFailedException $exception) { - $this->error($this->failureMessage($exception)); + $this->error("Skir generation failed with exit code {$exception->getProcess()->getExitCode()}."); return self::FAILURE; } @@ -54,21 +54,4 @@ public function handle(): int return self::SUCCESS; } - - private function failureMessage(ProcessFailedException $exception): string - { - $errorOutput = trim($exception->getProcess()->getErrorOutput()); - - if ($errorOutput !== '') { - return $errorOutput; - } - - $output = trim($exception->getProcess()->getOutput()); - - if ($output !== '') { - return $output; - } - - return $exception->getMessage(); - } } diff --git a/src/Exceptions/SkirClientException.php b/src/Exceptions/SkirClientException.php index c35c4b2..8ce3717 100644 --- a/src/Exceptions/SkirClientException.php +++ b/src/Exceptions/SkirClientException.php @@ -37,4 +37,9 @@ public static function invalidResponse(MethodDescriptor $method, Throwable $prev previous: $previous, ); } + + public static function missingBaseUrl(): self + { + return new self('Set [skir-client.base_url] (SKIR_CLIENT_BASE_URL) to the SkirRPC server URL.'); + } } diff --git a/src/SkirClientServiceProvider.php b/src/SkirClientServiceProvider.php index 9d8ecf7..b81d5c6 100644 --- a/src/SkirClientServiceProvider.php +++ b/src/SkirClientServiceProvider.php @@ -6,6 +6,7 @@ use Illuminate\Support\ServiceProvider; use Skir\Client\Commands\GenerateClientCommand; +use Skir\Client\Exceptions\SkirClientException; final class SkirClientServiceProvider extends ServiceProvider { @@ -14,10 +15,13 @@ public function register(): void $this->mergeConfigFrom(__DIR__.'/../config/skir-client.php', 'skir-client'); $this->app->bind(SkirClient::class, function (): SkirClient { - return new SkirClient( - baseUrl: (string) config('skir-client.base_url'), - endpoint: (string) config('skir-client.endpoint', '/'), - ); + $baseUrl = config('skir-client.base_url'); + + if (! is_string($baseUrl) || $baseUrl === '') { + throw SkirClientException::missingBaseUrl(); + } + + return new SkirClient($baseUrl, (string) config('skir-client.endpoint')); }); } diff --git a/tests/Feature/GenerateClientCommandTest.php b/tests/Feature/GenerateClientCommandTest.php index 5612e2f..cc3e6b2 100644 --- a/tests/Feature/GenerateClientCommandTest.php +++ b/tests/Feature/GenerateClientCommandTest.php @@ -108,6 +108,7 @@ public function it_reports_skir_compiler_failures(): void '--node' => PHP_BINARY, ]) ->expectsOutputToContain('Unable to parse skir.yml.') + ->expectsOutputToContain('Skir generation failed with exit code 1.') ->assertExitCode(1); } diff --git a/tests/Feature/SkirClientServiceProviderTest.php b/tests/Feature/SkirClientServiceProviderTest.php index 147c168..7683c9e 100644 --- a/tests/Feature/SkirClientServiceProviderTest.php +++ b/tests/Feature/SkirClientServiceProviderTest.php @@ -7,6 +7,7 @@ use PHPUnit\Framework\Attributes\Test; use Saloon\Http\Faking\MockClient; use Saloon\Http\Faking\MockResponse; +use Skir\Client\Exceptions\SkirClientException; use Skir\Client\Http\SkirRpcRequest; use Skir\Client\SkirClient; use Skir\Client\Tests\TestCase; @@ -39,4 +40,15 @@ public function it_resolves_a_configured_client_from_the_container(): void return $request->resolveEndpoint() === '/rpc'; }); } + + #[Test] + public function it_requires_a_base_url_when_resolving_the_client(): void + { + config()->set('skir-client.base_url', null); + + $this->expectException(SkirClientException::class); + $this->expectExceptionMessage('Set [skir-client.base_url] (SKIR_CLIENT_BASE_URL) to the SkirRPC server URL.'); + + app(SkirClient::class); + } }