From 668e3892c51d08e1575d7db566cabdb9d82b2790 Mon Sep 17 00:00:00 2001 From: Michalis Giannas Date: Mon, 6 Jun 2016 18:31:02 +0200 Subject: [PATCH] First Commit --- .gitignore | 5 + LICENSE | 21 ++++ composer.json | 22 ++++ config/browser.php | 76 +++++++++++++ readme.md | 62 +++++++++++ src/BrowserRequirement.php | 129 ++++++++++++++++++++++ src/BrowserRequirementServiceProvider.php | 38 +++++++ 7 files changed, 353 insertions(+) create mode 100644 .gitignore create mode 100644 LICENSE create mode 100644 composer.json create mode 100644 config/browser.php create mode 100644 readme.md create mode 100644 src/BrowserRequirement.php create mode 100644 src/BrowserRequirementServiceProvider.php diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..d750889 --- /dev/null +++ b/.gitignore @@ -0,0 +1,5 @@ +/.idea/ +*.txt +*.DS_Store +.Spotlight-V100 +.Trashes diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..b15bccd --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +The MIT License (MIT) + +Copyright (c) 2016 Michalis Giannas + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. \ No newline at end of file diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..7ba1643 --- /dev/null +++ b/composer.json @@ -0,0 +1,22 @@ +{ + "name": "tibian/browser-requirement", + "description": "Browser Requirement for Laravel", + "keywords": ["browser", "requirement", "laravel"], + "type": "project", + "license": "MIT", + "authors": [ + { + "name": "Michalis Giannas", + "email": "info@tibian.me", + "homepage": "https://tibian.me" + } + ], + "require": { + "sinergi/browser-detector": "^6.0" + }, + "autoload": { + "psr-4": { + "TiBian\\BrowserRequirement\\": "src" + } + } +} diff --git a/config/browser.php b/config/browser.php new file mode 100644 index 0000000..42474bf --- /dev/null +++ b/config/browser.php @@ -0,0 +1,76 @@ + [ + // OS X + Os::OSX => [ + Browser::CHROME => 25, + Browser::FIREFOX => 25, + Browser::OPERA => 29, + ], + // Linux + Os::LINUX => [ + Browser::CHROME => 25, + Browser::FIREFOX => 25, + Browser::OPERA => 29, + ], + // Windows + Os::WINDOWS => [ + Browser::CHROME => 25, + Browser::FIREFOX => 25, + Browser::OPERA => 29, + Browser::SAFARI => 8, + Browser::IE => 9, + Browser::EDGE => 11, + ], + // iOS + Os::IOS => [ + // + ], + // Android + Os::ANDROID => [ + // + ], + // Windows Phone + Os::WINDOWS_PHONE => [ + // + ] + ], + + /* + |-------------------------------------------------------------------------- + | Redirect Route on Unsupported Browser. + |-------------------------------------------------------------------------- + | + | Create a Route like the follow example. + | Route::get("PATH", "Controller@method")->name('requirement::browser'); + */ + 'routeUnsupportedBrowser' => 'requirement::browser', + + /* + |-------------------------------------------------------------------------- + | Redirect Route on Supported Browser. + |-------------------------------------------------------------------------- + | + | Redirect if supported version of browser want to visit + | the routeUnsupportedBrowser. + | + | Create a Route like the follow example. + | Route::get("/", "PagesController@index")->name('home'); + */ + 'routeSupportedBrowser' => 'home', + +]; diff --git a/readme.md b/readme.md new file mode 100644 index 0000000..bebec4b --- /dev/null +++ b/readme.md @@ -0,0 +1,62 @@ +## Browser Requirement for Laravel +Now you can easily Set-up your minimum Browser Requirement for your Application. + +### Install +Require this package with composer using the following command +``` +composer require tibian/browser-requirement +``` +After updating composer, add the service provider to the providers array in config/app.php +``` +TiBian\BrowserRequirement\BrowserRequirementServiceProvider::class, +``` + +### Config +Publish the config file to change it as you wish. +``` +php artisan vendor:publish --provider="TiBian\BrowserRequirement\BrowserRequirementServiceProvider" --tag=config +``` + +### Usage +Open the config/browser.php and you are ready to start. + +>Let set-up minimum Browser Requirement for OS X and Windows... +``` +Os::OSX => [ + Browser::CHROME => 25, + Browser::FIREFOX => 25, + Browser::OPERA => 29, +], +// Windows +Os::WINDOWS => [ + Browser::CHROME => 25, + Browser::FIREFOX => 25, + Browser::OPERA => 29, + Browser::SAFARI => 8, + Browser::IE => 9, + Browser::EDGE => 11, +], +``` + +### Routes +This is a Example from the Routes you need, you are free to customize the Routes like you wish. + +``` +Route::get("requirement-browser", "ErrorsController@browser") + ->name('requirement::browser'); +``` + +``` +Route::get("/", "PagesController@index") + ->name('home'); +``` + +### I'm looking for: +- Individuals who can contribute to the Documentation. +- Participation in other Open Source Projects. + +> Visit my Web Site and learn more [about me](https://tibian.me) + +##### Any idea for new projects, feel free to Contact me. + +##### Thank you for visiting my Repository. diff --git a/src/BrowserRequirement.php b/src/BrowserRequirement.php new file mode 100644 index 0000000..3277651 --- /dev/null +++ b/src/BrowserRequirement.php @@ -0,0 +1,129 @@ +supportedVersions = config('browser.requirement'); + $this->routeUnsupportedBrowser = route(config('browser.routeUnsupportedBrowser')); + $this->routeSupportedBrowser = route(config('browser.routeSupportedBrowser')); + $this->currentPage = request()->url(); + + $this->os = $os; + $this->browser = $browser; + + $this->isUnsupportedBrowser(); + } + + /** + * Determine if the Browser is Unsupported + * + * @return bool + */ + private function isUnsupportedBrowser() + { + if (array_key_exists($this->os->getName(), $this->supportedVersions)) { + + $browsers = $this->supportedVersions[$this->os->getName()]; + + foreach ($browsers as $browser => $version) { + if ($this->browser->getName() === $browser && + $this->browser->getVersion() < $version + ) { + return $this->isUnsupportedBrowser = true; + } + } + } + + return $this->isUnsupportedBrowser = false; + } + + /** + * Handle an incoming request. + * + * @param \Illuminate\Http\Request $request + * @param \Closure $next + * @return mixed + */ + public function handle(Request $request, Closure $next) + { + if ($this->isUnsupportedBrowser) { + if ($this->currentPage != $this->routeUnsupportedBrowser) { + return redirect($this->routeUnsupportedBrowser); + } + } else { + if ($this->currentPage == $this->routeUnsupportedBrowser) { + return redirect($this->routeSupportedBrowser); + } + } + + return $next($request); + } +} diff --git a/src/BrowserRequirementServiceProvider.php b/src/BrowserRequirementServiceProvider.php new file mode 100644 index 0000000..0f09597 --- /dev/null +++ b/src/BrowserRequirementServiceProvider.php @@ -0,0 +1,38 @@ +publishes([ + __DIR__.'/../config/browser.php' => config_path('browser.php'), + ], 'config'); + + $router->prependMiddlewareToGroup('web', BrowserRequirement::class); + } + + /** + * Register the application services. + * + * @return void + */ + public function register() + { + $this->mergeConfigFrom(__DIR__.'/../config/browser.php', 'browser'); + } +}