![]() Server : Apache System : Linux server2.corals.io 4.18.0-348.2.1.el8_5.x86_64 #1 SMP Mon Nov 15 09:17:08 EST 2021 x86_64 User : corals ( 1002) PHP Version : 7.4.33 Disable Function : exec,passthru,shell_exec,system Directory : /home/corals/old/vendor/magento/module-eav/Model/Entity/ |
<?php /** * Copyright © Magento, Inc. All rights reserved. * See COPYING.txt for license details. */ namespace Magento\Eav\Model\Entity; use Magento\Eav\Model\Config; use Magento\Eav\Model\Entity\Attribute\AbstractAttribute; use Magento\Framework\DataObject; use Magento\Framework\Exception\LocalizedException; use Magento\Framework\ObjectManagerInterface; /** * Attribute loader */ class AttributeLoader implements AttributeLoaderInterface { /** * Default attributes * * @var array */ private $defaultAttributes = []; /** * @var ObjectManagerInterface */ protected $objectManager; /** * @var Config */ private $config; /** * Constructor * * @param Config $config * @param ObjectManagerInterface $objectManager */ public function __construct( Config $config, ObjectManagerInterface $objectManager ) { $this->config = $config; $this->objectManager = $objectManager; } /** * Retrieve configuration for all attributes * * @param AbstractEntity $resource * @param DataObject|null $object * @return AbstractEntity * @throws LocalizedException */ public function loadAllAttributes(AbstractEntity $resource, DataObject $object = null) { $attributes = $this->config->getEntityAttributes($resource->getEntityType(), $object); $attributeCodes = array_keys($attributes); /** * Check and init default attributes */ $defaultAttributesCodes = array_diff($resource->getDefaultAttributes(), $attributeCodes); $resource->unsetAttributes(); foreach ($defaultAttributesCodes as $attributeCode) { $resource->addAttribute($this->_getDefaultAttribute($resource, $attributeCode), $object); } foreach ($attributes as $attributeCode => $attribute) { $resource->addAttribute($attribute, $object); } return $resource; } /** * Return default static virtual attribute that doesn't exists in EAV attributes * * @param AbstractEntity $resource * @param string $attributeCode * @return Attribute */ protected function _getDefaultAttribute(AbstractEntity $resource, $attributeCode) { $entityTypeId = $resource->getEntityType()->getId(); if (!isset($this->defaultAttributes[$entityTypeId][$attributeCode])) { $attribute = $this->objectManager->create($resource->getEntityType()->getAttributeModel()) ->setAttributeCode($attributeCode) ->setBackendType(AbstractAttribute::TYPE_STATIC) ->setIsGlobal(1) ->setEntityType($resource->getEntityType()) ->setEntityTypeId($resource->getEntityType()->getId()); $this->defaultAttributes[$entityTypeId][$attributeCode] = $attribute; } return $this->defaultAttributes[$entityTypeId][$attributeCode]; } }