PageRenderTime 26ms CodeModel.GetById 7ms RepoModel.GetById 1ms app.codeStats 0ms

/core/lib/Drupal/Core/Config/Entity/ConfigDependencyManager.php

https://gitlab.com/reasonat/test8
PHP | 351 lines | 97 code | 18 blank | 236 comment | 16 complexity | bda3ba30228e61293a9a219939402269 MD5 | raw file
  1. <?php
  2. namespace Drupal\Core\Config\Entity;
  3. use Drupal\Component\Graph\Graph;
  4. use Drupal\Component\Utility\SortArray;
  5. /**
  6. * Provides a class to discover configuration entity dependencies.
  7. *
  8. * Configuration entities can depend on modules, themes and other configuration
  9. * entities. The dependency system is used during configuration installation,
  10. * uninstallation, and synchronization to ensure that configuration entities are
  11. * handled in the correct order. For example, node types are created before
  12. * their fields, and both are created before the view display configuration.
  13. *
  14. * The configuration dependency value is structured like this:
  15. * @code
  16. * array(
  17. * 'config' => array(
  18. * // An array of configuration entity object names. Recalculated on save.
  19. * ),
  20. * 'content' => array(
  21. * // An array of content entity configuration dependency names. The default
  22. * // format is "ENTITY_TYPE_ID:BUNDLE:UUID". Recalculated on save.
  23. * ),
  24. * 'module' => array(
  25. * // An array of module names. Recalculated on save.
  26. * ),
  27. * 'theme' => array(
  28. * // An array of theme names. Recalculated on save.
  29. * ),
  30. * 'enforced' => array(
  31. * // An array of configuration dependencies that the config entity is
  32. * // ensured to have regardless of the details of the configuration. These
  33. * // dependencies are not recalculated on save.
  34. * 'config' => array(),
  35. * 'content' => array(),
  36. * 'module' => array(),
  37. * 'theme' => array(),
  38. * ),
  39. * );
  40. * @endcode
  41. *
  42. * Configuration entity dependencies are recalculated on save based on the
  43. * current values of the configuration. For example, a filter format will depend
  44. * on the modules that provide the filter plugins it configures. The filter
  45. * format can be reconfigured to use a different filter plugin provided by
  46. * another module. If this occurs, the dependencies will be recalculated on save
  47. * and the old module will be removed from the list of dependencies and replaced
  48. * with the new one.
  49. *
  50. * Configuration entity classes usually extend
  51. * \Drupal\Core\Config\Entity\ConfigEntityBase. The base class provides a
  52. * generic implementation of the calculateDependencies() method that can
  53. * discover dependencies due to plugins, and third party settings. If the
  54. * configuration entity has dependencies that cannot be discovered by the base
  55. * class's implementation, then it needs to implement
  56. * \Drupal\Core\Config\Entity\ConfigEntityInterface::calculateDependencies() to
  57. * calculate the dependencies. In this method, use
  58. * \Drupal\Core\Config\Entity\ConfigEntityBase::addDependency() to add
  59. * dependencies. Implementations should call the base class implementation to
  60. * inherit the generic functionality.
  61. *
  62. * Classes for configurable plugins are a special case. They can either declare
  63. * their configuration dependencies using the calculateDependencies() method
  64. * described in the paragraph above, or if they have only static dependencies,
  65. * these can be declared using the 'config_dependencies' annotation key.
  66. *
  67. * If an extension author wants a configuration entity to depend on something
  68. * that is not calculable then they can add these dependencies to the enforced
  69. * dependencies key. For example, the Forum module provides the forum node type
  70. * and in order for it to be deleted when the forum module is uninstalled it has
  71. * an enforced dependency on the module. The dependency on the Forum module can
  72. * not be calculated since there is nothing inherent in the state of the node
  73. * type configuration entity that depends on functionality provided by the Forum
  74. * module.
  75. *
  76. * Once declared properly, dependencies are saved to the configuration entity's
  77. * configuration object so that they can be checked without the module that
  78. * provides the configuration entity class being installed. This is important
  79. * for configuration synchronization, which needs to be able to validate
  80. * configuration in the sync directory before the synchronization has occurred.
  81. * Also, if you have a configuration entity object and you want to get the
  82. * current dependencies (without recalculation), you can use
  83. * \Drupal\Core\Config\Entity\ConfigEntityInterface::getDependencies().
  84. *
  85. * When uninstalling a module or a theme, configuration entities that are
  86. * dependent will also be removed. This default behavior can lead to undesirable
  87. * side effects, such as a node view mode being entirely removed when the module
  88. * defining a field or formatter it uses is uninstalled. To prevent this,
  89. * configuration entity classes can implement
  90. * \Drupal\Core\Config\Entity\ConfigEntityInterface::onDependencyRemoval(),
  91. * which allows the entity class to remove dependencies instead of being deleted
  92. * themselves. Implementations should save the entity if dependencies have been
  93. * successfully removed, in order to register the newly cleaned-out dependency
  94. * list. So, for example, the node view mode configuration entity class
  95. * should implement this method to remove references to formatters if the plugin
  96. * that supplies them depends on a module that is being uninstalled.
  97. *
  98. * If a configuration entity is provided as default configuration by an
  99. * extension (module, theme, or profile), the extension has to depend on any
  100. * modules or themes that the configuration depends on. For example, if a view
  101. * configuration entity is provided by an installation profile and the view will
  102. * not work without a certain module, the profile must declare a dependency on
  103. * this module in its info.yml file. If you do not want your extension to always
  104. * depend on a particular module that one of its default configuration entities
  105. * depends on, you can use a sub-module: move the configuration entity to the
  106. * sub-module instead of including it in the main extension, and declare the
  107. * module dependency in the sub-module only.
  108. *
  109. * @see \Drupal\Core\Config\Entity\ConfigEntityInterface::calculateDependencies()
  110. * @see \Drupal\Core\Config\Entity\ConfigEntityInterface::getDependencies()
  111. * @see \Drupal\Core\Config\Entity\ConfigEntityInterface::onDependencyRemoval()
  112. * @see \Drupal\Core\Config\Entity\ConfigEntityBase::addDependency()
  113. * @see \Drupal\Core\Config\ConfigInstallerInterface::installDefaultConfig()
  114. * @see \Drupal\Core\Config\ConfigManagerInterface::uninstall()
  115. * @see \Drupal\Core\Config\Entity\ConfigEntityDependency
  116. * @see \Drupal\Core\Entity\EntityInterface::getConfigDependencyName()
  117. * @see \Drupal\Core\Plugin\PluginDependencyTrait
  118. */
  119. class ConfigDependencyManager {
  120. /**
  121. * The config entity data.
  122. *
  123. * @var \Drupal\Core\Config\Entity\ConfigEntityDependency[]
  124. */
  125. protected $data = array();
  126. /**
  127. * The directed acyclic graph.
  128. *
  129. * @var array
  130. */
  131. protected $graph;
  132. /**
  133. * Gets dependencies.
  134. *
  135. * @param string $type
  136. * The type of dependency being checked. Either 'module', 'theme', 'config'
  137. * or 'content'.
  138. * @param string $name
  139. * The specific name to check. If $type equals 'module' or 'theme' then it
  140. * should be a module name or theme name. In the case of entity it should be
  141. * the full configuration object name.
  142. *
  143. * @return \Drupal\Core\Config\Entity\ConfigEntityDependency[]
  144. * An array of config entity dependency objects that are dependent.
  145. */
  146. public function getDependentEntities($type, $name) {
  147. $dependent_entities = array();
  148. $entities_to_check = array();
  149. if ($type == 'config') {
  150. $entities_to_check[] = $name;
  151. }
  152. else {
  153. if ($type == 'module' || $type == 'theme' || $type == 'content') {
  154. $dependent_entities = array_filter($this->data, function (ConfigEntityDependency $entity) use ($type, $name) {
  155. return $entity->hasDependency($type, $name);
  156. });
  157. }
  158. // If checking content, module, or theme dependencies, discover which
  159. // entities are dependent on the entities that have a direct dependency.
  160. foreach ($dependent_entities as $entity) {
  161. $entities_to_check[] = $entity->getConfigDependencyName();
  162. }
  163. }
  164. $dependencies = array_merge($this->createGraphConfigEntityDependencies($entities_to_check), $dependent_entities);
  165. // Sort dependencies in the reverse order of the graph. So the least
  166. // dependent is at the top. For example, this ensures that fields are
  167. // always after field storages. This is because field storages need to be
  168. // created before a field.
  169. $graph = $this->getGraph();
  170. uasort($graph, array($this, 'sortGraph'));
  171. return array_replace(array_intersect_key($graph, $dependencies), $dependencies);
  172. }
  173. /**
  174. * Sorts the dependencies in order of most dependent last.
  175. *
  176. * @return array
  177. * The list of entities in order of most dependent last, otherwise
  178. * alphabetical.
  179. */
  180. public function sortAll() {
  181. $graph = $this->getGraph();
  182. // Sort by weight and alphabetically. The most dependent entities
  183. // are last and entities with the same weight are alphabetically ordered.
  184. uasort($graph, array($this, 'sortGraphByWeight'));
  185. // Use array_intersect_key() to exclude modules and themes from the list.
  186. return array_keys(array_intersect_key($graph, $this->data));
  187. }
  188. /**
  189. * Sorts the dependency graph by weight and alphabetically.
  190. *
  191. * @param array $a
  192. * First item for comparison. The compared items should be associative
  193. * arrays that include a 'weight' and a 'name' key.
  194. * @param array $b
  195. * Second item for comparison.
  196. *
  197. * @return int
  198. * The comparison result for uasort().
  199. */
  200. protected static function sortGraphByWeight(array $a, array $b) {
  201. $weight_cmp = SortArray::sortByKeyInt($a, $b, 'weight');
  202. if ($weight_cmp === 0) {
  203. return SortArray::sortByKeyString($a, $b, 'name');
  204. }
  205. return $weight_cmp;
  206. }
  207. /**
  208. * Sorts the dependency graph by reverse weight and alphabetically.
  209. *
  210. * @param array $a
  211. * First item for comparison. The compared items should be associative
  212. * arrays that include a 'weight' and a 'name' key.
  213. * @param array $b
  214. * Second item for comparison.
  215. *
  216. * @return int
  217. * The comparison result for uasort().
  218. */
  219. public static function sortGraph(array $a, array $b) {
  220. $weight_cmp = SortArray::sortByKeyInt($a, $b, 'weight') * -1;
  221. if ($weight_cmp === 0) {
  222. return SortArray::sortByKeyString($a, $b, 'name');
  223. }
  224. return $weight_cmp;
  225. }
  226. /**
  227. * Creates a graph of config entity dependencies.
  228. *
  229. * @param array $entities_to_check
  230. * The configuration entity full configuration names to determine the
  231. * dependencies for.
  232. *
  233. * @return \Drupal\Core\Config\Entity\ConfigEntityDependency[]
  234. * A graph of config entity dependency objects that are dependent on the
  235. * supplied entities to check.
  236. */
  237. protected function createGraphConfigEntityDependencies($entities_to_check) {
  238. $dependent_entities = array();
  239. $graph = $this->getGraph();
  240. foreach ($entities_to_check as $entity) {
  241. if (isset($graph[$entity]) && !empty($graph[$entity]['paths'])) {
  242. foreach ($graph[$entity]['paths'] as $dependency => $value) {
  243. if (isset($this->data[$dependency])) {
  244. $dependent_entities[$dependency] = $this->data[$dependency];
  245. }
  246. }
  247. }
  248. }
  249. return $dependent_entities;
  250. }
  251. /**
  252. * Gets the dependency graph of all the config entities.
  253. *
  254. * @return array
  255. * The dependency graph of all the config entities.
  256. */
  257. protected function getGraph() {
  258. if (!isset($this->graph)) {
  259. $graph = array();
  260. foreach ($this->data as $entity) {
  261. $graph_key = $entity->getConfigDependencyName();
  262. if (!isset($graph[$graph_key])) {
  263. $graph[$graph_key] = [
  264. 'edges' => [],
  265. 'name' => $graph_key,
  266. ];
  267. }
  268. // Include all dependencies in the graph so that topographical sorting
  269. // works.
  270. foreach (array_merge($entity->getDependencies('config'), $entity->getDependencies('module'), $entity->getDependencies('theme')) as $dependency) {
  271. $graph[$dependency]['edges'][$graph_key] = TRUE;
  272. $graph[$dependency]['name'] = $dependency;
  273. }
  274. }
  275. // Ensure that order of the graph is consistent.
  276. krsort($graph);
  277. $graph_object = new Graph($graph);
  278. $this->graph = $graph_object->searchAndSort();
  279. }
  280. return $this->graph;
  281. }
  282. /**
  283. * Sets data to calculate dependencies for.
  284. *
  285. * The data is converted into lightweight ConfigEntityDependency objects.
  286. *
  287. * @param array $data
  288. * Configuration data keyed by configuration object name. Typically the
  289. * output of \Drupal\Core\Config\StorageInterface::loadMultiple().
  290. *
  291. * @return $this
  292. */
  293. public function setData(array $data) {
  294. array_walk($data, function (&$config, $name) {
  295. $config = new ConfigEntityDependency($name, $config);
  296. });
  297. $this->data = $data;
  298. $this->graph = NULL;
  299. return $this;
  300. }
  301. /**
  302. * Updates one of the lightweight ConfigEntityDependency objects.
  303. *
  304. * @param $name
  305. * The configuration dependency name.
  306. * @param array $dependencies
  307. * The configuration dependencies. The array is structured like this:
  308. * @code
  309. * array(
  310. * 'config' => array(
  311. * // An array of configuration entity object names.
  312. * ),
  313. * 'content' => array(
  314. * // An array of content entity configuration dependency names. The default
  315. * // format is "ENTITY_TYPE_ID:BUNDLE:UUID".
  316. * ),
  317. * 'module' => array(
  318. * // An array of module names.
  319. * ),
  320. * 'theme' => array(
  321. * // An array of theme names.
  322. * ),
  323. * );
  324. * @endcode
  325. *
  326. * @return $this
  327. */
  328. public function updateData($name, array $dependencies) {
  329. $this->graph = NULL;
  330. $this->data[$name] = new ConfigEntityDependency($name, ['dependencies' => $dependencies]);
  331. return $this;
  332. }
  333. }