1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the FOSHttpCacheBundle package. |
5
|
|
|
* |
6
|
|
|
* (c) FriendsOfSymfony <http://friendsofsymfony.github.com/> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace FOS\HttpCacheBundle\EventListener; |
13
|
|
|
|
14
|
|
|
use Symfony\Component\EventDispatcher\EventSubscriberInterface; |
15
|
|
|
use Symfony\Component\HttpKernel\Event\FilterResponseEvent; |
16
|
|
|
use Symfony\Component\HttpFoundation\Session\Session; |
17
|
|
|
use Symfony\Component\HttpFoundation\Cookie; |
18
|
|
|
use Symfony\Component\HttpFoundation\ResponseHeaderBag; |
19
|
|
|
use Symfony\Component\HttpKernel\HttpKernel; |
20
|
|
|
use Symfony\Component\HttpKernel\KernelEvents; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* This event handler reads all flash messages and moves them into a cookie. |
24
|
|
|
* |
25
|
|
|
* @author Lukas Kahwe Smith <[email protected]> |
26
|
|
|
*/ |
27
|
|
|
class FlashMessageListener implements EventSubscriberInterface |
28
|
|
|
{ |
29
|
|
|
/** |
30
|
|
|
* @var array |
31
|
|
|
*/ |
32
|
|
|
private $options; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var Session |
36
|
|
|
*/ |
37
|
|
|
private $session; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Set a serializer instance. |
41
|
|
|
* |
42
|
|
|
* @param Session $session A session instance |
43
|
|
|
* @param array $options |
44
|
|
|
*/ |
45
|
|
|
public function __construct($session, array $options = array()) |
46
|
|
|
{ |
47
|
|
|
$this->session = $session; |
48
|
|
|
$this->options = $options; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* {@inheritdoc} |
53
|
|
|
*/ |
54
|
|
|
public static function getSubscribedEvents() |
55
|
|
|
{ |
56
|
|
|
return array( |
57
|
|
|
KernelEvents::RESPONSE => 'onKernelResponse', |
58
|
|
|
); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Moves flash messages from the session to a cookie inside a Response Kernel listener. |
63
|
|
|
* |
64
|
|
|
* @param FilterResponseEvent $event |
65
|
|
|
*/ |
66
|
|
|
public function onKernelResponse(FilterResponseEvent $event) |
67
|
|
|
{ |
68
|
|
|
if ($event->getRequestType() !== HttpKernel::MASTER_REQUEST) { |
69
|
|
|
return; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
// Flash messages are stored in the session. If there is none, there |
73
|
|
|
// can't be any flash messages in it. $session->getFlashBag() would |
74
|
|
|
// create a session, we need to avoid that. |
75
|
|
|
if (!$this->session->isStarted()) { |
76
|
|
|
return; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
$flashBag = $this->session->getFlashBag(); |
80
|
|
|
$flashes = $flashBag->all(); |
81
|
|
|
|
82
|
|
|
if (empty($flashes)) { |
83
|
|
|
return; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
$response = $event->getResponse(); |
87
|
|
|
|
88
|
|
|
$cookies = $response->headers->getCookies(ResponseHeaderBag::COOKIES_ARRAY); |
89
|
|
|
if (isset($cookies[$this->options['host']][$this->options['path']][$this->options['name']])) { |
90
|
|
|
$rawCookie = $cookies[$this->options['host']][$this->options['path']][$this->options['name']]->getValue(); |
91
|
|
|
$flashes = array_merge($flashes, json_decode($rawCookie)); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
$cookie = new Cookie( |
95
|
|
|
$this->options['name'], |
96
|
|
|
json_encode($flashes), |
97
|
|
|
0, |
98
|
|
|
$this->options['path'], |
99
|
|
|
$this->options['host'], |
100
|
|
|
$this->options['secure'], |
101
|
|
|
false |
102
|
|
|
); |
103
|
|
|
|
104
|
|
|
$response->headers->setCookie($cookie); |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|