Git Product home page Git Product logo

swaggerslimdispatcher's Introduction

SlimSwaggerDispatcher - DISCONTINUED

Maybe it's about automation, or just about beign more declarative because a DSL non Touring complete language will just more correct, anyway 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 [email protected]:virgiliolino/SwaggerSlimDispatcher.git
cd SwaggerSlimDispatcher/Examples/HelloWorld/
composer install   #composer install will actually install Slim and SwaggerSlimDispatcher
php -S localhost:8080 -t public public/index.php #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 Api specification from Swagger 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

Even if its working, I'd not consider it really a stable package. So to install it you need to proceed in that way:

 composer require dispatcher/swagger-dispatcher dev-master

Examples/Helloworld

$app = new \Slim\App;
$container = $app->getContainer();
$container['HelloWorld'] = function ($c) {
    return new \HelloWorld\CommandHandlers\HelloWorld();
};
       
$swaggerApiFile = 'routes.json';
$commandHandler = new Dispatcher\Swagger\DefaultCommandRegisterer();
$swaggerConfigParser = Dispatcher\Swagger\ParserFactory::parserFor($swaggerFile);
$swaggerConfig = $swaggerConfigParser->parse($swaggerFile);
\Dispatcher\Swagger\SwaggerDispatcher::InjectRoutesFromConfig($app, $swaggerConfig);

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

swaggerslimdispatcher's People

Contributors

virgiliolino avatar

Stargazers

Mohammad Azam Rahmanpour avatar cymapk avatar Chun-Sheng, Li avatar

Watchers

 avatar James Cloos avatar cymapk avatar  avatar

Forkers

incubactor

swaggerslimdispatcher's Issues

Implement a RequestValidator

By using the OpenApi specification, the request can be defined together with all the rules that define those params.
We should automatically validate those In the command Handler there is already a call to a validator that just return true

Implement a parameters validator

By using the OpenApi specification, the parameters can be defined together with all the rules that define those params.
We should automatically validate those params. In the command Handler there is already a call to a validator that just return true

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.