-
Notifications
You must be signed in to change notification settings - Fork 156
Expand file tree
/
Copy pathMobileDetectExtension.php
More file actions
228 lines (199 loc) · 5.65 KB
/
MobileDetectExtension.php
File metadata and controls
228 lines (199 loc) · 5.65 KB
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
<?php
/*
* This file is part of the MobileDetectBundle.
*
* (c) Nikolay Ivlev <nikolay.kotovsky@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace SunCat\MobileDetectBundle\Twig\Extension;
use SunCat\MobileDetectBundle\Helper\DeviceView;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;
/**
* MobileDetectExtension
*
* @author suncat2000 <nikolay.kotovsky@gmail.com>
*/
class MobileDetectExtension extends \Twig_Extension
{
/**
* @var mixed
*/
private $mobileDetector;
/**
* @var \SunCat\MobileDetectBundle\Helper\DeviceView
*/
private $deviceView;
/**
* @var array
*/
private $redirectConf;
/**
* @var Request
*/
private $request;
/**
* MobileDetectExtension constructor.
*
* @param mixed $mobileDetector
* @param DeviceView $deviceView
* @param array $redirectConf
*/
public function __construct($mobileDetector, DeviceView $deviceView, array $redirectConf)
{
$this->mobileDetector = $mobileDetector;
$this->deviceView = $deviceView;
$this->redirectConf = $redirectConf;
}
/**
* Get extension twig function
* @return array
*/
public function getFunctions()
{
return array(
new \Twig_SimpleFunction('is_mobile', array($this, 'isMobile')),
new \Twig_SimpleFunction('is_tablet', array($this, 'isTablet')),
new \Twig_SimpleFunction('is_device', array($this, 'isDevice')),
new \Twig_SimpleFunction('is_full_view', array($this, 'isFullView')),
new \Twig_SimpleFunction('is_mobile_view', array($this, 'isMobileView')),
new \Twig_SimpleFunction('is_tablet_view', array($this, 'isTabletView')),
new \Twig_SimpleFunction('is_not_mobile_view', array($this, 'isNotMobileView')),
new \Twig_SimpleFunction('is_ios', array($this, 'isIOS')),
new \Twig_SimpleFunction('is_android_os', array($this, 'isAndroidOS')),
new \Twig_SimpleFunction('full_view_url', array($this, 'fullViewUrl'), array('is_safe' => array('html')))
);
}
/**
* Regardless of the current view, returns the URL that leads to the equivalent page
* in the full/desktop view. This is useful for generating <link rel="canonical"> tags
* on mobile pages for Search Engine Optimization.
* See: http://searchengineland.com/the-definitive-guide-to-mobile-technical-seo-166066
* @return string
*/
public function fullViewUrl($addCurrentPathAndQuery = true)
{
if (!isset($this->redirectConf[DeviceView::VIEW_FULL]['host'])) {
// The host property has not been configured for the full view
return null;
}
$fullHost = $this->redirectConf[DeviceView::VIEW_FULL]['host'];
if (empty($fullHost)) {
return null;
}
// If not in request scope, we can only return the base URL to the full view
if (!$this->request) {
return $fullHost;
}
if (false === $addCurrentPathAndQuery) {
return $fullHost;
}
// if fullHost ends with /, skip it since getPathInfo() also starts with /
$result = rtrim($fullHost, '/') . $this->request->getPathInfo();
$query = Request::normalizeQueryString(http_build_query($this->request->query->all(), null, '&'));
if ($query) {
$result .= '?' . $query;
}
return $result;
}
/**
* Is mobile
* @return boolean
*/
public function isMobile()
{
return $this->mobileDetector->isMobile();
}
/**
* Is tablet
* @return boolean
*/
public function isTablet()
{
return $this->mobileDetector->isTablet();
}
/**
* Is device
* @param string $deviceName is[iPhone|BlackBerry|HTC|Nexus|Dell|Motorola|Samsung|Sony|Asus|Palm|Vertu|...]
*
* @return boolean
*/
public function isDevice($deviceName)
{
$magicMethodName = 'is' . strtolower((string) $deviceName);
return $this->mobileDetector->$magicMethodName();
}
/**
* Is full view type
*
* @return boolean
*/
public function isFullView()
{
return $this->deviceView->isFullView();
}
/**
* Is mobile view type
*
* @return boolean
*/
public function isMobileView()
{
return $this->deviceView->isMobileView();
}
/**
* Is tablet view type
*
* @return boolean
*/
public function isTabletView()
{
return $this->deviceView->isTabletView();
}
/**
* Is not mobile view type
*
* @return boolean
*/
public function isNotMobileView()
{
return $this->deviceView->isNotMobileView();
}
/**
* Is iOS
*
* @return boolean
*/
public function isIOS()
{
return $this->mobileDetector->isIOS();
}
/**
* Is Android OS
*
* @return boolean
*/
public function isAndroidOS()
{
return $this->mobileDetector->isAndroidOS();
}
/**
* Sets the request from the current scope.
* @param Request $request
*/
public function setRequestByRequestStack(RequestStack $requestStack = null) {
if (null !== $requestStack) {
$this->request = $requestStack->getMasterRequest();
}
}
/**
* Extension name
* @return string
*/
public function getName()
{
return 'mobile_detect.twig.extension';
}
}