-
Notifications
You must be signed in to change notification settings - Fork 9.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #989 from magento-dragons/perf-pr-swatches
[Dragons][Performance] Swatches
- Loading branch information
Showing
29 changed files
with
1,238 additions
and
92 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
129 changes: 129 additions & 0 deletions
129
app/code/Magento/Swatches/Model/SwatchAttributeCodes.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
<?php | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
namespace Magento\Swatches\Model; | ||
|
||
use Magento\Framework\App\CacheInterface; | ||
use Magento\Framework\App\ResourceConnection; | ||
use Magento\Framework\DB\Select; | ||
|
||
/** | ||
* Class SwatchAttributeCodes for getting codes of swatch attributes. | ||
*/ | ||
class SwatchAttributeCodes | ||
{ | ||
/** | ||
* @var string | ||
*/ | ||
private $cacheKey; | ||
|
||
/** | ||
* @var CacheInterface | ||
*/ | ||
private $cache; | ||
|
||
/** | ||
* @var ResourceConnection | ||
*/ | ||
private $resourceConnection; | ||
|
||
/** | ||
* Key is attribute_id, value is attribute_code | ||
* | ||
* @var array | ||
*/ | ||
private $swatchAttributeCodes; | ||
|
||
/** | ||
* @var array | ||
*/ | ||
private $cacheTags; | ||
|
||
/** | ||
* SwatchAttributeList constructor. | ||
* | ||
* @param CacheInterface $cache | ||
* @param ResourceConnection $resourceConnection | ||
* @param string $cacheKey | ||
* @param array $cacheTags | ||
*/ | ||
public function __construct( | ||
CacheInterface $cache, | ||
ResourceConnection $resourceConnection, | ||
$cacheKey, | ||
array $cacheTags | ||
) { | ||
$this->cache = $cache; | ||
$this->resourceConnection = $resourceConnection; | ||
$this->cacheKey = $cacheKey; | ||
$this->cacheTags = $cacheTags; | ||
} | ||
|
||
/** | ||
* Returns list of known swatch attribute codes. Check cache and database. | ||
* Key is attribute_id, value is attribute_code | ||
* | ||
* @return array | ||
*/ | ||
public function getCodes() | ||
{ | ||
if ($this->swatchAttributeCodes === null) { | ||
$swatchAttributeCodesCache = $this->cache->load($this->cacheKey); | ||
if (false === $swatchAttributeCodesCache) { | ||
$swatchAttributeCodes = $this->getSwatchAttributeCodes(); | ||
$this->cache->save(json_encode($swatchAttributeCodes), $this->cacheKey, $this->cacheTags); | ||
} else { | ||
$swatchAttributeCodes = json_decode($swatchAttributeCodesCache, true); | ||
} | ||
$this->swatchAttributeCodes = $swatchAttributeCodes; | ||
} | ||
|
||
return $this->swatchAttributeCodes; | ||
} | ||
|
||
/** | ||
* Returns list of known swatch attributes. | ||
* | ||
* Returns a map of id and code for all EAV attributes with swatches | ||
* | ||
* @return array | ||
*/ | ||
private function getSwatchAttributeCodes() | ||
{ | ||
$select = $this->resourceConnection->getConnection()->select() | ||
->from( | ||
['a' => $this->resourceConnection->getTableName('eav_attribute')], | ||
[ | ||
'attribute_id' => 'a.attribute_id', | ||
'attribute_code' => 'a.attribute_code', | ||
] | ||
)->where( | ||
'a.attribute_id IN (?)', | ||
new \Zend_Db_Expr($this->getAttributeIdsSelect()) | ||
); | ||
$result = $this->resourceConnection->getConnection()->fetchPairs($select); | ||
return $result; | ||
} | ||
|
||
/** | ||
* Returns Select for attributes Ids. | ||
* | ||
* Builds a "Select" object which loads all EAV attributes that has "swatch" options | ||
* | ||
* @return Select | ||
*/ | ||
private function getAttributeIdsSelect() | ||
{ | ||
return $this->resourceConnection->getConnection()->select() | ||
->from( | ||
['o' => $this->resourceConnection->getTableName('eav_attribute_option')], | ||
['attribute_id' => 'o.attribute_id'] | ||
)->join( | ||
['s' => $this->resourceConnection->getTableName('eav_attribute_option_swatch')], | ||
'o.option_id = s.option_id', | ||
[] | ||
); | ||
} | ||
} |
72 changes: 72 additions & 0 deletions
72
app/code/Magento/Swatches/Model/SwatchAttributesProvider.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
<?php | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
namespace Magento\Swatches\Model; | ||
|
||
use Magento\ConfigurableProduct\Model\Product\Type\Configurable; | ||
use Magento\Catalog\Model\Product; | ||
use Magento\ConfigurableProduct\Model\Product\Type\Configurable\Attribute; | ||
|
||
/** | ||
* Provide list of swatch attributes for product. | ||
*/ | ||
class SwatchAttributesProvider | ||
{ | ||
/** | ||
* @var Configurable | ||
*/ | ||
private $typeConfigurable; | ||
|
||
/** | ||
* @var SwatchAttributeCodes | ||
*/ | ||
private $swatchAttributeCodes; | ||
|
||
/** | ||
* Key is productId, value is list of attributes | ||
* @var Attribute[] | ||
*/ | ||
private $attributesPerProduct; | ||
|
||
/** | ||
* @param Configurable $typeConfigurable | ||
* @param SwatchAttributeCodes $swatchAttributeCodes | ||
*/ | ||
public function __construct( | ||
Configurable $typeConfigurable, | ||
SwatchAttributeCodes $swatchAttributeCodes | ||
) { | ||
$this->typeConfigurable = $typeConfigurable; | ||
$this->swatchAttributeCodes = $swatchAttributeCodes; | ||
} | ||
|
||
/** | ||
* Provide list of swatch attributes for product. If product is not configurable return empty array | ||
* Key is productId, value is list of attributes | ||
* | ||
* @param Product $product | ||
* @return Attribute[] | ||
*/ | ||
public function provide(Product $product) | ||
{ | ||
if ($product->getTypeId() !== Configurable::TYPE_CODE) { | ||
return []; | ||
} | ||
if (!isset($this->attributesPerProduct[$product->getId()])) { | ||
$configurableAttributes = $this->typeConfigurable->getConfigurableAttributes($product); | ||
$swatchAttributeCodeMap = $this->swatchAttributeCodes->getCodes(); | ||
|
||
$swatchAttributes = []; | ||
foreach ($configurableAttributes as $configurableAttribute) { | ||
if (array_key_exists($configurableAttribute->getAttributeId(), $swatchAttributeCodeMap)) { | ||
$swatchAttributes[$configurableAttribute->getAttributeId()] | ||
= $configurableAttribute->getProductAttribute(); | ||
} | ||
} | ||
$this->attributesPerProduct[$product->getId()] = $swatchAttributes; | ||
} | ||
return $this->attributesPerProduct[$product->getId()]; | ||
} | ||
} |
Oops, something went wrong.