DeviceDetector jest narzędziem które na podstawie UserAgent wykrywa i zwraca nam przydatne informacje takie jak przeglądarka, system, urządzenie.
Ciekawszą opcją jest możliwość wykrywania czy UserAgent jest Botem czy nie.
Narzędzie ma możliwość używania Cache. Domyślnie wspierany jest doctrine/cache. Mamy też możliwość zaimplementowania własnego systemu cache. Aby to zrobić musimy zaimplementować interface DeviceDetector\Cache\Cache
.
Ja postanowiłem napisać bardzo proste Proxy które umożliwia nam użycie aktualnego systemu Cache które jest zaimplementowane w Laravel 5.
Opis
Klasa Proxy opiera się o aktualnie wybrany Cache Driver który możemy zdefiniować w pliku .env
w zmiennej CACHE_DRIVER
.
Klasa domyślnie powinna być ulokowana w app/Proxy/DeviceDetectorCacheProxy.php
Klasa
<?php
namespace App\Proxy;
use DeviceDetector\Cache\Cache;
/**
* Class used as Proxy between Device Detector and Laravel system cache.
*
* Class DeviceDetectorCacheProxy
* @package App\Proxy
*/
class DeviceDetectorCacheProxy implements Cache
{
/** @var \Illuminate\Contracts\Cache\Repository|\Illuminate\Contracts\Cache\Store $driver */
protected $driver;
/**
* DeviceDetectorCacheProxy constructor.
*
* @param null $driver Driver used in Laravel
*/
public function __construct($driver = null)
{
$this->driver = app('cache')->driver($driver);
}
/**
* Fetch item from Cache
*
* @param $id
* @return mixed
*/
public function fetch($id)
{
return $this->driver->get($id);
}
/**
* Check is cache has $id.
* @param $id
*/
public function contains($id)
{
$this->driver->has($id);
}
/**
* Save data to cache.
*
* @param $id
* @param $data
* @param int $lifeTime
*/
public function save($id, $data, $lifeTime = 0)
{
$this->driver->add($id, $data, $lifeTime);
}
/**
* Delete key from Cache
*
* @param $id
*/
public function delete($id)
{
$this->driver->forget($id);
}
/**
* Remove all data from cache
*/
public function flushAll()
{
$this->driver->flush();
}
/**
* Returns current used driver
*
* @return \Illuminate\Contracts\Cache\Repository|\Illuminate\Contracts\Cache\Store
*/
public function getDriver()
{
return $this->driver;
}
}