1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* GpsLab component. |
4
|
|
|
* |
5
|
|
|
* @author Peter Gribanov <[email protected]> |
6
|
|
|
* @copyright Copyright (c) 2016, Peter Gribanov |
7
|
|
|
* @license http://opensource.org/licenses/MIT |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace GpsLab\Bundle\DateBundle\Event\Listener; |
11
|
|
|
|
12
|
|
|
use GpsLab\Bundle\DateBundle\TimeZone\Keeper\KeeperInterface; |
13
|
|
|
use Symfony\Component\HttpKernel\Event\FilterResponseEvent; |
14
|
|
|
use Symfony\Component\HttpFoundation\Cookie; |
15
|
|
|
|
16
|
|
|
class ResponseListener |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* @var KeeperInterface |
20
|
|
|
*/ |
21
|
|
|
protected $keeper; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var string |
25
|
|
|
*/ |
26
|
|
|
protected $tz_param_name = ''; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var string |
30
|
|
|
*/ |
31
|
|
|
protected $tz_param_offset = ''; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @param KeeperInterface $tz_keeper |
35
|
|
|
* @param string $tz_param_name |
36
|
|
|
* @param string $tz_param_offset |
37
|
|
|
*/ |
38
|
|
|
public function __construct( |
39
|
|
|
KeeperInterface $tz_keeper, |
40
|
|
|
$tz_param_name, |
41
|
|
|
$tz_param_offset |
42
|
|
|
) { |
43
|
|
|
$this->keeper = $tz_keeper; |
44
|
|
|
$this->tz_param_name = $tz_param_name; |
45
|
|
|
$this->tz_param_offset = $tz_param_offset; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Save user time zone |
50
|
|
|
* |
51
|
|
|
* @param FilterResponseEvent $event |
52
|
|
|
*/ |
53
|
|
|
public function onKernelResponseSaveUserTimeZone(FilterResponseEvent $event) |
54
|
|
|
{ |
55
|
|
|
if ($event->isMasterRequest()) { |
56
|
|
|
$cookies = $event->getRequest()->cookies; |
57
|
|
|
$headers = $event->getResponse()->headers; |
58
|
|
|
$tz = $this->keeper->getUserTimeZone(); |
59
|
|
|
$offset = $tz->getOffset($this->keeper->getDefaultDateTime()); |
60
|
|
|
|
61
|
|
|
if ( |
62
|
|
|
$cookies->get($this->tz_param_name) != $tz->getName() || |
63
|
|
|
$cookies->get($this->tz_param_offset) != $offset |
64
|
|
|
) { |
65
|
|
|
$headers->setCookie($this->getCookie($this->tz_param_name, $tz->getName())); |
66
|
|
|
$headers->setCookie($this->getCookie($this->tz_param_offset, $offset)); |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @param string $name |
73
|
|
|
* @param string $value |
74
|
|
|
* |
75
|
|
|
* @return Cookie |
76
|
|
|
*/ |
77
|
|
|
protected function getCookie($name, $value) |
78
|
|
|
{ |
79
|
|
|
return new Cookie($name, $value, strtotime('+1 year'), '/', null, false, false); |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|