1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace MovingImage\Bundle\VMProApiBundle\EventListener; |
4
|
|
|
|
5
|
|
|
use MovingImage\Bundle\VMProApiBundle\Decorator\BlackholeCacheItemPoolDecorator; |
6
|
|
|
use MovingImage\Client\VMPro\ApiClient\AbstractApiClient; |
7
|
|
|
use Symfony\Component\DependencyInjection\ContainerInterface; |
8
|
|
|
use Symfony\Component\HttpKernel\Event\GetResponseEvent; |
9
|
|
|
use Symfony\Component\HttpKernel\KernelEvents; |
10
|
|
|
use Symfony\Component\EventDispatcher\EventSubscriberInterface; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* This listener kicks in only if the `cache_bypass_argument` bundle config option is set. |
14
|
|
|
* If the request contains an argument matching the value configured in the aforementioned config option |
15
|
|
|
* and the value of that argument evaluates to true, this listener will modify the cache pool implementation |
16
|
|
|
* used by the VMPro API client, by decorating it with a blackhole cache implementation: |
17
|
|
|
* one that stores responses to cache, but never returns a hit. |
18
|
|
|
*/ |
19
|
|
|
class BypassCacheListener implements EventSubscriberInterface |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* @var ContainerInterface |
23
|
|
|
*/ |
24
|
|
|
private $container; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var string|null |
28
|
|
|
*/ |
29
|
|
|
private $cacheBypassArgument; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @param ContainerInterface $container |
33
|
|
|
* @param string $cacheBypassArgument |
34
|
|
|
*/ |
35
|
|
|
public function __construct(ContainerInterface $container, $cacheBypassArgument = null) |
36
|
|
|
{ |
37
|
|
|
$this->container = $container; |
38
|
|
|
$this->cacheBypassArgument = $cacheBypassArgument; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @param GetResponseEvent $event |
43
|
|
|
*/ |
44
|
|
|
public function onKernelRequest(GetResponseEvent $event) |
45
|
|
|
{ |
46
|
|
|
if (!$this->cacheBypassArgument) { |
|
|
|
|
47
|
|
|
return; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
$request = $event->getRequest(); |
51
|
|
|
|
52
|
|
|
if ($request->get($this->cacheBypassArgument)) { |
53
|
|
|
/** @var AbstractApiClient $apiClient */ |
54
|
|
|
$apiClient = $this->container->get('vmpro_api.client'); |
55
|
|
|
$cachePool = new BlackholeCacheItemPoolDecorator($apiClient->getCacheItemPool()); |
|
|
|
|
56
|
|
|
$apiClient->setCacheItemPool($cachePool); |
|
|
|
|
57
|
|
|
} |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @return array |
62
|
|
|
*/ |
63
|
|
|
public static function getSubscribedEvents() |
64
|
|
|
{ |
65
|
|
|
return [ |
66
|
|
|
KernelEvents::REQUEST => 'onKernelRequest', |
67
|
|
|
]; |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
|
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
string
values, the empty string''
is a special case, in particular the following results might be unexpected: