Skip to content
This repository was archived by the owner on Apr 29, 2019. It is now read-only.

Commit 708017a

Browse files
authored
Merge pull request #3287 from magento-performance/pr-2.3-develop
[performance] performance improvements ### Tasks: * MAGETWO-91388: Enable caching of AJAX requests on Varnish/Fastly * MAGETWO-94671: Product Export fails * MAGETWO-95412: [2.3] Add missed dependencies for catalogsearch_fulltext indexer
2 parents 4f94998 + 6059152 commit 708017a

File tree

6 files changed

+92
-25
lines changed

6 files changed

+92
-25
lines changed

app/code/Magento/CatalogImportExport/Model/Export/Product.php

Lines changed: 39 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
* @SuppressWarnings(PHPMD.TooManyFields)
2121
* @SuppressWarnings(PHPMD.ExcessiveClassComplexity)
2222
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
23+
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
2324
* @since 100.0.2
2425
*/
2526
class Product extends \Magento\ImportExport\Model\Export\Entity\AbstractEntity
@@ -349,12 +350,13 @@ class Product extends \Magento\ImportExport\Model\Export\Entity\AbstractEntity
349350
private $productEntityLinkField;
350351

351352
/**
353+
* Product constructor.
352354
* @param \Magento\Framework\Stdlib\DateTime\TimezoneInterface $localeDate
353355
* @param \Magento\Eav\Model\Config $config
354356
* @param \Magento\Framework\App\ResourceConnection $resource
355357
* @param \Magento\Store\Model\StoreManagerInterface $storeManager
356358
* @param \Psr\Log\LoggerInterface $logger
357-
* @param \Magento\Catalog\Model\ResourceModel\Product\Collection $collection
359+
* @param \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $collectionFactory
358360
* @param \Magento\ImportExport\Model\Export\ConfigInterface $exportConfig
359361
* @param \Magento\Catalog\Model\ResourceModel\ProductFactory $productFactory
360362
* @param \Magento\Eav\Model\ResourceModel\Entity\Attribute\Set\CollectionFactory $attrSetColFactory
@@ -363,10 +365,10 @@ class Product extends \Magento\ImportExport\Model\Export\Entity\AbstractEntity
363365
* @param \Magento\Catalog\Model\ResourceModel\Product\Option\CollectionFactory $optionColFactory
364366
* @param \Magento\Catalog\Model\ResourceModel\Product\Attribute\CollectionFactory $attributeColFactory
365367
* @param Product\Type\Factory $_typeFactory
366-
* @param \Magento\Catalog\Model\Product\LinkTypeProvider $linkTypeProvider
367-
* @param \Magento\CatalogImportExport\Model\Export\RowCustomizerInterface $rowCustomizer
368+
* @param ProductEntity\LinkTypeProvider $linkTypeProvider
369+
* @param RowCustomizerInterface $rowCustomizer
368370
* @param array $dateAttrCodes
369-
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
371+
* @throws \Magento\Framework\Exception\LocalizedException
370372
*/
371373
public function __construct(
372374
\Magento\Framework\Stdlib\DateTime\TimezoneInterface $localeDate,
@@ -520,10 +522,13 @@ protected function getMediaGallery(array $productIds)
520522
if (empty($productIds)) {
521523
return [];
522524
}
525+
526+
$productEntityJoinField = $this->getProductEntityLinkField();
527+
523528
$select = $this->_connection->select()->from(
524529
['mgvte' => $this->_resourceModel->getTableName('catalog_product_entity_media_gallery_value_to_entity')],
525530
[
526-
"mgvte.{$this->getProductEntityLinkField()}",
531+
"mgvte.$productEntityJoinField",
527532
'mgvte.value_id'
528533
]
529534
)->joinLeft(
@@ -535,22 +540,22 @@ protected function getMediaGallery(array $productIds)
535540
]
536541
)->joinLeft(
537542
['mgv' => $this->_resourceModel->getTableName('catalog_product_entity_media_gallery_value')],
538-
'(mg.value_id = mgv.value_id)',
543+
"(mg.value_id = mgv.value_id) and (mgvte.$productEntityJoinField = mgv.$productEntityJoinField)",
539544
[
540545
'mgv.label',
541546
'mgv.position',
542547
'mgv.disabled',
543548
'mgv.store_id',
544549
]
545550
)->where(
546-
"mgvte.{$this->getProductEntityLinkField()} IN (?)",
551+
"mgvte.$productEntityJoinField IN (?)",
547552
$productIds
548553
);
549554

550555
$rowMediaGallery = [];
551556
$stmt = $this->_connection->query($select);
552557
while ($mediaRow = $stmt->fetch()) {
553-
$rowMediaGallery[$mediaRow[$this->getProductEntityLinkField()]][] = [
558+
$rowMediaGallery[$mediaRow[$productEntityJoinField]][] = [
554559
'_media_attribute_id' => $mediaRow['attribute_id'],
555560
'_media_image' => $mediaRow['filename'],
556561
'_media_label' => $mediaRow['label'],
@@ -692,7 +697,9 @@ protected function updateDataWithCategoryColumns(&$dataRow, &$rowCategories, $pr
692697
}
693698

694699
/**
695-
* {@inheritdoc}
700+
* Get header columns
701+
*
702+
* @return string[]
696703
*/
697704
public function _getHeaderColumns()
698705
{
@@ -751,7 +758,10 @@ protected function _getExportMainAttrCodes()
751758
}
752759

753760
/**
754-
* {@inheritdoc}
761+
* Get entity collection
762+
*
763+
* @param bool $resetCollection
764+
* @return \Magento\Framework\Data\Collection\AbstractDb
755765
*/
756766
protected function _getEntityCollection($resetCollection = false)
757767
{
@@ -858,7 +868,10 @@ public function export()
858868
}
859869

860870
/**
861-
* {@inheritdoc}
871+
* Apply filter to collection and add not skipped attributes to select.
872+
*
873+
* @param \Magento\Eav\Model\Entity\Collection\AbstractCollection $collection
874+
* @return \Magento\Eav\Model\Entity\Collection\AbstractCollection
862875
* @since 100.2.0
863876
*/
864877
protected function _prepareEntityCollection(\Magento\Eav\Model\Entity\Collection\AbstractCollection $collection)
@@ -920,8 +933,7 @@ protected function getExportData()
920933
}
921934

922935
/**
923-
* Load products' data from the collection
924-
* and filter it (if needed).
936+
* Load products' data from the collection and filter it (if needed).
925937
*
926938
* @return array Keys are product IDs, values arrays with keys as store IDs
927939
* and values as store-specific versions of Product entity.
@@ -1063,6 +1075,8 @@ private function wrapValue($value)
10631075
}
10641076

10651077
/**
1078+
* Collect multi raw data from
1079+
*
10661080
* @return array
10671081
*/
10681082
protected function collectMultirawData()
@@ -1104,6 +1118,8 @@ protected function collectMultirawData()
11041118
}
11051119

11061120
/**
1121+
* Check the current data has multiselect value
1122+
*
11071123
* @param \Magento\Catalog\Model\Product $item
11081124
* @param int $storeId
11091125
* @return bool
@@ -1116,6 +1132,8 @@ protected function hasMultiselectData($item, $storeId)
11161132
}
11171133

11181134
/**
1135+
* Collect multiselect values based on value
1136+
*
11191137
* @param \Magento\Catalog\Model\Product $item
11201138
* @param string $attrCode
11211139
* @param int $storeId
@@ -1140,6 +1158,8 @@ protected function collectMultiselectValues($item, $attrCode, $storeId)
11401158
}
11411159

11421160
/**
1161+
* Check attribute is valid
1162+
*
11431163
* @param string $code
11441164
* @param mixed $value
11451165
* @return bool
@@ -1159,6 +1179,8 @@ protected function isValidAttributeValue($code, $value)
11591179
}
11601180

11611181
/**
1182+
* Append multi row data
1183+
*
11621184
* @param array $dataRow
11631185
* @param array $multiRawData
11641186
* @return array
@@ -1288,6 +1310,8 @@ private function appendMultirowData(&$dataRow, $multiRawData)
12881310
}
12891311

12901312
/**
1313+
* Add multi row data to export
1314+
*
12911315
* @deprecated 100.1.0
12921316
* @param array $dataRow
12931317
* @param array $multiRawData
@@ -1335,6 +1359,8 @@ protected function _customHeadersMapping($rowData)
13351359
}
13361360

13371361
/**
1362+
* Convert option row to cell string
1363+
*
13381364
* @param array $option
13391365
* @return string
13401366
*/

app/code/Magento/Elasticsearch/etc/indexer.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Indexer/etc/indexer.xsd">
99
<indexer id="catalogsearch_fulltext">
1010
<dependencies>
11+
<indexer id="catalog_category_product" />
1112
<indexer id="cataloginventory_stock" />
1213
<indexer id="catalog_product_price" />
1314
</dependencies>

app/code/Magento/Swatches/Controller/Ajax/Media.php

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
/**
1313
* Class Media
1414
*/
15-
class Media extends \Magento\Framework\App\Action\Action
15+
class Media extends \Magento\Framework\App\Action\Action implements \Magento\Framework\App\Action\HttpGetActionInterface
1616
{
1717
/**
1818
* @var \Magento\Catalog\Model\Product Factory
@@ -24,18 +24,26 @@ class Media extends \Magento\Framework\App\Action\Action
2424
*/
2525
private $swatchHelper;
2626

27+
/**
28+
* @var \Magento\PageCache\Model\Config
29+
*/
30+
protected $config;
31+
2732
/**
2833
* @param Context $context
2934
* @param \Magento\Catalog\Model\ProductFactory $productModelFactory
3035
* @param \Magento\Swatches\Helper\Data $swatchHelper
36+
* @param \Magento\PageCache\Model\Config $config
3137
*/
3238
public function __construct(
3339
Context $context,
3440
\Magento\Catalog\Model\ProductFactory $productModelFactory,
35-
\Magento\Swatches\Helper\Data $swatchHelper
41+
\Magento\Swatches\Helper\Data $swatchHelper,
42+
\Magento\PageCache\Model\Config $config
3643
) {
3744
$this->productModelFactory = $productModelFactory;
3845
$this->swatchHelper = $swatchHelper;
46+
$this->config = $config;
3947

4048
parent::__construct($context);
4149
}
@@ -48,14 +56,23 @@ public function __construct(
4856
public function execute()
4957
{
5058
$productMedia = [];
59+
60+
/** @var \Magento\Framework\Controller\Result\Json $resultJson */
61+
$resultJson = $this->resultFactory->create(ResultFactory::TYPE_JSON);
62+
63+
/** @var \Magento\Framework\App\ResponseInterface $response */
64+
$response = $this->getResponse();
65+
5166
if ($productId = (int)$this->getRequest()->getParam('product_id')) {
67+
$product = $this->productModelFactory->create()->load($productId);
5268
$productMedia = $this->swatchHelper->getProductMediaGallery(
53-
$this->productModelFactory->create()->load($productId)
69+
$product
5470
);
71+
$resultJson->setHeader('X-Magento-Tags', implode(',', $product->getIdentities()));
72+
73+
$response->setPublicHeaders($this->config->getTtl());
5574
}
5675

57-
/** @var \Magento\Framework\Controller\Result\Json $resultJson */
58-
$resultJson = $this->resultFactory->create(ResultFactory::TYPE_JSON);
5976
$resultJson->setData($productMedia);
6077
return $resultJson;
6178
}

app/code/Magento/Swatches/Test/Unit/Controller/Ajax/MediaTest.php

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ class MediaTest extends \PHPUnit\Framework\TestCase
2020
/** @var \Magento\Catalog\Model\ProductFactory|\PHPUnit_Framework_MockObject_MockObject */
2121
private $productModelFactoryMock;
2222

23+
/** @var \Magento\PageCache\Model\Config|\PHPUnit_Framework_MockObject_MockObject */
24+
private $config;
25+
2326
/** @var \Magento\Catalog\Model\Product|\PHPUnit_Framework_MockObject_MockObject */
2427
private $productMock;
2528

@@ -29,6 +32,9 @@ class MediaTest extends \PHPUnit\Framework\TestCase
2932
/** @var \Magento\Framework\App\RequestInterface|\PHPUnit_Framework_MockObject_MockObject */
3033
private $requestMock;
3134

35+
/** @var \Magento\Framework\App\ResponseInterface|\PHPUnit_Framework_MockObject_MockObject */
36+
private $responseMock;
37+
3238
/** @var \Magento\Framework\Controller\ResultFactory|\PHPUnit_Framework_MockObject_MockObject */
3339
private $resultFactory;
3440

@@ -57,11 +63,20 @@ protected function setUp()
5763
\Magento\Catalog\Model\ProductFactory::class,
5864
['create']
5965
);
66+
$this->config = $this->createMock(\Magento\PageCache\Model\Config::class);
67+
$this->config->method('getTtl')->willReturn(1);
68+
6069
$this->productMock = $this->createMock(\Magento\Catalog\Model\Product::class);
6170
$this->contextMock = $this->createMock(\Magento\Framework\App\Action\Context::class);
6271

6372
$this->requestMock = $this->createMock(\Magento\Framework\App\RequestInterface::class);
6473
$this->contextMock->method('getRequest')->willReturn($this->requestMock);
74+
$this->responseMock = $this->getMockBuilder(\Magento\Framework\App\ResponseInterface::class)
75+
->disableOriginalConstructor()
76+
->setMethods(['setPublicHeaders'])
77+
->getMockForAbstractClass();
78+
$this->responseMock->method('setPublicHeaders')->willReturnSelf();
79+
$this->contextMock->method('getResponse')->willReturn($this->responseMock);
6580
$this->resultFactory = $this->createPartialMock(\Magento\Framework\Controller\ResultFactory::class, ['create']);
6681
$this->contextMock->method('getResultFactory')->willReturn($this->resultFactory);
6782

@@ -73,7 +88,8 @@ protected function setUp()
7388
[
7489
'context' => $this->contextMock,
7590
'swatchHelper' => $this->swatchHelperMock,
76-
'productModelFactory' => $this->productModelFactoryMock
91+
'productModelFactory' => $this->productModelFactoryMock,
92+
'config' => $this->config
7793
]
7894
);
7995
}
@@ -86,6 +102,10 @@ public function testExecute()
86102
->method('load')
87103
->with(59)
88104
->willReturn($this->productMock);
105+
$this->productMock
106+
->expects($this->once())
107+
->method('getIdentities')
108+
->willReturn(['tags']);
89109

90110
$this->productModelFactoryMock
91111
->expects($this->once())

app/code/Magento/Swatches/composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
"magento/module-configurable-product": "*",
1414
"magento/module-customer": "*",
1515
"magento/module-eav": "*",
16+
"magento/module-page-cache": "*",
1617
"magento/module-media-storage": "*",
1718
"magento/module-store": "*",
1819
"magento/module-theme": "*"

app/code/Magento/Swatches/view/frontend/web/js/swatch-renderer.js

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1029,12 +1029,14 @@ define([
10291029
mediaCallData.isAjax = true;
10301030
$widget._XhrKiller();
10311031
$widget._EnableProductMediaLoader($this);
1032-
$widget.xhr = $.get(
1033-
$widget.options.mediaCallback,
1034-
mediaCallData,
1035-
mediaSuccessCallback,
1036-
'json'
1037-
).done(function () {
1032+
$widget.xhr = $.ajax({
1033+
url: $widget.options.mediaCallback,
1034+
cache: true,
1035+
type: 'GET',
1036+
dataType: 'json',
1037+
data: mediaCallData,
1038+
success: mediaSuccessCallback
1039+
}).done(function () {
10381040
$widget._XhrKiller();
10391041
});
10401042
}

0 commit comments

Comments
 (0)