Skip to content

Commit

Permalink
Merge pull request #19 from membrane-php/integrate-reader
Browse files Browse the repository at this point in the history
Integrate reader
  • Loading branch information
carnage authored Sep 25, 2023
2 parents 04f6e42 + 86b1f87 commit d4066b7
Show file tree
Hide file tree
Showing 33 changed files with 605 additions and 3,096 deletions.
21 changes: 9 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,40 +5,38 @@ To make sure it runs quickly we've used techniques inspired
by [Nikita Popov](https://www.npopov.com/2014/02/18/Fast-request-routing-using-regular-expressions.html)
and [Nicolas Grekas](https://nicolas-grekas.medium.com/making-symfonys-router-77-7x-faster-1-2-958e3754f0e1).

# Requirements
## Requirements

- A valid [OpenAPI specification](https://github.com/OAI/OpenAPI-Specification#readme).
- An operationId on all [Operation Objects](https://spec.openapis.org/oas/v3.1.0#operation-object) so that each route is uniquely identifiable.

# Rules
## Rules

## Naming Conventions
### Naming Conventions

- Forward slashes at the end of a server url will be ignored since [paths MUST begin with a forward slash.](https://spec.openapis.org/oas/v3.1.0#paths-object)
- [Dynamic paths which are identical other than the variable names MUST NOT exist.](https://spec.openapis.org/oas/v3.1.0#paths-object)

## Routing Priorities
### Routing Priorities

- [Static urls MUST be prioritized over dynamic urls](https://spec.openapis.org/oas/v3.1.0#paths-object).
- Longer urls are prioritized over shorter urls.
- Hosted servers will be prioritized over hostless servers.

# Installation
## Installation

```text
composer require membrane/openapi-router
```

# Quick Start
## Quick Start

To read routes dynamically, you can do the following:

```php
<?php

use Membrane\OpenAPIRouter\Reader\OpenAPIFileReader;
use Membrane\OpenAPIRouter\Router\Collector\RouteCollector;
use Membrane\OpenAPIRouter\Router\Router;
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);
Expand All @@ -49,19 +47,18 @@ $requestedOperationId = $router->route('http://petstore.swagger.io/v1/pets', 'ge
echo $requestedOperationId; // listPets
```

# Caching Routes
## Caching Routes

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

```text
membrane:router:generate-routes <openapi-filepath> <destination-filepath>
```


```php
<?php

use Membrane\OpenAPIRouter\Router\Router;
use Membrane\OpenAPIRouter\Router;

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

Expand Down
4 changes: 2 additions & 2 deletions benchmark/CebeReaderNoResolveExternalOnly.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@

use cebe\openapi\Reader;
use cebe\openapi\ReferenceContext;
use Membrane\OpenAPIRouter\Router\Collector\RouteCollector;
use Membrane\OpenAPIRouter\Router\Router;
use Membrane\OpenAPIRouter\RouteCollector;
use Membrane\OpenAPIRouter\Router;

/**
* @param string[] $requests
Expand Down
4 changes: 2 additions & 2 deletions benchmark/CebeReaderResolveAll.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
include_once __DIR__ . '/../vendor/autoload.php';

use cebe\openapi\Reader;
use Membrane\OpenAPIRouter\Router\Collector\RouteCollector;
use Membrane\OpenAPIRouter\Router\Router;
use Membrane\OpenAPIRouter\RouteCollector;
use Membrane\OpenAPIRouter\Router;

/**
* @param string[] $requests
Expand Down
4 changes: 2 additions & 2 deletions benchmark/WeirdAndWonderful.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
include_once __DIR__ . '/../vendor/autoload.php';

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

/**
* @param string[] $requests
Expand Down
2 changes: 1 addition & 1 deletion bin/membrane-router
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

require __DIR__ . '/../vendor/autoload.php';

use Membrane\OpenAPIRouter\Console\Commands\CacheOpenAPIRoutes;
use Membrane\OpenAPIRouter\Console\Command\CacheOpenAPIRoutes;
use Symfony\Component\Console\Application;

$application = new Application();
Expand Down
4 changes: 3 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@
"autoload": {
"psr-4": {
"Membrane\\OpenAPIRouter\\": "src/",
"Membrane\\OpenAPIRouter\\Fixtures\\": "tests/fixtures/"
"Membrane\\OpenAPIRouter\\Fixtures\\": "tests/fixtures/",
"Membrane\\OpenAPIRouter\\Tests\\": "tests/"
}
},
"require": {
"php": "^8.1.0",
"cebe/php-openapi": "^1.7",
"membrane/openapi-reader": "^1.0.0",
"psr/http-message": "^1.0 || ^2.0",
"psr/log": "^3.0",
"symfony/console": "^6.2"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace Membrane\OpenAPIRouter\Console\Commands;
namespace Membrane\OpenAPIRouter\Console\Command;

use Membrane\OpenAPIRouter\Console\Service\CacheOpenAPIRoutes as CacheOpenAPIRoutesService;
use Symfony\Component\Console\Attribute\AsCommand;
Expand Down
19 changes: 10 additions & 9 deletions src/Console/Service/CacheOpenAPIRoutes.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@

namespace Membrane\OpenAPIRouter\Console\Service;

use Membrane\OpenAPIRouter\Exception\CannotProcessOpenAPI;
use Membrane\OpenAPIRouter\Exception\CannotReadOpenAPI;
use Membrane\OpenAPIRouter\Exception\CannotRouteOpenAPI;
use Membrane\OpenAPIRouter\Reader\OpenAPIFileReader;
use Membrane\OpenAPIRouter\Router\Collector\RouteCollector;
use Membrane\OpenAPIRouter\Router\RouteCollection;
use Membrane\OpenAPIReader\Exception\CannotRead;
use Membrane\OpenAPIReader\OpenAPIVersion;
use Membrane\OpenAPIReader\Reader;
use Membrane\OpenAPIRouter\Exception\CannotCollectRoutes;
use Membrane\OpenAPIRouter\RouteCollection;
use Membrane\OpenAPIRouter\RouteCollector;
use Psr\Log\LoggerInterface;

class CacheOpenAPIRoutes
Expand All @@ -31,15 +31,16 @@ public function cache(string $openAPIFilePath, string $cacheDestination): bool
}

try {
$openApi = (new OpenAPIFileReader())->readFromAbsoluteFilePath($openAPIFilePath);
} catch (CannotReadOpenAPI $e) {
$openApi = (new Reader([OpenAPIVersion::Version_3_0, OpenAPIVersion::Version_3_1]))
->readFromAbsoluteFilePath($openAPIFilePath);
} catch (CannotRead $e) {
$this->logger->error($e->getMessage());
return false;
}

try {
$routeCollection = (new RouteCollector())->collect($openApi);
} catch (CannotRouteOpenAPI | CannotProcessOpenAPI $e) {
} catch (CannotCollectRoutes $e) {
$this->logger->error($e->getMessage());
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@

/* This exception occurs when a route collection cannot be created from your OpenAPI */

class CannotRouteOpenAPI extends \RuntimeException
use RuntimeException;

class CannotCollectRoutes extends RuntimeException
{
public const NO_ROUTES = 0;

Expand Down
80 changes: 0 additions & 80 deletions src/Exception/CannotProcessOpenAPI.php

This file was deleted.

54 changes: 0 additions & 54 deletions src/Exception/CannotReadOpenAPI.php

This file was deleted.

50 changes: 0 additions & 50 deletions src/Reader/OpenAPIFileReader.php

This file was deleted.

2 changes: 1 addition & 1 deletion src/Router/Route/Path.php → src/Route/Path.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace Membrane\OpenAPIRouter\Router\Route;
namespace Membrane\OpenAPIRouter\Route;

use JsonSerializable;

Expand Down
2 changes: 1 addition & 1 deletion src/Router/Route/Route.php → src/Route/Route.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace Membrane\OpenAPIRouter\Router\Route;
namespace Membrane\OpenAPIRouter\Route;

class Route
{
Expand Down
Loading

0 comments on commit d4066b7

Please sign in to comment.