In Bidirekt, each side of an integration declares in a contract file what it provides and what it consumes. This SDK writes that file from the code of a service on PHP 8.0 or later: you annotate the schemas and the handlers that provide or consume each resource, with attributes or with @bidirekt tags in docblocks, run generate, and get contracts/generated.yaml, ready for bidirekt validate and bidirekt publish. It is the service's only contract file: once a service adopts the SDK, every resource and schema it provides or consumes is annotated in its code.
The SDK needs PHP 8.0 or later and Composer 2.2 or later. Install it as a dev dependency:
composer require --dev bidirekt/php-sdkTo read PHP 8.4 and 8.5 code, the SDK needs nikic/php-parser 5.6.2 or later. If Composer says your lock file fixes an older version (a Laravel project locks it through psy/psysh), run composer require --dev bidirekt/php-sdk -W.
The package holds the four attributes and the generate command. An attribute runs nothing: PHP loads its class only when code calls newInstance() on it, and generate reads the source instead of running it. So a production install with composer install --no-dev still runs your app. generate reads code of any PHP version; two forms need a newer PHP to run your app:
new BidirektField(...)insidefields:needs PHP 8.1, as does an enum. On PHP 8.0, declare a schema without a class with tags.Enum::Case->valueas an attribute argument needs PHP 8.2.
petstore_php is a Slim service. It provides GET /pets/* and POST /pets, and consumes GET /ratings/* from petstore_reviews. #[BidirektSchema] marks each schema and #[BidirektField] each field of the contract, on a property or a promoted constructor parameter; a field without it stays out of the contract. Pet uses each kind of field:
src/Pet.php
<?php
namespace App;
use Bidirekt\PhpSdk\BidirektField;
use Bidirekt\PhpSdk\BidirektSchema;
#[BidirektSchema]
final class Pet
{
/**
* @param list<string> $tags
* @param array<string, string> $notes
*/
public function __construct(
#[BidirektField]
public int $id,
#[BidirektField]
public string $name,
#[BidirektField]
public PetStatus $status,
#[BidirektField]
public Owner $owner,
#[BidirektField]
public array $tags,
#[BidirektField]
public array $notes,
#[BidirektField(type: 'float', description: 'In kilograms')]
public float $weight,
#[BidirektField]
public ?string $birthDate,
#[BidirektField(
union: [
'person' => Owner::class,
'shelter' => Shelter::class,
],
)]
public Owner|Shelter $adoptedFrom,
) {}
#[BidirektField(optional: true)]
public string $nickname;
}src/PetStatus.php
<?php
namespace App;
enum PetStatus: string
{
case Available = 'available';
case Adopted = 'adopted';
}src/Owner.php
<?php
namespace App;
use Bidirekt\PhpSdk\BidirektField;
use Bidirekt\PhpSdk\BidirektSchema;
#[BidirektSchema]
final class Owner
{
public function __construct(
#[BidirektField]
public string $name,
) {}
}src/Shelter.php
<?php
namespace App;
use Bidirekt\PhpSdk\BidirektField;
use Bidirekt\PhpSdk\BidirektSchema;
#[BidirektSchema]
final class Shelter
{
public function __construct(
#[BidirektField]
public string $city,
) {}
}src/NewPet.php
<?php
namespace App;
use Bidirekt\PhpSdk\BidirektField;
use Bidirekt\PhpSdk\BidirektSchema;
#[BidirektSchema]
final class NewPet
{
public function __construct(
#[BidirektField]
public string $name,
) {}
}src/Rating.php
<?php
namespace App;
use Bidirekt\PhpSdk\BidirektField;
use Bidirekt\PhpSdk\BidirektSchema;
#[BidirektSchema]
final class Rating
{
public function __construct(
#[BidirektField]
public float $average,
#[BidirektField]
public int $count,
) {}
}#[BidirektRestProvider] on a handler declares what it provides. Slim routes /pets/{id} to show, and the path is declared /pets/*: a * for each dynamic segment, never the framework's placeholder.
src/PetController.php
<?php
namespace App;
use Bidirekt\PhpSdk\BidirektRestProvider;
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
final class PetController
{
public function __construct(private PetRepository $pets) {}
#[BidirektRestProvider(
method: 'GET',
path: '/pets/*',
responses: [
200 => Pet::class,
404 => null,
],
)]
public function show(
Request $request,
Response $response,
array $args,
): Response {
$pet = $this->pets->find((int) $args['id']);
if ($pet === null) {
return $response->withStatus(404);
}
return self::json($response, $pet);
}
#[BidirektRestProvider(
method: 'POST',
path: '/pets',
request: NewPet::class,
responses: [
201 => Pet::class,
400 => null,
],
)]
public function create(Request $request, Response $response): Response
{
$name = $request->getParsedBody()['name'] ?? null;
if (!is_string($name)) {
return $response->withStatus(400);
}
return self::json($response->withStatus(201), $this->pets->add($name));
}
private static function json(Response $response, Pet $pet): Response
{
$response->getBody()->write(
(string) json_encode($pet, JSON_PRESERVE_ZERO_FRACTION),
);
return $response->withHeader('Content-Type', 'application/json');
}
}#[BidirektRestConsumer] declares what a client consumes, and from which provider:
src/ReviewsClient.php
<?php
namespace App;
use Bidirekt\PhpSdk\BidirektRestConsumer;
use GuzzleHttp\Client;
final class ReviewsClient
{
public function __construct(private Client $http) {}
#[BidirektRestConsumer(
provider: 'petstore_reviews',
method: 'GET',
path: '/ratings/*',
responses: [
200 => Rating::class,
404 => null,
],
)]
public function rating(int $petId): ?Rating
{
$response = $this->http->get(
"/ratings/{$petId}",
['http_errors' => false],
);
$body = json_decode((string) $response->getBody(), true);
if ($response->getStatusCode() === 404) {
return null;
}
return new Rating($body['average'], $body['count']);
}
}Tags say the same in /** */ docblocks, for code that can't use attributes or prefers docblocks. A service can mix both forms, never on the same handler, schema or field, so here Pet and PetController switch to tags and the other classes keep their attributes. A tag names a schema by its name in the contract, and ReviewsClient would declare @bidirekt rest consumes petstore_reviews GET /ratings/* with the same @bidirekt response lines:
src/Pet.php
<?php
namespace App;
/** @bidirekt schema */
final class Pet
{
/**
* @param list<string> $tags
* @param array<string, string> $notes
*/
public function __construct(
/** @bidirekt field */
public int $id,
/** @bidirekt field */
public string $name,
/** @bidirekt field */
public PetStatus $status,
/** @bidirekt field */
public Owner $owner,
/** @bidirekt field */
public array $tags,
/** @bidirekt field */
public array $notes,
/** @bidirekt field type=float description="In kilograms" */
public float $weight,
/** @bidirekt field */
public ?string $birthDate,
/** @bidirekt field union={person: Owner, shelter: Shelter} */
public Owner|Shelter $adoptedFrom,
) {}
/** @bidirekt field optional=true */
public string $nickname;
}src/PetController.php
<?php
namespace App;
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
final class PetController
{
public function __construct(private PetRepository $pets) {}
/**
* @bidirekt rest provides GET /pets/*
* @bidirekt response 200 Pet
* @bidirekt response 404
*/
public function show(
Request $request,
Response $response,
array $args,
): Response {
$pet = $this->pets->find((int) $args['id']);
if ($pet === null) {
return $response->withStatus(404);
}
return self::json($response, $pet);
}
/**
* @bidirekt rest provides POST /pets
* @bidirekt request NewPet
* @bidirekt response 201 Pet
* @bidirekt response 400
*/
public function create(Request $request, Response $response): Response
{
$name = $request->getParsedBody()['name'] ?? null;
if (!is_string($name)) {
return $response->withStatus(400);
}
return self::json($response->withStatus(201), $this->pets->add($name));
}
private static function json(Response $response, Pet $pet): Response
{
$response->getBody()->write(
(string) json_encode($pet, JSON_PRESERVE_ZERO_FRACTION),
);
return $response->withHeader('Content-Type', 'application/json');
}
}Run generate in the project folder, the one with composer.json:
vendor/bin/php-sdk generateEither form writes the same contracts/generated.yaml:
# Generated by bidirekt/php-sdk. Do not edit.
provides:
rest:
/pets:
post:
request: NewPet
responses:
201: Pet
400: ~
/pets/*:
get:
responses:
200: Pet
404: ~
consumes:
petstore_reviews:
rest:
/ratings/*:
get:
responses:
200: Rating
404: ~
schemas:
NewPet:
type: object
properties:
name:
type: string
Owner:
type: object
properties:
name:
type: string
Pet:
type: object
properties:
id:
type: integer
name:
type: string
status:
ref: PetStatus
owner:
ref: Owner
tags:
type: array
items:
type: string
notes:
type: map
values:
type: string
weight:
type: float
description: In kilograms
birthDate:
type: string
nullable: true
adoptedFrom:
type: union
members:
person:
ref: Owner
shelter:
ref: Shelter
nickname:
type: string
optional: true
PetStatus:
type: enum
members: ["available", "adopted"]
Rating:
type: object
properties:
average:
type: number
count:
type: integer
Shelter:
type: object
properties:
city:
type: stringField by field:
status: an enum with string values is arefto anenumschema named after it.owner: a schema class is aref, the nested object.tagsandnotes: anarraytakes the type of its items or values from the PHPDoc, here the constructor's@param:list<string>is a list,array<string, string>a map.weight: declares adescription, andtype: 'float'because the controller encodes withJSON_PRESERVE_ZERO_FRACTION; without it a PHPfloatis anumber, likeRating::$average(Whatjson_encodewrites).birthDate:?stringmakes itnullable.adoptedFrom: a union of schema classes declares the key of each member inunion.nickname: declaresoptional, because the code leaves it unset when the pet has none, andjson_encodeleaves an unset typed property out.
A body with no class to annotate, like an array a handler builds or a Laravel API Resource, declares its schema on the handler: #[BidirektSchema(name: ..., fields: [...])], where name is required and each field is a new BidirektField('<name>', type: ...) with a type or a union. With tags, @bidirekt schema <Name> in the handler's docblock opens a schema and each following @bidirekt field <name> type=... is a field of it, until the next schema or the end of the docblock. new in an attribute needs PHP 8.1; on PHP 8.0, use the tags. These schemas belong to the whole contract, so other handlers and fields reference them by name:
src/StatsController.php
<?php
namespace App;
use Bidirekt\PhpSdk\BidirektField;
use Bidirekt\PhpSdk\BidirektRestProvider;
use Bidirekt\PhpSdk\BidirektSchema;
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
final class StatsController
{
#[BidirektRestProvider(
method: 'GET',
path: '/stats',
responses: [200 => 'PetStats'],
)]
#[BidirektSchema(
name: 'PetStats',
fields: [
new BidirektField('total', type: 'integer'),
new BidirektField('byStatus', type: 'map', values: 'integer'),
],
)]
public function show(Request $request, Response $response): Response
{
$response->getBody()->write(
(string) json_encode([
'total' => 3,
'byStatus' => ['available' => 2, 'adopted' => 1],
]),
);
return $response->withHeader('Content-Type', 'application/json');
}
}src/StatsController.php
<?php
namespace App;
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
final class StatsController
{
/**
* @bidirekt rest provides GET /stats
* @bidirekt response 200 PetStats
*
* @bidirekt schema PetStats
* @bidirekt field total type=integer
* @bidirekt field byStatus type=map values=integer
*/
public function show(Request $request, Response $response): Response
{
$response->getBody()->write(
(string) json_encode([
'total' => 3,
'byStatus' => ['available' => 2, 'adopted' => 1],
]),
);
return $response->withHeader('Content-Type', 'application/json');
}
}# Generated by bidirekt/php-sdk. Do not edit.
provides:
rest:
/stats:
get:
responses:
200: PetStats
schemas:
PetStats:
type: object
properties:
total:
type: integer
byStatus:
type: map
values:
type: integerOn a class, fields: declares the fields of a body the class builds by hand, as in jsonSerialize() or a Laravel API Resource's toArray(). Without name, the schema is named after the class, and these fields come after the ones annotated in the class or its parents. A Laravel API Resource's parent, JsonResource, is read from vendor/, so run composer install before generate.
For GraphQL, generate writes what a service consumes, and the graph of a subgraph. A provider publishes the GraphQL schema its application already has, which the SDK does not write.
#[BidirektGraphqlConsumer] on a method declares a field the service calls: the provider, query or mutation with the field's name, the schema of the arguments it sends and the shape it returns. #[BidirektGraphqlField] takes the place of #[BidirektField] on a field that needs an option of GraphQL: scalar, a custom scalar that replaces the field's type, or arguments, the arguments the call passes to that field. Here petstore_web calls the GraphQL API of petstore_api:
src/PetstoreClient.php
<?php
namespace App;
use Bidirekt\PhpSdk\BidirektField;
use Bidirekt\PhpSdk\BidirektGraphqlConsumer;
use Bidirekt\PhpSdk\BidirektGraphqlField;
use Bidirekt\PhpSdk\BidirektSchema;
#[BidirektSchema]
final class PetArgs
{
#[BidirektField]
public string $id;
}
#[BidirektSchema]
final class ReviewsArgs
{
#[BidirektField]
public int $first;
}
#[BidirektSchema]
final class Review
{
#[BidirektField]
public int $stars;
}
#[BidirektSchema]
final class Pet
{
#[BidirektField]
public string $name;
#[BidirektGraphqlField(scalar: 'DateTime', optional: true)]
public \DateTimeImmutable $bornAt;
/** @var list<Review> */
#[BidirektGraphqlField(arguments: ReviewsArgs::class)]
public array $reviews;
}
final class PetstoreClient
{
#[BidirektGraphqlConsumer(provider: 'petstore_api', query: 'pets', returns: [Pet::class])]
#[BidirektGraphqlConsumer(provider: 'petstore_api', query: 'petCount', returns: 'integer')]
public function pets(): void {}
#[BidirektGraphqlConsumer(
provider: 'petstore_api',
query: 'pet',
arguments: PetArgs::class,
returns: ['nullable' => Pet::class, 'name' => 'MaybePet'],
)]
public function pet(string $id): void {}
}With tags, @bidirekt graphql consumes <provider> query|mutation <name> goes above the handler, followed by @bidirekt arguments <schema> and @bidirekt returns <type>, and @bidirekt graphql field takes the place of @bidirekt field:
src/PetstoreClient.php
<?php
namespace App;
/** @bidirekt schema */
final class PetArgs
{
/** @bidirekt field */
public string $id;
}
/** @bidirekt schema */
final class ReviewsArgs
{
/** @bidirekt field */
public int $first;
}
/** @bidirekt schema */
final class Review
{
/** @bidirekt field */
public int $stars;
}
/** @bidirekt schema */
final class Pet
{
/** @bidirekt field */
public string $name;
/** @bidirekt graphql field scalar=DateTime optional=true */
public \DateTimeImmutable $bornAt;
/**
* @bidirekt graphql field arguments=ReviewsArgs
* @var list<Review>
*/
public array $reviews;
}
final class PetstoreClient
{
/**
* @bidirekt graphql consumes petstore_api query pets
* @bidirekt returns Pet[]
*/
public function pets(): void {}
/**
* @bidirekt graphql consumes petstore_api query petCount
* @bidirekt returns integer
*/
public function petCount(): void {}
/**
* @bidirekt graphql consumes petstore_api query pet
* @bidirekt arguments PetArgs
* @bidirekt returns Pet? as MaybePet
*/
public function pet(string $id): void {}
}generate writes the calls under consumes.petstore_api.graphql. A returns that is not a schema returned as it is becomes a schema of its own: [Pet::class] is PetList, 'integer' is Integer, and ['nullable' => Pet::class, 'name' => 'MaybePet'] is MaybePet:
# Generated by bidirekt/php-sdk. Do not edit.
consumes:
petstore_api:
graphql:
query:
pet:
arguments: PetArgs
returns: MaybePet
petCount:
returns: Integer
pets:
returns: PetList
schemas:
Integer:
type: integer
MaybePet:
ref: Pet
nullable: true
Pet:
type: object
properties:
name:
type: string
bornAt:
type: scalar
name: DateTime
optional: true
reviews:
type: array
items:
ref: Review
arguments: ReviewsArgs
PetArgs:
type: object
properties:
id:
type: string
PetList:
type: array
items:
ref: Pet
Review:
type: object
properties:
stars:
type: integer
ReviewsArgs:
type: object
properties:
first:
type: integerprovider may name a federated graph, and the calls are then checked against the graph its subgraphs compose. A subgraph declares its graph with #[BidirektGraph('petstore_graph')] on a class, or @bidirekt graph petstore_graph above any element, and generate writes it as provides.graphql.graph. A subgraph's contract is that file together with its GraphQL schema: pass both to bidirekt validate and bidirekt publish, as bidirekt publish contracts/ src/schema.graphql --participant petstore_reviews --version 2.0.0. Every form returns takes is in GraphQL returns.
vendor/bin/php-sdk generate
vendor/bin/php-sdk generate --check
vendor/bin/php-sdk generate --project apps/api --out apps/api/contracts/generated.yaml--checkwrites nothing and exits with 1 when the file on disk differs from whatgeneratewould write (\r\ncounts as\n):"contracts/generated.yaml" is out of date: run generate without --check, or when it doesn't exist:"contracts/generated.yaml" does not exist: run generate without --check. Use it in CI.--project <folder>reads another project, and writes to its folder.--out <file>writes to another file.
contracts/generated.yaml is the service's only contract file. Commit it with the code, regenerate it whenever an annotation or an annotated type changes, and never edit it by hand: the next generate writes over it. From there the flow is the same as with a hand-written contract:
vendor/bin/php-sdk generate
bidirekt validate
bidirekt validate --participant petstore_php --environment production
bidirekt publish --participant petstore_php --version 1.4.0
bidirekt can-i-deploy petstore_php --version 1.4.0 --environment production
# deploy
bidirekt record-deployment petstore_php --version 1.4.0 --environment productionWithout file arguments, validate and publish read the contracts/ folder of the current directory, which needs the Bidirekt CLI 0.5.0 or later. validate without --participant and --environment checks only the file; with them, it also compares the file with what is deployed in that environment.
generate reads the source and never runs your app. With a composer.json, it reads every .php file of the folder except vendor/ (or the config.vendor-dir), node_modules/, hidden folders and the paths of autoload-dev; without one, every .php file except the same folders. It reads vendor/ only to find a parent, a trait, an enum or a constant your annotated code uses, through Composer's class maps (vendor/composer/autoload_psr4.php, autoload_namespaces.php, autoload_classmap.php) and vendor/composer/installed.json, from the composer.json closest to the project, going up as Composer does in a monorepo but never to your home folder or above. It loads those maps with require, since they only return arrays, and never loads vendor/autoload.php nor the autoload.files. A package installed from a Composer path repository is read as your code. A file that never mentions bidirekt is parsed only when your annotated code uses a class it declares; a syntax error stops generate in a file with annotations or in a file it parses for that reason.
The file starts with # Generated by bidirekt/php-sdk. Do not edit. and has the provides, consumes and schemas sections that have content: resources by path, method and status, schemas in alphabetical order, fields and members in the order of the code. Every schema is written, even one no handler references.
Each error goes to stderr as file:line message, with the file relative to the current folder, sorted by file and line; on an error nothing is written and the exit code is 1. A warning is printed as file:line warning: message in the same list and doesn't change the exit code. A project with no annotation writes nothing and exits with 1.
generate recognizes an attribute by its full name, Bidirekt\PhpSdk\BidirektField and the others, after it resolves the file's use, alias and namespace; a #[BidirektField] without its use is an error. Written without names, the arguments follow the order below.
| Attribute | Goes on | Arguments |
|---|---|---|
#[BidirektSchema] |
a class, an enum with string values, or a handler (without a class) | name, fields, graphqlType |
#[BidirektField] |
a property or a promoted constructor parameter, or new BidirektField(...) in fields: |
name, type, items, values, optional, nullable, description, union |
#[BidirektRestProvider] |
a method, a function, a closure or an arrow function | method, path, responses, request |
#[BidirektRestConsumer] |
a method, a function, a closure or an arrow function | provider, method, path, responses, request |
#[BidirektGraphqlConsumer] |
a method, a function, a closure or an arrow function | provider, query, mutation, arguments, returns, see GraphQL returns |
#[BidirektGraphqlField] |
a property or a promoted constructor parameter, in place of #[BidirektField] |
the arguments of #[BidirektField], scalar, arguments |
#[BidirektGraph] |
a class | name, the graph the service is a subgraph of |
An argument is read from the code, never run: a literal, an array, X::class, a class constant (also from a class or interface in vendor/), or Enum::Case->value, which PHP accepts in an attribute from 8.2. A status is an integer or a class constant that holds one, like StatusCodeInterface::STATUS_OK. Anything else, like a concatenation or a global constant, stops generate, as does a misspelled argument name or a missing required one.
#[BidirektField(...)] and @bidirekt field key=value ... take the same options:
| Attribute | Tag | What it declares |
|---|---|---|
name: 'birth_date' |
name=birth_date |
the field's name in the contract; without it, the property's name |
type: 'float' |
type=float |
the whole type of the field, see Types |
items: 'Owner' |
items=Owner |
the type of the items of type: 'array' |
values: 'string' |
values=string |
the type of the values of type: 'map' |
optional: true |
optional=true |
whether the field can be absent; without it, it can't |
nullable: true |
nullable=true |
whether the field can be null; without it, from ?T or null in the type |
description: 'In kilograms' |
description="In kilograms" |
the field's description, ASCII letters and digits with single spaces between words; a plain docblock is not one |
union: ['person' => Owner::class, 'shelter' => Shelter::class] |
union={person: Owner, shelter: Shelter} |
the key of each member of a union of schemas |
A promoted constructor parameter is a field like a property. An annotation on a parameter that is not promoted, or on a static property, is not read, and generate warns.
In a GraphQL contract, #[BidirektGraphqlField] takes the place of #[BidirektField] on a field that needs GraphQL's own options, and @bidirekt graphql field that of @bidirekt field. They take the options above, plus scalar: 'DateTime', a custom scalar or a built-in one (ID, String, Int, Float, Boolean) that replaces the field's type at the bottom of its lists and is written as type: scalar, name: DateTime, and arguments: ReviewsArgs::class, the schema of the arguments the call passes to the field, as first in reviews(first: 10). In a tag, scalar=DateTime and arguments=ReviewsArgs. A field takes one of the two attributes, never both.
A handler declares one resource: its method in uppercase (GET, POST, PUT, PATCH, DELETE) and its path as the contract writes it, with a * for each dynamic segment. generate never reads a framework's routes, and writes the path as you declare it; bidirekt validate rejects a {id}. The request and each response status take a body:
| Body | Attribute | Tag | Contract |
|---|---|---|---|
| a schema class | Pet::class |
Pet |
Pet |
| a schema by its name | 'PetStats' |
PetStats |
PetStats |
| a list | [Pet::class] |
Pet[] |
PetList |
| a list with its own name | ['list' => Pet::class, 'name' => 'PetCollection'] |
Pet[] as PetCollection |
PetCollection |
| no body | null |
nothing after the status or request |
~ |
Without a request (no request argument, no @bidirekt request), the resource has no request key. A status written twice (200 and '200', which PHP keeps as one) or with a leading zero (0201) stops generate. A list is written once however many handlers use it. The same method and path provided by two handlers stops generate; a handler written in a trait or a parent class counts once. The same resource consumed by several handlers is written once with the statuses of all of them, which must declare the same request and the same schema for a status they share.
returns is the shape the call reads, and arguments the schema of the arguments it sends, a schema class or a schema name:
| Returns | Attribute | Tag | Contract |
|---|---|---|---|
| a schema | Pet::class or 'Pet' |
Pet |
Pet |
| a schema or null | ['nullable' => Pet::class] |
Pet? |
PetOrNull |
| a list | [Pet::class] or ['list' => Pet::class] |
Pet[] |
PetList |
| a list or null | ['list' => Pet::class, 'nullable' => true] |
Pet[]? |
PetListOrNull |
| a list of items or null | ['list' => Pet::class, 'nullableItems' => true] |
Pet?[] |
PetOrNullList |
| a primitive | 'boolean' |
boolean |
Boolean |
| a named primitive | ['type' => 'boolean', 'name' => 'Flag'] |
boolean as Flag |
Flag |
| a custom scalar | ['scalar' => 'DateTime'] |
scalar:DateTime |
DateTime |
| an enum | ['enum' => ['AVAILABLE', 'SOLD'], 'name' => 'Status'] |
enum:AVAILABLE|SOLD as Status |
Status |
| a union | ['union' => ['Pet' => Pet::class, 'Owner' => Owner::class], 'name' => 'SearchResult'] |
union={Pet: Pet, Owner: Owner} as SearchResult |
SearchResult |
A primitive, a scalar, an enum or a union also takes the place of the schema in a nullable or a list (['nullable' => 'integer'], 'string[]', scalar:DateTime?[]). Each key of a union is the name of one of its types in the provider's GraphQL schema, and its value the schema the call reads from that type. Each form but a schema returned as it is becomes a schema of the contract, with the name above, and name in the attribute or as <name> in the tag gives it another one. They name only a schema generate creates: @bidirekt returns Pet as MyPet stops generate; to call it MyPet, name the schema itself. An enum or a union in a nullable or a list is a schema of its own, under the name it declares, and the nullable or the list references it under the name above: ['nullable' => ['enum' => ['AVAILABLE', 'SOLD'], 'name' => 'Status']] and enum:AVAILABLE|SOLD? as Status both write Status and StatusOrNull. A scalar in a nullable or a list takes no name of its own: the name goes on the nullable or the list. GraphQL's own names of the built-in scalars, as Int or Boolean, are not schemas: write integer or boolean, which accept any GraphQL type of that shape, or ['scalar' => 'ID'] and scalar:ID to send that exact GraphQL type, written as type: scalar, name: ID.
graphqlType on #[BidirektSchema], or graphqlType=PetInput on @bidirekt schema, declares the GraphQL type a class or an enum is sent or read as, written as name: PetInput on its schema: the check then breaks when the provider's type has another name. Without it, a type the provider renames with the same shape doesn't break. The broker takes it on an object or an enum that only GraphQL reaches, and never on the schema of arguments itself, which is no GraphQL type: put it on the input objects inside.
The same field called by several handlers is written once. A handler that declares other arguments or returns adds them: one schema is written as its name, several as a list in alphabetical order, with ~ first for a handler that sends no arguments, and the broker checks each one as if each handler were alone:
pet:
arguments: [~, PetArgs]
returns: [PetWithName, PetWithStatus]A tag is @bidirekt <keyword> at the start of a line of a /** */ docblock, with its arguments separated by spaces:
| Tag | Goes |
|---|---|
@bidirekt schema [name=PetResponse] [graphqlType=Pet] |
above a class or an enum with string values |
@bidirekt field [key=value ...] |
above a property, or a promoted constructor parameter |
@bidirekt schema <Name> and @bidirekt field <name> type=... |
in a handler's docblock (without a class) |
@bidirekt rest provides <METHOD> <path> |
above a method, a function, a closure, or a route call like $app->get(...) or Route::get(...) |
@bidirekt rest consumes <provider> <METHOD> <path> |
the same |
@bidirekt request [schema] |
after rest provides or rest consumes; with no schema, request: ~ |
@bidirekt response <status> [schema] |
after rest provides or rest consumes, one per status |
@bidirekt graphql consumes <provider> query|mutation <name> |
the same as rest provides |
@bidirekt arguments <schema>, @bidirekt returns <type> |
after graphql consumes, see GraphQL returns |
@bidirekt graphql field [key=value ...] |
above a property, in place of field, see Fields |
@bidirekt graph <name> |
above a class or any other element: the graph the service is a subgraph of |
- Only a
descriptionvalue is quoted, and it holds ASCII letters and digits with single spaces between words, as in the attribute; only aunion=value is grouped in{...}(spaces inside it are optional); quotes or braces anywhere else stopgenerate.items=andvalues=take a primitive or a schema name: a tag doesn't nest, so a list of lists needs the PHPDoc. - In a path, write a
*followed by another segment as*\/(/pets/*\/photos): a bare*/would end the docblock.generatereads*\/as*/. - A docblock declares at most one handler. It goes above the element and above its attributes, which is where PHP attaches it; tags in two docblocks on the same element stop
generate. /* */,//and#comments are not read, nor a@bidirektlater in a line, in a fenced block or in an@example.generatewarns about each tag it leaves unread, a docblock it can't attach to an element, and a misspelled@Bidirekt. Other tags in the docblock (@param,@var,@OA\Get) are left alone.
Other tools that read docblocks need to leave @bidirekt alone:
doctrine/annotations(Symfony annotation routing, Doctrine ORM 2, serializer or validator annotations) stops at@bidirektas an annotation that "was never imported". Ignore it with\Doctrine\Common\Annotations\AnnotationReader::addGlobalIgnoredName('bidirekt');in your app's bootstrap, or with@IgnoreAnnotation("bidirekt")in a class docblock.- PHP-CS-Fixer's
phpdoc_to_commentrule, in the@Symfonyand@PhpCsFixersets and in Pint'ssymfonypreset, turns a/** */above a statement like$app->get(...)into a/* */, whichgeneratedoes not read. Keep it with'phpdoc_to_comment' => ['ignored_tags' => ['bidirekt']]in.php-cs-fixer.php, or the same rule config inpint.json.
A field's type comes from PHP. The PHPDoc is read only where PHP can't say the type: the items of an array, and a property with no type. It is the property's @var or, on a promoted constructor parameter, its own @var or the constructor's @param, which must agree. A class name in the PHPDoc resolves through the file's use and namespace.
| PHP | Contract |
|---|---|
string, bool, int |
string, boolean, integer |
float, int|float |
number; float only when declared |
?T, T|null |
nullable: true |
| an enum with string values | ref to an enum schema named after it, its values quoted |
| an enum with int values | integer |
list<T>, T[] (PHPDoc) |
type: array |
array<string, T> (PHPDoc) |
type: map |
list<T|null>, array<string, T|null> (PHPDoc) |
nullable: true on the items or the values |
a class with #[BidirektSchema] or @bidirekt schema |
ref |
self, static |
ref to the class itself |
a union of schema classes, A|B, A|B|null |
type: union, with the keys from union |
optional comes only from a declaration: a PHP property is never optional. The keys of union match the type: one key for each schema of the union, and no key for a schema outside it; a union in the PHPDoc items or values takes union the same way.
A declared type is the whole type of the field, whatever PHP says, in the contract's words: a primitive (string, integer, float, number, boolean), the name of a schema, a class (Owner::class), 'array' with items, or 'map' with values (its keys are always strings). In an attribute, items and values take the same, or a PHP array with the keys type, items, values, nullable and union to nest, as in items: ['union' => ['person' => Owner::class, 'shelter' => 'Shelter']], where a member is a class or a schema name. A short form like 'Owner[]' or 'map<Owner>' stops generate, which shows the long one. optional and nullable still come from PHP when not declared, and a declared one wins.
A type with no single contract type stops generate at its line, and you declare a type or a schema: mixed, object, iterable, callable, void, never, null alone, a union of primitives like int|string, an intersection A&B, a union of a class and a primitive, an array without the type of its items, array<int, T>, an array shape array{...}, an enum without values, a class that is not a schema, a date like \DateTimeInterface, a generic like \ArrayObject<int, Pet>, a property with neither a PHP type nor a @var, and a union of schemas without union.
A schema is named after its class or enum; name gives it another name in the contract. An anonymous class has no name to fall back to, so its schema needs one: new #[BidirektSchema(name: 'Pet')] class { ... }, or /** @bidirekt schema name=Pet */ above class. A list body is named <Schema>List unless it declares its own name. A schema name in a handler or a tag is the name in the contract, which is not always the class's name.
An enum with string values used by a field is written as a schema; #[BidirektSchema] or @bidirekt schema on the enum names it and writes it even when no field uses it. On an enum without string values, an interface or a trait, it stops generate, as does fields: on an enum. A schema without a field stops generate. A schema class can reference a tagged class, and the other way round.
The contract describes what the code does, not the type the code declares. The PHP type is where each field starts, and the field's options apply the rule: a declared type, optional or nullable wins over the PHP type. In the Quickstart, weight declares type: 'float' because the code encodes it with JSON_PRESERVE_ZERO_FRACTION, and nickname declares optional: true because the code leaves it unset. In the same way:
- A
?string $namewhose database column allowsnull, but which the service always fills, declares#[BidirektField(nullable: false)](nullable=false). - A
\DateTimeImmutable $birthDatethat the body carries as a string declares#[BidirektField(type: 'string')](type=string).
PHP writes a body with json_encode, which changes three things the contract can't see:
- A
floatwith no fraction, like50.0, is written as50, so afloatfield isnumber. WithJSON_PRESERVE_ZERO_FRACTION,json_encodewrites50.0; then declare the fieldtype: 'float'(type=float), asPet::$weightdoes. - An empty PHP array,
[], is written as[]even when the field is amap, whose empty value is{}. Encode maps withJSON_FORCE_OBJECTor as astdClass, or leave an empty map out of the body and declare the fieldoptional. - Laravel wraps an API Resource in
datawhen it sends it. Describe the whole body, withdataas a field (a handler schemaPetEnvelopewithnew BidirektField('data', type: 'Pet')), or turn the wrapping off withJsonResource::withoutWrapping().
A schema class takes the annotated fields of its parent classes, whether or not they are schemas, and of the traits it uses: the parent's fields first, then the class's in the order of the code, with a trait's at its use and the promoted parameters at the constructor. A field the class declares again replaces the parent's in the same place. A parent or a trait from a package is read from vendor/, so run composer install before generate.
generate stops at a mistake and prints where it is and how to fix it. The ones you meet most:
cannot translate "mixed" to a contract type: declare it with #[BidirektField(type: …)]cannot translate "array" without the type of its items: write @var list<T> or array<string, T> in the docblock above the property and its attributes, or declare it with #[BidirektField(type: 'array', items: …)]cannot translate class "Address": add #[BidirektSchema] to itcannot translate union of classes "Owner|Shelter": declare each member's key with #[BidirektField(union: …)]schema "PetCollection" is not declared: reference a schema by its name in the contractschema name "Pet" is declared more than once: give each schema a distinct namemethod "get" must be uppercase: write "GET"#[BidirektRestProvider] and "@bidirekt rest provides GET /pets/*" are both on this handler: keep one#[BidirektField] resolves to "App\BidirektField", so it is not read: write use Bidirekt\PhpSdk\BidirektFieldcannot find class "Acme\Model\Entity" in the project, and the vendor folder is not installed: run composer install
With tags, the hint names the tag instead, like declare it with "@bidirekt field type=<type>".
Read CONTRIBUTING.md. With PHP 8.0, the version CI uses, install the dependencies and run the checks from the repository root before you open a pull request:
composer install
composer check
composer lint
composer testcomposer test also runs tests/ReadmeTest.php, which runs every example between ## Quickstart and ## The generate command through generate and compares the result with the YAML or the lines this README shows. An example is the fenced blocks right below a line with only the file's path in backticks, followed by a yaml block (contracts/generated.yaml) and a text block (stderr), or one of them; the blocks of one file repeated under a heading are the attribute and the tag forms of the same example.
MIT, see LICENSE.