1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Superdesk Web Publisher Template Engine Bundle. |
5
|
|
|
* |
6
|
|
|
* Copyright 2015 Sourcefabric z.u. and contributors. |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please see the |
9
|
|
|
* AUTHORS and LICENSE files distributed with this source code. |
10
|
|
|
* |
11
|
|
|
* @copyright 2015 Sourcefabric z.ú |
12
|
|
|
* @license http://www.superdesk.org/license |
13
|
|
|
*/ |
14
|
|
|
|
15
|
|
|
namespace SWP\Bundle\CoreBundle\EventSubscriber; |
16
|
|
|
|
17
|
|
|
use FOS\HttpCache\Exception\ExceptionCollection; |
18
|
|
|
use FOS\HttpCacheBundle\CacheManager; |
19
|
|
|
use Psr\Log\LoggerInterface; |
20
|
|
|
use SWP\Bundle\CoreBundle\Model\ArticleInterface; |
21
|
|
|
use SWP\Component\Common\Event\HttpCacheEvent; |
22
|
|
|
use SWP\Bundle\CoreBundle\Model\ContainerInterface; |
23
|
|
|
use SWP\Component\MultiTenancy\Context\TenantContextInterface; |
24
|
|
|
use SWP\Component\MultiTenancy\Model\TenantInterface; |
25
|
|
|
use Symfony\Component\EventDispatcher\EventSubscriberInterface; |
26
|
|
|
|
27
|
|
|
class HttpCacheSubscriber implements EventSubscriberInterface |
28
|
|
|
{ |
29
|
|
|
/** |
30
|
|
|
* @var CacheManager |
31
|
|
|
*/ |
32
|
|
|
protected $cacheManager; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var LoggerInterface |
36
|
1 |
|
*/ |
37
|
|
|
protected $logger; |
38
|
1 |
|
|
39
|
1 |
|
/** |
40
|
1 |
|
* @var TenantContextInterface |
41
|
|
|
*/ |
42
|
111 |
|
protected $tenantContext; |
43
|
|
|
|
44
|
|
|
/** |
45
|
111 |
|
* HttpCacheSubscriber constructor. |
46
|
|
|
* |
47
|
111 |
|
* @param CacheManager $cacheManager |
48
|
|
|
* @param LoggerInterface $logger |
49
|
|
|
*/ |
50
|
|
|
public function __construct(CacheManager $cacheManager, LoggerInterface $logger, TenantContextInterface $tenantContext) |
51
|
|
|
{ |
52
|
|
|
$this->cacheManager = $cacheManager; |
53
|
|
|
$this->logger = $logger; |
54
|
|
|
$this->tenantContext = $tenantContext; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @return array |
59
|
|
|
*/ |
60
|
|
|
public static function getSubscribedEvents() |
61
|
|
|
{ |
62
|
|
|
return [ |
63
|
|
|
HttpCacheEvent::EVENT_NAME => [ |
64
|
|
|
['clearCache', 0], |
65
|
|
|
], |
66
|
|
|
]; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
public function clearCache(HttpCacheEvent $event): void |
70
|
|
|
{ |
71
|
|
|
$headers = ['host' => $this->getHostName($this->tenantContext->getTenant())]; |
72
|
|
|
switch (true) { |
73
|
|
|
case $event->getSubject() instanceof ContainerInterface: |
74
|
|
|
$this->cacheManager->invalidateRoute('swp_api_templates_list_containers', [], $headers); |
75
|
|
|
$this->cacheManager->invalidateRoute('swp_api_templates_get_container', [ |
76
|
|
|
'uuid' => $event->getSubject()->getUuid(), |
77
|
|
|
], $headers); |
78
|
|
|
|
79
|
|
|
break; |
80
|
|
|
|
81
|
|
|
case $event->getSubject() instanceof ArticleInterface: |
82
|
|
|
/** @var ArticleInterface $article */ |
83
|
|
|
$article = $event->getSubject(); |
84
|
|
|
if (ArticleInterface::STATUS_PUBLISHED === $article->getStatus() && |
85
|
|
|
$article->getPublishedAt() >= (new \DateTime('now'))->modify('-1 hour') |
86
|
|
|
) { |
87
|
|
|
if (null !== $article->getRoute()) { |
88
|
|
|
$this->cacheManager->invalidateRoute($article, [], $headers); |
|
|
|
|
89
|
|
|
$this->cacheManager->invalidateRoute($article->getRoute(), [], $headers); |
|
|
|
|
90
|
|
|
} |
91
|
|
|
$this->cacheManager->invalidateRoute('swp_api_content_list_articles', [], $headers); |
92
|
|
|
$this->cacheManager->invalidateRoute('swp_api_content_show_articles', [ |
93
|
|
|
'id' => $article->getId(), |
94
|
|
|
], $headers); |
95
|
|
|
|
96
|
|
|
$this->cacheManager->invalidateRoute('homepage', [], $headers); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
break; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
try { |
103
|
|
|
$this->cacheManager->flush(); |
104
|
|
|
} catch (ExceptionCollection $e) { |
105
|
|
|
$this->logger->error($e->getMessage()); |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
private function getHostName(TenantInterface $tenant): string |
110
|
|
|
{ |
111
|
|
|
$hostName = $tenant->getDomainName(); |
112
|
|
|
|
113
|
|
|
if (null !== $tenant->getSubdomain()) { |
114
|
|
|
$hostName = $tenant->getSubdomain().'.'.$hostName; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
return $hostName; |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: