Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
InvalidateVarnishObserver.php
Go to the documentation of this file.
1 <?php
7 
9 
11 {
17  protected $config;
18 
22  protected $purgeCache;
23 
29  private $tagResolver;
30 
35  public function __construct(
36  \Magento\PageCache\Model\Config $config,
37  \Magento\CacheInvalidate\Model\PurgeCache $purgeCache
38  ) {
39  $this->config = $config;
40  $this->purgeCache = $purgeCache;
41  }
42 
50  public function execute(\Magento\Framework\Event\Observer $observer)
51  {
52  $object = $observer->getEvent()->getObject();
53  if (!is_object($object)) {
54  return;
55  }
56  if ($this->config->getType() == \Magento\PageCache\Model\Config::VARNISH && $this->config->isEnabled()) {
57  $bareTags = $this->getTagResolver()->getTags($object);
58 
59  $tags = [];
60  $pattern = "((^|,)%s(,|$))";
61  foreach ($bareTags as $tag) {
62  $tags[] = sprintf($pattern, $tag);
63  }
64  if (!empty($tags)) {
65  $this->purgeCache->sendPurgeRequest(implode('|', array_unique($tags)));
66  }
67  }
68  }
69 
74  private function getTagResolver()
75  {
76  if ($this->tagResolver === null) {
78  ->get(\Magento\Framework\App\Cache\Tag\Resolver::class);
79  }
80  return $this->tagResolver;
81  }
82 }
$pattern
Definition: website.php:22
__construct(\Magento\PageCache\Model\Config $config, \Magento\CacheInvalidate\Model\PurgeCache $purgeCache)