-
Notifications
You must be signed in to change notification settings - Fork 37
/
DeviceDetect.php
62 lines (51 loc) · 1.43 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
51
52
53
54
55
56
57
58
59
60
61
62
<?php
namespace alexandernst\devicedetect;
use Yii;
use Detection\MobileDetect;
/**
* DeviceDetect
*
* @author Alexander Nestorov <[email protected]>
* @method DeviceDetect isMobile()
* @method DeviceDetect isTablet()
* @version 0.0.12
*/
class DeviceDetect extends \yii\base\Component {
/**
* @var MobileDetect
*/
private $_mobileDetect;
// Automatically set view parameters based on device type
public $setParams = true;
// Automatically set alias parameters based on device type
public $setAlias = 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->params['devicedetect'] = [
'isMobile' => $this->_mobileDetect->isMobile() && !$this->_mobileDetect->isTablet(),
'isTablet' => $this->_mobileDetect->isTablet() && !$this->_mobileDetect->isMobile(),
'isDesktop' => !$this->_mobileDetect->isTablet() && !$this->_mobileDetect->isMobile(),
];
}
if ($this->setAlias) {
if ($this->_mobileDetect->isMobile()) {
\Yii::setAlias('@device', 'mobile');
} else if ($this->_mobileDetect->isTablet()) {
\Yii::setAlias('@device', 'tablet');
} else {
\Yii::setAlias('@device', 'desktop');
}
}
}
}