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