Module.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. <?php
  2. namespace Drupal\Core\Updater;
  3. use Drupal\Core\Url;
  4. /**
  5. * Defines a class for updating modules using
  6. * Drupal\Core\FileTransfer\FileTransfer classes via authorize.php.
  7. */
  8. class Module extends Updater implements UpdaterInterface {
  9. /**
  10. * Returns the directory where a module should be installed.
  11. *
  12. * If the module is already installed, drupal_get_path() will return a valid
  13. * path and we should install it there. If we're installing a new module, we
  14. * always want it to go into /modules, since that's where all the
  15. * documentation recommends users install their modules, and there's no way
  16. * that can conflict on a multi-site installation, since the Update manager
  17. * won't let you install a new module if it's already found on your system,
  18. * and if there was a copy in the top-level we'd see it.
  19. *
  20. * @return string
  21. * The absolute path of the directory.
  22. */
  23. public function getInstallDirectory() {
  24. if ($this->isInstalled() && ($relative_path = drupal_get_path('module', $this->name))) {
  25. // The return value of drupal_get_path() is always relative to the site,
  26. // so prepend DRUPAL_ROOT.
  27. return DRUPAL_ROOT . '/' . dirname($relative_path);
  28. }
  29. else {
  30. // When installing a new module, prepend the requested root directory.
  31. return $this->root . '/' . $this->getRootDirectoryRelativePath();
  32. }
  33. }
  34. /**
  35. * {@inheritdoc}
  36. */
  37. public static function getRootDirectoryRelativePath() {
  38. return 'modules';
  39. }
  40. /**
  41. * {@inheritdoc}
  42. */
  43. public function isInstalled() {
  44. // Check if the module exists in the file system, regardless of whether it
  45. // is enabled or not.
  46. /** @var \Drupal\Core\Extension\ExtensionList $module_extension_list */
  47. $module_extension_list = \Drupal::service('extension.list.module');
  48. return $module_extension_list->exists($this->name);
  49. }
  50. /**
  51. * {@inheritdoc}
  52. */
  53. public static function canUpdateDirectory($directory) {
  54. $info = static::getExtensionInfo($directory);
  55. return (isset($info['type']) && $info['type'] == 'module');
  56. }
  57. /**
  58. * Determines whether this class can update the specified project.
  59. *
  60. * @param string $project_name
  61. * The project to check.
  62. *
  63. * @return bool
  64. */
  65. public static function canUpdate($project_name) {
  66. return (bool) drupal_get_path('module', $project_name);
  67. }
  68. /**
  69. * Returns available database schema updates once a new version is installed.
  70. *
  71. * @return array
  72. */
  73. public function getSchemaUpdates() {
  74. require_once DRUPAL_ROOT . '/core/includes/install.inc';
  75. require_once DRUPAL_ROOT . '/core/includes/update.inc';
  76. if (!self::canUpdate($this->name)) {
  77. return [];
  78. }
  79. module_load_include('install', $this->name);
  80. if (!$updates = drupal_get_schema_versions($this->name)) {
  81. return [];
  82. }
  83. $modules_with_updates = update_get_update_list();
  84. if ($updates = $modules_with_updates[$this->name]) {
  85. if ($updates['start']) {
  86. return $updates['pending'];
  87. }
  88. }
  89. return [];
  90. }
  91. /**
  92. * {@inheritdoc}
  93. */
  94. public function postInstallTasks() {
  95. // Since this is being called outside of the primary front controller,
  96. // the base_url needs to be set explicitly to ensure that links are
  97. // relative to the site root.
  98. // @todo Simplify with https://www.drupal.org/node/2548095
  99. $default_options = [
  100. '#type' => 'link',
  101. '#options' => [
  102. 'absolute' => TRUE,
  103. 'base_url' => $GLOBALS['base_url'],
  104. ],
  105. ];
  106. return [
  107. $default_options + [
  108. '#url' => Url::fromRoute('update.module_install'),
  109. '#title' => t('Install another module'),
  110. ],
  111. $default_options + [
  112. '#url' => Url::fromRoute('system.modules_list'),
  113. '#title' => t('Enable newly added modules'),
  114. ],
  115. $default_options + [
  116. '#url' => Url::fromRoute('system.admin'),
  117. '#title' => t('Administration pages'),
  118. ],
  119. ];
  120. }
  121. /**
  122. * {@inheritdoc}
  123. */
  124. public function postUpdateTasks() {
  125. // We don't want to check for DB updates here, we do that once for all
  126. // updated modules on the landing page.
  127. }
  128. }