Skip to content

Commit

Permalink
doc: better doc
Browse files Browse the repository at this point in the history
  • Loading branch information
MarcFRICOU committed Oct 21, 2015
1 parent 2e6ac50 commit e0d8c7d
Showing 1 changed file with 6 additions and 33 deletions.
39 changes: 6 additions & 33 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,48 +23,21 @@ require '/path/to/vendor/autoload.php';

$lumenOperationParser = new \Iadvize\SwaggerizeFastRoute\OperationParser\LumenControllerOperationParser('Controllers\\Namespace\\');

$dispatcher = FastRoute\cachedDispatcher(function(FastRoute\RouteCollector $r, ['cacheFile' => 'route/file/path']) {
$dispatcher = FastRoute\simpleDispatcher(function(FastRoute\RouteCollector $r, ['cacheFile' => 'route/file/path']) {
\Iadvize\SwaggerizeFastRoute\addRoutes(
'https://raw.githubusercontent.com/wordnik/swagger-spec/master/examples/v2.0/json/petstore.json',
'path/to/swagger/json',
$r,
$lumenOperationParser,
['routeFile' => 'path/to/generated/route/file']
['routeFile' => 'path/to/generated/route/file', 'cacheEnabled' => false]
);
});

// Alternatively use cache
/**
$dispatcher = FastRoute\cachedDispatcher(function(FastRoute\RouteCollector $r, ['cacheFile' => 'route/file/path']) {
\Iadvize\SwaggerizeFastRoute\addRoutes(
'https://raw.githubusercontent.com/wordnik/swagger-spec/master/examples/v2.0/json/petstore.json',
$r,
$lumenOperationParser,
['routeFile' => 'path/to/generated/route/file', 'cacheEnabled' => true]
);
});
*/

// Fetch method and URI from somewhere
$httpMethod = $_SERVER['REQUEST_METHOD'];
$uri = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);

$routeInfo = $dispatcher->dispatch($httpMethod, $uri);
switch ($routeInfo[0]) {
case FastRoute\Dispatcher::NOT_FOUND:
// ... 404 Not Found
break;
case FastRoute\Dispatcher::METHOD_NOT_ALLOWED:
$allowedMethods = $routeInfo[1];
// ... 405 Method Not Allowed
break;
case FastRoute\Dispatcher::FOUND:
$handler = $routeInfo[1];
$vars = $routeInfo[2];
// ... call $handler with $vars
break;
}
// ... see FastRoute Dispatcher
```

Alternatively to generate routes, you can simply cache first parse by setting `'cacheEnabled' => true` in addRoute function.

# Apply this to Lumen application

To use this swagger routes in a Lumen Application (which use FastRoute as route library), you need to extends `Laravel\Lumen\Application` and override `createDispatcher` method.
Expand Down

0 comments on commit e0d8c7d

Please sign in to comment.