|
1
|
|
|
<?php |
|
2
|
|
|
namespace Strontium\PjaxBundle\EventListener; |
|
3
|
|
|
|
|
4
|
|
|
use Strontium\PjaxBundle\Helper\PjaxHelperInterface; |
|
5
|
|
|
use Symfony\Component\HttpFoundation\Cookie; |
|
6
|
|
|
use Symfony\Component\HttpFoundation\Response; |
|
7
|
|
|
use Symfony\Component\HttpKernel\Event\FilterResponseEvent; |
|
8
|
|
|
|
|
9
|
|
|
class KernelResponseListener |
|
10
|
|
|
{ |
|
11
|
|
|
/** |
|
12
|
|
|
* @var PjaxHelperInterface |
|
13
|
|
|
*/ |
|
14
|
|
|
protected $pjax; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* @param PjaxHelperInterface $pjax |
|
18
|
|
|
*/ |
|
19
|
|
|
public function __construct(PjaxHelperInterface $pjax) |
|
20
|
|
|
{ |
|
21
|
|
|
$this->pjax = $pjax; |
|
22
|
|
|
} |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @param FilterResponseEvent $event |
|
26
|
|
|
*/ |
|
27
|
|
|
public function addPjaxVersion(FilterResponseEvent $event) |
|
28
|
|
|
{ |
|
29
|
|
|
$request = $event->getRequest(); |
|
30
|
|
|
|
|
31
|
|
|
if ($this->pjax->isPjaxRequest($request) && $this->pjax->haveGenerator()) { |
|
32
|
|
|
$response = $event->getResponse(); |
|
33
|
|
|
$response->headers->set('X-PJAX-Version', $this->pjax->generateVersion($request)); |
|
34
|
|
|
} |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @param FilterResponseEvent $event |
|
39
|
|
|
* |
|
40
|
|
|
* @return Response |
|
41
|
|
|
*/ |
|
42
|
|
|
public function pjaxRedirect(FilterResponseEvent $event) |
|
43
|
|
|
{ |
|
44
|
|
|
$request = $event->getRequest(); |
|
45
|
|
|
$response = $event->getResponse(); |
|
46
|
|
|
|
|
47
|
|
|
if ($this->pjax->isPjaxRequest($request) && $response->isRedirect()) { |
|
48
|
|
|
$redirectCookieName = sprintf('pjax_redirect_%s', $request->headers->get('X-PJAX-Target')); |
|
49
|
|
|
$redirectTo = $response->headers->get('Location'); |
|
50
|
|
|
$response->headers->setCookie( |
|
51
|
|
|
new Cookie(rawurlencode($redirectCookieName), $redirectTo, 0, '/', null, false, false) |
|
|
|
|
|
|
52
|
|
|
); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
return $response; |
|
56
|
|
|
} |
|
57
|
|
|
} |
|
58
|
|
|
|
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.