-
Notifications
You must be signed in to change notification settings - Fork 9.4k
Only resize images (cache) for themes which are being used instead of all installed themes #11514
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 2 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 |
|---|---|---|
|
|
@@ -8,8 +8,11 @@ | |
| 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; | ||
| use Magento\Eav\Model\Config; | ||
| use Magento\Eav\Model\Entity\Attribute; | ||
|
|
||
| class Cache | ||
| { | ||
|
|
@@ -23,11 +26,27 @@ class Cache | |
| */ | ||
| protected $themeCollection; | ||
|
|
||
| /** | ||
| * @var ThemeCustomizationConfig | ||
| */ | ||
| protected $themeCustomizationConfig; | ||
|
|
||
| /** | ||
| * @var ImageHelper | ||
| */ | ||
| protected $imageHelper; | ||
|
|
||
| /** | ||
| * @var Config | ||
| */ | ||
| protected $eavConfig; | ||
|
Contributor
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 use private modifier for object dependencies |
||
|
|
||
| /** | ||
| * @var Attribute | ||
| */ | ||
| protected $attribute; | ||
|
Contributor
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 use private modifier for object dependencies |
||
|
|
||
|
|
||
| /** | ||
| * @var array | ||
| */ | ||
|
|
@@ -41,11 +60,17 @@ class Cache | |
| public function __construct( | ||
| ConfigInterface $viewConfig, | ||
| ThemeCollection $themeCollection, | ||
| ImageHelper $imageHelper | ||
| ImageHelper $imageHelper, | ||
| ThemeCustomizationConfig $themeCustomizationConfig, | ||
|
Contributor
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. Dependencies added with backward compatibility standards violation, right way described in http://devdocs.magento.com/guides/v2.2/contributor-guide/backward-compatible-development/ section: Adding a constructor parameter |
||
| Config $eavConfig, | ||
| Attribute $attribute | ||
| ) { | ||
| $this->viewConfig = $viewConfig; | ||
| $this->themeCollection = $themeCollection; | ||
| $this->imageHelper = $imageHelper; | ||
| $this->themeCustomizationConfig = $themeCustomizationConfig; | ||
| $this->eavConfig = $eavConfig; | ||
| $this->attribute = $attribute; | ||
| } | ||
|
|
||
| /** | ||
|
|
@@ -58,8 +83,10 @@ public function __construct( | |
| protected function getData() | ||
| { | ||
| if (!$this->data) { | ||
| $themesInUse = $this->getThemesInUse(); | ||
|
|
||
| /** @var \Magento\Theme\Model\Theme $theme */ | ||
| foreach ($this->themeCollection->loadRegisteredThemes() as $theme) { | ||
| foreach ($themesInUse as $theme) { | ||
| $config = $this->viewConfig->getViewConfig([ | ||
| 'area' => Area::AREA_FRONTEND, | ||
| 'themeModel' => $theme, | ||
|
|
@@ -127,4 +154,93 @@ protected function processImageData(Product $product, array $imageData, $file) | |
|
|
||
| return $this; | ||
| } | ||
|
|
||
| /** | ||
| * Get themes in use | ||
| * | ||
| * @return array | ||
| */ | ||
| public function getThemesInUse() | ||
|
||
| { | ||
| $themesInUse = []; | ||
|
|
||
| $registeredThemes = $this->themeCollection->loadRegisteredThemes(); | ||
| $storesByThemes = $this->themeCustomizationConfig->getStoresByThemes(); | ||
|
|
||
| foreach ($registeredThemes as $registeredTheme) { | ||
| if (array_key_exists($registeredTheme->getThemeId(), $storesByThemes)) { | ||
|
Contributor
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. Use $registeredTheme->getCode() instead of $registeredTheme->getThemeId() otherwise this expression will return false permanently and $themesInUse array will be always empty. Without this fix cache directory is empty. |
||
| $themesInUse[] = $registeredTheme; | ||
| } | ||
| } | ||
|
|
||
| $connection = $this->attribute->getResource()->getConnection(); | ||
|
Contributor
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. Getting dependencies in such way (one dependency via another one) violates the principle of least knowledge. |
||
|
|
||
| $productCustomDesignAttributeId = $this->attribute->loadByCode(4, 'custom_design')->getId(); | ||
|
||
|
|
||
| $productSql = $connection | ||
|
Contributor
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. Direct access to the data layer is out of responsibilities scope for Magento Models. I suggest to move it into appropriate resource model. This would also help to resolve the commend above (on the least knowledge principle). |
||
| ->select() | ||
| ->from( | ||
| ['eav' => $connection->getTableName('catalog_product_entity_varchar')], | ||
| ['value'] | ||
| ) | ||
| ->where('eav.attribute_id = ?', $productCustomDesignAttributeId) | ||
| ->where('eav.value > 0') | ||
| ->group('value'); | ||
|
|
||
| $productThemeIds = $connection->fetchCol($productSql); | ||
|
|
||
| if (count($productThemeIds)) { | ||
| foreach ($productThemeIds as $productThemeId) { | ||
| if (array_key_exists($productThemeId, $storesByThemes) | ||
| && !array_key_exists($productThemeId, $themesInUse) ) { | ||
| $themesInUse[] = $this->themeCollection->load($productThemeId); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| $categoryCustomDesignAttributeId = $this->attribute->loadByCode(3, 'custom_design')->getId(); | ||
|
|
||
| $categorySql = $connection | ||
| ->select() | ||
| ->from( | ||
| ['eav' => $connection->getTableName('catalog_category_entity_varchar')], | ||
| ['value'] | ||
| ) | ||
| ->where('eav.attribute_id = ?', $categoryCustomDesignAttributeId) | ||
| ->where('eav.value > 0') | ||
| ->group('value'); | ||
|
|
||
| $categoryThemeIds = $connection->fetchCol($categorySql); | ||
|
|
||
| if (count($categoryThemeIds)) { | ||
| foreach ($categoryThemeIds as $categoryThemeId) { | ||
| if (array_key_exists($categoryThemeId, $storesByThemes) | ||
| && !array_key_exists($categoryThemeId, $themesInUse) ) { | ||
| $themesInUse[] = $this->themeCollection->load($categoryThemeId); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| $pageSql = $connection | ||
| ->select() | ||
| ->from( | ||
| ['page' => $connection->getTableName('cms_page')], | ||
| ['custom_theme'] | ||
| ) | ||
| ->where('custom_theme > 0') | ||
| ->group('custom_theme'); | ||
|
|
||
| $pageThemeIds = $connection->fetchCol($pageSql); | ||
|
|
||
| if (count($pageThemeIds)) { | ||
| foreach ($pageThemeIds as $pageThemeId) { | ||
| if (array_key_exists($pageThemeId, $storesByThemes) | ||
| && !array_key_exists($pageThemeId, $themesInUse) ) { | ||
| $themesInUse[] = $this->themeCollection->load($pageThemeId); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return $themesInUse; | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Adding new
protectedproperties is expanding the contract of the class, which is not really desired in this case. Please change toprivateThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
changed to private