Skip to content

Latest commit

 

History

History
105 lines (73 loc) · 2.87 KB

File metadata and controls

105 lines (73 loc) · 2.87 KB

Getting Started

This guide gets you from an OpenAPI spec to generated PHP models and API code in a few minutes.

It targets teams that want a PHP-only workflow: a small dev dependency, generated code committed or regenerated in CI, and no extra JS codegen toolchain.

1) Install the package

composer require --dev maxbeckers/php-openapi-generator

Allow the Composer plugin in your root composer.json:

{
  "config": {
    "allow-plugins": {
      "maxbeckers/php-openapi-generator": true
    }
  }
}

2) Add php-openapi-generator.php

Create this config file in your project root:

<?php

declare(strict_types=1);

use MaxBeckers\OpenApiGenerator\Config\FrameworkTarget;
use MaxBeckers\OpenApiGenerator\Config\GenerationTarget;
use MaxBeckers\OpenApiGenerator\Config\GeneratorConfig;
use MaxBeckers\OpenApiGenerator\Config\HttpClientAdapter;

$config = new GeneratorConfig();

$config->specFile = 'openapi.yaml';
$config->outputDir = 'generated';

$config->modelNamespace = 'App\\Model';
$config->modelOutputDir = 'Model';

$config->apiNamespace = 'App\\Api';
$config->apiOutputDir = 'Api';

$config->generationTarget = GenerationTarget::Server;
$config->frameworkTarget = FrameworkTarget::None;

// Optional when generating clients:
// $config->httpClient = HttpClientAdapter::Guzzle;

return $config;

3) Generate code

vendor/bin/openapi-gen

4) Pick your target style

  • GenerationTarget::Server: generates API contracts from paths.
  • GenerationTarget::Client: generates typed API client classes.

Useful CLI overrides:

vendor/bin/openapi-gen --target=server --framework=none
vendor/bin/openapi-gen --target=server --framework=symfony
vendor/bin/openapi-gen --target=server --framework=laravel
vendor/bin/openapi-gen --target=client --http-client=symfony
vendor/bin/openapi-gen --target=client --http-client=guzzle
vendor/bin/openapi-gen --target=client --http-client=psr18

5) Integrate generated code

  • Server mode: implement generated *ApiInterface methods.
  • Client mode: inject generated *ApiClient into your services.
  • Model DTOs: use fromArray() for hydration and toArray() for serialization.

If you want a framework-first path, jump to:

6) Regenerate on spec changes

Run generation each time your OpenAPI spec changes. You can run it explicitly with vendor/bin/openapi-gen or rely on the Composer plugin hook in your workflow.

Next Docs