diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..95f152a --- /dev/null +++ b/.gitignore @@ -0,0 +1,6 @@ +/.direnv/ +/.idea/ +/build/ +/vendor +composer.lock +.php_cs.cache diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..4841726 --- /dev/null +++ b/LICENSE @@ -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. diff --git a/README.MD b/README.MD new file mode 100644 index 0000000..1a93dce --- /dev/null +++ b/README.MD @@ -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'); +``` \ No newline at end of file diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..f3dffa0 --- /dev/null +++ b/composer.json @@ -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": { + } +} diff --git a/src/Extension/Application.php b/src/Extension/Application.php new file mode 100644 index 0000000..b698f1a --- /dev/null +++ b/src/Extension/Application.php @@ -0,0 +1,28 @@ +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; + } + } +}