Skip to content

Commit

Permalink
Initial Commit
Browse files Browse the repository at this point in the history
  • Loading branch information
karmendra committed Feb 17, 2020
0 parents commit 6f4c7e2
Show file tree
Hide file tree
Showing 7 changed files with 993 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/.idea
/vendor
composer.lock
674 changes: 674 additions & 0 deletions LICENSE

Large diffs are not rendered by default.

123 changes: 123 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
Laravel Agent Detector
=====

[![Latest Stable Version](http://img.shields.io/packagist/v/karmendra/laravel-agent-detector.svg)](https://packagist.org/packages/karmendra/laravel-agent-detector) [![Total Downloads](http://img.shields.io/packagist/dm/karmendra/laravel-agent-detector.svg)](https://packagist.org/packages/karmendra/laravel-agent-detector)

A PHP desktop/mobile user agent parser for Laravel, based on [Device Detector](https://github.com/matomo-org/device-detector) that will parse any User Agent and detect the browser, operating system, device used (desktop, tablet, mobile, tv, cars, console, etc.), brand and model.

Goto http://devicedetector.net for a quick demo of what this library can detect.

Installation
------------

Install using composer:

```bash
composer require karmendra/laravel-agent-detector
```

Laravel (optional)
------------------

Add the service provider in `config/app.php`:

```php
Karmendra\LaravelAgentDetector\AgentDetectorServiceProvider::class,
```

And add the AgentDetector alias to `config/app.php`:

```php
'AgentDetector' => Karmendra\LaravelAgentDetector\Facades\AgentDetector::class,
```

Basic Usage
-----------

Start by creating an `AgentDetector` instance (or use the `AgentDetector` Facade):

```php
use Karmendra\LaravelAgentDetector\AgentDetector;

$dd = new AgentDetector();
```

All of the original [Device Detector](https://github.com/matomo-org/device-detector) methods are still available


Additional Functionality
------------------------

### Device type

Get the device type. (`smartphone`, `mobile`, `tablet`, `desktop`, `bot` ...)

```php
$type = $ad->device();
```

### Device brand name

Get the device manufactures brand name, if mobile. (`Apple`, `Nexus`, `Samsung`, ...)

```php
$brand = $ad->deviceBrand();
```

### Device model

Get the device model, if mobile. (Apple: `iPhone`, Samsung: `Galaxy S9` ...)

```php
$model = $ad->deviceModel();
```

### Platform / Operating system name

Get the operating system. (`Ubuntu`, `Android`, `Windows`, `Mac`, `iOS`, ...)

```php
$platform = $ad->platform();
```

### Platform version (OS version).

Get the platform version. (Windows: `10`, Mac: `10.11`, Android: `9.0`, ...)

```php
$platform_version = $ad->platformVersion();
```

### Browser name

Get the browser name. (`Chrome`, `IE`, `Safari`, `UC Browser`, ...)

```php
$browser = $ad->browser();
```

### Browser version

Get the browser name. (Chrome: `56`, Safari: `9.0`, Firefox: `48`, ...)

```php
$browser_version = $ad->browserVersion();
```

### Robot detection

Check if the user is a robot. (`true` or `false`)

```php
$ad->isBot();
```

To-do (PR welcome):
------
- Adding test cases
- Adding additional functions for getting robot info, enable disable robot detection etc.

License
-------

Laravel Agent Detector is licensed under [The MIT License (MIT)](LICENSE).
36 changes: 36 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
{
"name": "karmendra/laravel-agent-detector",
"description": "Laravel wrapper for matomo-org/device-detector user agent parser",
"keywords": ["laravel", "useragent", "agent", "user agent", "browser", "platform", "mobile", "desktop", "device-detector"],
"homepage": "https://github.com/karmendra/laravel-agent-detector",
"license" : "MIT",
"authors": [
{
"name": "Karmendra Suthar",
"email": "karmendra.js@gmail.com"
}
],
"require": {
"piwik/device-detector": "^3.12"
},
"require-dev": {
},
"autoload": {
"psr-4": {
"Karmendra\\LaravelAgentDetector\\": "src/"
}
},
"extra": {
"laravel": {
"providers": [
"Karmendra\\LaravelAgentDetector\\AgentDetectorServiceProvider"
],
"aliases": {
"Agent": "Karmendra\\LaravelAgentDetector\\Facades\\AgentDetector"
}
}
},
"suggest": {
"illuminate/support": "Required for laravel service providers"
}
}
102 changes: 102 additions & 0 deletions src/AgentDetector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
<?php

namespace Karmendra\LaravelAgentDetector;

use DeviceDetector\DeviceDetector;

class AgentDetector extends DeviceDetector
{
/**
* AgentDetector constructor.
*
* @param string $userAgent
*/
public function __construct($userAgent = '')
{
parent::__construct($userAgent);
$this->parse();
}

/**
* Get the device name.
*
* @return string
*/
public function device()
{
if ($this->isBot()) {
return 'bot';
}

return $this->getDeviceName();
}

/**
* Get the device Brand Name.
*
* @return string
*/
public function deviceBrand()
{
return $this->getBrandName();
}

/**
* Get the device Brand Name.
*
* @return string
*/
public function deviceModel()
{
return $this->getModel();
}

/**
* Get the browser name.
*
* @return string
*/
public function browser()
{
$browser = $this->getClient('name');

return $browser == 'UNK' ? '' : $browser;
}

/**
* Get the browser version.
*
* @return string
*/
public function browserVersion()
{
$browser_version = $this->getClient('version');

return $browser_version == 'UNK' ? '' : $browser_version;
}

/**
* Get the platform/os name.
*
* @return string
*/
public function platform()
{
$platform = $this->getOs('name');

return $platform == 'UNK' ? '' : $platform;
}

/**
* Get the platform/os Version.
*
* @return string
*/
public function platformVersion()
{
$platform_version = $this->getOs('version');

return $platform_version == 'UNK' ? '' : $platform_version;
}

}
37 changes: 37 additions & 0 deletions src/AgentDetectorServiceProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

namespace Karmendra\LaravelAgentDetector;

use Illuminate\Support\ServiceProvider;

class AgentDetectorServiceProvider extends ServiceProvider
{
/**
* Indicates if loading of the provider is deferred.
*
* @var bool
*/
protected $defer = true;

/**
* Register the service provider.
*/
public function register()
{
$this->app->singleton('agent_detector', function ($app) {
return new AgentDetector($app['request']->server());
});

$this->app->alias('agent_detector', AgentDetector::class);
}

/**
* Get the services provided by the provider.
*
* @return array
*/
public function provides()
{
return ['agent_detector', AgentDetector::class];
}
}
18 changes: 18 additions & 0 deletions src/Facades/AgentDetector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace Karmendra\LaravelAgentDetector\Facades;

use Illuminate\Support\Facades\Facade;

class AgentDetector extends Facade
{
/**
* Get the registered name of the component.
*
* @return string
*/
protected static function getFacadeAccessor()
{
return 'agent_detector';
}
}

0 comments on commit 6f4c7e2

Please sign in to comment.