SystemManager.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. <?php
  2. namespace Drupal\system;
  3. use Drupal\Core\Menu\MenuActiveTrailInterface;
  4. use Drupal\Core\Menu\MenuLinkTreeInterface;
  5. use Drupal\Core\Menu\MenuLinkInterface;
  6. use Drupal\Core\Menu\MenuTreeParameters;
  7. use Drupal\Core\Extension\ModuleHandlerInterface;
  8. use Symfony\Component\HttpFoundation\RequestStack;
  9. /**
  10. * System Manager Service.
  11. */
  12. class SystemManager {
  13. /**
  14. * Module handler service.
  15. *
  16. * @var \Drupal\Core\Extension\ModuleHandlerInterface
  17. */
  18. protected $moduleHandler;
  19. /**
  20. * The request stack.
  21. *
  22. * @var \Symfony\Component\HttpFoundation\RequestStack
  23. */
  24. protected $requestStack;
  25. /**
  26. * The menu link tree manager.
  27. *
  28. * @var \Drupal\Core\Menu\MenuLinkTreeInterface
  29. */
  30. protected $menuTree;
  31. /**
  32. * The active menu trail service.
  33. *
  34. * @var \Drupal\Core\Menu\MenuActiveTrailInterface
  35. */
  36. protected $menuActiveTrail;
  37. /**
  38. * A static cache of menu items.
  39. *
  40. * @var array
  41. */
  42. protected $menuItems;
  43. /**
  44. * Requirement severity -- Requirement successfully met.
  45. */
  46. const REQUIREMENT_OK = 0;
  47. /**
  48. * Requirement severity -- Warning condition; proceed but flag warning.
  49. */
  50. const REQUIREMENT_WARNING = 1;
  51. /**
  52. * Requirement severity -- Error condition; abort installation.
  53. */
  54. const REQUIREMENT_ERROR = 2;
  55. /**
  56. * Constructs a SystemManager object.
  57. *
  58. * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
  59. * The module handler.
  60. * @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager
  61. * The entity manager.
  62. * @param \Symfony\Component\HttpFoundation\RequestStack $request_stack
  63. * The request stack.
  64. * @param \Drupal\Core\Menu\MenuLinkTreeInterface $menu_tree
  65. * The menu tree manager.
  66. * @param \Drupal\Core\Menu\MenuActiveTrailInterface $menu_active_trail
  67. * The active menu trail service.
  68. */
  69. public function __construct(ModuleHandlerInterface $module_handler, $entity_manager, RequestStack $request_stack, MenuLinkTreeInterface $menu_tree, MenuActiveTrailInterface $menu_active_trail) {
  70. $this->moduleHandler = $module_handler;
  71. $this->requestStack = $request_stack;
  72. $this->menuTree = $menu_tree;
  73. $this->menuActiveTrail = $menu_active_trail;
  74. }
  75. /**
  76. * Checks for requirement severity.
  77. *
  78. * @return bool
  79. * Returns the status of the system.
  80. */
  81. public function checkRequirements() {
  82. $requirements = $this->listRequirements();
  83. return $this->getMaxSeverity($requirements) == static::REQUIREMENT_ERROR;
  84. }
  85. /**
  86. * Displays the site status report. Can also be used as a pure check.
  87. *
  88. * @return array
  89. * An array of system requirements.
  90. */
  91. public function listRequirements() {
  92. // Load .install files
  93. include_once DRUPAL_ROOT . '/core/includes/install.inc';
  94. drupal_load_updates();
  95. // Check run-time requirements and status information.
  96. $requirements = $this->moduleHandler->invokeAll('requirements', ['runtime']);
  97. uasort($requirements, function ($a, $b) {
  98. if (!isset($a['weight'])) {
  99. if (!isset($b['weight'])) {
  100. return strcasecmp($a['title'], $b['title']);
  101. }
  102. return -$b['weight'];
  103. }
  104. return isset($b['weight']) ? $a['weight'] - $b['weight'] : $a['weight'];
  105. });
  106. return $requirements;
  107. }
  108. /**
  109. * Extracts the highest severity from the requirements array.
  110. *
  111. * @param $requirements
  112. * An array of requirements, in the same format as is returned by
  113. * hook_requirements().
  114. *
  115. * @return
  116. * The highest severity in the array.
  117. */
  118. public function getMaxSeverity(&$requirements) {
  119. $severity = static::REQUIREMENT_OK;
  120. foreach ($requirements as $requirement) {
  121. if (isset($requirement['severity'])) {
  122. $severity = max($severity, $requirement['severity']);
  123. }
  124. }
  125. return $severity;
  126. }
  127. /**
  128. * Loads the contents of a menu block.
  129. *
  130. * This function is often a destination for these blocks.
  131. * For example, 'admin/structure/types' needs to have a destination to be
  132. * valid in the Drupal menu system, but too much information there might be
  133. * hidden, so we supply the contents of the block.
  134. *
  135. * @return array
  136. * A render array suitable for
  137. * \Drupal\Core\Render\RendererInterface::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. }