├── .gitignore ├── src ├── Facades │ └── Browscap.php ├── Console │ ├── ParserCommand.php │ ├── CheckUpdateCommand.php │ ├── ConvertCommand.php │ ├── UpdateCommand.php │ └── FetchCommand.php └── BrowscapServiceProvider.php ├── composer.json ├── LICENSE ├── config └── browscap.php └── README.md /.gitignore: -------------------------------------------------------------------------------- 1 | # ecliplse 2 | /.settings 3 | .buildpath 4 | .project 5 | .idea 6 | vendor/ 7 | composer.lock 8 | -------------------------------------------------------------------------------- /src/Facades/Browscap.php: -------------------------------------------------------------------------------- 1 | 12 | */ 13 | class Browscap extends \Illuminate\Support\Facades\Facade 14 | { 15 | /** 16 | * Get the registered name of the component. 17 | * 18 | * @return string 19 | */ 20 | protected static function getFacadeAccessor(): string 21 | { 22 | return 'browscap'; 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /src/Console/ParserCommand.php: -------------------------------------------------------------------------------- 1 | 13 | */ 14 | class ParserCommand extends Command 15 | { 16 | /** 17 | * Create a new command instance. 18 | * 19 | * @return void 20 | */ 21 | public function __construct() 22 | { 23 | parent::__construct(config('browscap.cache')); 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /src/Console/CheckUpdateCommand.php: -------------------------------------------------------------------------------- 1 | 13 | */ 14 | class CheckUpdateCommand extends Command 15 | { 16 | /** 17 | * Create a new command instance. 18 | * 19 | * @return void 20 | */ 21 | public function __construct() 22 | { 23 | parent::__construct(config('browscap.cache')); 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /src/Console/ConvertCommand.php: -------------------------------------------------------------------------------- 1 | 13 | */ 14 | class ConvertCommand extends Command 15 | { 16 | /** 17 | * Create a new command instance. 18 | * 19 | * @return void 20 | */ 21 | public function __construct() 22 | { 23 | parent::__construct(config('browscap.cache'), config('browscap.file')); 24 | 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /src/Console/UpdateCommand.php: -------------------------------------------------------------------------------- 1 | 13 | */ 14 | class UpdateCommand extends Command 15 | { 16 | /** 17 | * Create a new command instance. 18 | * 19 | * @return void 20 | */ 21 | public function __construct() 22 | { 23 | parent::__construct(config('browscap.cache')); 24 | 25 | // set default option according to config option 26 | $this->getDefinition()->getOption('remote-file')->setDefault(config('browscap.remote-file')); 27 | 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /src/Console/FetchCommand.php: -------------------------------------------------------------------------------- 1 | 13 | */ 14 | class FetchCommand extends Command 15 | { 16 | /** 17 | * Create a new command instance. 18 | * 19 | * @return void 20 | */ 21 | public function __construct() 22 | { 23 | parent::__construct(config('browscap.cache'), config('browscap.file')); 24 | 25 | // set default option according to config option 26 | $this->getDefinition()->getOption('remote-file')->setDefault(config('browscap.remote-file')); 27 | 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "propa/laravel-browscap", 3 | "description": "Browscap-PHP integration for Laravel 5-12", 4 | "keywords": ["laravel", "browscap", "browscap-php", "user-agent", "browser"], 5 | "license": "MIT", 6 | "authors": [ 7 | { 8 | "name": "Pavel Kulbakin", 9 | "email": "p.kulbakin@gmail.com" 10 | } 11 | ], 12 | "require": { 13 | "php": "^7.1.0 || ^8.0", 14 | "illuminate/support": "^5.0 || ^6.0 || ^7.0 || ^8.0 || ^9.0 || ^10.0 || ^11.0|| ^12.0", 15 | "browscap/browscap-php": "^6.0 || ^7.0" 16 | }, 17 | "autoload": { 18 | "psr-4": { 19 | "Propa\\BrowscapPHP\\": "src/" 20 | } 21 | }, 22 | "minimum-stability": "stable", 23 | "extra": { 24 | "laravel": { 25 | "providers": [ 26 | "Propa\\BrowscapPHP\\BrowscapServiceProvider" 27 | ], 28 | "aliases": { 29 | "Browscap": "Propa\\BrowscapPHP\\Facades\\Browscap" 30 | } 31 | } 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2019 Pavel Kulbakin 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /config/browscap.php: -------------------------------------------------------------------------------- 1 | env('BROWSCAP_REMOTE_FILE', 'PHP_BrowscapINI'), 16 | 17 | /* 18 | |-------------------------------------------------------------------------- 19 | | Cache location 20 | |-------------------------------------------------------------------------- 21 | | 22 | | Where the cache files are located 23 | | 24 | */ 25 | 'cache' => storage_path('framework/cache/browscap'), 26 | 27 | /* 28 | |-------------------------------------------------------------------------- 29 | | browscap.ini location 30 | |-------------------------------------------------------------------------- 31 | | 32 | | Where database ini file is located or stored, only used by some console commands 33 | | 34 | */ 35 | 'file' => storage_path('framework/cache/browscap/browscap.ini'), 36 | ]; 37 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # laravel-browscap 2 | 3 | [Browscap-PHP](https://github.com/browscap/browscap-php) for [Laravel 5](http://laravel.com)/[Lumen 5](https://lumen.laravel.com/) 4 | 5 | ## Installation 6 | 7 | Run `composer require propa/laravel-browscap` 8 | 9 | If you're using Laravel 5.5 or 6 and didn't disabled package auto discovery referencing 10 | 11 | ### Laravel 12 | Reference service provider and corresponding alias in your `app.php` config 13 | 14 | ```php 15 | 'providers' => [ 16 | // ... 17 | Propa\BrowscapPHP\BrowscapServiceProvider::class, 18 | ], 19 | ``` 20 | 21 | ```php 22 | 'aliases' => [ 23 | // ... 24 | 'Browscap' => Propa\BrowscapPHP\Facades\Browscap::class, 25 | ], 26 | ``` 27 | 28 | Publish package config if necessary 29 | 30 | ```cli 31 | php artisan vendor:publish 32 | ``` 33 | 34 | ### Lumen 35 | 36 | For Lumen, register a different Provider in `bootstrap/app.php`: 37 | 38 | ```php 39 | $app->register(\Propa\BrowscapPHP\BrowscapServiceProvider::class); 40 | ``` 41 | and also a facade 42 | ```php 43 | class_alias(\Propa\BrowscapPHP\Facades\Browscap::class, Browscap::class); 44 | ``` 45 | 46 | ## Usage 47 | 48 | Console commands defined by BrowscapPHP can be accessed via `artisan`, for the full list see 49 | 50 | ```cli 51 | php artisan list browscap 52 | ``` 53 | 54 | Firstly, it is necessary to import browscap.ini and cache it, for that run 55 | ```cli 56 | php artisan browscap:update 57 | ``` 58 | 59 | When necessary cache files are created by the above command, one can call `Browscap::getBrowser()` and analyze detected 60 | browser type and features. The extent of feature detection depends on `browscap.ini` file imported (there are lite, default and full versions available). 61 | 62 | For more information, look into docs for underlying [BrowscapPHP](https://github.com/browscap/browscap-php). 63 | -------------------------------------------------------------------------------- /src/BrowscapServiceProvider.php: -------------------------------------------------------------------------------- 1 | 23 | */ 24 | class BrowscapServiceProvider extends ServiceProvider 25 | { 26 | /** 27 | * Bootstrap the application events. 28 | * 29 | * @return void 30 | */ 31 | public function boot(): void 32 | { 33 | $this->setupConfig(); 34 | } 35 | 36 | /** 37 | * Register the service provider. 38 | * 39 | * @return void 40 | */ 41 | public function register(): void 42 | { 43 | $this->app->singleton('browscap', function (ContainerContract $app) { 44 | $adapter = new LocalFilesystemAdapter(Config::get('browscap.cache')); 45 | $filesystem = new Filesystem($adapter); 46 | $cache = new SimpleCache( 47 | new Flysystem($filesystem) 48 | ); 49 | 50 | $bc = new Browscap( 51 | $cache, 52 | $app->make('log') 53 | ); 54 | 55 | return $bc; 56 | }); 57 | $this->app->bind(BrowscapInterface::class, 'browscap'); 58 | 59 | if ($this->app->runningInConsole()) { 60 | $this->commands([ 61 | Console\CheckUpdateCommand::class, 62 | Console\ConvertCommand::class, 63 | Console\FetchCommand::class, 64 | Console\ParserCommand::class, 65 | Console\UpdateCommand::class, 66 | ]); 67 | } 68 | } 69 | 70 | /** 71 | * Get the services provided by the provider. 72 | * 73 | * @return array 74 | */ 75 | public function provides(): array 76 | { 77 | return ['browscap']; 78 | } 79 | 80 | protected function setupConfig() 81 | { 82 | $source = realpath($raw = __DIR__.'/../config/browscap.php') ?: $raw; 83 | 84 | if ($this->app instanceof LaravelApplication) { 85 | $this->publishes([$source => config_path('browscap.php')]); 86 | } elseif ($this->app instanceof LumenApplication) { 87 | $this->app->configure('browscap'); 88 | } 89 | 90 | $this->mergeConfigFrom($source, 'browscap'); 91 | } 92 | } 93 | --------------------------------------------------------------------------------