swagger-php Documentation

repository·master·Indexed 26 days ago

https://github.com/zircote/swagger-php

A PHP library used to generate interactive OpenAPI documentation for RESTful APIs using PHP 8+ attributes or Doctrine annotations. It includes a CLI tool (bin/openapi) to scan source code and output specifications. Key features include automatic augmentation of schema data types, summary, and description fields from PHP docblocks and type hints, as well as a processor pipeline for transforming raw analysis into valid OpenAPI specifications.

Tokens
53.4K
Snippets
78
Records
334
Agent score
87%

What's inside swagger-php

  1. Overview of Swagger-PHP

    master
    swagger-php is a library designed to extract API metadata directly from your PHP source code. It works by scanning your codebase for annotations or attributes placed next to your PHP code. The library then converts these metadata declarations into machine-readable OpenAPI documentation. This approach allows you to maintain your API documentation in the same file as your implementation, ensuring they stay synchronized.
  2. Explore related projects for swagger-php

    master

    The following projects are related to swagger-php and can be used to extend its functionality, integrate it with specific frameworks, or visualize the generated documentation:

    Documentation Visualization

    • Swagger UI: The standard web interface for reading generated OpenAPI documentation.
    • Scalar: An alternative web interface for generating and viewing documentation.
    • Swagger Explained: A tool to browse the specification using an swagger.json file.
    • auto-swagger-ui: Automatically adds Swagger UI and JSON endpoints to your application.

    Framework Integrations

    • Symfony: NelmioApiDocBundle (bundle built on top of swagger-php) and OpenAPI-Symfony-Routing (loads routes in Symfony based on OpenAPI annotations).
    • Laravel / Lumen: Lumen swagger and Laravel Swagger (OpenApi or Swagger integration for Laravel).
    • Yii2: yii2-swagger (integration for Yii2).
    • Silex: silex2swagger (generates documentation from Silex annotations).

    Utilities and Extensions

    • openapi-router: Configure framework routes from OpenAPI annotations.
    • openapi-verifier: Verify responses against the OpenAPI specification within PHPUnit.
    • openapi-filter: Filter internal paths, operations, parameters, etc.
    • Swag It PHP: Convert JSON to PHP Swagger annotations.
    • openapi-extras: Provides extra annotations for swagger-php.
    • openapi-serialize: Serialize an object using swagger-php.
  3. Understand the swagger-php core concepts

    master

    swagger-php is a PHP library that generates OpenAPI specification documents from PHP source code by scanning annotations (PHP 8+ attributes or legacy docblock comments).

    Key concepts include:

    • Annotation: An OpenAPI element declared via PHP attributes or docblocks.
    • Analysis: The aggregate result of scanning source code containing all discovered annotations.
    • Context: Metadata describing the source hierarchy (file, namespace, class, method, property) where an annotation was found.
    • Generator: The orchestrator that coordinates scanning, processing, and outputting the OpenAPI spec.
    • Processor: A transformation step in a pipeline that converts raw Analysis into a valid OpenAPI specification.
  4. Choose a swagger-php processing mode

    master

    Swagger-php offers three processing modes that determine how source code is transformed into an OpenAPI document. While all modes produce equivalent OpenAPI output, they differ in their internal pipelines and attribute support.

    ModeStatusAttributesAnnotationsBest For
    ClassicStableOpenApi\AttributesYesExisting projects
    HybridBetaOpenApi\AttributesYesGradual migration
    SpecBetaOpenApi\SpecNoNew projects
  5. Understand the Spec Attributes Pipeline architecture

    master

    The Spec Attributes pipeline (Beta) is a modern replacement for the classic annotation-based pipeline, utilizing PHP 8.1+ attributes. It separates the collection of user-defined attributes from the generation of the OpenAPI document through a five-stage process:

    1. Assembler: Scans source files, instantiates attributes via reflection, and resolves nesting.
    2. Specification: A flat, typed container holding all collected attributes.
    3. Augmenters: Enriches the specification with inferred data (types, descriptions, refs, tags) using a resolve → reduce → augment pipeline.
    4. Compiler: Transforms the specification into a versioned OpenAPI document array (supports 3.0, 3.1, and 3.2).
    5. Builder: The unified entry point that orchestrates the entire pipeline.

    This architecture ensures that attributes are immutable data containers and that serialization logic is decoupled from the attribute definitions themselves.

  6. Understand the Spec Pipeline Architecture

    master

    The swagger-php spec attributes pipeline follows a structured flow to transform source files into an OpenAPI document:

    1. Assembler: Scans source files, instantiates attributes via reflection, and resolves nesting.
    2. Specification: A flat, typed container holding collected attributes in buckets.
    3. Augmenters: Enriches the specification with inferred data (types, refs, tags, etc.) through three phases: Resolve, Reduce, and Augment.
    4. Compiler: Transforms the specification into a versioned OpenAPI document array.
    5. Builder: The entry point that orchestrates the entire pipeline.
  7. Understand the swagger-php processing flow

    master

    The swagger-php generation process follows these steps:

    1. Iteration: The Generator iterates over provided sources (such as a Symfony Finder instance or a list of files/directories).
    2. Analysis: A configured AnalyserInterface (defaulting to ReflectionAnalyser) reads the files and constructs an Analysis object.
    3. Processing: The Analysis object and its contained annotations are passed through configured processors.
    4. Validation: If enabled, the analysis and annotations undergo validation.
    5. Serialization: The root OpenApi annotation, which aggregates all annotations, is serialized into the final YAML or JSON output.
  8. Understand the annotation lifecycle

    master

    During the generation process, annotations move through several lifecycle stages:

    1. Unmerged: The annotation has been discovered but not yet incorporated into the OpenAPI root object.
    2. Merge: The process of incorporating an annotation into its correct position within the OpenAPI object tree based on the Nesting map.
    3. Augment: Filling in missing annotation fields using values inferred from PHP code (e.g., deriving a schema type from a PHP type hint).
    4. Expand: Resolving PHP inheritance (classes, interfaces, traits, enums) by copying parent annotations into child schemas.
  9. Generate OpenAPI documents using the CLI

    master

    Use the ./vendor/bin/openapi command line tool to scan your project and generate OpenAPI documentation. By default, the output format is YAML. If you provide a filename via --output or -o, the tool determines the format based on the file extension. You can force a format using the --format option.

    ./vendor/bin/openapi app -o openapi.yaml
  10. Enable the Spec Attributes pipeline

    master
    The Spec Attributes pipeline is currently in beta and is opt-in in version 6.x. To use the new spec-based pipeline instead of the classic annotation-based pipeline, use the setMode method on your Builder instance with the 'spec' argument. In version 7, this will become the default mode.