Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Public Member Functions | Data Fields | Protected Member Functions | Protected Attributes
Config Class Reference
Inheritance diagram for Config:
Config

Public Member Functions

 __construct (\Magento\Framework\App\CacheInterface $cache, \Magento\Eav\Model\Entity\TypeFactory $entityTypeFactory, \Magento\Eav\Model\ResourceModel\Entity\Type\CollectionFactory $entityTypeCollectionFactory, \Magento\Framework\App\Cache\StateInterface $cacheState, \Magento\Framework\Validator\UniversalFactory $universalFactory, SerializerInterface $serializer=null)
 
 getCache ()
 
 clear ()
 
 isCacheEnabled ()
 
 getEntityType ($code)
 
 getAttributes ($entityType)
 
 getAttribute ($entityType, $code)
 
 getEntityAttributeCodes ($entityType, $object=null)
 
 getEntityAttributes ($entityType, $object=null)
 
 importAttributesData ($entityType, array $attributes)
 

Data Fields

const ENTITIES_CACHE_ID = 'EAV_ENTITY_TYPES'
 
const ATTRIBUTES_CACHE_ID = 'EAV_ENTITY_ATTRIBUTES'
 
const ATTRIBUTES_CODES_CACHE_ID = 'EAV_ENTITY_ATTRIBUTES_CODES'
 

Protected Member Functions

 _load ($id)
 
 _save ($obj, $id)
 
 _addEntityTypeReference ($id, $code)
 
 _getEntityTypeReference ($id)
 
 _addAttributeReference ($id, $code, $entityTypeCode)
 
 _getAttributeReference ($id, $entityTypeCode)
 
 _getEntityKey ($code)
 
 _getAttributeKey ($entityTypeCode, $attributeCode)
 
 _initEntityTypes ()
 
 _initAttributes ($entityType)
 
 _validateAttributeData ($attributeData=null)
 

Protected Attributes

 $_entityTypeData
 
 $_attributeData
 
 $_attributeCodes
 
 $_objects
 
 $_references
 
 $_isCacheEnabled = null
 
 $_cache
 
 $_cacheState
 
 $_entityTypeFactory
 
 $entityTypeCollectionFactory
 
 $_universalFactory
 

Detailed Description

@api @SuppressWarnings(PHPMD.CouplingBetweenObjects)

Since
100.0.2

Definition at line 18 of file Config.php.

Constructor & Destructor Documentation

◆ __construct()

__construct ( \Magento\Framework\App\CacheInterface  $cache,
\Magento\Eav\Model\Entity\TypeFactory  $entityTypeFactory,
\Magento\Eav\Model\ResourceModel\Entity\Type\CollectionFactory  $entityTypeCollectionFactory,
\Magento\Framework\App\Cache\StateInterface  $cacheState,
\Magento\Framework\Validator\UniversalFactory  $universalFactory,
SerializerInterface  $serializer = null 
)
Parameters
\Magento\Framework\App\CacheInterface$cache
\Magento\Eav\Model\Entity\TypeFactory$entityTypeFactory
\Magento\Eav\Model\ResourceModel\Entity\Type\CollectionFactory$entityTypeCollectionFactory
\Magento\Framework\App\Cache\StateInterface$cacheState
\Magento\Framework\Validator\UniversalFactory$universalFactory
SerializerInterface$serializer@codeCoverageIgnore

Definition at line 135 of file Config.php.

142  {
143  $this->_cache = $cache;
144  $this->_entityTypeFactory = $entityTypeFactory;
145  $this->entityTypeCollectionFactory = $entityTypeCollectionFactory;
146  $this->_cacheState = $cacheState;
147  $this->_universalFactory = $universalFactory;
148  $this->serializer = $serializer ?: ObjectManager::getInstance()->get(SerializerInterface::class);
149  }

Member Function Documentation

◆ _addAttributeReference()

_addAttributeReference (   $id,
  $code,
  $entityTypeCode 
)
protected

Specify reference between entity attribute id and attribute code

Parameters
int$id
string$code
string$entityTypeCode
Returns
$this

Definition at line 266 of file Config.php.

267  {
268  $this->_references['attribute'][$entityTypeCode][$id] = $code;
269  return $this;
270  }
$id
Definition: fieldset.phtml:14
$code
Definition: info.phtml:12

◆ _addEntityTypeReference()

_addEntityTypeReference (   $id,
  $code 
)
protected

Specify reference for entity type id

Parameters
int$id
string$code
Returns
$this @codeCoverageIgnore

Definition at line 241 of file Config.php.

242  {
243  $this->_references['entity'][$id] = $code;
244  return $this;
245  }
$id
Definition: fieldset.phtml:14
$code
Definition: info.phtml:12

◆ _getAttributeKey()

_getAttributeKey (   $entityTypeCode,
  $attributeCode 
)
protected

Get internal cache key for attribute object cache

Parameters
string$entityTypeCode
string$attributeCode
Returns
string @codeCoverageIgnore

Definition at line 307 of file Config.php.

308  {
309  $codeSegments = explode('.', $attributeCode);
310 
311  return 'ATTRIBUTE/' . $entityTypeCode . '/' . array_pop($codeSegments);
312  }
$attributeCode
Definition: extend.phtml:12

◆ _getAttributeReference()

_getAttributeReference (   $id,
  $entityTypeCode 
)
protected

Get attribute code by attribute id

Parameters
int$id
string$entityTypeCode
Returns
string|null

Definition at line 279 of file Config.php.

280  {
281  if (isset($this->_references['attribute'][$entityTypeCode][$id])) {
282  return $this->_references['attribute'][$entityTypeCode][$id];
283  }
284  return null;
285  }
$id
Definition: fieldset.phtml:14

◆ _getEntityKey()

_getEntityKey (   $code)
protected

Get internal cache key for entity type code

Parameters
string$code
Returns
string @codeCoverageIgnore

Definition at line 294 of file Config.php.

295  {
296  return 'ENTITY/' . $code;
297  }
$code
Definition: info.phtml:12

◆ _getEntityTypeReference()

_getEntityTypeReference (   $id)
protected

Get entity type code by id

Parameters
int$id
Returns
string

Definition at line 253 of file Config.php.

254  {
255  return isset($this->_references['entity'][$id]) ? $this->_references['entity'][$id] : null;
256  }
$id
Definition: fieldset.phtml:14

◆ _initAttributes()

_initAttributes (   $entityType)
protected

Initialize all attributes for entity type

Parameters
string$entityType
Returns
$this

Definition at line 424 of file Config.php.

425  {
427  $entityTypeCode = $entityType->getEntityTypeCode();
428 
429  if (is_array($this->_attributeData) && isset($this->_attributeData[$entityTypeCode])) {
430  return $this;
431  }
432 
433  if ($this->initAttributesFromCache($entityType)) {
434  return $this;
435  }
436 
437  \Magento\Framework\Profiler::start('EAV: ' . __METHOD__, ['group' => 'EAV', 'method' => __METHOD__]);
438 
439  $attributes = $this->_universalFactory->create(
440  $entityType->getEntityAttributeCollection()
441  )->setEntityTypeFilter(
443  )->getData();
444 
445  $this->_attributeData[$entityTypeCode] = [];
446  foreach ($attributes as $attribute) {
447  if (empty($attribute['attribute_model'])) {
448  $attribute['attribute_model'] = $entityType->getAttributeModel();
449  }
450  $attributeObject = $this->_createAttribute($entityType, $attribute);
451  $this->saveAttribute($attributeObject, $entityTypeCode, $attributeObject->getAttributeCode());
452  $this->_attributeData[$entityTypeCode][$attribute['attribute_code']] = $attributeObject->toArray();
453  }
454  if ($this->isCacheEnabled()) {
455  $this->_cache->save(
456  $this->serializer->serialize($this->_attributeData[$entityTypeCode]),
457  self::ATTRIBUTES_CACHE_ID . $entityTypeCode,
458  [
460  \Magento\Eav\Model\Entity\Attribute::CACHE_TAG
461  ]
462  );
463  }
464 
465  \Magento\Framework\Profiler::stop('EAV: ' . __METHOD__);
466  return $this;
467  }

◆ _initEntityTypes()

_initEntityTypes ( )
protected

Initialize all entity types data

Returns
$this

Definition at line 332 of file Config.php.

333  {
334  if (is_array($this->_entityTypeData)) {
335  return $this;
336  }
337  \Magento\Framework\Profiler::start('EAV: ' . __METHOD__, ['group' => 'EAV', 'method' => __METHOD__]);
338 
339  if ($this->isCacheEnabled() && ($cache = $this->_cache->load(self::ENTITIES_CACHE_ID))) {
340  $this->_entityTypeData = $this->serializer->unserialize($cache);
341  foreach ($this->_entityTypeData as $typeCode => $data) {
342  $typeId = $data['entity_type_id'];
343  $this->_addEntityTypeReference($typeId, $typeCode);
344  }
345  \Magento\Framework\Profiler::stop('EAV: ' . __METHOD__);
346  return $this;
347  }
348 
349  $entityTypesData = $this->entityTypeCollectionFactory->create()->getData();
350  foreach ($entityTypesData as $typeData) {
351  if (!isset($typeData['attribute_model'])) {
352  $typeData['attribute_model'] = \Magento\Eav\Model\Entity\Attribute::class;
353  }
354 
355  $typeCode = $typeData['entity_type_code'];
356  $typeId = $typeData['entity_type_id'];
357 
358  $this->_addEntityTypeReference($typeId, $typeCode);
359  $this->_entityTypeData[$typeCode] = $typeData;
360  }
361 
362  if ($this->isCacheEnabled()) {
363  $this->_cache->save(
364  $this->serializer->serialize($this->_entityTypeData),
366  [
368  \Magento\Eav\Model\Entity\Attribute::CACHE_TAG
369  ]
370  );
371  }
372  \Magento\Framework\Profiler::stop('EAV: ' . __METHOD__);
373  return $this;
374  }
_addEntityTypeReference($id, $code)
Definition: Config.php:241

◆ _load()

_load (   $id)
protected

Get object by identifier

Parameters
mixed$id
Returns
mixed

Definition at line 191 of file Config.php.

192  {
193  return isset($this->_objects[$id]) ? $this->_objects[$id] : null;
194  }
$id
Definition: fieldset.phtml:14

◆ _save()

_save (   $obj,
  $id 
)
protected

Associate object with identifier

Parameters
mixed$obj
mixed$id
Returns
void @codeCoverageIgnore

Definition at line 215 of file Config.php.

216  {
217  $this->_objects[$id] = $obj;
218  }
$id
Definition: fieldset.phtml:14

◆ _validateAttributeData()

_validateAttributeData (   $attributeData = null)
protected

Validate attribute data from import

Parameters
array$attributeData
Returns
bool

Definition at line 670 of file Config.php.

671  {
672  if (!is_array($attributeData)) {
673  return false;
674  }
675  $requiredKeys = ['attribute_id', 'attribute_code', 'entity_type_id', 'attribute_model'];
676  foreach ($requiredKeys as $key) {
677  if (!array_key_exists($key, $attributeData)) {
678  return false;
679  }
680  }
681 
682  return true;
683  }

◆ clear()

clear ( )

Reset object state

Returns
$this

Definition at line 167 of file Config.php.

168  {
169  $this->_entityTypeData = null;
170  $this->_attributeData = null;
171  $this->_objects = null;
172  $this->attributes = null;
173  $this->_references = null;
174  $this->_attributeCodes = null;
175  $this->attributesPerSet = [];
176  $this->_cache->clean(
177  [
178  \Magento\Eav\Model\Cache\Type::CACHE_TAG,
179  \Magento\Eav\Model\Entity\Attribute::CACHE_TAG,
180  ]
181  );
182  return $this;
183  }

◆ getAttribute()

getAttribute (   $entityType,
  $code 
)

Get attribute by code for entity type

Parameters
mixed$entityType
mixed$code
Returns
AbstractAttribute
Exceptions

Definition at line 492 of file Config.php.

493  {
494  if ($code instanceof \Magento\Eav\Model\Entity\Attribute\AttributeInterface) {
495  return $code;
496  }
497 
498  \Magento\Framework\Profiler::start('EAV: ' . __METHOD__, ['group' => 'EAV', 'method' => __METHOD__]);
499  $entityTypeCode = $this->getEntityType($entityType)->getEntityTypeCode();
500 
501  if (is_numeric($code)) { // if code is numeric, try to map attribute id to code
503  }
504 
505  if (isset($this->attributes[$entityTypeCode][$code])) {
506  \Magento\Framework\Profiler::stop('EAV: ' . __METHOD__);
507  return $this->attributes[$entityTypeCode][$code];
508  }
509 
510  $attributes = $this->loadAttributes($entityTypeCode);
511  $attribute = isset($attributes[$code]) ? $attributes[$code] : null;
512  if (!$attribute) {
513  $attribute = $this->createAttributeByAttributeCode($entityType, $code);
514  $this->_addAttributeReference(
515  $attribute->getAttributeId(),
516  $attribute->getAttributeCode(),
518  );
519  $this->saveAttribute($attribute, $entityTypeCode, $attribute->getAttributeCode());
520  }
521  \Magento\Framework\Profiler::stop('EAV: ' . __METHOD__);
522  return $attribute;
523  }
_getAttributeReference($id, $entityTypeCode)
Definition: Config.php:279
_addAttributeReference($id, $code, $entityTypeCode)
Definition: Config.php:266
$code
Definition: info.phtml:12

◆ getAttributes()

getAttributes (   $entityType)

Get attributes by entity type

Deprecated:
101.0.0
See also
\Magento\Eav\Model\Config::getEntityAttributes
Parameters
string$entityType
Returns
AbstractAttribute[]
Since
101.0.0

Definition at line 479 of file Config.php.

480  {
481  return $this->getEntityAttributes($entityType);
482  }
getEntityAttributes($entityType, $object=null)
Definition: Config.php:566

◆ getCache()

getCache ( )

Get cache interface

Returns
\Magento\Framework\App\CacheInterface @codeCoverageIgnore

Definition at line 157 of file Config.php.

158  {
159  return $this->_cache;
160  }

◆ getEntityAttributeCodes()

getEntityAttributeCodes (   $entityType,
  $object = null 
)

Get codes of all entity type attributes

Deprecated:
101.0.0
See also
\Magento\Eav\Model\Config::getEntityAttributes
Parameters
mixed$entityType
\Magento\Framework\DataObject$object
Returns
string[]

Definition at line 550 of file Config.php.

551  {
552  return array_keys($this->getEntityAttributes($entityType, $object));
553  }
getEntityAttributes($entityType, $object=null)
Definition: Config.php:566

◆ getEntityAttributes()

getEntityAttributes (   $entityType,
  $object = null 
)

Get all entity type attributes

Parameters
int | string | Type$entityType
\Magento\Framework\DataObject | null$object
Returns
AbstractAttribute[]

@SuppressWarnings(PHPMD.CyclomaticComplexity) @SuppressWarnings(PHPMD.NPathComplexity)

Since
101.0.0

Definition at line 566 of file Config.php.

567  {
569  $attributeSetId = 0;
570  $storeId = 0;
571  if ($object instanceof \Magento\Framework\DataObject) {
572  $attributeSetId = $object->getAttributeSetId() ?: $attributeSetId;
573  $storeId = $object->getStoreId() ?: $storeId;
574  }
575  $cacheKey = self::ATTRIBUTES_CACHE_ID . '-' . $entityType->getId() . '-' . $storeId . '-' . $attributeSetId;
576 
577  if (isset($this->attributesPerSet[$cacheKey])) {
578  return $this->attributesPerSet[$cacheKey];
579  }
580 
581  $attributesData = $this->isCacheEnabled() && ($attributes = $this->_cache->load($cacheKey))
582  ? $this->serializer->unserialize($attributes)
583  : null;
584 
585  $attributes = [];
586  if ($attributesData === null) {
587  if ($attributeSetId) {
588  $attributesData = $this->_universalFactory->create(
589  $entityType->getEntityAttributeCollection()
590  )->setEntityTypeFilter(
592  )->setAttributeSetFilter(
594  )->addStoreLabel(
595  $storeId
596  )->getData();
597  } else {
599  $attributesData = $this->_attributeData[$entityType->getEntityTypeCode()];
600  }
601 
602  if ($this->isCacheEnabled()) {
603  $this->_cache->save(
604  $this->serializer->serialize($attributesData),
605  $cacheKey,
606  [
608  \Magento\Eav\Model\Entity\Attribute::CACHE_TAG
609  ]
610  );
611  }
612  }
613 
614  foreach ($attributesData as $attributeData) {
615  $attributes[$attributeData['attribute_code']] = $this->_createAttribute($entityType, $attributeData);
616  }
617 
618  $this->attributesPerSet[$cacheKey] = $attributes;
619 
620  return $attributes;
621  }
_initAttributes($entityType)
Definition: Config.php:424
$attributesData

◆ getEntityType()

getEntityType (   $code)

Get entity type object by entity type code/identifier

Parameters
int | string | Type$code
Returns
Type
Exceptions

Definition at line 383 of file Config.php.

384  {
385  if ($code instanceof Type) {
386  return $code;
387  }
388  $this->_initEntityTypes();
389  \Magento\Framework\Profiler::start('EAV: ' . __METHOD__, ['group' => 'EAV', 'method' => __METHOD__]);
390 
391  if (is_numeric($code)) {
392  $entityCode = $this->_getEntityTypeReference($code);
393  if ($entityCode !== null) {
394  $code = $entityCode;
395  }
396  }
397 
398  $entityKey = $this->_getEntityKey($code);
399  $entityType = $this->_load($entityKey);
400  if ($entityType) {
401  \Magento\Framework\Profiler::stop('EAV: ' . __METHOD__);
402  return $entityType;
403  }
404 
405  $entityType = $this->_entityTypeFactory->create(
406  ['data' => isset($this->_entityTypeData[$code]) ? $this->_entityTypeData[$code] : []]
407  );
408  if (!$entityType->getId()) {
409  throw new \Magento\Framework\Exception\LocalizedException(__('Invalid entity_type specified: %1', $code));
410  }
411  $this->_addEntityTypeReference($entityType->getId(), $entityType->getEntityTypeCode());
412  $this->_save($entityType, $entityKey);
413 
414  \Magento\Framework\Profiler::stop('EAV: ' . __METHOD__);
415  return $entityType;
416  }
_addEntityTypeReference($id, $code)
Definition: Config.php:241
__()
Definition: __.php:13
$code
Definition: info.phtml:12

◆ importAttributesData()

importAttributesData (   $entityType,
array  $attributes 
)

Import attributes data from external source

Parameters
string | Type$entityType
array$attributes
Returns
$this

Definition at line 692 of file Config.php.

693  {
695  foreach ($attributes as $attributeData) {
696  if (!$this->_validateAttributeData($attributeData)) {
697  continue;
698  }
699  $this->_createAttribute($entityType, $attributeData);
700  }
701 
702  return $this;
703  }
_validateAttributeData($attributeData=null)
Definition: Config.php:670

◆ isCacheEnabled()

isCacheEnabled ( )

Check EAV cache availability

Returns
bool

Definition at line 319 of file Config.php.

320  {
321  if ($this->_isCacheEnabled === null) {
322  $this->_isCacheEnabled = $this->_cacheState->isEnabled(\Magento\Eav\Model\Cache\Type::TYPE_IDENTIFIER);
323  }
324  return $this->_isCacheEnabled;
325  }

Field Documentation

◆ $_attributeCodes

$_attributeCodes
protected

Definition at line 43 of file Config.php.

◆ $_attributeData

$_attributeData
protected

Definition at line 36 of file Config.php.

◆ $_cache

$_cache
protected

Definition at line 87 of file Config.php.

◆ $_cacheState

$_cacheState
protected

Definition at line 92 of file Config.php.

◆ $_entityTypeData

$_entityTypeData
protected

#- #-

Definition at line 29 of file Config.php.

◆ $_entityTypeFactory

$_entityTypeFactory
protected

Definition at line 97 of file Config.php.

◆ $_isCacheEnabled

$_isCacheEnabled = null
protected

Definition at line 82 of file Config.php.

◆ $_objects

$_objects
protected

Definition at line 52 of file Config.php.

◆ $_references

$_references
protected

Definition at line 75 of file Config.php.

◆ $_universalFactory

$_universalFactory
protected

Definition at line 107 of file Config.php.

◆ $entityTypeCollectionFactory

$entityTypeCollectionFactory
protected

Definition at line 102 of file Config.php.

◆ ATTRIBUTES_CACHE_ID

const ATTRIBUTES_CACHE_ID = 'EAV_ENTITY_ATTRIBUTES'

Definition at line 24 of file Config.php.

◆ ATTRIBUTES_CODES_CACHE_ID

const ATTRIBUTES_CODES_CACHE_ID = 'EAV_ENTITY_ATTRIBUTES_CODES'

Definition at line 25 of file Config.php.

◆ ENTITIES_CACHE_ID

const ENTITIES_CACHE_ID = 'EAV_ENTITY_TYPES'

#+ EAV cache ids

Definition at line 23 of file Config.php.


The documentation for this class was generated from the following file: