Getting Started

Installation

If you use the API Platform distribution, the Schema Generator is already installed as a development dependency of your project and can be invoked through Docker:

API プラットフォーム ディストリビューションを使用する場合、スキーマ ジェネレーターはプロジェクトの開発依存関係として既にインストールされており、Docker から呼び出すことができます。

docker compose exec php \
    vendor/bin/schema

The Schema Generator can also be downloaded independently as a PHAR or installed in an existing project using Composer:

Schema Generator は、PHAR として個別にダウンロードするか、Composer を使用して既存のプロジェクトにインストールすることもできます。

composer require --dev api-platform/schema-generator

Configuration

The Schema Generator can either be used with Schema.org types (see model scaffolding) or with an OpenAPI documentation (see OpenAPI generation).

スキーマ ジェネレーターは、Schema.org 型 (モデルのスキャフォールディングを参照) または OpenAPI ドキュメント (OpenAPI 生成を参照) で使用できます。

Choose your preferred way of designing your API, and run the generator!

API を設計するお好みの方法を選択し、ジェネレーターを実行してください!

Model Scaffolding

Start by browsing Schema.org (or any other RDF vocabulary) and pick types applicable to your application. Schema.org provides tons of schemas including (but not limited to) representations of people, organizations, events, postal addresses, creative work and e-commerce structures. Many other open vocabularies can be found on the LOV website.

Schema.org (またはその他の RDF ボキャブラリ) を閲覧することから始めて、アプリケーションに適用可能なタイプを選択します。Schema.org は、人、組織、イベント、住所、創作活動、および-商業構造。LOV Web サイトには、他にも多くのオープン語彙があります。

Then, write a simple YAML config file similar to the following.

次に、次のような単純な YAML 構成ファイルを作成します。

Here we will generate a data model for an address book with the following data:

ここでは、次のデータを使用してアドレス帳のデータ モデルを生成します。

  • a Person which inherits from Thing;
    Thing を継承する Person。
  • a PostalAddress (without its class hierarchy).
    PostalAddress (クラス階層なし)。
# api/config/schema.yaml
# The list of types and properties we want to use
types:
    # Parent class of Person
    Thing:
        properties:
            name: ~
    Person:
        # Enable the generation of the class hierarchy (not enabled by default)
        parent: ~
        properties:
            familyName: ~
            givenName: ~
            additionalName: ~
            address: ~
    PostalAddress:
        properties:
            # Force the type of the addressCountry property to text
            addressCountry: { range: "Text" }
            addressLocality: ~
            addressRegion: ~
            postOfficeBoxNumber: ~
            postalCode: ~
            streetAddress: ~

Note: If no properties are specified for a given type, all its properties will be generated.

注: 特定のタイプにプロパティが指定されていない場合、そのすべてのプロパティが生成されます。

The generator also supports enumeration generation. For subclasses of Enumeration, the generator will automatically create a class extending the Enum type provided by myclabs/php-enum. Don't forget to install this library in your project. Refer you to PHP Enum documentation to see how to use it. The Symfony validation annotation generator automatically takes care of enumerations to validate choices values.

ジェネレーターは、列挙型の生成もサポートしています。 Enumeration のサブクラスの場合、ジェネレーターは、myclabs/php-enum によって提供される Enum タイプを拡張するクラスを自動的に作成します。このライブラリーをプロジェクトにインストールすることを忘れないでください。使用方法については、PHP Enum のドキュメントを参照してください。Symfony の検証アノテーション ジェネレーターは、選択肢の値を検証するための列挙を自動的に処理します。

A config file generating an enum class:

列挙型クラスを生成する構成ファイル:

types:
    OfferItemCondition: # The generator will automatically guess that OfferItemCondition is subclass of Enum
        properties: {} # Remove all properties of the parent class

OpenAPI Generation

Design your API with tools like Stoplight.

Stoplight などのツールを使用して API を設計します。

Export your OpenAPI documentation to a JSON or to a YAML file and place it somewhere in your project (for instance in api/openapi.yaml).

OpenAPI ドキュメントを JSON または YAML ファイルにエクスポートし、プロジェクトのどこかに配置します (たとえば、api/openapi.yaml など)。

Write the following config file:

次の構成ファイルを記述します。

# api/config/schema.yaml
openApi:
    file: '../openapi.yaml'

Usage

Run the generator with the config file as parameter:

構成ファイルをパラメーターとしてジェネレーターを実行します。

vendor/bin/schema generate api/src/ api/config/schema.yaml -vv

Using the API Platform Distribution:

API プラットフォーム ディストリビューションの使用:

docker compose exec php \
    vendor/bin/schema generate src/ config/schema.yaml -vv

The corresponding PHP classes will be automatically generated in the src/ directory! Note that the generator takes care of creating directories corresponding to the namespace structure.

対応する PHP クラスが src/ ディレクトリに自動的に生成されます。名前空間構造に対応するディレクトリの作成はジェネレータが処理することに注意してください。

Without configuration file, the tool will build the entire Schema.org vocabulary.

構成ファイルがなければ、このツールは Schema.org 語彙全体を構築します。

Load Previously Generated Files

If you launch the schema generator again, the previously generated files will be loaded.

スキーマ ジェネレーターを再度起動すると、以前に生成されたファイルが読み込まれます。

It will try to keep as much user-added changes as possible while adding the new changes from the configuration file.

構成ファイルから新しい変更を追加する間、ユーザーが追加した変更を可能な限り保持しようとします。

You can also choose to overwrite the file instead.

代わりにファイルを上書きすることも選択できます。

Going Further

Browse the configuration documentation.

構成ドキュメントを参照します。

Cardinality Extraction

The Cardinality Extractor is a standalone tool (also used internally by the generator) extracting a property's cardinality. It extracts cardinality described with the Web Ontology Language (OWL) vocabulary or in GoodRelations. When cardinality cannot be automatically extracted, its value is set to unknown.

カーディナリティ エクストラクタは、プロパティのカーディナリティを抽出するスタンドアロン ツールです (ジェネレータによって内部的にも使用されます)。Web オントロジー言語 (OWL) ボキャブラリまたは GoodRelations で記述されたカーディナリティを抽出します。カーディナリティを自動的に抽出できない場合、その値は不明に設定されます。

Usage:

使用法:

vendor/bin/schema extract-cardinalities