Config.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\Interception\Config;
  7. use Magento\Framework\Serialize\SerializerInterface;
  8. /**
  9. * Interception config.
  10. *
  11. * Responsible for providing list of plugins configured for instance
  12. */
  13. class Config implements \Magento\Framework\Interception\ConfigInterface
  14. {
  15. /**
  16. * Type configuration
  17. *
  18. * @var \Magento\Framework\Interception\ObjectManager\ConfigInterface
  19. */
  20. protected $_omConfig;
  21. /**
  22. * Class relations info
  23. *
  24. * @var \Magento\Framework\ObjectManager\RelationsInterface
  25. */
  26. protected $_relations;
  27. /**
  28. * List of interceptable classes
  29. *
  30. * @var \Magento\Framework\ObjectManager\DefinitionInterface
  31. */
  32. protected $_classDefinitions;
  33. /**
  34. * Cache
  35. * @deprecated 102.0.1
  36. * @var \Magento\Framework\Cache\FrontendInterface
  37. */
  38. protected $_cache;
  39. /**
  40. * Cache identifier
  41. *
  42. * @var string
  43. */
  44. protected $_cacheId;
  45. /**
  46. * Configuration reader
  47. *
  48. * @var \Magento\Framework\Config\ReaderInterface
  49. */
  50. protected $_reader;
  51. /**
  52. * Inherited list of intercepted types
  53. *
  54. * @var array
  55. */
  56. protected $_intercepted = [];
  57. /**
  58. * List of class types that can not be pluginized
  59. *
  60. * @var array
  61. */
  62. protected $_serviceClassTypes = ['Interceptor'];
  63. /**
  64. * @var \Magento\Framework\Config\ScopeListInterface
  65. */
  66. protected $_scopeList;
  67. /**
  68. * @var CacheManager
  69. */
  70. private $cacheManager;
  71. /**
  72. * Config constructor
  73. *
  74. * @param \Magento\Framework\Config\ReaderInterface $reader
  75. * @param \Magento\Framework\Config\ScopeListInterface $scopeList
  76. * @param \Magento\Framework\Cache\FrontendInterface $cache @deprecated
  77. * @param \Magento\Framework\ObjectManager\RelationsInterface $relations
  78. * @param \Magento\Framework\Interception\ObjectManager\ConfigInterface $omConfig
  79. * @param \Magento\Framework\ObjectManager\DefinitionInterface $classDefinitions
  80. * @param string $cacheId
  81. * @param SerializerInterface|null $serializer @deprecated
  82. * @param CacheManager $cacheManager
  83. *
  84. * @SuppressWarnings(PHPMD.UnusedFormalParameter)
  85. */
  86. public function __construct(
  87. \Magento\Framework\Config\ReaderInterface $reader,
  88. \Magento\Framework\Config\ScopeListInterface $scopeList,
  89. \Magento\Framework\Cache\FrontendInterface $cache,
  90. \Magento\Framework\ObjectManager\RelationsInterface $relations,
  91. \Magento\Framework\Interception\ObjectManager\ConfigInterface $omConfig,
  92. \Magento\Framework\ObjectManager\DefinitionInterface $classDefinitions,
  93. $cacheId = 'interception',
  94. SerializerInterface $serializer = null,
  95. CacheManager $cacheManager = null
  96. ) {
  97. $this->_omConfig = $omConfig;
  98. $this->_relations = $relations;
  99. $this->_classDefinitions = $classDefinitions;
  100. $this->_cache = $cache;
  101. $this->_cacheId = $cacheId;
  102. $this->_reader = $reader;
  103. $this->_scopeList = $scopeList;
  104. $this->cacheManager =
  105. $cacheManager ?? \Magento\Framework\App\ObjectManager::getInstance()->get(CacheManager::class);
  106. $intercepted = $this->cacheManager->load($cacheId);
  107. if ($intercepted !== null) {
  108. $this->_intercepted = $intercepted;
  109. } else {
  110. $this->initializeUncompiled($this->_classDefinitions->getClasses());
  111. }
  112. }
  113. /**
  114. * Initialize interception config
  115. *
  116. * @param array $classDefinitions
  117. * @return void
  118. */
  119. public function initialize($classDefinitions = [])
  120. {
  121. $this->generateIntercepted($classDefinitions);
  122. $this->cacheManager->saveCompiled($this->_cacheId, $this->_intercepted);
  123. }
  124. /**
  125. * Process interception inheritance
  126. *
  127. * @param string $type
  128. * @return bool
  129. */
  130. protected function _inheritInterception($type)
  131. {
  132. $type = ltrim($type, '\\');
  133. if (!isset($this->_intercepted[$type])) {
  134. $realType = $this->_omConfig->getOriginalInstanceType($type);
  135. if ($type !== $realType) {
  136. if ($this->_inheritInterception($realType)) {
  137. $this->_intercepted[$type] = true;
  138. return true;
  139. }
  140. } else {
  141. $parts = explode('\\', $type);
  142. if (!in_array(end($parts), $this->_serviceClassTypes) && $this->_relations->has($type)) {
  143. $relations = $this->_relations->getParents($type);
  144. foreach ($relations as $relation) {
  145. if ($relation && $this->_inheritInterception($relation)) {
  146. $this->_intercepted[$type] = true;
  147. return true;
  148. }
  149. }
  150. }
  151. }
  152. $this->_intercepted[$type] = false;
  153. }
  154. return $this->_intercepted[$type];
  155. }
  156. /**
  157. * @inheritdoc
  158. */
  159. public function hasPlugins($type)
  160. {
  161. if (isset($this->_intercepted[$type])) {
  162. return $this->_intercepted[$type];
  163. }
  164. return $this->_inheritInterception($type);
  165. }
  166. /**
  167. * Write interception config to cache
  168. *
  169. * @param array $classDefinitions
  170. */
  171. private function initializeUncompiled($classDefinitions = [])
  172. {
  173. $this->cacheManager->clean($this->_cacheId);
  174. $this->generateIntercepted($classDefinitions);
  175. $this->cacheManager->save($this->_cacheId, $this->_intercepted);
  176. }
  177. /**
  178. * Generate intercepted array to store in compiled metadata or frontend cache
  179. *
  180. * @param array $classDefinitions
  181. */
  182. private function generateIntercepted($classDefinitions)
  183. {
  184. $config = [];
  185. foreach ($this->_scopeList->getAllScopes() as $scope) {
  186. $config = array_replace_recursive($config, $this->_reader->read($scope));
  187. }
  188. unset($config['preferences']);
  189. foreach ($config as $typeName => $typeConfig) {
  190. if (!empty($typeConfig['plugins'])) {
  191. $this->_intercepted[ltrim($typeName, '\\')] = true;
  192. }
  193. }
  194. foreach ($config as $typeName => $typeConfig) {
  195. $this->hasPlugins($typeName);
  196. }
  197. foreach ($classDefinitions as $class) {
  198. $this->hasPlugins($class);
  199. }
  200. }
  201. }