Skip to content

Commit

Permalink
lumen 10 support and segment_no config
Browse files Browse the repository at this point in the history
  • Loading branch information
iamBiB committed Mar 13, 2023
1 parent ab535f5 commit 8e836c2
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 16 deletions.
4 changes: 4 additions & 0 deletions README.MD
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ return [
'api_fallbacks' => [
'v3', 'v2', 'v1',
],
/*
* URL segment number of your api version
*/
'segment_no' => 0,
];
```
`bootstrap/app.php`
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"homepage":"https://github.com/iambib/",
"require":{
"php":">= 8.0.2",
"laravel/lumen-framework":"^9.0"
"laravel/lumen-framework":"^9.0 || ^10.0"
},
"require-dev":{

Expand Down
40 changes: 25 additions & 15 deletions src/Middleware/ApiVersioningFallback.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
/**
* @Author: BiB
* @Date: 2023-02-15 08:08:27
* @Last Modified by: BiB
* @Last Modified time: 2023-02-15 08:22:37
* @Last Modified by: Bogdan Bocioaca
* @Last Modified time: 2023-03-13 19:35:12
*/
/* ####################################### */
/* I don`t always test my code */
Expand All @@ -22,44 +22,54 @@ class ApiVersioningFallback
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle($request, Closure $next)
{
if (!config('api-versioning.enable')) {
if (!config('api-versioning.enable'))
{
return $next($request);
}
[$method, $pathInfo] = [$request->getMethod(), '/' . trim($request->getPathInfo(), '/')];

if (isset(app()->router->getRoutes()[$method . $pathInfo])) {
if (isset(app()->router->getRoutes()[$method . $pathInfo]))
{
return $next($request);
}
$api_fallbacks = config('api-versioning.api_fallbacks');
$api_fallbacks = config('api-versioning.api_fallbacks');
$available_version = config('api-versioning.available_versions');
$segments = $request->segments();
if (!in_array($segments[0], $available_version)) {
$segments = $request->segments();
$segment_no = data_get(config('api-versioning'), 'segment_no', 0);
if (!isset($segments[$segment_no]) || !\in_array($segments[$segment_no], $available_version))
{
return $next($request);
}
foreach ($api_fallbacks as $fallback) {
foreach ($api_fallbacks as $fallback)
{
$segments[0] = $fallback;
$path = '/' . implode('/', $segments);
$result = $this->handleDispatcherResponse(
$path = '/' . implode('/', $segments);
$result = $this->handleDispatcherResponse(
app()->createDispatcher()->dispatch($method, $path)
);
if ($result) {
if ($result)
{
break;
}
}
if (!$result) {
if (!$result)
{
return $next($request);
}

return app()->handleFoundRoute($result);
}

protected function handleDispatcherResponse($routeInfo)
{
switch ($routeInfo[0]) {
switch ($routeInfo[0])
{
case Dispatcher::NOT_FOUND:
case Dispatcher::METHOD_NOT_ALLOWED:
return false;
Expand Down

0 comments on commit 8e836c2

Please sign in to comment.