ImporterPool.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Deploy\Model\DeploymentConfig;
  7. use Magento\Framework\App\DeploymentConfig\ValidatorInterface;
  8. use Magento\Framework\Exception\ConfigurationMismatchException;
  9. use Magento\Framework\ObjectManagerInterface;
  10. /**
  11. * Pool of all deployment configuration importers.
  12. *
  13. * All importers should implement Magento\Framework\App\DeploymentConfig\ImporterInterface interface.
  14. */
  15. class ImporterPool
  16. {
  17. /**
  18. * List of sections and their importers.
  19. *
  20. * Sections are defined with importers in di.xml. Every section may have data validator
  21. * E.g.
  22. * ```xml
  23. * <type name="Magento\Deploy\Model\DeploymentConfig\ImporterPool">
  24. * <arguments>
  25. * <argument name="importers" xsi:type="array">
  26. * <item name="scopes" xsi:type="array">
  27. * <item name="sort_order" xsi:type="number">20</item>
  28. * <item name="importer_class" xsi:type="string">Magento\Store\Model\StoreImporter</item>
  29. * <item name="validator_class" xsi:type="string">Magento\Store\Model\Config\StoreValidator</item>
  30. * </item>
  31. * <item name="themes" xsi:type="array">
  32. * <item name="sort_order" xsi:type="number">10</item>
  33. * <item name="importer_class" xsi:type="string">Magento\Theme\Model\ThemeImporter</item>
  34. * </item>
  35. * </argument>
  36. * </arguments>
  37. * </type>
  38. * ```
  39. *
  40. * The example of section in deployment configuration file:
  41. * ```php
  42. * [
  43. * 'scopes' => [
  44. * 'websites' => [
  45. * ...
  46. * ],
  47. * 'groups' => [
  48. * ...
  49. * ],
  50. * 'stores' => [
  51. * ...
  52. * ],
  53. * ...
  54. * ]
  55. * ]
  56. * ```
  57. *
  58. * @var array
  59. */
  60. private $importers = [];
  61. /**
  62. * Sorted list of importers class names.
  63. *
  64. * This list sorted by parameter "sort_order", that defined in di.xml
  65. *
  66. * ```php
  67. * [
  68. * 'themes' => 'Magento\Theme\Model\ThemeImporter',
  69. * 'scopes' => 'Magento\Store\Model\StoreImporter',
  70. * ...
  71. * ]
  72. * ```
  73. *
  74. * @var array
  75. */
  76. private $sortedImporters = [];
  77. /**
  78. * Magento object manager.
  79. *
  80. * @var ObjectManagerInterface
  81. */
  82. private $objectManager;
  83. /**
  84. * Factory that creates validator objects by class name.
  85. * Validators should be instances of Magento\Framework\App\DeploymentConfig\ValidatorInterface
  86. *
  87. * @var ValidatorFactory
  88. */
  89. private $validatorFactory;
  90. /**
  91. * @param ObjectManagerInterface $objectManager the Magento object manager
  92. * @param ValidatorFactory $validatorFactory the validator factory
  93. * @param array $importers the list of sections and their importers
  94. */
  95. public function __construct(
  96. ObjectManagerInterface $objectManager,
  97. ValidatorFactory $validatorFactory,
  98. array $importers = []
  99. ) {
  100. $this->objectManager = $objectManager;
  101. $this->validatorFactory = $validatorFactory;
  102. $this->importers = $importers;
  103. }
  104. /**
  105. * Retrieves sections names
  106. *
  107. * Retrieves names of sections for configuration files whose data is read from these files for import
  108. * to appropriate application sources.
  109. *
  110. * @return array the list of sections
  111. * E.g.
  112. * ```php
  113. * [
  114. * 'scopes',
  115. * 'themes',
  116. * ...
  117. * ]
  118. * ```
  119. */
  120. public function getSections()
  121. {
  122. return array_keys($this->importers);
  123. }
  124. /**
  125. * Retrieves list of all sections with their importer class names, sorted by sort_order.
  126. *
  127. * E.g.
  128. * ```php
  129. * [
  130. * 'scopes' => Magento\Store\Model\StoreImporter,
  131. * ...
  132. * ]
  133. * ```
  134. *
  135. * @return array the list of all sections with their importer class names
  136. * @throws ConfigurationMismatchException is thrown when parameter class is empty
  137. */
  138. public function getImporters()
  139. {
  140. if (!$this->sortedImporters) {
  141. $sortedImporters = [];
  142. foreach ($this->sort($this->importers) as $section => $importer) {
  143. if (empty($importer['importer_class'])) {
  144. throw new ConfigurationMismatchException(
  145. __('The parameter "importer_class" is missing. Set the "importer_class" and try again.')
  146. );
  147. }
  148. $sortedImporters[$section] = $importer['importer_class'];
  149. }
  150. $this->sortedImporters = $sortedImporters;
  151. }
  152. return $this->sortedImporters;
  153. }
  154. /**
  155. * Returns validator object for section if it was declared, otherwise returns null.
  156. *
  157. * @param string $section Section name
  158. * @return ValidatorInterface|null
  159. * @throws \InvalidArgumentException
  160. */
  161. public function getValidator($section)
  162. {
  163. if (isset($this->importers[$section]) && !empty($this->importers[$section]['validator_class'])) {
  164. return $this->validatorFactory->create($this->importers[$section]['validator_class']);
  165. }
  166. return null;
  167. }
  168. /**
  169. * Sorts importers according to sort order.
  170. *
  171. * @param array $data
  172. * @return array
  173. */
  174. private function sort(array $data)
  175. {
  176. uasort($data, function (array $a, array $b) {
  177. return $this->getSortOrder($a) <=> $this->getSortOrder($b);
  178. });
  179. return $data;
  180. }
  181. /**
  182. * Retrieves sort order from array.
  183. *
  184. * @param array $variable
  185. * @return int
  186. */
  187. private function getSortOrder(array $variable)
  188. {
  189. return !empty($variable['sort_order']) ? $variable['sort_order'] : 0;
  190. }
  191. }