-
Notifications
You must be signed in to change notification settings - Fork 9.4k
Move additional dependencies from private getters to constructor - Magento_PageCache #26684
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,25 @@ | ||
<?php | ||
/** | ||
* | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace Magento\PageCache\Observer; | ||
|
||
use Magento\Framework\App\ObjectManager; | ||
use Magento\Framework\App\PageCache\Cache; | ||
use Magento\Framework\Event\Observer; | ||
use Magento\Framework\Event\ObserverInterface; | ||
use Magento\PageCache\Model\Cache\Type; | ||
use Magento\PageCache\Model\Config; | ||
|
||
/** | ||
* Observer used to flush all caches with built-in full page cache | ||
*/ | ||
class FlushAllCache implements ObserverInterface | ||
{ | ||
/** | ||
* @var \Magento\Framework\App\PageCache\Cache | ||
* @var Cache | ||
* | ||
* @deprecated 100.1.0 | ||
*/ | ||
|
@@ -21,48 +28,42 @@ class FlushAllCache implements ObserverInterface | |
/** | ||
* Application config object | ||
* | ||
* @var \Magento\PageCache\Model\Config | ||
* @var Config | ||
*/ | ||
protected $_config; | ||
|
||
/** | ||
* @var \Magento\PageCache\Model\Cache\Type | ||
* @var Type | ||
*/ | ||
private $fullPageCache; | ||
|
||
/** | ||
* @param \Magento\PageCache\Model\Config $config | ||
* @param \Magento\Framework\App\PageCache\Cache $cache | ||
* @param Config $config | ||
* @param Cache $cache | ||
* @param Type $fullPageCache | ||
*/ | ||
public function __construct(\Magento\PageCache\Model\Config $config, \Magento\Framework\App\PageCache\Cache $cache) | ||
{ | ||
public function __construct( | ||
Config $config, | ||
Cache $cache, | ||
Type $fullPageCache | ||
) { | ||
$this->_config = $config; | ||
$this->_cache = $cache; | ||
$this->fullPageCache = $fullPageCache; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please, check my previous comment regarding the backward compatibility |
||
} | ||
|
||
/** | ||
* Flash Built-In cache | ||
* @param \Magento\Framework\Event\Observer $observer | ||
* | ||
* @param Observer $observer | ||
* | ||
* @return void | ||
* @SuppressWarnings(PHPMD.UnusedFormalParameter) | ||
*/ | ||
public function execute(\Magento\Framework\Event\Observer $observer) | ||
{ | ||
if ($this->_config->getType() == \Magento\PageCache\Model\Config::BUILT_IN) { | ||
$this->getCache()->clean(); | ||
} | ||
} | ||
|
||
/** | ||
* TODO: Workaround to support backwards compatibility, will rework to use Dependency Injection in MAGETWO-49547 | ||
* | ||
* @return \Magento\PageCache\Model\Cache\Type | ||
*/ | ||
private function getCache() | ||
public function execute(Observer $observer) | ||
{ | ||
if (!$this->fullPageCache) { | ||
$this->fullPageCache = ObjectManager::getInstance()->get(\Magento\PageCache\Model\Cache\Type::class); | ||
if ($this->_config->getType() == Config::BUILT_IN) { | ||
$this->fullPageCache->clean(); | ||
} | ||
return $this->fullPageCache; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,27 @@ | ||
<?php | ||
/** | ||
* | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace Magento\PageCache\Observer; | ||
|
||
use Magento\Framework\App\ObjectManager; | ||
use Magento\Framework\App\Cache\Tag\Resolver; | ||
use Magento\Framework\App\PageCache\Cache; | ||
use Magento\Framework\Event\Observer; | ||
use Magento\Framework\Event\ObserverInterface; | ||
use Magento\PageCache\Model\Cache\Type; | ||
use Magento\PageCache\Model\Config; | ||
use Zend_Cache; | ||
|
||
/** | ||
* Observer used to cache by tags when using built-in full page cache | ||
*/ | ||
class FlushCacheByTags implements ObserverInterface | ||
{ | ||
/** | ||
* @var \Magento\Framework\App\PageCache\Cache | ||
* @var Cache | ||
* | ||
* @deprecated 100.1.0 | ||
*/ | ||
|
@@ -21,78 +30,59 @@ class FlushCacheByTags implements ObserverInterface | |
/** | ||
* Application config object | ||
* | ||
* @var \Magento\PageCache\Model\Config | ||
* @var Config | ||
*/ | ||
protected $_config; | ||
|
||
/** | ||
* @var \Magento\PageCache\Model\Cache\Type | ||
* @var Type | ||
*/ | ||
private $fullPageCache; | ||
|
||
/** | ||
* Invalidation tags resolver | ||
* | ||
* @var \Magento\Framework\App\Cache\Tag\Resolver | ||
* @var Resolver | ||
*/ | ||
private $tagResolver; | ||
|
||
/** | ||
* @param \Magento\PageCache\Model\Config $config | ||
* @param \Magento\Framework\App\PageCache\Cache $cache | ||
* @param Config $config | ||
* @param Cache $cache | ||
* @param Type $fullPageCache | ||
* @param Resolver $tagResolver | ||
*/ | ||
public function __construct(\Magento\PageCache\Model\Config $config, \Magento\Framework\App\PageCache\Cache $cache) | ||
{ | ||
public function __construct( | ||
Config $config, | ||
Cache $cache, | ||
Type $fullPageCache, | ||
Resolver $tagResolver | ||
) { | ||
$this->_config = $config; | ||
$this->_cache = $cache; | ||
$this->fullPageCache = $fullPageCache; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hi @Bartlomiejsz. We can consider this change as backward incompatible. If some other class inherits the current class in some Magento based project, we will have an error upon calling What I suggest here is to instantiate the corresponding object if it's not being passed as an argument. See the following instantiation as an example. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hi @rogyar, I asked similar question in one of my previous PRs, such non-backward compatible changes I'm doing in those PRs are based on info I received in #26269, where I was asked to replace backward-compatible change with one that doesn't use ObjectManager, since we're developing for 2.4 now. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hi @Bartlomiejsz. You are right, that's fair since we are editing a non-API class. That's rather a professional habit avoid bringing backward-incompatible changes :) |
||
$this->tagResolver = $tagResolver; | ||
} | ||
|
||
/** | ||
* If Built-In caching is enabled it collects array of tags | ||
* of incoming object and asks to clean cache. | ||
* If Built-In caching is enabled it collects array of tags of incoming object and asks to clean cache. | ||
* | ||
* @param Observer $observer | ||
* | ||
* @param \Magento\Framework\Event\Observer $observer | ||
* @return void | ||
*/ | ||
public function execute(\Magento\Framework\Event\Observer $observer) | ||
public function execute(Observer $observer) | ||
{ | ||
if ($this->_config->getType() == \Magento\PageCache\Model\Config::BUILT_IN && $this->_config->isEnabled()) { | ||
if ($this->_config->getType() == Config::BUILT_IN && $this->_config->isEnabled()) { | ||
$object = $observer->getEvent()->getObject(); | ||
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 |
---|---|---|
@@ -1,64 +1,94 @@ | ||
<?php | ||
/** | ||
* | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
|
||
namespace Magento\PageCache\Test\Unit\Observer; | ||
|
||
class FlushAllCacheTest extends \PHPUnit\Framework\TestCase | ||
{ | ||
/** @var \Magento\PageCache\Observer\FlushAllCache */ | ||
private $_model; | ||
use Magento\Framework\Event\Observer; | ||
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager; | ||
use Magento\PageCache\Model\Cache\Type; | ||
use Magento\PageCache\Model\Config; | ||
use Magento\PageCache\Observer\FlushAllCache; | ||
use PHPUnit\Framework\MockObject\MockObject; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
/** @var \PHPUnit_Framework_MockObject_MockObject|\Magento\PageCache\Model\Config */ | ||
private $_configMock; | ||
/** | ||
* Test class for \Magento\PageCache\Observer\FlushAllCache | ||
*/ | ||
class FlushAllCacheTest extends TestCase | ||
{ | ||
/** | ||
* @var FlushAllCache | ||
*/ | ||
private $model; | ||
|
||
/** @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Framework\App\PageCache\Cache */ | ||
private $_cacheMock; | ||
/** | ||
* @var Config|MockObject | ||
*/ | ||
private $configMock; | ||
|
||
/** @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Framework\Event\Observer */ | ||
/** | ||
* @var Observer|MockObject | ||
*/ | ||
private $observerMock; | ||
|
||
/** @var \PHPUnit_Framework_MockObject_MockObject|\Magento\PageCache\Model\Cache\Type */ | ||
/** | ||
* @var Type|MockObject | ||
*/ | ||
private $fullPageCacheMock; | ||
|
||
/** | ||
* Set up all mocks and data for test | ||
* @inheritDoc | ||
*/ | ||
protected function setUp() | ||
{ | ||
$this->_configMock = $this->createPartialMock(\Magento\PageCache\Model\Config::class, ['getType', 'isEnabled']); | ||
$this->_cacheMock = $this->createPartialMock(\Magento\Framework\App\PageCache\Cache::class, ['clean']); | ||
$this->fullPageCacheMock = $this->createPartialMock(\Magento\PageCache\Model\Cache\Type::class, ['clean']); | ||
$this->observerMock = $this->createMock(\Magento\Framework\Event\Observer::class); | ||
$this->configMock = $this->createPartialMock(Config::class, ['getType', 'isEnabled']); | ||
$this->fullPageCacheMock = $this->createPartialMock(Type::class, ['clean']); | ||
$this->observerMock = $this->createMock(Observer::class); | ||
|
||
$this->_model = new \Magento\PageCache\Observer\FlushAllCache( | ||
$this->_configMock, | ||
$this->_cacheMock | ||
$objectManager = new ObjectManager($this); | ||
$this->model = $objectManager->getObject( | ||
FlushAllCache::class, | ||
[ | ||
'config' => $this->configMock, | ||
'fullPageCache' => $this->fullPageCacheMock | ||
] | ||
); | ||
|
||
$reflection = new \ReflectionClass(\Magento\PageCache\Observer\FlushAllCache::class); | ||
$reflectionProperty = $reflection->getProperty('fullPageCache'); | ||
$reflectionProperty->setAccessible(true); | ||
$reflectionProperty->setValue($this->_model, $this->fullPageCacheMock); | ||
} | ||
|
||
/** | ||
* Test case for flushing all the cache | ||
*/ | ||
public function testExecute() | ||
{ | ||
$this->_configMock->expects( | ||
$this->configMock->expects( | ||
$this->once() | ||
)->method( | ||
'getType' | ||
)->will( | ||
$this->returnValue(\Magento\PageCache\Model\Config::BUILT_IN) | ||
)->willReturn( | ||
Config::BUILT_IN | ||
); | ||
|
||
$this->fullPageCacheMock->expects($this->once())->method('clean'); | ||
$this->_model->execute($this->observerMock); | ||
$this->model->execute($this->observerMock); | ||
} | ||
|
||
/** | ||
* Test case for flushing all the cache with varnish enabled | ||
*/ | ||
public function testExecuteWithVarnish() | ||
{ | ||
$this->configMock->expects( | ||
$this->once() | ||
)->method( | ||
'getType' | ||
)->willReturn( | ||
Config::VARNISH | ||
); | ||
|
||
$this->fullPageCacheMock->expects($this->never())->method('clean'); | ||
$this->model->execute($this->observerMock); | ||
} | ||
} |
Uh oh!
There was an error while loading. Please reload this page.