membrane/openapi-router

0.4.0 2024-04-17 14:06 UTC

README

This library routes HTTP requests to operationIds in your OpenAPI specification. To make sure it runs quickly we've used techniques inspired by Nikita Popov and Nicolas Grekas.

Requirements

Rules

Naming Conventions

Routing Priorities

Installation

composer require membrane/openapi-router

Quick Start

To read routes dynamically, you can do the following:

<?php

use Membrane\OpenAPIRouter\Reader\OpenAPIFileReader;use Membrane\OpenAPIRouter\RouteCollector;use Membrane\OpenAPIRouter\Router;

$openApi = (new OpenAPIFileReader())->readFromAbsoluteFilePath('/app/petstore.yaml');
$routeCollection = (new RouteCollector())->collect($openApi);

$router = new Router($routeCollection);
$requestedOperationId = $router->route('http://petstore.swagger.io/v1/pets', 'get');

echo $requestedOperationId; // listPets

Caching Routes

Run the following console command to cache the routes from your OpenAPI, to avoid reading your OpenAPI file everytime:

membrane:router:generate-routes <openapi-filepath> <destination-filepath>
<?php

use Membrane\OpenAPIRouter\Router;

$routeCollection = include '/app/cache/routes.php';

$router = new Router($routeCollection);
$requestedOperationId = $router->route('http://petstore.swagger.io/v1/pets', 'get');

echo $requestedOperationId; // listPets