-
Notifications
You must be signed in to change notification settings - Fork 154
/
DeviceView.php
547 lines (481 loc) · 11.8 KB
/
DeviceView.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
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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
<?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\Helper;
use Symfony\Component\HttpFoundation\Cookie;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpFoundation\Response;
/**
* DeviceView
*
* @author suncat2000 <nikolay.kotovsky@gmail.com>
*/
class DeviceView
{
const VIEW_MOBILE = 'mobile';
const VIEW_TABLET = 'tablet';
const VIEW_FULL = 'full';
const VIEW_NOT_MOBILE = 'not_mobile';
const COOKIE_KEY_DEFAULT = 'device_view';
const COOKIE_PATH_DEFAULT = '/';
const COOKIE_DOMAIN_DEFAULT = '';
const COOKIE_SECURE_DEFAULT = false;
const COOKIE_HTTP_ONLY_DEFAULT = true;
const COOKIE_RAW_DEFAULT = false;
const COOKIE_SAMESITE_DEFAULT = null;
const COOKIE_EXPIRE_DATETIME_MODIFIER_DEFAULT = '1 month';
const SWITCH_PARAM_DEFAULT = 'device_view';
/**
* @var Request
*/
protected $request;
/**
* @var string
*/
protected $requestedViewType;
/**
* @var string
*/
protected $viewType;
/**
* @var string
*/
protected $cookieKey = self::COOKIE_KEY_DEFAULT;
/**
* @var string
*/
protected $cookiePath = self::COOKIE_PATH_DEFAULT;
/**
* @var string
*/
protected $cookieDomain = self::COOKIE_DOMAIN_DEFAULT;
/**
* @var bool
*/
protected $cookieSecure = self::COOKIE_SECURE_DEFAULT;
/**
* @var bool
*/
protected $cookieHttpOnly = self::COOKIE_HTTP_ONLY_DEFAULT;
/**
* @var bool
*/
protected $cookieRaw = self::COOKIE_RAW_DEFAULT;
/**
* @var string|null
*/
protected $cookieSamesite = self::COOKIE_SAMESITE_DEFAULT;
/**
* @var string
*/
protected $cookieExpireDatetimeModifier = self::COOKIE_EXPIRE_DATETIME_MODIFIER_DEFAULT;
/**
* @var string
*/
protected $switchParam = self::SWITCH_PARAM_DEFAULT;
/**
* @var array
*/
protected $redirectConfig;
/**
* Constructor
*
* @param RequestStack $requestStack
*/
public function __construct(RequestStack $requestStack = null)
{
if (!$requestStack || !$this->request = $requestStack->getMasterRequest()) {
$this->viewType = self::VIEW_NOT_MOBILE;
return;
}
if ($this->request->query->has($this->switchParam)) {
$this->viewType = $this->request->query->get($this->switchParam);
} elseif ($this->request->cookies->has($this->cookieKey)) {
$this->viewType = $this->request->cookies->get($this->cookieKey);
}
$this->requestedViewType = $this->viewType;
}
/**
* Gets the view type for a device.
*
* @return string
*/
public function getViewType()
{
return $this->viewType;
}
/**
* Gets the view type that has explicitly been requested either by switch param, or by cookie.
*
* @return string The requested view type or null if no view type has been explicitly requested.
*/
public function getRequestedViewType()
{
return $this->requestedViewType;
}
/**
* Is the device in full view.
*
* @return boolean
*/
public function isFullView()
{
return $this->viewType === self::VIEW_FULL;
}
/**
* Is the device a tablet view type.
*
* @return boolean
*/
public function isTabletView()
{
return $this->viewType === self::VIEW_TABLET;
}
/**
* Is the device a mobile view type.
*
* @return boolean
*/
public function isMobileView()
{
return $this->viewType === self::VIEW_MOBILE;
}
/**
* Is not the device a mobile view type (PC, Mac, etc.).
*
* @return boolean
*/
public function isNotMobileView()
{
return $this->viewType === self::VIEW_NOT_MOBILE;
}
/**
* Has the Request the switch param in the query string (GET header).
*
* @return boolean
*/
public function hasSwitchParam()
{
return $this->request && $this->request->query->has($this->switchParam);
}
/**
* Sets the view type.
*
* @param string $view
*/
public function setView($view)
{
$this->viewType = $view;
}
/**
* Sets the full (desktop) view type.
*/
public function setFullView()
{
$this->viewType = self::VIEW_FULL;
}
/**
* Sets the tablet view type.
*/
public function setTabletView()
{
$this->viewType = self::VIEW_TABLET;
}
/**
* Sets the mobile view type.
*/
public function setMobileView()
{
$this->viewType = self::VIEW_MOBILE;
}
/**
* Sets the not mobile view type.
*/
public function setNotMobileView()
{
$this->viewType = self::VIEW_NOT_MOBILE;
}
/**
* Gets the switch param value from the query string (GET header).
*
* @return string|null
*/
public function getSwitchParamValue()
{
if (!$this->request) {
return null;
}
return $this->request->query->get($this->switchParam, self::VIEW_FULL);
}
/**
* Getter of RedirectConfig.
*
* @return array
*/
public function getRedirectConfig()
{
return $this->redirectConfig;
}
/**
* Setter of RedirectConfig.
*
* @param array $redirectConfig
*/
public function setRedirectConfig($redirectConfig)
{
$this->redirectConfig = $redirectConfig;
}
/**
* Gets the RedirectResponse by switch param value.
*
* @param string $redirectUrl
*
* @return RedirectResponseWithCookie
*/
public function getRedirectResponseBySwitchParam($redirectUrl)
{
switch ($this->getSwitchParamValue()) {
case self::VIEW_MOBILE:
$viewType = self::VIEW_MOBILE;
break;
case self::VIEW_TABLET:
$viewType = self::VIEW_TABLET;
if (isset($this->redirectConfig['detect_tablet_as_mobile']) && $this->redirectConfig['detect_tablet_as_mobile'] === true) {
$viewType = self::VIEW_MOBILE;
}
break;
default:
$viewType = self::VIEW_FULL;
}
return new RedirectResponseWithCookie($redirectUrl, $this->getStatusCode($viewType), $this->createCookie($viewType));
}
/**
* Modifies the Response for the specified device view.
*
* @param string $view The device view for which the response should be modified.
* @param Response $response
*
* @return Response
*/
public function modifyResponse($view, Response $response)
{
$response->headers->setCookie($this->createCookie($view));
return $response;
}
/**
* Gets the RedirectResponse for the specified device view.
*
* @param string $view The device view for which we want the RedirectResponse.
* @param string $host Uri host
* @param int $statusCode Status code
*
* @return RedirectResponseWithCookie
*/
public function getRedirectResponse($view, $host, $statusCode)
{
return new RedirectResponseWithCookie($host, $statusCode, $this->createCookie($view));
}
/**
* Setter of CookieKey
*
* @param string $cookieKey
*/
public function setCookieKey($cookieKey)
{
$this->cookieKey = $cookieKey;
}
/**
* Getter of CookieKey
*
* @return string
*/
public function getCookieKey()
{
return $this->cookieKey;
}
/**
* Getter of CookiePath.
*
* @return string
*/
public function getCookiePath()
{
return $this->cookiePath;
}
/**
* Setter of CookiePath.
*
* @param string $cookiePath
*/
public function setCookiePath($cookiePath)
{
$this->cookiePath = $cookiePath;
}
/**
* Getter of CookieDomain.
*
* @return string
*/
public function getCookieDomain()
{
return $this->cookieDomain;
}
/**
* Setter of CookieDomain.
*
* @param string $cookieDomain
*/
public function setCookieDomain($cookieDomain)
{
$this->cookieDomain = $cookieDomain;
}
/**
* Is the cookie secure.
*
* @return bool
*/
public function isCookieSecure()
{
return $this->cookieSecure;
}
/**
* Setter of CookieSecure.
*
* @param bool $cookieSecure
*/
public function setCookieSecure($cookieSecure)
{
$this->cookieSecure = $cookieSecure;
}
/**
* Is the cookie http only.
*
* @return bool
*/
public function isCookieHttpOnly()
{
return $this->cookieHttpOnly;
}
/**
* Setter of CookieHttpOnly.
*
* @param bool $cookieHttpOnly
*/
public function setCookieHttpOnly($cookieHttpOnly)
{
$this->cookieHttpOnly = $cookieHttpOnly;
}
/**
* Is the cookie raw.
*
* @return bool
*/
public function isCookieRaw()
{
return $this->cookieRaw;
}
/**
* Setter of CookieRaw.
*
* @param bool $cookieRaw
*/
public function setCookieRaw($cookieRaw)
{
$this->cookieRaw = $cookieRaw;
}
/**
* Getter of CookieSamesite.
*
* @return string|null
*/
public function getCookieSamesite()
{
return $this->cookieSamesite;
}
/**
* Setter of CookieSamesite.
*
* @param string|null $cookieSamesite
*/
public function setCookieSamesite($cookieSamesite)
{
$this->cookieSamesite = $cookieSamesite;
}
/**
* Setter of SwitchParam.
*
* @param string $switchParam
*/
public function setSwitchParam($switchParam)
{
$this->switchParam = $switchParam;
}
/**
* Getter of SwitchParam
*
* @return string
*/
public function getSwitchParam()
{
return $this->switchParam;
}
/**
* @param string $cookieExpireDatetimeModifier
*/
public function setCookieExpireDatetimeModifier($cookieExpireDatetimeModifier)
{
$this->cookieExpireDatetimeModifier = $cookieExpireDatetimeModifier;
}
/**
* @return string
*/
public function getCookieExpireDatetimeModifier()
{
return $this->cookieExpireDatetimeModifier;
}
/**
* Create the Cookie object
*
* @param string $value
*
* @return Cookie
*/
protected function createCookie($value)
{
try {
$expire = new \Datetime($this->getCookieExpireDatetimeModifier());
} catch (\Exception $e) {
$expire = new \Datetime(self::COOKIE_EXPIRE_DATETIME_MODIFIER_DEFAULT);
}
return new Cookie(
$this->getCookieKey(),
$value,
$expire,
$this->getCookiePath(),
$this->getCookieDomain(),
$this->isCookieSecure(),
$this->isCookieHttpOnly(),
$this->isCookieRaw(),
$this->getCookieSamesite()
);
}
/**
* @param string $view
*
* @return integer
*/
protected function getStatusCode($view)
{
if (isset($this->redirectConfig[$view]['status_code'])) {
return $this->redirectConfig[$view]['status_code'];
}
return 302;
}
}