forked from alexandernst/yii2-device-detect
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDeviceDetect.php
50 lines (38 loc) · 1.08 KB
/
DeviceDetect.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
<?php
namespace alexandernst\devicedetect;
use Yii;
use Detection\MobileDetect;
/**
* DeviceDetect
*
* @author Alexander Nestorov <[email protected]>
* @version 0.0.8
*/
class DeviceDetect extends \yii\base\Component {
private $_mobileDetect;
// Automatically set view parameters based on device type
public $setParams = true;
public function __call($name, $parameters) {
return call_user_func_array(
array($this->_mobileDetect, $name),
$parameters
);
}
public function __construct($config = array()) {
parent::__construct($config);
}
public function init() {
$this->_mobileDetect = new MobileDetect();
parent::init();
if ($this->setParams)
\Yii::$app->on(\yii\base\Application::EVENT_BEFORE_REQUEST, function($event){
\Yii::$app->params['devicedetect'] = [
'isMobile' => \Yii::$app->devicedetect->isMobile(),
'isTablet' => \Yii::$app->devicedetect->isTablet()
];
\Yii::$app->params['devicedetect']['isDesktop'] =
!\Yii::$app->params['devicedetect']['isMobile'] &&
!\Yii::$app->params['devicedetect']['isTablet'];
});
}
}