Skip to content
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
8 changes: 8 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -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
10 changes: 0 additions & 10 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down
8 changes: 4 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
16 changes: 8 additions & 8 deletions docs/error-handling-and-testing.md
Original file line number Diff line number Diff line change
@@ -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:

Expand All @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion docs/laravel-data.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
2 changes: 1 addition & 1 deletion docs/simple-data-objects.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand Down Expand Up @@ -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.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
19 changes: 1 addition & 18 deletions src/Commands/GenerateClientCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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();
}
}
5 changes: 5 additions & 0 deletions src/Exceptions/SkirClientException.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.');
}
}
12 changes: 8 additions & 4 deletions src/SkirClientServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use Illuminate\Support\ServiceProvider;
use Skir\Client\Commands\GenerateClientCommand;
use Skir\Client\Exceptions\SkirClientException;

final class SkirClientServiceProvider extends ServiceProvider
{
Expand All @@ -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'));
});
}

Expand Down
1 change: 1 addition & 0 deletions tests/Feature/GenerateClientCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
12 changes: 12 additions & 0 deletions tests/Feature/SkirClientServiceProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
}
Loading