Skip to content

Commit

Permalink
First release
Browse files Browse the repository at this point in the history
  • Loading branch information
iamBiB committed Feb 15, 2023
0 parents commit b587a5e
Show file tree
Hide file tree
Showing 6 changed files with 211 additions and 0 deletions.
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/.direnv/
/.idea/
/build/
/vendor
composer.lock
.php_cs.cache
29 changes: 29 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
BSD 3-Clause License

Copyright (c) 2019-2023, Bogdan Bocioaca.
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

1. Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.

2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.

3. Neither the name of the copyright holder nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
52 changes: 52 additions & 0 deletions README.MD
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# Lumen Api Versioning

A package that allows you to have a fallback version for your Lumen api

## Description
This package allows you to have fallback versions for your api. Basically if you have v1 in place and you want to update your app to v2 but you don't have time to update all methods, this pack allows you to take a break.
Eg. #1 **api.dev/v2/users** will fallback to **api.dev/v1/users** if v2 is not found
Eg. #2 **api.dev/v3/users** will fallback to **api.dev/v2/users** if found, if not **api.dev/v1/users**

## Installation
```shell
composer require iambib/lumen-api-versioning
```
`config file`
Create a config file named api-versioning.php and add the following lines
```php
return [
/**
* Enable the fallback
*/
'enable' => true,
/**
* Available api versions
*/

'available_versions' => [
'v1',
'v2',
],
/**
* Set them in order you want the fallback to happen
* Eg. If v4 is not found v3 is the first one to check if exists. If not, v2 then v1.
*/
'api_fallbacks' => [
'v3', 'v2', 'v1',
],
];
```
`bootstrap/app.php`

```php
$app = new \iAmBiB\ApiVersionFallback\Extension\Application(
$_ENV['APP_BASE_PATH'] ?? dirname(__DIR__)
);
...
$app->middleware([
...
'api_versioning_fallback' => \iAmBiB\ApiVersionFallback\Middleware\ApiVersioningFallback::class,
]);
...
$app->configure('api-versioning');
```
26 changes: 26 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"name": "iambib/api-version-fallback",
"description": "A package that allows you to have a fallback version of your api",
"license": "BSD-3-Clause",
"keywords": [
"lumen",
"laravel",
"api",
"versioning",
],
"homepage": "https://github.com/iambib/",
"require": {
"php": ">= 8.0.2",
"laravel/lumen-framework": "^9.0"
},
"require-dev": {
},
"suggest": {},
"autoload": {
"psr-4": {
"iAmBiB\\ApiVersionFallback\\": "src/"
}
},
"config": {
}
}
28 changes: 28 additions & 0 deletions src/Extension/Application.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

/**
* @Author: BiB
* @Date: 2023-02-14 17:55:50
* @Last Modified by: BiB
* @Last Modified time: 2023-02-15 08:10:13
*/
/* ####################################### */
/* I don`t always test my code */
/* But when I do, I do it in production */
/* ####################################### */
namespace iAmBiB\ApiVersionFallback\Extension;
use Laravel\Lumen\Application as LumenApplication;


final class Application extends LumenApplication{
public function __construct($path){
parent::__construct($path);
}

public function handleFoundRoute($routeInfo){
return parent::handleFoundRoute($routeInfo);
}
public function createDispatcher(){
return parent::createDispatcher();
}
}
70 changes: 70 additions & 0 deletions src/Middleware/ApiVersioningFallback.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?php

/**
* @Author: BiB
* @Date: 2023-02-15 08:08:27
* @Last Modified by: BiB
* @Last Modified time: 2023-02-15 08:22:37
*/
/* ####################################### */
/* I don`t always test my code */
/* But when I do, I do it in production */
/* ####################################### */

namespace iAmBiB\ApiVersionFallback\Middleware;

use Closure;
use FastRoute\Dispatcher;
use Illuminate\Http\Request;

class ApiVersioningFallback
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle($request, Closure $next)
{
if (!config('api-versioning.enable')) {
return $next($request);
}
[$method, $pathInfo] = [$request->getMethod(), '/' . trim($request->getPathInfo(), '/')];

if (isset(app()->router->getRoutes()[$method . $pathInfo])) {
return $next($request);
}
$api_fallbacks = config('api-versioning.api_fallbacks');
$available_version = config('api-versioning.available_versions');
$segments = $request->segments();
if (!in_array($segments[0], $available_version)) {
return $next($request);
}
foreach ($api_fallbacks as $fallback) {
$segments[0] = $fallback;
$path = '/' . implode('/', $segments);
$result = $this->handleDispatcherResponse(
app()->createDispatcher()->dispatch($method, $path)
);
if ($result) {
break;
}
}
if (!$result) {
return $next($request);
}
return app()->handleFoundRoute($result);
}
protected function handleDispatcherResponse($routeInfo)
{
switch ($routeInfo[0]) {
case Dispatcher::NOT_FOUND:
case Dispatcher::METHOD_NOT_ALLOWED:
return false;
case Dispatcher::FOUND:
return $routeInfo;
}
}
}

0 comments on commit b587a5e

Please sign in to comment.