2017 © Pedro Peláez
 

library swagger-bundle

Implementation of swagger-php for Symfony

image

timeinc/swagger-bundle

Implementation of swagger-php for Symfony

  • Thursday, June 1, 2017
  • by andythorne
  • Repository
  • 9 Watchers
  • 29 Stars
  • 17,303 Installations
  • PHP
  • 2 Dependents
  • 0 Suggesters
  • 9 Forks
  • 1 Open issues
  • 8 Versions
  • 14 % Grown

The README.md

SwaggerBundle

Travis Packagist, (*1)

This bundle provides integration of swagger-php in Symfony. It should only run in the dev environment, as searching for annotations at runtime is not performant. The bundle comes with the ability to generate a swagger.json file that can be statically served., (*2)

Installation

Install via Composer:, (*3)

composer require timeinc/swagger-bundle --dev

Enable Bundle

In your app/AppKernel.php file, add the following to your list of dev bundles:, (*4)

<?php

class AppKernel extends Kernel
{
    public function registerBundles()
    {
        // ...

        if (in_array($this->getEnvironment(), ['dev', 'test'], true)) {
            // ...
            $bundles[] = new TimeInc\SwaggerBundle\SwaggerBundle();
        }

        // ...
    }
}

Configure Bundle

Application Configuration

Open ./app/config/config_dev.yml and add your configuration like below., (*5)

swagger:
    version: '2.0'
    info:
        title: 'My API'
        version: '1.0.0'
        description: 'My API Description'
    host: 'my-api-host:8080'
    base_path: '/v2'
    schemes:
        - https
    alternative_hosts:
        - { name: 'production', host: 'productionhost', schemes: [https], base_path: '/api' }
    produces:
        - application/json
    consumes:
        - application/json
    annotations:
        bundles:
            - AcmeDemoBundle
    pretty_json: true

The full configuration reference for a comprehensive list of all values and defaults., (*6)

Routing

The bundle comes with routes to view the default swagger.json schema or schemas with overridden host, schemes and base_path as defined by your presets. To enable the routes, add the following to your ./app/config/routing_dev.yml:, (*7)

_swagger:
    resource: "@SwaggerBundle/Resources/config/routing.xml"
    prefix:   /

You can then access your default schema through /_swagger/swagger.json or your alternative schemas through /_swagger/swagger-{alternative-host-name}.json. As noted before, this should not be added to the production routes., (*8)

Usage

Once you have registered your bundles under swagger > bundles in config_dev.yml, the swagger-bundle will automatically search for any swagger-php annotations under:, (*9)

  • [bundle_dir]/Controller
  • [bundle_dir]/Document
  • [bundle_dir]/Entity
  • [bundle_dir]/Form
  • [bundle_dir]/Model
  • [bundle_dir]/Swagger

Please see swagger-php's annotation reference for details about swagger-php's syntax., (*10)

Symfony Routes

This bundle comes with an extra \TimeInc\SwaggerBundle\Swagger\Annotation\Route annotation that allows you to define a symfony route as a swagger endpoint. using the annotation will set a swagger path for each method and add parameters for the path variables and any defaults as query strings., (*11)

The annotation can be defined on a class or a method with the following properies:, (*12)

Property Context Default Notes
method CLASS The method name to inspect
route CLASS\ METHOD | The Symfony route to inspect
returns CLASS\ METHOD entity | The response data type (collection|entity)
entity CLASS\ METHOD | The entity that is returned. If omitted, will be guessed based on controller name
queryParams CLASS\ METHOD [] | Any extra query parameters as Key/Value array of queryParameter/dataType
headers CLASS\ METHOD [] | Any extra header parameters as Key/Value array of headerName/dataType

NOTE: If the entity is omitted, the bundle will search for an entity name of the same name as the controller in the same bundle. For Example, the below example controller is Acme\PetBundle\Controller\PetController, so the bundle will search for an entity in Acme\PetBundle\Entity\Pet, (*13)

<?php

namespace Acme\PetBundle\Controller;

use TimeInc\SwaggerBundle\Swagger\Annotation\Route;

/**
 * @Route(
 *     method="cgetAction",
 *     route="api_get_pets",
 *     returns="collection"
 * )
 */
class PetController
{
    /**
     * This annotation defines no entity. The bundle will check an entity exists in:
     *     Acme\PetBundle\Entity\Pet
     * 
     * @Route(
     *     route="api_get_pet"
     * )
     */
    public function getAction()
    {}

    /**
     * @Route(
     *     route="api_get_pet_owner",
     *     entity="Acme\PetBundle\Entity\Owner",
     *     headers={"X-TEST": "string"},
     *     queryParams={"query": "string", "page": "integer"}
     * )
     */
    public function getOwnerAction()
    {}   
}

Command Line

You can dump the json schema to the console with the swagger:dump command:, (*14)

./bin/console -e=dev swagger:dump --pretty

HINT: You can use the option --alternative-host=product to override host, schemes and base_path with your production preset, (*15)

API Gateway

The bundle can also generate a swagger schema for an AWS API Gateway using the HTTP Proxy., (*16)

The schema can then be imported into the AWS console to generate a 1-1 route mapping over a HTTP proxy for your API. All parameters and headers and imported into AWS and passed through., (*17)

API Gateway

Please see the docs on how to integrate your API with API Gateway., (*18)

The Versions

01/06 2017

dev-master

9999999-dev

Implementation of swagger-php for Symfony

  Sources   Download

MIT

The Requires

 

The Development Requires

01/06 2017

0.2.2

0.2.2.0

Implementation of swagger-php for Symfony

  Sources   Download

MIT

The Requires

 

The Development Requires

26/04 2017

0.2.1

0.2.1.0

Implementation of swagger-php for Symfony

  Sources   Download

MIT

The Requires

 

The Development Requires

02/12 2016

0.2.0

0.2.0.0

Implementation of swagger-php for Symfony

  Sources   Download

MIT

The Requires

 

The Development Requires

12/11 2016

1.x-dev

1.9999999.9999999.9999999-dev

Implementation of swagger-php for Symfony

  Sources   Download

MIT

The Requires

 

The Development Requires

12/09 2016

0.1.2

0.1.2.0

Implementation of swagger-php for Symfony

  Sources   Download

MIT

The Requires

 

The Development Requires

09/08 2016

0.1.1

0.1.1.0

Implementation of swagger-php for Symfony

  Sources   Download

MIT

The Requires

 

The Development Requires

19/07 2016

0.1.0

0.1.0.0

Implementation of swagger-php for Symfony

  Sources   Download

MIT

The Requires

 

The Development Requires