Config.php 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Eav\Model;
  7. use Magento\Eav\Model\Entity\Attribute\AbstractAttribute;
  8. use Magento\Eav\Model\Entity\Type;
  9. use Magento\Framework\App\ObjectManager;
  10. use Magento\Framework\Serialize\SerializerInterface;
  11. /**
  12. * @api
  13. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  14. * @since 100.0.2
  15. */
  16. class Config
  17. {
  18. /**#@+
  19. * EAV cache ids
  20. */
  21. const ENTITIES_CACHE_ID = 'EAV_ENTITY_TYPES';
  22. const ATTRIBUTES_CACHE_ID = 'EAV_ENTITY_ATTRIBUTES';
  23. const ATTRIBUTES_CODES_CACHE_ID = 'EAV_ENTITY_ATTRIBUTES_CODES';
  24. /**#@-*/
  25. /**#@-*/
  26. protected $_entityTypeData;
  27. /**
  28. * Attributes data
  29. *
  30. * @var array
  31. */
  32. protected $_attributeData;
  33. /**
  34. * Attribute codes cache array
  35. *
  36. * @var array
  37. */
  38. protected $_attributeCodes;
  39. /**
  40. * Initialized objects
  41. *
  42. * array ($objectId => $object)
  43. *
  44. * @var \Magento\Eav\Model\Entity\Type[]
  45. */
  46. protected $_objects;
  47. /**
  48. * Initialized attributes
  49. *
  50. * array ($entityTypeCode =>
  51. * ($attributeCode => $object)
  52. * )
  53. *
  54. * @var AbstractAttribute[][]
  55. */
  56. private $attributes;
  57. /**
  58. * References between codes and identifiers
  59. *
  60. * array (
  61. * 'attributes'=> array ($attributeId => $attributeCode),
  62. * 'entities' => array ($entityId => $entityCode)
  63. * )
  64. *
  65. * @var array
  66. */
  67. protected $_references;
  68. /**
  69. * Cache flag
  70. *
  71. * @var bool|null
  72. */
  73. protected $_isCacheEnabled = null;
  74. /**
  75. * @var \Magento\Framework\App\CacheInterface
  76. */
  77. protected $_cache;
  78. /**
  79. * @var \Magento\Framework\App\Cache\StateInterface
  80. */
  81. protected $_cacheState;
  82. /**
  83. * @var \Magento\Eav\Model\Entity\TypeFactory
  84. */
  85. protected $_entityTypeFactory;
  86. /**
  87. * @var \Magento\Eav\Model\ResourceModel\Entity\Type\CollectionFactory
  88. */
  89. protected $entityTypeCollectionFactory;
  90. /**
  91. * @var \Magento\Framework\Validator\UniversalFactory
  92. */
  93. protected $_universalFactory;
  94. /**
  95. * @var AbstractAttribute[]
  96. */
  97. private $attributeProto = [];
  98. /**
  99. * @var SerializerInterface
  100. */
  101. private $serializer;
  102. /**
  103. * Cache of attributes per set
  104. *
  105. * @var array
  106. */
  107. private $attributesPerSet = [];
  108. /**
  109. * @param \Magento\Framework\App\CacheInterface $cache
  110. * @param \Magento\Eav\Model\Entity\TypeFactory $entityTypeFactory
  111. * @param \Magento\Eav\Model\ResourceModel\Entity\Type\CollectionFactory $entityTypeCollectionFactory
  112. * @param \Magento\Framework\App\Cache\StateInterface $cacheState
  113. * @param \Magento\Framework\Validator\UniversalFactory $universalFactory
  114. * @param SerializerInterface $serializer
  115. * @codeCoverageIgnore
  116. */
  117. public function __construct(
  118. \Magento\Framework\App\CacheInterface $cache,
  119. \Magento\Eav\Model\Entity\TypeFactory $entityTypeFactory,
  120. \Magento\Eav\Model\ResourceModel\Entity\Type\CollectionFactory $entityTypeCollectionFactory,
  121. \Magento\Framework\App\Cache\StateInterface $cacheState,
  122. \Magento\Framework\Validator\UniversalFactory $universalFactory,
  123. SerializerInterface $serializer = null
  124. ) {
  125. $this->_cache = $cache;
  126. $this->_entityTypeFactory = $entityTypeFactory;
  127. $this->entityTypeCollectionFactory = $entityTypeCollectionFactory;
  128. $this->_cacheState = $cacheState;
  129. $this->_universalFactory = $universalFactory;
  130. $this->serializer = $serializer ?: ObjectManager::getInstance()->get(SerializerInterface::class);
  131. }
  132. /**
  133. * Get cache interface
  134. *
  135. * @return \Magento\Framework\App\CacheInterface
  136. * @codeCoverageIgnore
  137. */
  138. public function getCache()
  139. {
  140. return $this->_cache;
  141. }
  142. /**
  143. * Reset object state
  144. *
  145. * @return $this
  146. */
  147. public function clear()
  148. {
  149. $this->_entityTypeData = null;
  150. $this->_attributeData = null;
  151. $this->_objects = null;
  152. $this->attributes = null;
  153. $this->_references = null;
  154. $this->_attributeCodes = null;
  155. $this->attributesPerSet = [];
  156. $this->_cache->clean(
  157. [
  158. \Magento\Eav\Model\Cache\Type::CACHE_TAG,
  159. \Magento\Eav\Model\Entity\Attribute::CACHE_TAG,
  160. ]
  161. );
  162. return $this;
  163. }
  164. /**
  165. * Get object by identifier
  166. *
  167. * @param mixed $id
  168. * @return mixed
  169. */
  170. protected function _load($id)
  171. {
  172. return isset($this->_objects[$id]) ? $this->_objects[$id] : null;
  173. }
  174. /**
  175. * Get attributes by entity type code
  176. *
  177. * @param string $entityTypeCode
  178. * @return AbstractAttribute[]
  179. */
  180. private function loadAttributes($entityTypeCode)
  181. {
  182. return isset($this->attributes[$entityTypeCode]) ? $this->attributes[$entityTypeCode] : [];
  183. }
  184. /**
  185. * Associate object with identifier
  186. *
  187. * @param mixed $obj
  188. * @param mixed $id
  189. * @return void
  190. * @codeCoverageIgnore
  191. */
  192. protected function _save($obj, $id)
  193. {
  194. $this->_objects[$id] = $obj;
  195. }
  196. /**
  197. * Associate object with identifier
  198. *
  199. * @param AbstractAttribute $attribute
  200. * @param string $entityTypeCode
  201. * @param string $attributeCode
  202. * @return void
  203. */
  204. private function saveAttribute(AbstractAttribute $attribute, $entityTypeCode, $attributeCode)
  205. {
  206. $this->attributes[$entityTypeCode][$attributeCode] = $attribute;
  207. }
  208. /**
  209. * Specify reference for entity type id
  210. *
  211. * @param int $id
  212. * @param string $code
  213. * @return $this
  214. * @codeCoverageIgnore
  215. */
  216. protected function _addEntityTypeReference($id, $code)
  217. {
  218. $this->_references['entity'][$id] = $code;
  219. return $this;
  220. }
  221. /**
  222. * Get entity type code by id
  223. *
  224. * @param int $id
  225. * @return string
  226. */
  227. protected function _getEntityTypeReference($id)
  228. {
  229. return isset($this->_references['entity'][$id]) ? $this->_references['entity'][$id] : null;
  230. }
  231. /**
  232. * Specify reference between entity attribute id and attribute code
  233. *
  234. * @param int $id
  235. * @param string $code
  236. * @param string $entityTypeCode
  237. * @return $this
  238. */
  239. protected function _addAttributeReference($id, $code, $entityTypeCode)
  240. {
  241. $this->_references['attribute'][$entityTypeCode][$id] = $code;
  242. return $this;
  243. }
  244. /**
  245. * Get attribute code by attribute id
  246. *
  247. * @param int $id
  248. * @param string $entityTypeCode
  249. * @return string|null
  250. */
  251. protected function _getAttributeReference($id, $entityTypeCode)
  252. {
  253. if (isset($this->_references['attribute'][$entityTypeCode][$id])) {
  254. return $this->_references['attribute'][$entityTypeCode][$id];
  255. }
  256. return null;
  257. }
  258. /**
  259. * Get internal cache key for entity type code
  260. *
  261. * @param string $code
  262. * @return string
  263. * @codeCoverageIgnore
  264. */
  265. protected function _getEntityKey($code)
  266. {
  267. return 'ENTITY/' . $code;
  268. }
  269. /**
  270. * Get internal cache key for attribute object cache
  271. *
  272. * @param string $entityTypeCode
  273. * @param string $attributeCode
  274. * @return string
  275. * @codeCoverageIgnore
  276. */
  277. protected function _getAttributeKey($entityTypeCode, $attributeCode)
  278. {
  279. $codeSegments = explode('.', $attributeCode);
  280. return 'ATTRIBUTE/' . $entityTypeCode . '/' . array_pop($codeSegments);
  281. }
  282. /**
  283. * Check EAV cache availability
  284. *
  285. * @return bool
  286. */
  287. public function isCacheEnabled()
  288. {
  289. if ($this->_isCacheEnabled === null) {
  290. $this->_isCacheEnabled = $this->_cacheState->isEnabled(\Magento\Eav\Model\Cache\Type::TYPE_IDENTIFIER);
  291. }
  292. return $this->_isCacheEnabled;
  293. }
  294. /**
  295. * Initialize all entity types data
  296. *
  297. * @return $this
  298. */
  299. protected function _initEntityTypes()
  300. {
  301. if (is_array($this->_entityTypeData)) {
  302. return $this;
  303. }
  304. \Magento\Framework\Profiler::start('EAV: ' . __METHOD__, ['group' => 'EAV', 'method' => __METHOD__]);
  305. if ($this->isCacheEnabled() && ($cache = $this->_cache->load(self::ENTITIES_CACHE_ID))) {
  306. $this->_entityTypeData = $this->serializer->unserialize($cache);
  307. foreach ($this->_entityTypeData as $typeCode => $data) {
  308. $typeId = $data['entity_type_id'];
  309. $this->_addEntityTypeReference($typeId, $typeCode);
  310. }
  311. \Magento\Framework\Profiler::stop('EAV: ' . __METHOD__);
  312. return $this;
  313. }
  314. $entityTypesData = $this->entityTypeCollectionFactory->create()->getData();
  315. foreach ($entityTypesData as $typeData) {
  316. if (!isset($typeData['attribute_model'])) {
  317. $typeData['attribute_model'] = \Magento\Eav\Model\Entity\Attribute::class;
  318. }
  319. $typeCode = $typeData['entity_type_code'];
  320. $typeId = $typeData['entity_type_id'];
  321. $this->_addEntityTypeReference($typeId, $typeCode);
  322. $this->_entityTypeData[$typeCode] = $typeData;
  323. }
  324. if ($this->isCacheEnabled()) {
  325. $this->_cache->save(
  326. $this->serializer->serialize($this->_entityTypeData),
  327. self::ENTITIES_CACHE_ID,
  328. [
  329. \Magento\Eav\Model\Cache\Type::CACHE_TAG,
  330. \Magento\Eav\Model\Entity\Attribute::CACHE_TAG
  331. ]
  332. );
  333. }
  334. \Magento\Framework\Profiler::stop('EAV: ' . __METHOD__);
  335. return $this;
  336. }
  337. /**
  338. * Get entity type object by entity type code/identifier
  339. *
  340. * @param int|string|Type $code
  341. * @return Type
  342. * @throws \Magento\Framework\Exception\LocalizedException
  343. */
  344. public function getEntityType($code)
  345. {
  346. if ($code instanceof Type) {
  347. return $code;
  348. }
  349. $this->_initEntityTypes();
  350. \Magento\Framework\Profiler::start('EAV: ' . __METHOD__, ['group' => 'EAV', 'method' => __METHOD__]);
  351. if (is_numeric($code)) {
  352. $entityCode = $this->_getEntityTypeReference($code);
  353. if ($entityCode !== null) {
  354. $code = $entityCode;
  355. }
  356. }
  357. $entityKey = $this->_getEntityKey($code);
  358. $entityType = $this->_load($entityKey);
  359. if ($entityType) {
  360. \Magento\Framework\Profiler::stop('EAV: ' . __METHOD__);
  361. return $entityType;
  362. }
  363. $entityType = $this->_entityTypeFactory->create(
  364. ['data' => isset($this->_entityTypeData[$code]) ? $this->_entityTypeData[$code] : []]
  365. );
  366. if (!$entityType->getId()) {
  367. throw new \Magento\Framework\Exception\LocalizedException(__('Invalid entity_type specified: %1', $code));
  368. }
  369. $this->_addEntityTypeReference($entityType->getId(), $entityType->getEntityTypeCode());
  370. $this->_save($entityType, $entityKey);
  371. \Magento\Framework\Profiler::stop('EAV: ' . __METHOD__);
  372. return $entityType;
  373. }
  374. /**
  375. * Initialize all attributes for entity type
  376. *
  377. * @param string $entityType
  378. * @return $this
  379. */
  380. protected function _initAttributes($entityType)
  381. {
  382. $entityType = $this->getEntityType($entityType);
  383. $entityTypeCode = $entityType->getEntityTypeCode();
  384. if (is_array($this->_attributeData) && isset($this->_attributeData[$entityTypeCode])) {
  385. return $this;
  386. }
  387. if ($this->initAttributesFromCache($entityType)) {
  388. return $this;
  389. }
  390. \Magento\Framework\Profiler::start('EAV: ' . __METHOD__, ['group' => 'EAV', 'method' => __METHOD__]);
  391. $attributes = $this->_universalFactory->create(
  392. $entityType->getEntityAttributeCollection()
  393. )->setEntityTypeFilter(
  394. $entityType
  395. )->getData();
  396. $this->_attributeData[$entityTypeCode] = [];
  397. foreach ($attributes as $attribute) {
  398. if (empty($attribute['attribute_model'])) {
  399. $attribute['attribute_model'] = $entityType->getAttributeModel();
  400. }
  401. $attributeObject = $this->_createAttribute($entityType, $attribute);
  402. $this->saveAttribute($attributeObject, $entityTypeCode, $attributeObject->getAttributeCode());
  403. $this->_attributeData[$entityTypeCode][$attribute['attribute_code']] = $attributeObject->toArray();
  404. }
  405. if ($this->isCacheEnabled()) {
  406. $this->_cache->save(
  407. $this->serializer->serialize($this->_attributeData[$entityTypeCode]),
  408. self::ATTRIBUTES_CACHE_ID . $entityTypeCode,
  409. [
  410. \Magento\Eav\Model\Cache\Type::CACHE_TAG,
  411. \Magento\Eav\Model\Entity\Attribute::CACHE_TAG
  412. ]
  413. );
  414. }
  415. \Magento\Framework\Profiler::stop('EAV: ' . __METHOD__);
  416. return $this;
  417. }
  418. /**
  419. * Get attributes by entity type
  420. *
  421. * @deprecated 101.0.0
  422. * @see \Magento\Eav\Model\Config::getEntityAttributes
  423. *
  424. * @param string $entityType
  425. * @return AbstractAttribute[]
  426. * @since 101.0.0
  427. */
  428. public function getAttributes($entityType)
  429. {
  430. return $this->getEntityAttributes($entityType);
  431. }
  432. /**
  433. * Get attribute by code for entity type
  434. *
  435. * @param mixed $entityType
  436. * @param mixed $code
  437. * @return AbstractAttribute
  438. * @throws \Magento\Framework\Exception\LocalizedException
  439. */
  440. public function getAttribute($entityType, $code)
  441. {
  442. if ($code instanceof \Magento\Eav\Model\Entity\Attribute\AttributeInterface) {
  443. return $code;
  444. }
  445. \Magento\Framework\Profiler::start('EAV: ' . __METHOD__, ['group' => 'EAV', 'method' => __METHOD__]);
  446. $entityTypeCode = $this->getEntityType($entityType)->getEntityTypeCode();
  447. if (is_numeric($code)) { // if code is numeric, try to map attribute id to code
  448. $code = $this->_getAttributeReference($code, $entityTypeCode) ?: $code;
  449. }
  450. if (isset($this->attributes[$entityTypeCode][$code])) {
  451. \Magento\Framework\Profiler::stop('EAV: ' . __METHOD__);
  452. return $this->attributes[$entityTypeCode][$code];
  453. }
  454. $attributes = $this->loadAttributes($entityTypeCode);
  455. $attribute = isset($attributes[$code]) ? $attributes[$code] : null;
  456. if (!$attribute) {
  457. $attribute = $this->createAttributeByAttributeCode($entityType, $code);
  458. $this->_addAttributeReference(
  459. $attribute->getAttributeId(),
  460. $attribute->getAttributeCode(),
  461. $entityTypeCode
  462. );
  463. $this->saveAttribute($attribute, $entityTypeCode, $attribute->getAttributeCode());
  464. }
  465. \Magento\Framework\Profiler::stop('EAV: ' . __METHOD__);
  466. return $attribute;
  467. }
  468. /**
  469. * Create attribute
  470. *
  471. * @param string $model
  472. * @return Entity\Attribute\AbstractAttribute
  473. */
  474. private function createAttribute($model)
  475. {
  476. if (!isset($this->attributeProto[$model])) {
  477. /** @var AbstractAttribute $attribute */
  478. $this->attributeProto[$model] = $this->_universalFactory->create($model);
  479. }
  480. return clone $this->attributeProto[$model];
  481. }
  482. /**
  483. * Get codes of all entity type attributes
  484. *
  485. * @deprecated 101.0.0
  486. * @see \Magento\Eav\Model\Config::getEntityAttributes
  487. *
  488. * @param mixed $entityType
  489. * @param \Magento\Framework\DataObject $object
  490. * @return string[]
  491. */
  492. public function getEntityAttributeCodes($entityType, $object = null)
  493. {
  494. return array_keys($this->getEntityAttributes($entityType, $object));
  495. }
  496. /**
  497. * Get all entity type attributes
  498. *
  499. * @param int|string|Type $entityType
  500. * @param \Magento\Framework\DataObject|null $object
  501. * @return AbstractAttribute[]
  502. *
  503. * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  504. * @SuppressWarnings(PHPMD.NPathComplexity)
  505. * @since 101.0.0
  506. */
  507. public function getEntityAttributes($entityType, $object = null)
  508. {
  509. $entityType = $this->getEntityType($entityType);
  510. $attributeSetId = 0;
  511. $storeId = 0;
  512. if ($object instanceof \Magento\Framework\DataObject) {
  513. $attributeSetId = $object->getAttributeSetId() ?: $attributeSetId;
  514. $storeId = $object->getStoreId() ?: $storeId;
  515. }
  516. $cacheKey = self::ATTRIBUTES_CACHE_ID . '-' . $entityType->getId() . '-' . $storeId . '-' . $attributeSetId;
  517. if (isset($this->attributesPerSet[$cacheKey])) {
  518. return $this->attributesPerSet[$cacheKey];
  519. }
  520. $attributesData = $this->isCacheEnabled() && ($attributes = $this->_cache->load($cacheKey))
  521. ? $this->serializer->unserialize($attributes)
  522. : null;
  523. $attributes = [];
  524. if ($attributesData === null) {
  525. if ($attributeSetId) {
  526. $attributesData = $this->_universalFactory->create(
  527. $entityType->getEntityAttributeCollection()
  528. )->setEntityTypeFilter(
  529. $entityType
  530. )->setAttributeSetFilter(
  531. $attributeSetId
  532. )->addStoreLabel(
  533. $storeId
  534. )->getData();
  535. } else {
  536. $this->_initAttributes($entityType);
  537. $attributesData = $this->_attributeData[$entityType->getEntityTypeCode()];
  538. }
  539. if ($this->isCacheEnabled()) {
  540. $this->_cache->save(
  541. $this->serializer->serialize($attributesData),
  542. $cacheKey,
  543. [
  544. \Magento\Eav\Model\Cache\Type::CACHE_TAG,
  545. \Magento\Eav\Model\Entity\Attribute::CACHE_TAG
  546. ]
  547. );
  548. }
  549. }
  550. foreach ($attributesData as $attributeData) {
  551. $attributes[$attributeData['attribute_code']] = $this->_createAttribute($entityType, $attributeData);
  552. }
  553. $this->attributesPerSet[$cacheKey] = $attributes;
  554. return $attributes;
  555. }
  556. /**
  557. * Create attribute from attribute data array
  558. *
  559. * @param string $entityType
  560. * @param array $attributeData
  561. * @return AbstractAttribute
  562. */
  563. protected function _createAttribute($entityType, $attributeData)
  564. {
  565. $entityType = $this->getEntityType($entityType);
  566. $entityTypeCode = $entityType->getEntityTypeCode();
  567. $code = $attributeData['attribute_code'];
  568. $attributes = $this->loadAttributes($entityTypeCode);
  569. $attribute = isset($attributes[$code]) ? $attributes[$code] : null;
  570. if ($attribute) {
  571. $existsFullAttribute = $attribute->hasIsRequired();
  572. $fullAttributeData = array_key_exists('is_required', $attributeData);
  573. if ($existsFullAttribute || !$existsFullAttribute && !$fullAttributeData) {
  574. return $attribute;
  575. }
  576. }
  577. if (!empty($attributeData['attribute_model'])) {
  578. $model = $attributeData['attribute_model'];
  579. } else {
  580. $model = $entityType->getAttributeModel();
  581. }
  582. /** @var AbstractAttribute $attribute */
  583. $attribute = $this->createAttribute($model)->setData($attributeData);
  584. $this->_addAttributeReference(
  585. $attributeData['attribute_id'],
  586. $code,
  587. $entityTypeCode
  588. );
  589. $this->saveAttribute($attribute, $entityTypeCode, $code);
  590. return $attribute;
  591. }
  592. /**
  593. * Validate attribute data from import
  594. *
  595. * @param array $attributeData
  596. * @return bool
  597. */
  598. protected function _validateAttributeData($attributeData = null)
  599. {
  600. if (!is_array($attributeData)) {
  601. return false;
  602. }
  603. $requiredKeys = ['attribute_id', 'attribute_code', 'entity_type_id', 'attribute_model'];
  604. foreach ($requiredKeys as $key) {
  605. if (!array_key_exists($key, $attributeData)) {
  606. return false;
  607. }
  608. }
  609. return true;
  610. }
  611. /**
  612. * Import attributes data from external source
  613. *
  614. * @param string|Type $entityType
  615. * @param array $attributes
  616. * @return $this
  617. */
  618. public function importAttributesData($entityType, array $attributes)
  619. {
  620. $entityType = $this->getEntityType($entityType);
  621. foreach ($attributes as $attributeData) {
  622. if (!$this->_validateAttributeData($attributeData)) {
  623. continue;
  624. }
  625. $this->_createAttribute($entityType, $attributeData);
  626. }
  627. return $this;
  628. }
  629. /**
  630. * Create attribute by attribute code
  631. *
  632. * @param string $entityType
  633. * @param string $attributeCode
  634. * @return AbstractAttribute
  635. */
  636. private function createAttributeByAttributeCode($entityType, $attributeCode)
  637. {
  638. $entityType = $this->getEntityType($entityType);
  639. $attribute = $this->createAttribute($entityType->getAttributeModel());
  640. if (is_numeric($attributeCode)) {
  641. $attribute->load($attributeCode);
  642. if ($attribute->getEntityTypeId() != $entityType->getId()) {
  643. $attribute = $this->createAttribute($entityType->getAttributeModel());
  644. }
  645. } else {
  646. $attribute->loadByCode($entityType->getEntityTypeId(), $attributeCode);
  647. $attribute->setAttributeCode($attributeCode);
  648. }
  649. $entity = $entityType->getEntity();
  650. if ($entity instanceof \Magento\Eav\Model\ResourceModel\Attribute\DefaultEntityAttributes\ProviderInterface
  651. && in_array($attribute->getAttributeCode(), $entity->getDefaultAttributes(), true)
  652. ) {
  653. $attribute->setBackendType(AbstractAttribute::TYPE_STATIC)->setIsGlobal(1);
  654. }
  655. $attribute->setEntityType($entityType)->setEntityTypeId($entityType->getId());
  656. return $attribute;
  657. }
  658. /**
  659. * Initialize attributes from cache for given entity type
  660. *
  661. * @param Type $entityType
  662. * @return bool
  663. */
  664. private function initAttributesFromCache(Type $entityType)
  665. {
  666. $entityTypeCode = $entityType->getEntityTypeCode();
  667. $cacheKey = self::ATTRIBUTES_CACHE_ID . $entityTypeCode;
  668. if ($this->isCacheEnabled() && ($attributes = $this->_cache->load($cacheKey))) {
  669. $attributes = $this->serializer->unserialize($attributes);
  670. if ($attributes) {
  671. foreach ($attributes as $attribute) {
  672. $this->_createAttribute($entityType, $attribute);
  673. $this->_attributeData[$entityTypeCode][$attribute['attribute_code']] = $attribute;
  674. }
  675. return true;
  676. }
  677. }
  678. return false;
  679. }
  680. }