• Stars
    star
    5,078
  • Rank 8,190 (Top 0.2 %)
  • Language
    PHP
  • License
    Apache License 2.0
  • Created over 12 years ago
  • Updated about 2 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A php swagger annotation and parsing library

Build Status Total Downloads License

swagger-php

Generate interactive OpenAPI documentation for your RESTful API using doctrine annotations (optional as of version 4.8; if required the doctrine/annotations library must be installed in addition to swagger.php). or PHP attributes.

For a full list of supported annotations, please have look at the OpenApi\Annotations namespace or the documentation website.

Features

  • Compatible with the OpenAPI 3.0 and 3.1 specification.
  • Extracts information from code & existing phpdoc annotations.
  • Command-line interface available.
  • Documentation site with a getting started guide.
  • Exceptional error reporting (with hints, context)
  • As of PHP 8.1 all annotations are also available as PHP attributes

OpenAPI version support

swagger-php allows to generate specs either for OpenAPI 3.0.0 or OpenAPI 3.1.0. By default the spec will be in version 3.0.0. The command line option --version may be used to change this to 3.1.0.

Programmatically, the method Generator::setVersion() can be used to change the version.

Requirements

swagger-php requires at least PHP 7.2 for annotations and PHP 8.1 for using attributes.

Installation (with Composer)

composer require zircote/swagger-php

For cli usage from anywhere install swagger-php globally and make sure to place the ~/.composer/vendor/bin directory in your PATH so the openapi executable can be located by your system.

composer global require zircote/swagger-php

doctrine/annotations

As of version 4.8 the doctrine annotations library is optional and no longer installed by default.

To use PHPDoc annotations this needs to be installed on top of swagger-php:

composer require doctrine/annotations

If your code uses PHPDoc annotations you will need to install this as well:

composer require doctrine/annotations

Usage

Add annotations to your php files.

/**
 * @OA\Info(title="My First API", version="0.1")
 */

/**
 * @OA\Get(
 *     path="/api/resource.json",
 *     @OA\Response(response="200", description="An example resource")
 * )
 */

Visit the Documentation website for the Getting started guide or look at the Examples directory for more examples.

Usage from php

Generate always-up-to-date documentation.

<?php
require("vendor/autoload.php");
$openapi = \OpenApi\Generator::scan(['/path/to/project']);
header('Content-Type: application/x-yaml');
echo $openapi->toYaml();

Documentation of how to use the Generator class can be found in the Generator reference.

Usage from the Command Line Interface

The openapi command line interface can be used to generate the documentation to a static yaml/json file.

./vendor/bin/openapi --help

Starting with version 4 the default analyser used on the command line is the new ReflectionAnalyser.

Using the --legacy flag (-l) the legacy TokenAnalyser can still be used.

Usage from the Deserializer

Generate the OpenApi annotation object from a json string, which makes it easier to manipulate objects programmatically.

<?php

use OpenApi\Serializer;

$serializer = new Serializer();
$openapi = $serializer->deserialize($jsonString, 'OpenApi\Annotations\OpenApi');
echo $openapi->toJson();

More on OpenApi & Swagger

More Repositories

1

Hal

A PHP implementation of HAL http://stateless.co/hal_specification.html
PHP
93
star
2

chef-composer

a opscode chef recipe for composer http://getcomposer.org
Ruby
24
star
3

Bloom

a drunken investigation into feasibility of pure php bloom filters
PHP
15
star
4

Rhubarb

A PHP Celery Client Implementation
PHP
13
star
5

ApiProblem

A PHP Exception to provide the functionality of the `application/api-problem` RFC proposal. This class creates simple JSON [RFC4627] and XML [W3C.REC-xml-20081126] document formats to suit the purpose described in [Problem Details](http://tools.ietf.org/html/draft-nottingham-http-problem).
PHP
12
star
6

Uuid

Uuid object for PHP
PHP
6
star
7

collectd-aurora

An collectd plugin for Apache Aurora
Python
6
star
8

Zend_Log_Writer_Mongo

A Zend Framework Log Writer for MongoDb
PHP
5
star
9

Expressive

PHP
5
star
10

zircote-ansible

roles and plays for my $h17
Python
4
star
11

eve-lib

An EvE Online API library based on PHP5/Zend Framework.
PHP
3
star
12

Jazsl

Just Another Zend Server & Zend Server Cluster Manager Library
PHP
2
star
13

ansible-redis

Ansible roles for redis, redis-server and redis-sentinel
2
star
14

Zend_Cache_Backend_MongoDb

A Zend Framework Extended Cache Backend for MongoDb
PHP
2
star
15

chef-zend

an opscode chef cookbook for Zend Server
Ruby
2
star
16

Skulk

A PHP/Zend Prowl library enabling iOS push notifications.
PHP
2
star
17

AMQP

AMQP -- This is experimental only and not intended for production use at this time, If this is a concern, look elsewhere.
PHP
2
star
18

ZfApiVersioning

An example of API versioning with zend framework version 1 modules
PHP
1
star
19

Zircote_Hal

Deprecated Repository for formerly Zircote_Hal
1
star
20

chef-aws_base

An AWS/Route53 cookbook for hostname provisioning based on Name tag.
Ruby
1
star
21

php_murmurhash

an inport of a php_murmurhash extension for php
C
1
star