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
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
"php": "^8.4",
"laravel/framework": "^13.0",
"nikic/php-parser": "^5.8",
"php-skir/runtime": "^0.2@dev",
"php-skir/runtime": "^0.2.1@dev",
"symfony/process": "^7.4|^8.0"
},
"require-dev": {
Expand Down
2 changes: 1 addition & 1 deletion docs/routing.md
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,7 @@ Calling `studio()` explicitly enables Studio for that endpoint, regardless of `s

The Studio app reads the endpoint's methods from a GET request to `/api/skir?list`. The answer follows the upstream Skir services: every method with its name, number, and the type descriptors of its request and response. The list is available only while Studio is enabled for the endpoint; otherwise it answers `404`.

The PHP runtime types carry no record names, so the names in the list are derived. A method's request and response are named `<Method>Request` and `<Method>Response`, and a nested struct or enum is named after the field or variant that holds it. Structs and enums with the same shape share one definition. These names only label the Studio forms; they do not affect the wire format.
Records are listed under the name the schema gives them, with their doc comments, when the generated types carry that information: regenerate with a generator built on `@php-skir/generator-core` 0.3 or later. Types without it get derived names: a method's request and response are named `<Method>Request` and `<Method>Response`, a nested struct or enum is named after the field or variant that holds it, and structs and enums with the same shape share one definition. Names only label the Studio forms; they do not affect the wire format.

Studio sends its calls from the browser as `{"method": ..., "request": ...}` POST requests, with the `Authorization` header you enter in the app. Outer route middleware applies to the Studio page, the method list, and every call.

Expand Down
46 changes: 33 additions & 13 deletions src/Studio/MethodList.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Skir\Server\Studio;

use Skir\Runtime\MethodDescriptor;
use Skir\Server\RegisteredProcedure;
use Skir\Server\SkirServer;

Expand All @@ -16,28 +17,47 @@ final class MethodList
* method: string,
* number: int,
* request: array<string, mixed>,
* response: array<string, mixed>
* response: array<string, mixed>,
* doc?: string
* }>
* }
*/
public function toArray(SkirServer $server): array
{
return [
'methods' => array_values(array_map(
fn (RegisteredProcedure $procedure): array => [
'method' => $procedure->descriptor->name,
'number' => $procedure->descriptor->number,
'request' => (new TypeDescriptor)->describe(
$procedure->descriptor->requestType,
"{$procedure->descriptor->name}Request",
),
'response' => (new TypeDescriptor)->describe(
$procedure->descriptor->responseType,
"{$procedure->descriptor->name}Response",
),
],
fn (RegisteredProcedure $procedure): array => $this->method($procedure->descriptor),
$server->procedures(),
)),
];
}

/**
* @return array{
* method: string,
* number: int,
* request: array<string, mixed>,
* response: array<string, mixed>,
* doc?: string
* }
*/
private function method(MethodDescriptor $descriptor): array
{
$method = [
'method' => $descriptor->name,
'number' => $descriptor->number,
'request' => (new TypeDescriptor)->describe($descriptor->requestType, "{$descriptor->name}Request"),
'response' => (new TypeDescriptor)->describe($descriptor->responseType, "{$descriptor->name}Response"),
];

if ($descriptor->doc === null) {
return $method;
}

if ($descriptor->doc === '') {
return $method;
}

return [...$method, 'doc' => $descriptor->doc];
}
}
56 changes: 46 additions & 10 deletions src/Studio/TypeDescriptor.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@
* Describes a runtime type in the JSON format of Skir's type descriptors: a type signature plus the
* definition of every struct and enum it refers to.
*
* Runtime types carry no record names, so names are derived: the root is named by the caller, and a nested
* record is named after the field or variant that holds it. Records with the same shape share one definition.
* A record is identified by the id its generator gave it. Types built without one get a derived name: the
* root is named by the caller, a nested record after the field or variant that holds it, and records with
* the same shape share one definition.
*/
final class TypeDescriptor
{
Expand Down Expand Up @@ -69,6 +70,10 @@ private function signature(Type $type, string $name): array

private function recordId(Type $type, string $name): string
{
if ($type->recordId !== null) {
return $this->namedRecordId($type, $type->recordId);
}

$shape = serialize($type);

if (isset($this->recordIdsByShape[$shape])) {
Expand All @@ -87,6 +92,20 @@ private function recordId(Type $type, string $name): string
return $recordId;
}

private function namedRecordId(Type $type, string $recordId): string
{
if (isset($this->records[$recordId])) {
return $recordId;
}

$this->records[$recordId] = [];
$this->records[$recordId] = $type->kind === TypeKind::Struct
? $this->structDefinition($type, $recordId)
: $this->enumDefinition($type, $recordId);

return $recordId;
}

private function uniqueRecordId(string $name): string
{
$studlyName = Str::studly($name);
Expand Down Expand Up @@ -117,11 +136,11 @@ private function structDefinition(Type $type, string $recordId): array
$fields[] = $this->fieldDefinition($field);
}

$definition = [
$definition = $this->withDoc([
'kind' => 'struct',
'id' => $recordId,
'fields' => $fields,
];
], $type->doc);

if ($removedNumbers !== []) {
$definition['removed_numbers'] = $removedNumbers;
Expand All @@ -135,11 +154,11 @@ private function fieldDefinition(Field $field): array
{
$name = (string) $field->name;

return [
return $this->withDoc([
'name' => $name,
'number' => $field->number,
'type' => $this->signature($field->type ?? Type::bool(), $name),
];
], $field->doc);
}

/** @return array<string, mixed> */
Expand All @@ -156,20 +175,20 @@ private function enumDefinition(Type $type, string $recordId): array
$variants[] = $this->variantDefinition($variant);
}

return [
return $this->withDoc([
'kind' => 'enum',
'id' => $recordId,
'variants' => $variants,
];
], $type->doc);
}

/** @return array<string, mixed> */
private function variantDefinition(Variant $variant): array
{
$definition = [
$definition = $this->withDoc([
'name' => $variant->name,
'number' => $variant->number,
];
], $variant->doc);

if ($variant->payloadType !== null) {
$definition['type'] = $this->signature($variant->payloadType, $variant->name);
Expand All @@ -178,6 +197,23 @@ private function variantDefinition(Variant $variant): array
return $definition;
}

/**
* @param array<string, mixed> $definition
* @return array<string, mixed>
*/
private function withDoc(array $definition, ?string $doc): array
{
if ($doc === null) {
return $definition;
}

if ($doc === '') {
return $definition;
}

return [...$definition, 'doc' => $doc];
}

private function itemType(Type $type): Type
{
return $type->itemType ?? Type::bool();
Expand Down
60 changes: 60 additions & 0 deletions tests/Feature/StudioMethodListTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,66 @@ public function records_with_the_same_shape_share_one_definition_and_clashing_na
);
}

#[Test]
public function it_uses_the_schema_names_and_doc_comments_when_types_carry_them(): void
{
$status = Type::enum(
[Variant::constant('pending', 1, doc: 'Not answered yet.'), Variant::constant('accepted', 2)],
recordId: 'invitations/invitations.skir:Invitation.Status',
doc: 'Where an invitation stands.',
);
$invitation = Type::struct(
[
Field::value('status', 0, $status, doc: 'Changes when the invitee answers.'),
Field::value('previous_status', 1, $status),
],
recordId: 'invitations/invitations.skir:Invitation',
doc: 'One invitation.',
);

$server = new SkirServer(new ProcedureRegistry);
$server->addMethod(
new MethodDescriptor('GetInvitation', 7003, Type::string(), $invitation, doc: 'Looks up an invitation.'),
fn (string $request): array => [],
);

$method = (new MethodList)->toArray($server)['methods'][0];

$this->assertSame('Looks up an invitation.', $method['doc']);
$this->assertSame([
'type' => ['kind' => 'record', 'value' => 'invitations/invitations.skir:Invitation'],
'records' => [
[
'kind' => 'struct',
'id' => 'invitations/invitations.skir:Invitation',
'fields' => [
[
'name' => 'status',
'number' => 0,
'type' => ['kind' => 'record', 'value' => 'invitations/invitations.skir:Invitation.Status'],
'doc' => 'Changes when the invitee answers.',
],
[
'name' => 'previous_status',
'number' => 1,
'type' => ['kind' => 'record', 'value' => 'invitations/invitations.skir:Invitation.Status'],
],
],
'doc' => 'One invitation.',
],
[
'kind' => 'enum',
'id' => 'invitations/invitations.skir:Invitation.Status',
'variants' => [
['name' => 'pending', 'number' => 1, 'doc' => 'Not answered yet.'],
['name' => 'accepted', 'number' => 2],
],
'doc' => 'Where an invitation stands.',
],
],
], $method['response']);
}

private function inviteDescriptor(): MethodDescriptor
{
return new MethodDescriptor(
Expand Down
Loading