|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © 2013-2017 Magento, Inc. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | +namespace Magento\Customer\Model\Metadata; |
| 7 | + |
| 8 | +use Magento\Framework\App\CacheInterface; |
| 9 | +use Magento\Framework\App\Cache\StateInterface; |
| 10 | +use Magento\Customer\Api\Data\AttributeMetadataInterface; |
| 11 | +use Magento\Customer\Api\Data\AttributeMetadataInterfaceFactory; |
| 12 | +use Magento\Customer\Api\Data\OptionInterface; |
| 13 | +use Magento\Customer\Api\Data\OptionInterfaceFactory; |
| 14 | +use Magento\Customer\Api\Data\ValidationRuleInterfaceFactory; |
| 15 | +use Magento\Framework\Serialize\SerializerInterface; |
| 16 | +use Magento\Eav\Model\Cache\Type; |
| 17 | +use Magento\Eav\Model\Entity\Attribute; |
| 18 | +use Magento\Config\App\Config\Type\System; |
| 19 | + |
| 20 | +/** |
| 21 | + * Cache for attribute metadata |
| 22 | + */ |
| 23 | +class AttributeMetadataCache |
| 24 | +{ |
| 25 | + /** |
| 26 | + * Cache prefix |
| 27 | + */ |
| 28 | + const ATTRIBUTE_METADATA_CACHE_PREFIX = 'ATTRIBUTE_METADATA_INSTANCES_CACHE'; |
| 29 | + |
| 30 | + /** |
| 31 | + * @var CacheInterface |
| 32 | + */ |
| 33 | + private $cache; |
| 34 | + |
| 35 | + /** |
| 36 | + * @var StateInterface |
| 37 | + */ |
| 38 | + private $state; |
| 39 | + |
| 40 | + /** |
| 41 | + * @var AttributeMetadataInterface[] |
| 42 | + */ |
| 43 | + private $attributes; |
| 44 | + |
| 45 | + /** |
| 46 | + * @var bool |
| 47 | + */ |
| 48 | + private $isAttributeCacheEnabled; |
| 49 | + |
| 50 | + /** |
| 51 | + * @var AttributeMetadataInterfaceFactory |
| 52 | + */ |
| 53 | + private $attributeMetadataFactory; |
| 54 | + |
| 55 | + /** |
| 56 | + * @var OptionInterfaceFactory |
| 57 | + */ |
| 58 | + private $optionFactory; |
| 59 | + |
| 60 | + /** |
| 61 | + * @var ValidationRuleInterfaceFactory |
| 62 | + */ |
| 63 | + private $validationRuleFactory; |
| 64 | + |
| 65 | + /** |
| 66 | + * @var SerializerInterface |
| 67 | + */ |
| 68 | + private $serializer; |
| 69 | + |
| 70 | + /** |
| 71 | + * Constructor |
| 72 | + * |
| 73 | + * @param CacheInterface $cache |
| 74 | + * @param StateInterface $state |
| 75 | + * @param AttributeMetadataInterfaceFactory $attributeMetadataFactory |
| 76 | + * @param OptionInterfaceFactory $optionFactory |
| 77 | + * @param ValidationRuleInterfaceFactory $validationRuleFactory |
| 78 | + * @param SerializerInterface $serializer |
| 79 | + */ |
| 80 | + public function __construct( |
| 81 | + CacheInterface $cache, |
| 82 | + StateInterface $state, |
| 83 | + AttributeMetadataInterfaceFactory $attributeMetadataFactory, |
| 84 | + OptionInterfaceFactory $optionFactory, |
| 85 | + ValidationRuleInterfaceFactory $validationRuleFactory, |
| 86 | + SerializerInterface $serializer |
| 87 | + ) { |
| 88 | + $this->cache = $cache; |
| 89 | + $this->state = $state; |
| 90 | + $this->attributeMetadataFactory = $attributeMetadataFactory; |
| 91 | + $this->optionFactory = $optionFactory; |
| 92 | + $this->validationRuleFactory = $validationRuleFactory; |
| 93 | + $this->serializer = $serializer; |
| 94 | + } |
| 95 | + |
| 96 | + /** |
| 97 | + * Load attributes metadata from cache |
| 98 | + * |
| 99 | + * @param string $entityType |
| 100 | + * @param string $suffix |
| 101 | + * @return AttributeMetadataInterface[]|bool |
| 102 | + */ |
| 103 | + public function load($entityType, $suffix = '') |
| 104 | + { |
| 105 | + if (isset($this->attributes[$entityType . $suffix])) { |
| 106 | + return $this->attributes[$entityType . $suffix]; |
| 107 | + } |
| 108 | + if ($this->isEnabled()) { |
| 109 | + $cacheKey = self::ATTRIBUTE_METADATA_CACHE_PREFIX . $entityType . $suffix; |
| 110 | + $serializedData = $this->cache->load($cacheKey); |
| 111 | + if ($serializedData) { |
| 112 | + $attributesData = $this->serializer->unserialize($serializedData); |
| 113 | + $attributes = []; |
| 114 | + foreach ($attributesData as $key => $attributeData) { |
| 115 | + $attributes[$key] = $this->createMetadataAttribute($attributeData); |
| 116 | + } |
| 117 | + $this->attributes[$entityType . $suffix] = $attributes; |
| 118 | + return $attributes; |
| 119 | + } |
| 120 | + } |
| 121 | + return false; |
| 122 | + } |
| 123 | + |
| 124 | + /** |
| 125 | + * Save attributes metadata to cache |
| 126 | + * |
| 127 | + * @param string $entityType |
| 128 | + * @param AttributeMetadataInterface[] $attributes |
| 129 | + * @param string $suffix |
| 130 | + * @return void |
| 131 | + */ |
| 132 | + public function save($entityType, $attributes, $suffix = '') |
| 133 | + { |
| 134 | + $this->attributes[$entityType . $suffix] = $attributes; |
| 135 | + if ($this->isEnabled()) { |
| 136 | + $cacheKey = self::ATTRIBUTE_METADATA_CACHE_PREFIX . $entityType . $suffix; |
| 137 | + $attributesData = []; |
| 138 | + foreach ($attributes as $key => $attribute) { |
| 139 | + $attributesData[$key] = $attribute->__toArray(); |
| 140 | + } |
| 141 | + $serializedData = $this->serializer->serialize($attributesData); |
| 142 | + $this->cache->save( |
| 143 | + $serializedData, |
| 144 | + $cacheKey, |
| 145 | + [ |
| 146 | + Type::CACHE_TAG, |
| 147 | + Attribute::CACHE_TAG, |
| 148 | + System::CACHE_TAG |
| 149 | + ] |
| 150 | + ); |
| 151 | + } |
| 152 | + } |
| 153 | + |
| 154 | + /** |
| 155 | + * Clean attributes metadata cache |
| 156 | + * |
| 157 | + * @return void |
| 158 | + */ |
| 159 | + public function clean() |
| 160 | + { |
| 161 | + unset($this->attributes); |
| 162 | + if ($this->isEnabled()) { |
| 163 | + $this->cache->clean( |
| 164 | + [ |
| 165 | + Type::CACHE_TAG, |
| 166 | + Attribute::CACHE_TAG, |
| 167 | + ] |
| 168 | + ); |
| 169 | + } |
| 170 | + } |
| 171 | + |
| 172 | + /** |
| 173 | + * Check if cache enabled |
| 174 | + * |
| 175 | + * @return bool |
| 176 | + */ |
| 177 | + private function isEnabled() |
| 178 | + { |
| 179 | + if (null === $this->isAttributeCacheEnabled) { |
| 180 | + $this->isAttributeCacheEnabled = $this->state->isEnabled(Type::TYPE_IDENTIFIER); |
| 181 | + } |
| 182 | + return $this->isAttributeCacheEnabled; |
| 183 | + } |
| 184 | + |
| 185 | + /** |
| 186 | + * Create and populate with data AttributeMetadataInterface |
| 187 | + * |
| 188 | + * @param array $data |
| 189 | + * @return AttributeMetadataInterface |
| 190 | + */ |
| 191 | + private function createMetadataAttribute($data) |
| 192 | + { |
| 193 | + if (isset($data[AttributeMetadataInterface::OPTIONS])) { |
| 194 | + $data[AttributeMetadataInterface::OPTIONS] = $this->createOptions( |
| 195 | + $data[AttributeMetadataInterface::OPTIONS] |
| 196 | + ); |
| 197 | + } |
| 198 | + if (isset($data[AttributeMetadataInterface::VALIDATION_RULES])) { |
| 199 | + $validationRules = []; |
| 200 | + foreach ($data[AttributeMetadataInterface::VALIDATION_RULES] as $validationRuleData) { |
| 201 | + $validationRules[] = $this->validationRuleFactory->create(['data' => $validationRuleData]); |
| 202 | + } |
| 203 | + $data[AttributeMetadataInterface::VALIDATION_RULES] = $validationRules; |
| 204 | + } |
| 205 | + return $this->attributeMetadataFactory->create(['data' => $data]); |
| 206 | + } |
| 207 | + |
| 208 | + /** |
| 209 | + * Create and populate with data OptionInterface |
| 210 | + * |
| 211 | + * @param array $data |
| 212 | + * @return OptionInterface[] |
| 213 | + */ |
| 214 | + private function createOptions($data) |
| 215 | + { |
| 216 | + foreach ($data as $key => $optionData) { |
| 217 | + if (isset($optionData[OptionInterface::OPTIONS])) { |
| 218 | + $optionData[OptionInterface::OPTIONS] = $this->createOptions($optionData[OptionInterface::OPTIONS]); |
| 219 | + } |
| 220 | + $data[$key] = $this->optionFactory->create(['data' => $optionData]); |
| 221 | + } |
| 222 | + return $data; |
| 223 | + } |
| 224 | +} |
0 commit comments