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\ContentBundle\ArticleEvents; |
21
|
|
|
use SWP\Bundle\ContentBundle\Event\ArticleEvent; |
22
|
|
|
use SWP\Bundle\CoreBundle\HttpCache\HttpCacheArticleTagGeneratorInterface; |
23
|
|
|
use SWP\Bundle\CoreBundle\HttpCache\HttpCacheRouteTagGeneratorInterface; |
24
|
|
|
use SWP\Component\MultiTenancy\Context\TenantContextInterface; |
25
|
|
|
use SWP\Component\MultiTenancy\Model\TenantInterface; |
26
|
|
|
use Symfony\Component\EventDispatcher\EventSubscriberInterface; |
27
|
|
|
|
28
|
|
|
class HttpCacheSubscriber implements EventSubscriberInterface |
29
|
|
|
{ |
30
|
|
|
protected $cacheManager; |
31
|
|
|
|
32
|
|
|
protected $logger; |
33
|
|
|
|
34
|
|
|
protected $tenantContext; |
35
|
|
|
|
36
|
|
|
private $articleTagGenerator; |
37
|
|
|
|
38
|
|
|
private $routeTagGenerator; |
39
|
|
|
|
40
|
|
|
public function __construct( |
41
|
|
|
CacheManager $cacheManager, |
42
|
|
|
LoggerInterface $logger, |
43
|
|
|
TenantContextInterface $tenantContext, |
44
|
|
|
HttpCacheArticleTagGeneratorInterface $articleTagGenerator, |
45
|
|
|
HttpCacheRouteTagGeneratorInterface $routeTagGenerator |
46
|
|
|
) { |
47
|
|
|
$this->cacheManager = $cacheManager; |
48
|
|
|
$this->logger = $logger; |
49
|
|
|
$this->tenantContext = $tenantContext; |
50
|
|
|
$this->articleTagGenerator = $articleTagGenerator; |
51
|
|
|
$this->routeTagGenerator = $routeTagGenerator; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
public static function getSubscribedEvents(): array |
55
|
|
|
{ |
56
|
|
|
return [ |
57
|
|
|
ArticleEvents::POST_UPDATE => 'clearCache', |
58
|
|
|
]; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
public function clearCache(ArticleEvent $event): void |
62
|
|
|
{ |
63
|
|
|
$headers = ['host' => $this->getHostName($this->tenantContext->getTenant())]; |
64
|
|
|
|
65
|
|
|
$article = $event->getArticle(); |
66
|
|
|
if ( |
67
|
|
|
null !== $article->getId() |
68
|
|
|
) { |
69
|
|
|
$tags = [ |
70
|
|
|
$this->articleTagGenerator->generateTags($article), |
71
|
|
|
]; |
72
|
|
|
|
73
|
|
|
// Clear article route page (usually article is listed there) |
74
|
|
|
if (null !== $article->getRoute()) { |
75
|
|
|
$tags[] = $this->routeTagGenerator->generateTags($article->getRoute()); |
76
|
|
|
if (null !== $article->getRoute()->getParent()) { |
77
|
|
|
$tags[] = $this->routeTagGenerator->generateTags($article->getRoute()->getParent()); |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
$this->cacheManager->invalidateTags($tags); |
82
|
|
|
|
83
|
|
|
// Invalidate API responses |
84
|
|
|
$this->cacheManager->invalidateRoute('swp_api_content_list_articles', [], $headers); |
85
|
|
|
$this->cacheManager->invalidateRoute('swp_api_content_show_articles', ['id' => $article->getId()], $headers); |
86
|
|
|
|
87
|
|
|
// To be sure that we have fresh front page - clear cache also there |
88
|
|
|
$this->cacheManager->invalidateRoute('homepage', [], $headers); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
try { |
92
|
|
|
$this->cacheManager->flush(); |
93
|
|
|
} catch (ExceptionCollection $e) { |
94
|
|
|
$this->logger->error($e->getMessage()); |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
private function getHostName(TenantInterface $tenant): string |
99
|
|
|
{ |
100
|
|
|
$hostName = $tenant->getDomainName(); |
101
|
|
|
|
102
|
|
|
if (null !== $tenant->getSubdomain()) { |
103
|
|
|
$hostName = $tenant->getSubdomain().'.'.$hostName; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
return $hostName; |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
|