Open API PHP8 skeleton client generator

v1.0.0 2024-05-23 13:34 UTC

This package is auto-updated.

Last update: 2024-05-23 13:35:33 UTC


Latest Version on Packagist Software License Build Status Quality Score

OpenApi PHP8 client generator

This library is designed to generate PHP8 model and operation classes from an Open API json file.


Install using Composer :

$ composer require prometee/openapi-client-generator



$loader = require_once( __DIR__.'/vendor/autoload.php');

use Prometee\PhpClassGenerator\Builder\ClassBuilder;
use Prometee\PhpClassGenerator\Builder\Model\ModelFactoryBuilder;
use Prometee\PhpClassGenerator\Builder\View\ViewFactoryBuilder;
use Prometee\SwaggerClientGenerator\OpenApi\Helper\ModelHelper;
use Prometee\SwaggerClientGenerator\OpenApi\Helper\OperationsHelper;
use Prometee\SwaggerClientGenerator\PhpGenerator\Converter\ModelConverter;
use Prometee\SwaggerClientGenerator\PhpGenerator\Converter\OpenApiConverter;
use Prometee\SwaggerClientGenerator\PhpGenerator\Converter\OperationsConverter;
use Prometee\SwaggerClientGenerator\PhpGenerator\PhpGenerator;
use Prometee\SwaggerClientGenerator\PhpGenerator\Operation\OperationsMethodGenerator;

$baseUri = '';
$folder = __DIR__ . '/build';
$namespace = 'Tests\\Prometee\\SwaggerClientGenerator\\Build';
$abstractOperationClass = \MyVendor\MyApi\AbstractOperations::class;
$throwClasses = [
    \MyVendorMyApi\piException => 'ApiException',
    \Http\Client\Exception => 'HttpClientException',
    \Symfony\Component\Serializer\Exception\ExceptionInterface => 'SerializerExceptionInterface',

// Instantiate the PHP class generator.
// It builds PHP classes from a given array config.
$phpGenerator = new PhpGenerator(
    new ClassBuilder(
        new ModelFactoryBuilder(),
        new ViewFactoryBuilder()

// Instantiate the Open API model converter.
// It will convert model definitions to an array of config for the PhpGenerator.
$modelConverter = new ModelConverter(
    new ModelHelper()

// Instantiate the Open API operations converter.
// It will create array of config for the PhpGenerator to create Operations classes.
// They will contain for example each "GET /pets" "GET /pets/{id}" methods to query the API. 
$operationsConverter = new OperationsConverter(
    $namespace . "\\" . $modelConverter->getModelNamespacePrefix(),
    new OperationsHelper(),
    new OperationsMethodGenerator()

// 0.1. Configure the PHP Generator
$phpGenerator->configure($path, $namespace);
// 0.2. Configure the Operations classes with some default extending class
// 0.3. Configure the Operations classes with some PHPDoc @throw class on each generated methods

// 1. Convert OpenApi schema to an array understandable by the PhpGenerator
$openApiConverter = new OpenApiConverter($baseUri, $modelConverter, $operationsConverter);
$classConfig = $openApiConverter->convert();

// 2. Create PHP classes thank to the config given