membrane / openapi-router
Installs: 11 097
Dependents: 2
Suggesters: 0
Security: 0
Stars: 2
Watchers: 2
Forks: 2
Open Issues: 3
Requires
- php: ^8.1.0
- membrane/openapi-reader: 2.1.0
- psr/http-message: ^1.0 || ^2.0
- psr/log: ^3.0
- symfony/console: ^6.0 || ^7.0
Requires (Dev)
- guzzlehttp/psr7: ^2.4
- infection/infection: ^0.27.0
- mikey179/vfsstream: ^1.6.7
- phpstan/phpstan: ^1.12.7
- phpunit/phpunit: ^10.5.38
- squizlabs/php_codesniffer: ^3.7
This package is auto-updated.
Last update: 2024-11-12 12:05:00 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
- A valid OpenAPI specification.
- An operationId on all Operation Objects so that each route is uniquely identifiable.
Rules
Naming Conventions
- Forward slashes at the end of a server url will be ignored since paths MUST begin with a forward slash.
- Dynamic paths which are identical other than the variable names MUST NOT exist.
Routing Priorities
- Static urls MUST be prioritized over dynamic urls.
- Longer urls are prioritized over shorter urls.
- Hosted servers will be prioritized over hostless servers.
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