Extending

Without Laravel

Use the pure-PHP core directly, and read the declarations, export and changelog from a generator.

The core of the library has no Laravel dependency. The Laravel layer is a thin wrapper around it: you can project any class implementing Revisioned, and tools can read the same data as the commands.

Declare a class

A class implements Schema\Revisioned itself: its declaration kinds, and the descriptors for a kind and a subject.

use Ark4ne\ApiRevisions\Schema\DeclarationKind;
use Ark4ne\ApiRevisions\Schema\Revisioned;
use Ark4ne\ApiRevisions\Schema\Schema;

final readonly class OrderDeclaration implements Revisioned
{
    public function __construct(public mixed $resource) {}

    public static function declarationKinds(): array
    {
        return [DeclarationKind::Response];
    }

    public static function describeRevisioned(
        DeclarationKind $kind,
        mixed $subject,
    ): array
    {
        return (new self($subject))->toArray();
    }

    public function toArray(): array
    {
        return [
            'title' => Schema::string(),
            'status' => Schema::enum(OrderStatus::class)
                ->was(ApiRevision::Winter25, name: 'state'),
        ];
    }
}

Such a class is compiled, cached, checked, locked and exported like a resource.

Project it

use Ark4ne\ApiRevisions\Compilation\Shapes;
use Ark4ne\ApiRevisions\Projection\Projector;

$shapes = new Shapes();
$shape = $shapes->for(
    OrderDeclaration::class,
    DeclarationKind::Response,
    ApiRevision::Winter25,
);
$data = (new Projector(shapes: $shapes))->project(
    $shape,
    OrderDeclaration::describeRevisioned(DeclarationKind::Response, $order),
    $order,
);

Shapes::for() compiles a shape once and memoizes it.

Render many subjects

A consumer rendering many subjects of one declaration resolves a plan once per render, then runs it per subject:

$projector = new Projector(shapes: $shapes);
$descriptors = OrderDeclaration::describeRevisioned(
    DeclarationKind::Response,
    $orders[0],
);
$plan = $projector->plan($shape, $descriptors, [
    'title',
    'total',
    'address.city',
    'tags.*.name',
]);

foreach ($orders as $order) {
    $rows[] = $plan->run($order);
}

The optional keys are the head keys kept, a sparse fieldset:

  • a dotted path keeps one member of an object (address.city) or of each item of a list (tags.*.name); the members left out are never read, converted or validated;
  • a shorter path wins: address keeps it whole;
  • a path into a field converted at that revision, a list read through a key, a nested resource or a scalar keeps the field whole; a path naming no member is ignored.

The descriptors are built once, so their closures must read the $subject they receive, not the instance they were declared on. Build a plan per render, never keep it: it holds the closures of that declaration.

Reading the declarations

A generator (documentation, SDK) reads the same data as revision:export and revision:changelog:

use Ark4ne\ApiRevisions\Checking\ClassFinder;
use Ark4ne\ApiRevisions\Checking\Declarations;
use Ark4ne\ApiRevisions\Compilation\Shapes;
use Ark4ne\ApiRevisions\Export\Changelog;
use Ark4ne\ApiRevisions\Export\JsonSchemaExporter;

$declarations = new Declarations(new class implements ClassFinder {
    public function find(): array
    {
        return [OrderDeclaration::class];
    }
});

$shapes = new Shapes();
$exporter = new JsonSchemaExporter($shapes);
foreach ($declarations->at(ApiRevision::Winter25) as $class => $kinds) {
    foreach ($kinds as $kind) {
        $schema = JsonSchemaExporter::objects(
            $exporter->export($class, $kind, ApiRevision::Winter25),
        );
    }
}

$markdown = (new Changelog($shapes))->markdown(
    array_keys($declarations->declarations()),
    ApiRevision::Genesis,
    ApiRevision::Winter25,
);
  • Declarations reads its classes from a ClassFinder: list them yourself, or scan your own directories. In a Laravel application, app(Declarations::class) reads the discovery paths and classes of the configuration.
  • Share one Shapes between the exporter and the changelog: each shape is compiled once.
  • Declarations::declarations() lists the discovered classes with their declaration kinds; at($revision) keeps the classes existing at that revision.
  • JsonSchemaExporter::export() returns the schema as arrays; objects() makes it ready for json_encode(), so an empty schema is written {}, never [].
  • Changelog::changes($class, $kind, $revision) returns the structured changes of one revision; markdown() renders them.
  • Export\RegexPattern::of() and Export\DatePattern::of() give the ECMA pattern the exporter writes for a PCRE or a numeric date format, null when there is none.

These classes, the structure printed by revision:export and Changelog::changes() are public contract: from 1.0, they change only in a major version.