psx / api
Parse and generate API specification formats
Fund package maintenance!
chriskapp
Patreon
www.paypal.me/fusioapi
Installs: 140 619
Dependents: 4
Suggesters: 0
Security: 0
Stars: 36
Watchers: 3
Forks: 9
Open Issues: 0
Requires
- php: >=8.1
- psx/json: ^3.0
- psx/openapi: ^1.0
- psx/schema: ^7.0
- symfony/cache: ^5.0|^6.0|^7.0
- symfony/console: ^5.0|^6.0|^7.0
- symfony/property-access: ^5.0|^6.0|^7.0
- symfony/yaml: ^5.0|^6.0|^7.0
- twig/twig: ^2.4|^3.0
Requires (Dev)
- phpunit/phpunit: ^9.0
- vimeo/psalm: ^5.0
- dev-master
- v7.2.5
- v7.2.4
- v7.2.3
- v7.2.2
- v7.2.1
- v7.2.0
- v7.1.6
- v7.1.5
- v7.1.4
- v7.1.3
- v7.1.2
- v7.1.1
- v7.1.0
- v7.0.8
- v7.0.7
- v7.0.6
- v7.0.5
- v7.0.4
- v7.0.3
- v7.0.2
- v7.0.1
- v7.0.0
- 6.x-dev
- v6.2.4
- v6.2.3
- v6.2.2
- v6.2.1
- v6.2.0
- v6.1.9
- v6.1.8
- v6.1.7
- v6.1.6
- v6.1.5
- v6.1.4
- v6.1.3
- v6.1.2
- v6.1.1
- v6.1.0
- v6.0.9
- v6.0.8
- v6.0.7
- v6.0.6
- v6.0.5
- v6.0.4
- v6.0.3
- v6.0.2
- v6.0.1
- v6.0.0
- 5.x-dev
- v5.4.3
- v5.4.2
- v5.4.1
- v5.4.0
- v5.3.7
- v5.3.6
- v5.3.5
- v5.3.4
- v5.3.3
- v5.3.2
- v5.3.1
- v5.3.0
- v5.2.9
- v5.2.8
- v5.2.7
- v5.2.6
- v5.2.5
- v5.2.4
- v5.2.3
- v5.2.2
- v5.2.1
- v5.2.0
- v5.1.1
- v5.1.0
- v5.0.8
- v5.0.7
- v5.0.6
- v5.0.5
- v5.0.4
- v5.0.3
- v5.0.2
- v5.0.1
- v5.0.0
- 4.x-dev
- v4.3.3
- v4.3.2
- v4.3.1
- v4.3.0
- v4.2.4
- v4.2.3
- v4.2.2
- v4.2.1
- v4.2.0
- v4.1.9
- v4.1.8
- v4.1.7
- v4.1.6
- v4.1.5
- v4.1.4
- v4.1.3
- v4.1.2
- v4.1.1
- v4.1.0
- v4.0.9
- v4.0.8
- v4.0.7
- v4.0.6
- v4.0.5
- v4.0.4
- v4.0.3
- v4.0.2
- v4.0.1
- v4.0.0
- 3.x-dev
- v3.0.9
- v3.0.8
- v3.0.7
- v3.0.6
- v3.0.5
- v3.0.4
- v3.0.3
- v3.0.2
- v3.0.1
- v3.0.0
- v3.0.0-RC5
- v3.0.0-RC4
- v3.0.0-RC3
- v3.0.0-RC2
- v3.0.0-RC1
- 2.x-dev
- v2.7.4
- v2.7.3
- v2.7.2
- v2.7.1
- v2.7.0
- v2.6.6
- v2.6.5
- v2.6.4
- v2.6.3
- v2.6.2
- v2.6.1
- v2.6.0
- v2.5.6
- v2.5.5
- v2.5.4
- v2.5.3
- v2.5.2
- v2.5.1
- v2.5.0
- v2.4.9
- v2.4.8
- v2.4.7
- v2.4.6
- v2.4.5
- v2.4.4
- v2.4.3
- v2.4.2
- v2.4.1
- v2.4.0
- v2.3.1
- v2.3.0
- v2.2.6
- v2.2.5
- v2.2.4
- v2.2.3
- v2.2.2
- v2.2.1
- v2.2.0
- v2.1.2
- v2.1.1
- v2.1.0
- v2.0.6
- v2.0.5
- v2.0.4
- v2.0.3
- v2.0.2
- v2.0.1
- v2.0.0
- v1.0.3
- v1.0.2
- v1.0.1
- v1.0.0
- v0.1.4
- v0.1.3
- v0.1.2
- v0.1.1
- v0.1.0
This package is auto-updated.
Last update: 2024-12-07 16:04:53 UTC
README
This library provides an attribute parser to dynamically generate a TypeAPI specification from any controller (code-first). Based on the specification it is then possible to generate client SDKs or an OpenAPI specification.
We provide also a hosted version of this code generator. For more integration options you can also take a look at the SDKgen project which provides a CLI binary or GitHub action to integrate the code generator.
Usage
The root model object is called a Specification
which contains Operations
and Definitions
. Each operation
maps to a specific REST API endpoint and the definitions represent the schemas to describe the JSON request or response
payload.
Framework
You can use PHP attributes to describe the structure of your endpoints. You can then use the attribute parser (PSX\Api\Parser\Attribute
)
to automatically generate a specification for your controller. A controller class could then look like:
<?php class MyController { #[Get] #[Path('/my/endpoint/:id')] public function getModel(#[Param] int $id, #[Query] int $year): \My\Response\Model { // @TODO implement } #[Post] #[Path('/my/endpoint')] public function insertModel(#[Body] \My\Request\Model $model): \My\Response\Model { // @TODO implement } }
This would be enough for the API component to generate either an OpenAPI specification or a client SDK.
Note this library only needs the meta information, if you can get those meta information at your framework in another
way you can also implement a custom ParserInterface
.
Standalone
Beside the framework integration you can use this component also to simply parse existing TypeAPI specification and generate specific output. The following is a simple example how to use the PHP API and how to generate code.
<?php // use the API manager to obtain a specification from different sources $manager = new \PSX\Api\ApiManager(new \PSX\Schema\SchemaManager()); // reads the TypeAPI specification and generates a specification $specification = $manager->getApi('./typeapi.json'); // contains all schema type definitions $definitions = $specification->getDefinitions(); // returns the resource foo from the specification $operation = $specification->getOperations()->get('my.operation'); // returns all available arguments $operation->getArguments(); // returns the return type $operation->getReturn(); // returns all exceptions which are described $operation->getThrows(); // returns the assigned HTTP method $operation->getMethod(); // returns the assigned HTTP path $operation->getPath(); // creates a PHP client which consumes the defined operations $registry = \PSX\Api\GeneratorFactory::fromLocal()->factory(); $generator = $registry->getGenerator(\PSX\Api\Repository\LocalRepository::CLIENT_PHP) $source = $generator->generate($resource);