PluginList.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Developer\Model\Di;
  7. use Magento\Framework\Interception;
  8. use Magento\Framework\Interception\DefinitionInterface;
  9. /**
  10. * Provides plugin list configuration
  11. */
  12. class PluginList extends Interception\PluginList\PluginList
  13. {
  14. /**#@+
  15. * Constants for the plugin types
  16. */
  17. const PLUGIN_TYPE_BEFORE = 'before';
  18. const PLUGIN_TYPE_AROUND = 'around';
  19. const PLUGIN_TYPE_AFTER = 'after';
  20. /**#@-*/
  21. /**#@-*/
  22. private $pluginList = [
  23. self::PLUGIN_TYPE_BEFORE => [],
  24. self::PLUGIN_TYPE_AROUND => [],
  25. self::PLUGIN_TYPE_AFTER => []
  26. ];
  27. /**
  28. * Mapping of plugin type codes to plugin types
  29. * @var array
  30. */
  31. private $pluginTypeMapping = [
  32. DefinitionInterface::LISTENER_AROUND => self::PLUGIN_TYPE_AROUND,
  33. DefinitionInterface::LISTENER_BEFORE => self::PLUGIN_TYPE_BEFORE,
  34. DefinitionInterface::LISTENER_AFTER => self::PLUGIN_TYPE_AFTER
  35. ];
  36. /**
  37. * Returns plugins config
  38. *
  39. * @return array
  40. */
  41. public function getPluginsConfig()
  42. {
  43. $this->_loadScopedData();
  44. return $this->_inherited;
  45. }
  46. /**
  47. * Sets scope priority scheme
  48. *
  49. * @param array $areaCodes
  50. *
  51. * @return void
  52. */
  53. public function setScopePriorityScheme($areaCodes)
  54. {
  55. $this->_scopePriorityScheme = $areaCodes;
  56. }
  57. /**
  58. * Whether scope code is current scope code
  59. *
  60. * @param string $scopeCode
  61. *
  62. * @return bool
  63. * @SuppressWarnings(PHPMD.UnusedFormalParameter)
  64. */
  65. protected function isCurrentScope($scopeCode)
  66. {
  67. return false;
  68. }
  69. /**
  70. * Load the plugins information
  71. *
  72. * @param string $type
  73. * @return array
  74. */
  75. private function getPlugins($type)
  76. {
  77. $this->_loadScopedData();
  78. if (!isset($this->_inherited[$type]) && !array_key_exists($type, $this->_inherited)) {
  79. $this->_inheritPlugins($type);
  80. }
  81. return $this->_inherited[$type];
  82. }
  83. /**
  84. * Return the list of plugins for the class
  85. *
  86. * @param string $className
  87. * @return array
  88. * @throws \InvalidArgumentException
  89. */
  90. public function getPluginsListByClass($className)
  91. {
  92. $this->getPlugins($className);
  93. if (!isset($this->_inherited[$className])) {
  94. return $this->pluginList;
  95. }
  96. foreach ($this->_inherited[$className] as $plugin) {
  97. foreach ($this->_definitions->getMethodList($plugin['instance']) as $pluginMethod => $methodTypes) {
  98. $this->addPluginToList(
  99. $plugin['instance'],
  100. $pluginMethod,
  101. $methodTypes,
  102. DefinitionInterface::LISTENER_AROUND
  103. );
  104. $this->addPluginToList(
  105. $plugin['instance'],
  106. $pluginMethod,
  107. $methodTypes,
  108. DefinitionInterface::LISTENER_BEFORE
  109. );
  110. $this->addPluginToList(
  111. $plugin['instance'],
  112. $pluginMethod,
  113. $methodTypes,
  114. DefinitionInterface::LISTENER_AFTER
  115. );
  116. }
  117. }
  118. return $this->pluginList;
  119. }
  120. /**
  121. * Add plugin to the appropriate type bucket
  122. *
  123. * @param string $pluginInstance
  124. * @param string $pluginMethod
  125. * @param int $methodTypes
  126. * @param int $typeCode
  127. * @return void
  128. */
  129. private function addPluginToList($pluginInstance, $pluginMethod, $methodTypes, $typeCode)
  130. {
  131. if ($methodTypes & $typeCode) {
  132. if (!array_key_exists($pluginInstance, $this->pluginList[$this->pluginTypeMapping[$typeCode]])) {
  133. $this->pluginList[$this->pluginTypeMapping[$typeCode]][$pluginInstance] = [];
  134. }
  135. if (!in_array($pluginMethod, $this->pluginList[$this->pluginTypeMapping[$typeCode]][$pluginInstance])) {
  136. $this->pluginList[$this->pluginTypeMapping[$typeCode]][$pluginInstance][] = $pluginMethod;
  137. }
  138. }
  139. }
  140. }