SystemManager.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. <?php
  2. namespace Drupal\system;
  3. use Drupal\Core\Entity\EntityManagerInterface;
  4. use Drupal\Core\Menu\MenuActiveTrailInterface;
  5. use Drupal\Core\Menu\MenuLinkTreeInterface;
  6. use Drupal\Core\Menu\MenuLinkInterface;
  7. use Drupal\Core\Menu\MenuTreeParameters;
  8. use Drupal\Core\Extension\ModuleHandlerInterface;
  9. use Symfony\Component\HttpFoundation\RequestStack;
  10. /**
  11. * System Manager Service.
  12. */
  13. class SystemManager {
  14. /**
  15. * Module handler service.
  16. *
  17. * @var \Drupal\Core\Extension\ModuleHandlerInterface
  18. */
  19. protected $moduleHandler;
  20. /**
  21. * The request stack.
  22. *
  23. * @var \Symfony\Component\HttpFoundation\RequestStack
  24. */
  25. protected $requestStack;
  26. /**
  27. * The menu link tree manager.
  28. *
  29. * @var \Drupal\Core\Menu\MenuLinkTreeInterface
  30. */
  31. protected $menuTree;
  32. /**
  33. * The active menu trail service.
  34. *
  35. * @var \Drupal\Core\Menu\MenuActiveTrailInterface
  36. */
  37. protected $menuActiveTrail;
  38. /**
  39. * A static cache of menu items.
  40. *
  41. * @var array
  42. */
  43. protected $menuItems;
  44. /**
  45. * Requirement severity -- Requirement successfully met.
  46. */
  47. const REQUIREMENT_OK = 0;
  48. /**
  49. * Requirement severity -- Warning condition; proceed but flag warning.
  50. */
  51. const REQUIREMENT_WARNING = 1;
  52. /**
  53. * Requirement severity -- Error condition; abort installation.
  54. */
  55. const REQUIREMENT_ERROR = 2;
  56. /**
  57. * Constructs a SystemManager object.
  58. *
  59. * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
  60. * The module handler.
  61. * @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager
  62. * The entity manager.
  63. * @param \Symfony\Component\HttpFoundation\RequestStack $request_stack
  64. * The request stack.
  65. * @param \Drupal\Core\Menu\MenuLinkTreeInterface $menu_tree
  66. * The menu tree manager.
  67. * @param \Drupal\Core\Menu\MenuActiveTrailInterface $menu_active_trail
  68. * The active menu trail service.
  69. */
  70. public function __construct(ModuleHandlerInterface $module_handler, EntityManagerInterface $entity_manager, RequestStack $request_stack, MenuLinkTreeInterface $menu_tree, MenuActiveTrailInterface $menu_active_trail) {
  71. $this->moduleHandler = $module_handler;
  72. $this->requestStack = $request_stack;
  73. $this->menuTree = $menu_tree;
  74. $this->menuActiveTrail = $menu_active_trail;
  75. }
  76. /**
  77. * Checks for requirement severity.
  78. *
  79. * @return bool
  80. * Returns the status of the system.
  81. */
  82. public function checkRequirements() {
  83. $requirements = $this->listRequirements();
  84. return $this->getMaxSeverity($requirements) == static::REQUIREMENT_ERROR;
  85. }
  86. /**
  87. * Displays the site status report. Can also be used as a pure check.
  88. *
  89. * @return array
  90. * An array of system requirements.
  91. */
  92. public function listRequirements() {
  93. // Load .install files
  94. include_once DRUPAL_ROOT . '/core/includes/install.inc';
  95. drupal_load_updates();
  96. // Check run-time requirements and status information.
  97. $requirements = $this->moduleHandler->invokeAll('requirements', ['runtime']);
  98. uasort($requirements, function ($a, $b) {
  99. if (!isset($a['weight'])) {
  100. if (!isset($b['weight'])) {
  101. return strcasecmp($a['title'], $b['title']);
  102. }
  103. return -$b['weight'];
  104. }
  105. return isset($b['weight']) ? $a['weight'] - $b['weight'] : $a['weight'];
  106. });
  107. return $requirements;
  108. }
  109. /**
  110. * Extracts the highest severity from the requirements array.
  111. *
  112. * @param $requirements
  113. * An array of requirements, in the same format as is returned by
  114. * hook_requirements().
  115. *
  116. * @return
  117. * The highest severity in the array.
  118. */
  119. public function getMaxSeverity(&$requirements) {
  120. $severity = static::REQUIREMENT_OK;
  121. foreach ($requirements as $requirement) {
  122. if (isset($requirement['severity'])) {
  123. $severity = max($severity, $requirement['severity']);
  124. }
  125. }
  126. return $severity;
  127. }
  128. /**
  129. * Loads the contents of a menu block.
  130. *
  131. * This function is often a destination for these blocks.
  132. * For example, 'admin/structure/types' needs to have a destination to be
  133. * valid in the Drupal menu system, but too much information there might be
  134. * hidden, so we supply the contents of the block.
  135. *
  136. * @return array
  137. * A render array suitable for drupal_render.
  138. */
  139. public function getBlockContents() {
  140. // We hard-code the menu name here since otherwise a link in the tools menu
  141. // or elsewhere could give us a blank block.
  142. $link = $this->menuActiveTrail->getActiveLink('admin');
  143. if ($link && $content = $this->getAdminBlock($link)) {
  144. $output = [
  145. '#theme' => 'admin_block_content',
  146. '#content' => $content,
  147. ];
  148. }
  149. else {
  150. $output = [
  151. '#markup' => t('You do not have any administrative items.'),
  152. ];
  153. }
  154. return $output;
  155. }
  156. /**
  157. * Provide a single block on the administration overview page.
  158. *
  159. * @param \Drupal\Core\Menu\MenuLinkInterface $instance
  160. * The menu item to be displayed.
  161. *
  162. * @return array
  163. * An array of menu items, as expected by admin-block-content.html.twig.
  164. */
  165. public function getAdminBlock(MenuLinkInterface $instance) {
  166. $content = [];
  167. // Only find the children of this link.
  168. $link_id = $instance->getPluginId();
  169. $parameters = new MenuTreeParameters();
  170. $parameters->setRoot($link_id)->excludeRoot()->setTopLevelOnly()->onlyEnabledLinks();
  171. $tree = $this->menuTree->load(NULL, $parameters);
  172. $manipulators = [
  173. ['callable' => 'menu.default_tree_manipulators:checkAccess'],
  174. ['callable' => 'menu.default_tree_manipulators:generateIndexAndSort'],
  175. ];
  176. $tree = $this->menuTree->transform($tree, $manipulators);
  177. foreach ($tree as $key => $element) {
  178. // Only render accessible links.
  179. if (!$element->access->isAllowed()) {
  180. // @todo Bubble cacheability metadata of both accessible and
  181. // inaccessible links. Currently made impossible by the way admin
  182. // blocks are rendered.
  183. continue;
  184. }
  185. /** @var $link \Drupal\Core\Menu\MenuLinkInterface */
  186. $link = $element->link;
  187. $content[$key]['title'] = $link->getTitle();
  188. $content[$key]['options'] = $link->getOptions();
  189. $content[$key]['description'] = $link->getDescription();
  190. $content[$key]['url'] = $link->getUrlObject();
  191. }
  192. ksort($content);
  193. return $content;
  194. }
  195. }