Skip to content
This repository has been archived by the owner on Sep 12, 2022. It is now read-only.

Commit

Permalink
First Commit
Browse files Browse the repository at this point in the history
  • Loading branch information
TiBianMod committed Jun 6, 2016
0 parents commit 668e389
Show file tree
Hide file tree
Showing 7 changed files with 353 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/.idea/
*.txt
*.DS_Store
.Spotlight-V100
.Trashes
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -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.
22 changes: 22 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -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": "[email protected]",
"homepage": "https://tibian.me"
}
],
"require": {
"sinergi/browser-detector": "^6.0"
},
"autoload": {
"psr-4": {
"TiBian\\BrowserRequirement\\": "src"
}
}
}
76 changes: 76 additions & 0 deletions config/browser.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<?php

use Sinergi\BrowserDetector\Browser;
use Sinergi\BrowserDetector\Os;

return [

/*
|--------------------------------------------------------------------------
| Browser Requirement
|--------------------------------------------------------------------------
|
| Minimum Version of Supported Browsers.
| The following array is a good start point how
| to build your Browser Requirement.
*/
'requirement' => [
// 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',

];
62 changes: 62 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
@@ -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.
129 changes: 129 additions & 0 deletions src/BrowserRequirement.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
<?php

namespace TiBian\BrowserRequirement;

use Closure;
use Illuminate\Http\Request;
use Sinergi\BrowserDetector\Os;
use Sinergi\BrowserDetector\Browser;

/**
* Class BrowserRequirement
*
* @package TiBian\BrowserRequirement
*/
class BrowserRequirement
{
/**
* Minimum Version of Supported Browsers
*
* @var array
*/
protected $supportedVersions = [];

/**
* Redirect Route on Unsupported Browser
*
* @var string
*/
protected $routeUnsupportedBrowser;

/**
* Redirect Route on Supported Browser
*
* @var string
*/
protected $routeSupportedBrowser;

/**
* Current Page
*
* @var string
*/
protected $currentPage;

/**
* Is Unsupported Browser
*
* @var bool
*/
protected $isUnsupportedBrowser;

/**
* OS Object
*
* @var object Os
*/
protected $os;

/**
* Browser Object
*
* @var object Browser
*/
protected $browser;

/**
* BrowserRequirement constructor.
*
* @param Browser $browser
* @param Os $os
*/
public function __construct(Browser $browser, Os $os)
{
$this->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);
}
}
38 changes: 38 additions & 0 deletions src/BrowserRequirementServiceProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

namespace TiBian\BrowserRequirement;

use Illuminate\Routing\Router;
use Illuminate\Support\ServiceProvider;

/**
* Class BrowserRequirementServiceProvider
*
* @package TiBian\BrowserRequirement
*/
class BrowserRequirementServiceProvider extends ServiceProvider
{
/**
* Bootstrap the application services.
*
* @return void
*/
public function boot(Router $router)
{
$this->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');
}
}

0 comments on commit 668e389

Please sign in to comment.