From 35e3225f0478a7b952119dca2a800a76b9ae5f3e Mon Sep 17 00:00:00 2001 From: Pieter Hoste Date: Sun, 15 Jan 2017 12:17:29 +0100 Subject: [PATCH 1/2] Only resize images for themes which are being used instead of all installed themes. --- .../Catalog/Model/Product/Image/Cache.php | 30 +++++++++++++++++-- .../Unit/Model/Product/Image/CacheTest.php | 16 ++++++++++ 2 files changed, 44 insertions(+), 2 deletions(-) diff --git a/app/code/Magento/Catalog/Model/Product/Image/Cache.php b/app/code/Magento/Catalog/Model/Product/Image/Cache.php index 2a5316583ff6e..6cda7a862a991 100644 --- a/app/code/Magento/Catalog/Model/Product/Image/Cache.php +++ b/app/code/Magento/Catalog/Model/Product/Image/Cache.php @@ -8,6 +8,7 @@ use Magento\Catalog\Helper\Image as ImageHelper; use Magento\Catalog\Model\Product; use Magento\Theme\Model\ResourceModel\Theme\Collection as ThemeCollection; +use Magento\Theme\Model\Config\Customization as ThemeCustomizationConfig; use Magento\Framework\App\Area; use Magento\Framework\View\ConfigInterface; @@ -23,6 +24,11 @@ class Cache */ protected $themeCollection; + /** + * @var ThemeCustomizationConfig + */ + protected $themeCustomizationConfig; + /** * @var ImageHelper */ @@ -41,10 +47,12 @@ class Cache public function __construct( ConfigInterface $viewConfig, ThemeCollection $themeCollection, - ImageHelper $imageHelper + ImageHelper $imageHelper, + ThemeCustomizationConfig $themeCustomizationConfig ) { $this->viewConfig = $viewConfig; $this->themeCollection = $themeCollection; + $this->themeCustomizationConfig = $themeCustomizationConfig; $this->imageHelper = $imageHelper; } @@ -58,8 +66,10 @@ public function __construct( protected function getData() { if (!$this->data) { + $themes = $this->getThemesInUse(); + /** @var \Magento\Theme\Model\Theme $theme */ - foreach ($this->themeCollection->loadRegisteredThemes() as $theme) { + foreach ($themes as $theme) { $config = $this->viewConfig->getViewConfig([ 'area' => Area::AREA_FRONTEND, 'themeModel' => $theme, @@ -127,4 +137,20 @@ protected function processImageData(Product $product, array $imageData, $file) return $this; } + + protected function getThemesInUse() + { + $themesInUse = []; + + $registeredThemes = $this->themeCollection->loadRegisteredThemes(); + $storesByThemes = $this->themeCustomizationConfig->getStoresByThemes(); + + foreach ($registeredThemes as $registeredTheme) { + if (array_key_exists($registeredTheme->getThemeId(), $storesByThemes)) { + $themesInUse[] = $registeredTheme; + } + } + + return $themesInUse; + } } diff --git a/app/code/Magento/Catalog/Test/Unit/Model/Product/Image/CacheTest.php b/app/code/Magento/Catalog/Test/Unit/Model/Product/Image/CacheTest.php index f12638ac40c8f..a925b516eaf77 100644 --- a/app/code/Magento/Catalog/Test/Unit/Model/Product/Image/CacheTest.php +++ b/app/code/Magento/Catalog/Test/Unit/Model/Product/Image/CacheTest.php @@ -43,6 +43,11 @@ class CacheTest extends \PHPUnit_Framework_TestCase */ protected $themeCollection; + /** + * @var \Magento\Theme\Model\Config\Customization|\PHPUnit_Framework_MockObject_MockObject + */ + protected $themeCustomizationConfig; + /** * @var \Magento\Catalog\Helper\Image|\PHPUnit_Framework_MockObject_MockObject */ @@ -70,6 +75,10 @@ protected function setUp() ->disableOriginalConstructor() ->getMock(); + $this->themeCustomizationConfig = $this->getMockBuilder(\Magento\Theme\Model\Config\Customization::class) + ->disableOriginalConstructor() + ->getMock(); + $this->imageHelper = $this->getMockBuilder(\Magento\Catalog\Helper\Image::class) ->disableOriginalConstructor() ->getMock(); @@ -84,6 +93,7 @@ protected function setUp() [ 'viewConfig' => $this->viewConfig, 'themeCollection' => $this->themeCollection, + 'themeCustomizationConfig' => $this->themeCustomizationConfig, 'imageHelper' => $this->imageHelper, ] ); @@ -126,6 +136,12 @@ public function testGenerate() ->method('loadRegisteredThemes') ->willReturn([$themeMock]); + $this->themeCustomizationConfig->expects($this->once()) + ->method('getStoresByThemes') + ->willReturn([ + $themeMock->getThemeId() => [1] + ]); + $this->viewConfig->expects($this->once()) ->method('getViewConfig') ->with([ From 242d74338c5b45e9079590d79f817e92a2829c65 Mon Sep 17 00:00:00 2001 From: Pieter Hoste Date: Sun, 15 Jan 2017 12:34:23 +0100 Subject: [PATCH 2/2] Should fix static tests failing. --- app/code/Magento/Catalog/Model/Product/Image/Cache.php | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/app/code/Magento/Catalog/Model/Product/Image/Cache.php b/app/code/Magento/Catalog/Model/Product/Image/Cache.php index 6cda7a862a991..253a99a03baa6 100644 --- a/app/code/Magento/Catalog/Model/Product/Image/Cache.php +++ b/app/code/Magento/Catalog/Model/Product/Image/Cache.php @@ -43,6 +43,7 @@ class Cache * @param ConfigInterface $viewConfig * @param ThemeCollection $themeCollection * @param ImageHelper $imageHelper + * @param ThemeCustomizationConfig $themeCustomizationConfig */ public function __construct( ConfigInterface $viewConfig, @@ -138,6 +139,11 @@ protected function processImageData(Product $product, array $imageData, $file) return $this; } + /** + * Retrieve themes which are currently active in the frontend + * + * @return array + */ protected function getThemesInUse() { $themesInUse = [];