ModuleList.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\Module;
  7. use Magento\Framework\App\DeploymentConfig;
  8. use Magento\Framework\Config\ConfigOptionsListConstants;
  9. /**
  10. * A list of modules in the Magento application
  11. *
  12. * Encapsulates information about whether modules are enabled or not.
  13. * Represents only enabled modules through its interface
  14. */
  15. class ModuleList implements ModuleListInterface
  16. {
  17. /**
  18. * Deployment configuration
  19. *
  20. * @var DeploymentConfig
  21. */
  22. private $config;
  23. /**
  24. * Loader of module information from source code
  25. *
  26. * @var ModuleList\Loader
  27. */
  28. private $loader;
  29. /**
  30. * An associative array of modules
  31. *
  32. * The possible values are 1 (enabled) or 0 (disabled)
  33. *
  34. * @var int[]
  35. */
  36. private $configData;
  37. /**
  38. * Enumeration of the enabled module names
  39. *
  40. * @var string[]
  41. */
  42. private $enabled;
  43. /**
  44. * Constructor
  45. *
  46. * @param DeploymentConfig $config
  47. * @param ModuleList\Loader $loader
  48. */
  49. public function __construct(DeploymentConfig $config, ModuleList\Loader $loader)
  50. {
  51. $this->config = $config;
  52. $this->loader = $loader;
  53. }
  54. /**
  55. * {@inheritdoc}
  56. *
  57. * Note that this triggers loading definitions of all existing modules in the system.
  58. * Use this method only when you actually need modules' declared meta-information.
  59. *
  60. * @see getNames()
  61. */
  62. public function getAll()
  63. {
  64. if (null === $this->enabled) {
  65. $all = $this->loader->load();
  66. if (empty($all)) {
  67. return []; // don't record erroneous value into memory
  68. }
  69. $this->enabled = [];
  70. foreach ($all as $key => $value) {
  71. if ($this->has($key)) {
  72. $this->enabled[$key] = $value;
  73. }
  74. }
  75. }
  76. return $this->enabled;
  77. }
  78. /**
  79. * {@inheritdoc}
  80. * @see has()
  81. */
  82. public function getOne($name)
  83. {
  84. $enabled = $this->getAll();
  85. return $enabled[$name] ?? null;
  86. }
  87. /**
  88. * {@inheritdoc}
  89. */
  90. public function getNames()
  91. {
  92. $this->loadConfigData();
  93. if (!$this->configData) {
  94. return [];
  95. }
  96. $result = array_keys(array_filter($this->configData));
  97. return $result;
  98. }
  99. /**
  100. * {@inheritdoc}
  101. */
  102. public function has($name)
  103. {
  104. $this->loadConfigData();
  105. if (!$this->configData) {
  106. return false;
  107. }
  108. return !empty($this->configData[$name]);
  109. }
  110. /**
  111. * Checks if module list information is available.
  112. *
  113. * @return bool
  114. */
  115. public function isModuleInfoAvailable()
  116. {
  117. $this->loadConfigData();
  118. if ($this->configData) {
  119. return true;
  120. }
  121. return false;
  122. }
  123. /**
  124. * Loads configuration data only
  125. *
  126. * @return void
  127. */
  128. private function loadConfigData()
  129. {
  130. $this->config->resetData();
  131. if (null === $this->configData && null !== $this->config->get(ConfigOptionsListConstants::KEY_MODULES)) {
  132. $this->configData = $this->config->get(ConfigOptionsListConstants::KEY_MODULES);
  133. }
  134. }
  135. }