ScriptHandler.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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. if (!$fs->exists($drupalRoot . '/sites/default/settings.php') and $fs->exists($drupalRoot . '/sites/default/default.settings.php')) {
  32. $fs->copy($drupalRoot . '/sites/default/default.settings.php', $drupalRoot . '/sites/default/settings.php');
  33. require_once $drupalRoot . '/core/includes/bootstrap.inc';
  34. require_once $drupalRoot . '/core/includes/install.inc';
  35. $settings['config_directories'] = [
  36. CONFIG_SYNC_DIRECTORY => (object) [
  37. 'value' => Path::makeRelative($drupalFinder->getComposerRoot() . '/config/sync', $drupalRoot),
  38. 'required' => TRUE,
  39. ],
  40. ];
  41. drupal_rewrite_settings($settings, $drupalRoot . '/sites/default/settings.php');
  42. $fs->chmod($drupalRoot . '/sites/default/settings.php', 0666);
  43. $event->getIO()->write("Create a sites/default/settings.php file with chmod 0666");
  44. }
  45. // Create the files directory with chmod 0777
  46. if (!$fs->exists($drupalRoot . '/sites/default/files')) {
  47. $oldmask = umask(0);
  48. $fs->mkdir($drupalRoot . '/sites/default/files', 0777);
  49. umask($oldmask);
  50. $event->getIO()->write("Create a sites/default/files directory with chmod 0777");
  51. }
  52. }
  53. /**
  54. * Checks if the installed version of Composer is compatible.
  55. *
  56. * Composer 1.0.0 and higher consider a `composer install` without having a
  57. * lock file present as equal to `composer update`. We do not ship with a lock
  58. * file to avoid merge conflicts downstream, meaning that if a project is
  59. * installed with an older version of Composer the scaffolding of Drupal will
  60. * not be triggered. We check this here instead of in drupal-scaffold to be
  61. * able to give immediate feedback to the end user, rather than failing the
  62. * installation after going through the lengthy process of compiling and
  63. * downloading the Composer dependencies.
  64. *
  65. * @see https://github.com/composer/composer/pull/5035
  66. */
  67. public static function checkComposerVersion(Event $event) {
  68. $composer = $event->getComposer();
  69. $io = $event->getIO();
  70. $version = $composer::VERSION;
  71. // The dev-channel of composer uses the git revision as version number,
  72. // try to the branch alias instead.
  73. if (preg_match('/^[0-9a-f]{40}$/i', $version)) {
  74. $version = $composer::BRANCH_ALIAS_VERSION;
  75. }
  76. // If Composer is installed through git we have no easy way to determine if
  77. // it is new enough, just display a warning.
  78. if ($version === '@package_version@' || $version === '@package_branch_alias_version@') {
  79. $io->writeError('<warning>You are running a development version of Composer. If you experience problems, please update Composer to the latest stable version.</warning>');
  80. }
  81. elseif (Comparator::lessThan($version, '1.0.0')) {
  82. $io->writeError('<error>Drupal-project requires Composer version 1.0.0 or higher. Please update your Composer before continuing</error>.');
  83. exit(1);
  84. }
  85. }
  86. }