l5-swagger Documentation

repository·master·Indexed 25 days ago

https://github.com/darkaonline/l5-swagger

A Laravel wrapper for swagger-php and swagger-ui that integrates OpenAPI/Swagger specifications into Laravel projects. It provides tools for scanning project annotations, generating JSON and YAML documentation files via the L5Swagger\Generator class or the l5-swagger:generate CLI command, and managing security schemes and configurations.

Tokens
1.8K
Snippets
1
Records
13
Agent score
58%

What's inside l5-swagger

  1. Overview of L5 Swagger

    master

    L5 Swagger is a Laravel wrapper for swagger-php and swagger-ui. It is designed to make generating and serving OpenAPI or Swagger specifications easy within a Laravel application.

    Note: This package does not define the Swagger specification itself; it provides the integration layer. For instructions on how to write the actual Swagger specification using annotations, refer to the swagger-php documentation.

  2. Publish L5-Swagger configuration and views

    master

    To customize the package behavior or the UI, you can publish the configuration file and the view files to your application.

    Use the config tag to publish the configuration file to config/l5-swagger.php:

    php artisan vendor:publish --tag=config

    Use the views tag to publish the views to the path defined in your configuration (defaulting to the path specified in l5-swagger.defaults.paths.views):

    php artisan vendor:publish --tag=views
  3. Configure security schemes and security settings

    master

    L5-Swagger allows you to define security schemes (like Bearer tokens or API keys) and apply security requirements to your OpenAPI documentation. This is handled via the SecurityDefinitions class, which injects settings from your L5-Swagger configuration into the generated JSON documentation.

    To use this, ensure your l5-swagger configuration file contains the appropriate security_schemes and security keys. The generate() method will then read your existing JSON documentation, append these security definitions, and overwrite the file with the updated content.

  4. Generate documentation with the Generator class

    master

    The L5Swagger\Generator class is responsible for scanning your project's annotations and producing OpenAPI documentation files (JSON and optionally YAML). To trigger the generation process, call the generateDocs() method. This method handles directory preparation, constant definition, file scanning, server population, and file saving.

    $generator = new \L5Swagger\Generator(
        $paths, 
        $constants, 
        $yamlCopyRequired, 
        $security, 
        $scanOptions
    );
    $generator->generateDocs();
  5. Generate documentation using GeneratorFactory::make()

    master

    The GeneratorFactory::make() method creates and returns a new Generator instance configured for a specific documentation set. It uses the documentation identifier (the name/key defined in your configuration) to resolve paths, scan options, constants, and security definitions.

    When calling make(string $documentation), the factory resolves the following configuration keys from your documentation settings:

    • paths: The directories to scan for annotations.
    • scanOptions: Options for the scanner (optional).
    • constants: Constants to be used during generation (optional).
    • generate_yaml_copy: Boolean indicating if a YAML copy should be generated (defaults to false).
    • securityDefinitions: Configuration for securitySchemes and security settings.
  6. Initialize the Generator with paths and security

    master

    The L5Swagger\Generator constructor requires several configuration parameters to define where documentation is read from and written to:

    • $paths: An associative array containing:
      • annotations: The directory or directories to scan for annotations.
      • docs: The directory where documentation files will be stored.
      • docs_json (optional): The filename for the JSON documentation (defaults to api-docs.json).
      • docs_yaml (optional): The filename for the YAML documentation (defaults to api-docs.yaml).
      • excludes: An array of directories to exclude from scanning.
      • base: The base path to be used for the OpenAPI servers list.
    • $constants: An array of key-value pairs to be defined as PHP constants during generation.
    • $yamlCopyRequired: A boolean indicating whether a YAML version of the documentation should be created.
    • $security: An instance of L5Swagger\SecurityDefinitions used to apply security measures to the generated files.
    • $scanOptions: An array of scan-specific configuration options (see Configure scan options).
  7. Retrieve documentation configuration with documentationConfig()

    master

    Use the documentationConfig() method to retrieve the merged configuration for a specific documentation set.

    • If you pass null, it retrieves the configuration for the documentation specified in l5-swagger.default.
    • If you pass a string, it retrieves the configuration for the documentation key specified in l5-swagger.documentations.

    The method performs a recursive merge where values from the specific documentation configuration override the values in l5-swagger.defaults.

    Throws L5Swagger amespace\ L5SwaggerException if the specified documentation configuration is not found in the l5-swagger.documentations array.

  8. Configure scan options for the Generator

    master

    When instantiating the Generator, you can pass a $scanOptions array to customize how the OpenAPI specification is scanned and processed.

    Available scan option keys:

    • pattern: The file pattern used to find documentation (defaults to *.php).
    • analyser: A custom OpenApi analyser to use.
    • analysis: Configuration for the OpenApi analysis process.
    • exclude: Directories to exclude from the scan.
    • processors: An array of processors to add to the pipeline. Each entry can be a class name or an array containing ['class' => ..., 'after' => ...] to specify where in the pipeline the processor should be inserted.
    • open_api_spec_version: The version of the OpenAPI spec to use (defaults to 3.0.0).
    • generator_factory: A class name that implements CustomGeneratorInterface to provide a custom OpenApiGenerator instance.
    • default_processors_configuration: Configuration passed to the OpenApiGenerator via setConfig().