alireza-h / php-openapi
PHP Open Api Generator
Requires
- php: ^7.4|^8.0
- ext-json: *
- fakerphp/faker: ^1.21.0
This package is auto-updated.
Last update: 2024-11-22 14:43:14 UTC
README
Installation
Standalone
-
Clone project
-
Run
composer update
-
Check
src/Document
directory as sample and create your ownOpenApiOperationGenerator
andOpenApiDocumentGenerator
OpenApiOperationGenerator
is responsible to generate Open API Operations andOpenApiDocumentGenerator
is responsible to generate Open API json document. Each public method ofOpenApiOperationGenerator
generates one Open API Operation (Using reflection). -
Run
php -S localhost:8001
in project root directory- Open
http://loclahost:8001
in your browser for Open API json document - Open
http://loclahost:8001?swagger
in your browser for Open API Swagger UI
- Open
Dependency of existing project
-
Install package
composer require alireza-h/php-openapi
-
Check
src/Document
directory as sample and create your ownOpenApiOperationGenerator
andOpenApiDocumentGenerator
Instead of using
OpenApiDocumentGenerator
, you can create your ownOpenApiBuilder
object and addOpenApiOperation
to builder object.OpenApiBuilder::openapi() ->info( [ 'title' => 'API', 'description' => 'API Description', 'version' => '1.0.0' ] ) ->server( [ 'url' => '{scheme}://{host}/{base_path}', 'variables' => [ 'scheme' => [ 'enum' => [ 'http', 'https' ], 'default' => 'http' ], 'host' => [ 'default' => 'localhost:8000' ], 'base_path' => [ 'default' => 'api' ], ] ] ) ->component( 'securitySchemes', 'bearerAuth', [ 'type' => 'http', 'scheme' => 'bearer', 'bearerFormat' => 'JWT', ] ) ->security( [ 'bearerAuth' => [] ] ) ->operation( OpenApiOperation::post('/auth/signup') ->tags(['Auth']) ->summary('Signup') ->description('Signup description') ->requestBody( OpenApiRequestBody::create() ->properties( [ [ 'name' => 'email', 'type' => 'string', 'format' => 'email', 'example' => 'email@example.com', 'description' => 'Email', ], ... ] ) ->mediaTypeMultipartFormData() ) ->response( OpenApiResponse::create() ->example( [ 'data' => [], 'message' => null ] ) ) ) ->operation( OpenApiOperation::put('/auth/confirm') ->tags(['Auth']) ->summary('ConfirmSignup') ->description('Confirm signup description') ->requestBody( OpenApiRequestBody::create() ->properties( [ [ 'name' => 'email', 'type' => 'string', 'format' => 'email', 'example' => 'email@example.com', 'description' => 'Email', ], [ 'name' => 'code', 'example' => 12345, ] ] ) ->mediaTypeXWwwFormUrlencoded() ) ->response( OpenApiResponse::create() ->example( [ 'data' => [], 'message' => null ] ) ) ) ->docs();
-
Create your own custom route to serve Open API json document and swagger UI