Skip to content

Commit c166940

Browse files
🔃 [EngCom] Public Pull Requests - 2.3-develop
Accepted Public Pull Requests: - #13308: Improve getCustomAttribute() performance (by @schmengler)
2 parents 74a70a0 + 48d132f commit c166940

16 files changed

+463
-88
lines changed

app/code/Magento/Catalog/Api/Data/CategoryInterface.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,37 @@
1414
*/
1515
interface CategoryInterface extends \Magento\Framework\Api\CustomAttributesDataInterface
1616
{
17+
/**#@+
18+
* Constants defined for keys of data array
19+
*/
20+
const KEY_PARENT_ID = 'parent_id';
21+
const KEY_NAME = 'name';
22+
const KEY_IS_ACTIVE = 'is_active';
23+
const KEY_POSITION = 'position';
24+
const KEY_LEVEL = 'level';
25+
const KEY_UPDATED_AT = 'updated_at';
26+
const KEY_CREATED_AT = 'created_at';
27+
const KEY_PATH = 'path';
28+
const KEY_AVAILABLE_SORT_BY = 'available_sort_by';
29+
const KEY_INCLUDE_IN_MENU = 'include_in_menu';
30+
const KEY_PRODUCT_COUNT = 'product_count';
31+
const KEY_CHILDREN_DATA = 'children_data';
32+
33+
const ATTRIBUTES = [
34+
'id',
35+
self::KEY_PARENT_ID,
36+
self::KEY_NAME,
37+
self::KEY_IS_ACTIVE,
38+
self::KEY_POSITION,
39+
self::KEY_LEVEL,
40+
self::KEY_UPDATED_AT,
41+
self::KEY_CREATED_AT,
42+
self::KEY_AVAILABLE_SORT_BY,
43+
self::KEY_INCLUDE_IN_MENU,
44+
self::KEY_CHILDREN_DATA,
45+
];
46+
/**#@-*/
47+
1748
/**
1849
* @return int|null
1950
*/

app/code/Magento/Catalog/Api/Data/ProductInterface.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,24 @@ interface ProductInterface extends \Magento\Framework\Api\CustomAttributesDataIn
3636

3737
const UPDATED_AT = 'updated_at';
3838

39+
const MEDIA_GALLERY = 'media_gallery';
40+
41+
const TIER_PRICE = 'tier_price';
42+
43+
const ATTRIBUTES = [
44+
self::SKU,
45+
self::NAME,
46+
self::PRICE,
47+
self::WEIGHT,
48+
self::STATUS,
49+
self::VISIBILITY,
50+
self::ATTRIBUTE_SET_ID,
51+
self::TYPE_ID,
52+
self::CREATED_AT,
53+
self::UPDATED_AT,
54+
self::MEDIA_GALLERY,
55+
self::TIER_PRICE,
56+
];
3957
/**#@-*/
4058

4159
/**

app/code/Magento/Catalog/Model/Category.php

Lines changed: 19 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,12 @@
66
namespace Magento\Catalog\Model;
77

88
use Magento\Catalog\Api\CategoryRepositoryInterface;
9+
use Magento\Catalog\Api\Data\CategoryInterface;
10+
use Magento\Catalog\Model\Entity\GetCategoryCustomAttributeCodes;
911
use Magento\CatalogUrlRewrite\Model\CategoryUrlRewriteGenerator;
12+
use Magento\Eav\Model\Entity\GetCustomAttributeCodesInterface;
1013
use Magento\Framework\Api\AttributeValueFactory;
14+
use Magento\Framework\App\ObjectManager;
1115
use Magento\Framework\Convert\ConvertArray;
1216
use Magento\Framework\Exception\NoSuchEntityException;
1317
use Magento\Framework\Profiler;
@@ -69,23 +73,6 @@ class Category extends \Magento\Catalog\Model\AbstractModel implements
6973

7074
const CACHE_TAG = 'cat_c';
7175

72-
/**#@+
73-
* Constants
74-
*/
75-
const KEY_PARENT_ID = 'parent_id';
76-
const KEY_NAME = 'name';
77-
const KEY_IS_ACTIVE = 'is_active';
78-
const KEY_POSITION = 'position';
79-
const KEY_LEVEL = 'level';
80-
const KEY_UPDATED_AT = 'updated_at';
81-
const KEY_CREATED_AT = 'created_at';
82-
const KEY_PATH = 'path';
83-
const KEY_AVAILABLE_SORT_BY = 'available_sort_by';
84-
const KEY_INCLUDE_IN_MENU = 'include_in_menu';
85-
const KEY_PRODUCT_COUNT = 'product_count';
86-
const KEY_CHILDREN_DATA = 'children_data';
87-
/**#@-*/
88-
8976
/**#@-*/
9077
protected $_eventPrefix = 'catalog_category';
9178

@@ -142,21 +129,11 @@ class Category extends \Magento\Catalog\Model\AbstractModel implements
142129
/**
143130
* Attributes are that part of interface
144131
*
132+
* @deprecated
133+
* @see CategoryInterface::ATTRIBUTES
145134
* @var array
146135
*/
147-
protected $interfaceAttributes = [
148-
'id',
149-
self::KEY_PARENT_ID,
150-
self::KEY_NAME,
151-
self::KEY_IS_ACTIVE,
152-
self::KEY_POSITION,
153-
self::KEY_LEVEL,
154-
self::KEY_UPDATED_AT,
155-
self::KEY_CREATED_AT,
156-
self::KEY_AVAILABLE_SORT_BY,
157-
self::KEY_INCLUDE_IN_MENU,
158-
self::KEY_CHILDREN_DATA,
159-
];
136+
protected $interfaceAttributes = CategoryInterface::ATTRIBUTES;
160137

161138
/**
162139
* Category tree model
@@ -230,6 +207,11 @@ class Category extends \Magento\Catalog\Model\AbstractModel implements
230207
*/
231208
protected $metadataService;
232209

210+
/**
211+
* @var GetCustomAttributeCodesInterface
212+
*/
213+
private $getCustomAttributeCodes;
214+
233215
/**
234216
* @param \Magento\Framework\Model\Context $context
235217
* @param \Magento\Framework\Registry $registry
@@ -252,6 +234,7 @@ class Category extends \Magento\Catalog\Model\AbstractModel implements
252234
* @param \Magento\Framework\Model\ResourceModel\AbstractResource $resource
253235
* @param \Magento\Framework\Data\Collection\AbstractDb $resourceCollection
254236
* @param array $data
237+
* @param GetCustomAttributeCodesInterface|null $getCustomAttributeCodes
255238
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
256239
*/
257240
public function __construct(
@@ -275,7 +258,8 @@ public function __construct(
275258
CategoryRepositoryInterface $categoryRepository,
276259
\Magento\Framework\Model\ResourceModel\AbstractResource $resource = null,
277260
\Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null,
278-
array $data = []
261+
array $data = [],
262+
GetCustomAttributeCodesInterface $getCustomAttributeCodes = null
279263
) {
280264
$this->metadataService = $metadataService;
281265
$this->_treeModel = $categoryTreeResource;
@@ -290,6 +274,9 @@ public function __construct(
290274
$this->urlFinder = $urlFinder;
291275
$this->indexerRegistry = $indexerRegistry;
292276
$this->categoryRepository = $categoryRepository;
277+
$this->getCustomAttributeCodes = $getCustomAttributeCodes ?? ObjectManager::getInstance()->get(
278+
GetCategoryCustomAttributeCodes::class
279+
);
293280
parent::__construct(
294281
$context,
295282
$registry,
@@ -323,11 +310,7 @@ protected function _construct()
323310
*/
324311
protected function getCustomAttributesCodes()
325312
{
326-
if ($this->customAttributesCodes === null) {
327-
$this->customAttributesCodes = $this->getEavAttributesCodes($this->metadataService);
328-
$this->customAttributesCodes = array_diff($this->customAttributesCodes, $this->interfaceAttributes);
329-
}
330-
return $this->customAttributesCodes;
313+
return $this->getCustomAttributeCodes->execute($this->metadataService);
331314
}
332315

333316
/**
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
namespace Magento\Catalog\Model\Entity;
8+
9+
use Magento\Catalog\Api\Data\CategoryInterface;
10+
use Magento\Eav\Model\Entity\GetCustomAttributeCodesInterface;
11+
use Magento\Framework\Api\MetadataServiceInterface;
12+
13+
class GetCategoryCustomAttributeCodes implements GetCustomAttributeCodesInterface
14+
{
15+
/**
16+
* @var GetCustomAttributeCodesInterface
17+
*/
18+
private $baseCustomAttributeCodes;
19+
20+
/**
21+
* @param GetCustomAttributeCodesInterface $baseCustomAttributeCodes
22+
*/
23+
public function __construct(
24+
GetCustomAttributeCodesInterface $baseCustomAttributeCodes
25+
) {
26+
$this->baseCustomAttributeCodes = $baseCustomAttributeCodes;
27+
}
28+
29+
/**
30+
* @inheritdoc
31+
*/
32+
public function execute(MetadataServiceInterface $metadataService): array
33+
{
34+
$customAttributesCodes = $this->baseCustomAttributeCodes->execute($metadataService);
35+
return array_diff($customAttributesCodes, CategoryInterface::ATTRIBUTES);
36+
}
37+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
namespace Magento\Catalog\Model\Entity;
8+
9+
use Magento\Catalog\Api\Data\ProductInterface;
10+
use Magento\Eav\Model\Entity\GetCustomAttributeCodesInterface;
11+
use Magento\Framework\Api\MetadataServiceInterface;
12+
13+
class GetProductCustomAttributeCodes implements GetCustomAttributeCodesInterface
14+
{
15+
/**
16+
* @var GetCustomAttributeCodesInterface
17+
*/
18+
private $baseCustomAttributeCodes;
19+
20+
/**
21+
* @param GetCustomAttributeCodesInterface $baseCustomAttributeCodes
22+
*/
23+
public function __construct(
24+
GetCustomAttributeCodesInterface $baseCustomAttributeCodes
25+
) {
26+
$this->baseCustomAttributeCodes = $baseCustomAttributeCodes;
27+
}
28+
29+
/**
30+
* @inheritdoc
31+
*/
32+
public function execute(MetadataServiceInterface $metadataService): array
33+
{
34+
$customAttributesCodes = $this->baseCustomAttributeCodes->execute($metadataService);
35+
return array_diff($customAttributesCodes, ProductInterface::ATTRIBUTES);
36+
}
37+
}

app/code/Magento/Catalog/Model/Product.php

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,12 @@
99
use Magento\Catalog\Api\Data\ProductAttributeMediaGalleryEntryInterface;
1010
use Magento\Catalog\Api\Data\ProductInterface;
1111
use Magento\Catalog\Api\ProductLinkRepositoryInterface;
12+
use Magento\Catalog\Model\Entity\GetProductCustomAttributeCodes;
1213
use Magento\Catalog\Model\Product\Attribute\Backend\Media\EntryConverterPool;
14+
use Magento\Eav\Model\Entity\GetCustomAttributeCodesInterface;
1315
use Magento\Framework\Api\AttributeValueFactory;
1416
use Magento\Framework\App\Filesystem\DirectoryList;
17+
use Magento\Framework\App\ObjectManager;
1518
use Magento\Framework\DataObject\IdentityInterface;
1619
use Magento\Framework\Pricing\SaleableInterface;
1720

@@ -305,22 +308,12 @@ class Product extends \Magento\Catalog\Model\AbstractModel implements
305308

306309
/**
307310
* List of attributes in ProductInterface
311+
*
312+
* @deprecated
313+
* @see ProductInterface::ATTRIBUTES
308314
* @var array
309315
*/
310-
protected $interfaceAttributes = [
311-
ProductInterface::SKU,
312-
ProductInterface::NAME,
313-
ProductInterface::PRICE,
314-
ProductInterface::WEIGHT,
315-
ProductInterface::STATUS,
316-
ProductInterface::VISIBILITY,
317-
ProductInterface::ATTRIBUTE_SET_ID,
318-
ProductInterface::TYPE_ID,
319-
ProductInterface::CREATED_AT,
320-
ProductInterface::UPDATED_AT,
321-
'media_gallery',
322-
'tier_price',
323-
];
316+
protected $interfaceAttributes = ProductInterface::ATTRIBUTES;
324317

325318
/**
326319
* @var \Magento\Framework\Api\ExtensionAttribute\JoinProcessorInterface
@@ -346,6 +339,11 @@ class Product extends \Magento\Catalog\Model\AbstractModel implements
346339
*/
347340
protected $linkTypeProvider;
348341

342+
/**
343+
* @var GetCustomAttributeCodesInterface
344+
*/
345+
private $getCustomAttributeCodes;
346+
349347
/**
350348
* Product constructor.
351349
* @param \Magento\Framework\Model\Context $context
@@ -383,7 +381,7 @@ class Product extends \Magento\Catalog\Model\AbstractModel implements
383381
* @param \Magento\Framework\Api\DataObjectHelper $dataObjectHelper
384382
* @param \Magento\Framework\Api\ExtensionAttribute\JoinProcessorInterface $joinProcessor
385383
* @param array $data
386-
*
384+
* @param GetCustomAttributeCodesInterface|null $getCustomAttributeCodes
387385
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
388386
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
389387
*/
@@ -422,7 +420,8 @@ public function __construct(
422420
EntryConverterPool $mediaGalleryEntryConverterPool,
423421
\Magento\Framework\Api\DataObjectHelper $dataObjectHelper,
424422
\Magento\Framework\Api\ExtensionAttribute\JoinProcessorInterface $joinProcessor,
425-
array $data = []
423+
array $data = [],
424+
GetCustomAttributeCodesInterface $getCustomAttributeCodes = null
426425
) {
427426
$this->metadataService = $metadataService;
428427
$this->_itemOptionFactory = $itemOptionFactory;
@@ -451,6 +450,9 @@ public function __construct(
451450
$this->mediaGalleryEntryConverterPool = $mediaGalleryEntryConverterPool;
452451
$this->dataObjectHelper = $dataObjectHelper;
453452
$this->joinProcessor = $joinProcessor;
453+
$this->getCustomAttributeCodes = $getCustomAttributeCodes ?? ObjectManager::getInstance()->get(
454+
GetProductCustomAttributeCodes::class
455+
);
454456
parent::__construct(
455457
$context,
456458
$registry,
@@ -478,11 +480,7 @@ protected function _construct()
478480
*/
479481
protected function getCustomAttributesCodes()
480482
{
481-
if ($this->customAttributesCodes === null) {
482-
$this->customAttributesCodes = $this->getEavAttributesCodes($this->metadataService);
483-
$this->customAttributesCodes = array_diff($this->customAttributesCodes, $this->interfaceAttributes);
484-
}
485-
return $this->customAttributesCodes;
483+
return $this->getCustomAttributeCodes->execute($this->metadataService);
486484
}
487485

488486
/**

0 commit comments

Comments
 (0)