You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

94 lines
3.4 KiB

5 years ago
  1. <?php
  2. /**
  3. * @file
  4. * Contains \DrupalProject\composer\ScriptHandler.
  5. */
  6. namespace DrupalProject\composer;
  7. use Composer\Script\Event;
  8. use Composer\Semver\Comparator;
  9. use DrupalFinder\DrupalFinder;
  10. use Symfony\Component\Filesystem\Filesystem;
  11. use Webmozart\PathUtil\Path;
  12. class ScriptHandler {
  13. public static function createRequiredFiles(Event $event) {
  14. $fs = new Filesystem();
  15. $drupalFinder = new DrupalFinder();
  16. $drupalFinder->locateRoot(getcwd());
  17. $drupalRoot = $drupalFinder->getDrupalRoot();
  18. $dirs = [
  19. 'modules',
  20. 'profiles',
  21. 'themes',
  22. ];
  23. // Required for unit testing
  24. foreach ($dirs as $dir) {
  25. if (!$fs->exists($drupalRoot . '/'. $dir)) {
  26. $fs->mkdir($drupalRoot . '/'. $dir);
  27. $fs->touch($drupalRoot . '/'. $dir . '/.gitkeep');
  28. }
  29. }
  30. // Prepare the settings file for installation
  31. /*
  32. if (!$fs->exists($drupalRoot . '/sites/default/settings.php') and $fs->exists($drupalRoot . '/sites/default/default.settings.php')) {
  33. $fs->copy($drupalRoot . '/sites/default/default.settings.php', $drupalRoot . '/sites/default/settings.php');
  34. require_once $drupalRoot . '/core/includes/bootstrap.inc';
  35. require_once $drupalRoot . '/core/includes/install.inc';
  36. $settings['config_directories'] = [
  37. CONFIG_SYNC_DIRECTORY => (object) [
  38. 'value' => Path::makeRelative($drupalFinder->getComposerRoot() . '/config/sync', $drupalRoot),
  39. 'required' => TRUE,
  40. ],
  41. ];
  42. drupal_rewrite_settings($settings, $drupalRoot . '/sites/default/settings.php');
  43. $fs->chmod($drupalRoot . '/sites/default/settings.php', 0666);
  44. $event->getIO()->write("Create a sites/default/settings.php file with chmod 0666");
  45. }*/
  46. }
  47. /**
  48. * Checks if the installed version of Composer is compatible.
  49. *
  50. * Composer 1.0.0 and higher consider a `composer install` without having a
  51. * lock file present as equal to `composer update`. We do not ship with a lock
  52. * file to avoid merge conflicts downstream, meaning that if a project is
  53. * installed with an older version of Composer the scaffolding of Drupal will
  54. * not be triggered. We check this here instead of in drupal-scaffold to be
  55. * able to give immediate feedback to the end user, rather than failing the
  56. * installation after going through the lengthy process of compiling and
  57. * downloading the Composer dependencies.
  58. *
  59. * @see https://github.com/composer/composer/pull/5035
  60. */
  61. public static function checkComposerVersion(Event $event) {
  62. $composer = $event->getComposer();
  63. $io = $event->getIO();
  64. $version = $composer::VERSION;
  65. // The dev-channel of composer uses the git revision as version number,
  66. // try to the branch alias instead.
  67. if (preg_match('/^[0-9a-f]{40}$/i', $version)) {
  68. $version = $composer::BRANCH_ALIAS_VERSION;
  69. }
  70. // If Composer is installed through git we have no easy way to determine if
  71. // it is new enough, just display a warning.
  72. if ($version === '@package_version@' || $version === '@package_branch_alias_version@') {
  73. $io->writeError('<warning>You are running a development version of Composer. If you experience problems, please update Composer to the latest stable version.</warning>');
  74. }
  75. elseif (Comparator::lessThan($version, '1.0.0')) {
  76. $io->writeError('<error>Drupal-project requires Composer version 1.0.0 or higher. Please update your Composer before continuing</error>.');
  77. exit(1);
  78. }
  79. }
  80. }