PHP Classes
elePHPant
Icontem

PHP OpenAPI Library: Implement APIs defined using OpenAPI specification

Recommend this page to a friend!
  Info   View files Example   View files View files (25)   DownloadInstall with Composer Download .zip   Reputation   Support forum   Blog    
Last Updated Ratings Unique User Downloads Download Rankings
2019-07-26 (1 month ago) RSS 2.0 feedNot yet rated by the usersTotal: 14 All time: 9,688 This week: 326Up
Version License PHP version Categories
open-api 1.0GNU General Publi...5PHP 5, Libraries, Web services
Description Author

This package can implement APIs defined using OpenAPI specification.

It takes as parameter a specification file defined JSON or YAML formats according to the OpenAPI specification to describes details of an API.

The package can handle API requests and dispatches to classes defined according to the specified OpenAPI description file.

Innovation Award
PHP Programming Innovation award nominee
July 2019
Number 5
OpenAPI is a specification to describe APIs using a specific description file format.

It is useful for implementing tools to help the developers create and test APIs with less effort.

This package provides a PHP implementation of the OpenAPI specification so you can create new APIs very quickly.

Manuel Lemos
  Performance   Level  
Name: Virgilio lino <contact>
Classes: 5 packages by
Country: Italy Italy
Age: 40
All time rank: 166465 in Italy Italy
Week rank: 710 Up39 in Italy Italy Up
Innovation award
Innovation award
Nominee: 4x

Winner: 1x

 

Details

Open-api

Maybe it's about automation, or just about being more declarative because a non Touring complete DSL will just more be correct, I find it just amazing the possibility to describe an API by using the Open-API-Specification and let this specification be your code: this class will set every route using the Slim functionalities, and for every route point to a CommandHandler.

I'd suggest, the best way to see it in action is just to clone the repository and try the Example Hello World Application:

git clone git@github.com:virgiliolino/open-api.git
cd open-api/Examples/HelloWorld/
composer install   #composer install will actually install Slim and open-api
php -S localhost:8080 -t public #start the server
curl localhost:8080/hello/world # or just open the browser localhost:8080/hello/world

For a fully working application, you could take a look at a ReactJS + Slim Skeleton that provides all the functionalities needed for a modern application. The url is here The OpenApi specification is here

You will not have a few overpopulated Controllers, but instead for every entry point a command handler. You can read at this blog post for some ideas of how we intend our architecutre

Furthermore, the Open-Api specification can be automatically validated, tested Swagger

In the end you'll have a yml or json file that describe your API, something like this: Json Specification

By using our library all routes will automatically be set. Every route pointing to a CommandHandler indicated by a unique operationId. So in the image of the example, you can see that there is a route: /pet that accept post requests. It will be enough to use our class, when you start the application the route /pet will accept a post. And so for the gets that you see below, like /pet/findByStatus, etc. For every path, it will be executed the command handler with the operationI. In the example for /pet, you can see the operationId: addPet. So making a post request to /pet, the system will try to execute the class AddPet::execute passing the params. The operationId must be a fully qualified name of a class. Something like this for example: operationId: \MyApplication\CommandHandlers\AddPett which means that will execue AddPett::execute

You may find an example of a fully working Open-Api specification here the full json file

Installation

composer require dispatcher/open-api

Examples/Helloworld

require 'vendor/autoload.php';
$app = new \Slim\App;
$container = $app->getContainer();
//your command Handlers need to be injected by operationId
$container['HelloWorld'] = function () {
    return new \HelloWorld\CommandHandlers\HelloWorld();
};
$openApiFile = 'routes.json';
$openApiConfigParser = Dispatcher\OpenApi\ParserFactory::parserFor($openApiFile);
$openApiConfig = $openApiConfigParser->parse($openApiFile);
$applicationBridge = new \Sab\Application\Bridge\SlimBridge($app);
$routesInjector = new \Dispatcher\OpenApi\Route\DefaultRouteInjector();
$openApiDispatcher = new \Dispatcher\OpenApi\OpenApiDispatcher($routesInjector);
$openApiDispatcher->InjectRoutesFromConfig($applicationBridge, $openApiConfig);

$app->run();

As you may see we're injecting HelloWorld, a command Handler with the same id of operationId that you may find on routes.json

That's all folks.

Help wanted

There is no validation at all. This process can be automatized. Class CommandHandler on the file called SwaggerDispatcher.

Thanks, Virgilio

  Files folder image Files  
File Role Description
Files folder imageDispatcher (1 directory)
Files folder imageExamples (1 directory)
Accessible without login Plain text file composer.json Data Auxiliary data
Accessible without login Plain text file LICENSE Lic. License text
Accessible without login Plain text file README.md Doc. Documentation
Accessible without login Image file swagger.png Data Auxiliary data

  Files folder image Files  /  Dispatcher  
File Role Description
Files folder imageOpenApi (7 files, 4 directories)

  Files folder image Files  /  Dispatcher  /  OpenApi  
File Role Description
Files folder imageHelper (1 file)
Files folder imageParser (2 files)
Files folder imageRoute (2 files)
Files folder imageValidators (4 files)
  Plain text file CommandHandler.php Class Class source
  Plain text file CommandRegisterer.php Class Class source
  Plain text file Elements.php Class Class source
  Plain text file OpenApiDispatcher.php Class Class source
  Plain text file Parser.php Class Class source
  Plain text file ParserFactory.php Class Class source
  Plain text file Route.php Class Class source

  Files folder image Files  /  Dispatcher  /  OpenApi  /  Helper  
File Role Description
  Plain text file Enum.php Class Class source

  Files folder image Files  /  Dispatcher  /  OpenApi  /  Parser  
File Role Description
  Plain text file Json.php Class Class source
  Plain text file Yaml.php Class Class source

  Files folder image Files  /  Dispatcher  /  OpenApi  /  Route  
File Role Description
  Plain text file DefaultRouteInjector.php Class Class source
  Plain text file RouteInjector.php Class Class source

  Files folder image Files  /  Dispatcher  /  OpenApi  /  Validators  
File Role Description
  Plain text file NullParamsValidator.php Class Class source
  Plain text file NullRequestValidator.php Class Class source
  Plain text file ParamsValidator.php Class Class source
  Plain text file RequestValidator.php Class Class source

  Files folder image Files  /  Examples  
File Role Description
Files folder imageHelloWorld (3 files, 2 directories)

  Files folder image Files  /  Examples  /  HelloWorld  
File Role Description
Files folder imageHelloWorld (1 directory)
Files folder imagepublic (1 file)
  Accessible without login Plain text file composer.json Data Auxiliary data
  Accessible without login Plain text file routes.json Data Auxiliary data
  Accessible without login Plain text file routes.yaml Data Auxiliary data

  Files folder image Files  /  Examples  /  HelloWorld  /  HelloWorld  
File Role Description
Files folder imageCommandHandlers (1 file)

  Files folder image Files  /  Examples  /  HelloWorld  /  HelloWorld  /  CommandHandlers  
File Role Description
  Plain text file HelloWorld.php Class Class source

  Files folder image Files  /  Examples  /  HelloWorld  /  public  
File Role Description
  Accessible without login Plain text file index.php Example Example script

 Version Control Unique User Downloads Download Rankings  
 100%
Total:14
This week:0
All time:9,688
This week:326Up