Skip to content

Removed ObjectManager usage #26711

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 18 additions & 33 deletions app/code/Magento/PageCache/Observer/FlushCacheByTags.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
<?php
/**
*
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
Expand All @@ -9,6 +8,9 @@
use Magento\Framework\App\ObjectManager;
use Magento\Framework\Event\ObserverInterface;

/**
* Class to flush cache by tags
*/
class FlushCacheByTags implements ObserverInterface
{
/**
Expand Down Expand Up @@ -40,15 +42,25 @@ class FlushCacheByTags implements ObserverInterface
/**
* @param \Magento\PageCache\Model\Config $config
* @param \Magento\Framework\App\PageCache\Cache $cache
* @param \Magento\Framework\App\Cache\Tag\Resolver $tagResolver
* @param \Magento\PageCache\Model\Cache\Type $fullPageCache
*/
public function __construct(\Magento\PageCache\Model\Config $config, \Magento\Framework\App\PageCache\Cache $cache)
{
public function __construct(
\Magento\PageCache\Model\Config $config,
\Magento\Framework\App\PageCache\Cache $cache,
\Magento\Framework\App\Cache\Tag\Resolver $tagResolver,
\Magento\PageCache\Model\Cache\Type $fullPageCache
) {
$this->_config = $config;
$this->_cache = $cache;
$this->tagResolver = $tagResolver;
$this->fullPageCache = $fullPageCache;
}

/**
* If Built-In caching is enabled it collects array of tags
* Flushes cache
*
* If built-in caching is enabled it collects array of tags
* of incoming object and asks to clean cache.
*
* @param \Magento\Framework\Event\Observer $observer
Expand All @@ -61,38 +73,11 @@ public function execute(\Magento\Framework\Event\Observer $observer)
if (!is_object($object)) {
return;
}
$tags = $this->getTagResolver()->getTags($object);
$tags = $this->tagResolver->getTags($object);

if (!empty($tags)) {
$this->getCache()->clean(\Zend_Cache::CLEANING_MODE_MATCHING_ANY_TAG, array_unique($tags));
$this->fullPageCache->clean(\Zend_Cache::CLEANING_MODE_MATCHING_ANY_TAG, array_unique($tags));
}
}
}

/**
* TODO: Workaround to support backwards compatibility, will rework to use Dependency Injection in MAGETWO-49547
*
*
* @return \Magento\PageCache\Model\Cache\Type
*/
private function getCache()
{
if (!$this->fullPageCache) {
$this->fullPageCache = ObjectManager::getInstance()->get(\Magento\PageCache\Model\Cache\Type::class);
}
return $this->fullPageCache;
}

/**
* @deprecated 100.1.2
* @return \Magento\Framework\App\Cache\Tag\Resolver
*/
private function getTagResolver()
{
if ($this->tagResolver === null) {
$this->tagResolver = \Magento\Framework\App\ObjectManager::getInstance()
->get(\Magento\Framework\App\Cache\Tag\Resolver::class);
}
return $this->tagResolver;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,23 +29,17 @@ class FlushCacheByTagsTest extends \PHPUnit\Framework\TestCase
*/
protected function setUp()
{
$helper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
$this->_configMock = $this->createPartialMock(\Magento\PageCache\Model\Config::class, ['getType', 'isEnabled']);
$this->_cacheMock = $this->createPartialMock(\Magento\Framework\App\PageCache\Cache::class, ['clean']);
$this->tagResolver = $this->createMock(\Magento\Framework\App\Cache\Tag\Resolver::class);
$this->fullPageCacheMock = $this->createPartialMock(\Magento\PageCache\Model\Cache\Type::class, ['clean']);

$this->_model = new \Magento\PageCache\Observer\FlushCacheByTags(
$this->_configMock,
$this->_cacheMock
$this->_cacheMock,
$this->tagResolver,
$this->fullPageCacheMock
);

$this->tagResolver = $this->createMock(\Magento\Framework\App\Cache\Tag\Resolver::class);

$helper->setBackwardCompatibleProperty($this->_model, 'tagResolver', $this->tagResolver);
$reflection = new \ReflectionClass(\Magento\PageCache\Observer\FlushCacheByTags::class);
$reflectionProperty = $reflection->getProperty('fullPageCache');
$reflectionProperty->setAccessible(true);
$reflectionProperty->setValue($this->_model, $this->fullPageCacheMock);
}

/**
Expand Down