From aa759f73f7e1e0cf2bd3ff6353cf5461295a5fa1 Mon Sep 17 00:00:00 2001 From: Alex Skrypnyk Date: Wed, 6 Feb 2019 09:39:30 +1100 Subject: [PATCH] Fixed composer scripts. (#5) * Fixed composer script handler not correctly working within container install. --- composer.dev.json | 5 ++- scripts/composer/ScriptHandler.php | 66 ++++++++++++++++++++++++------ 2 files changed, 56 insertions(+), 15 deletions(-) diff --git a/composer.dev.json b/composer.dev.json index e7bcd43..1c74304 100644 --- a/composer.dev.json +++ b/composer.dev.json @@ -182,12 +182,13 @@ ], "post-install-cmd": [ "DrupalComposer\\DrupalScaffold\\Plugin::scaffold", - "rm -f docroot/sites/default/settings.php", "DrupalProject\\composer\\ScriptHandler::createRequiredFiles", "Utilities\\composer\\DrupalSettings::create" ], "post-update-cmd": [ - "DrupalProject\\composer\\ScriptHandler::createRequiredFiles" + "DrupalComposer\\DrupalScaffold\\Plugin::scaffold", + "DrupalProject\\composer\\ScriptHandler::createRequiredFiles", + "Utilities\\composer\\DrupalSettings::create" ] }, "extra": { diff --git a/scripts/composer/ScriptHandler.php b/scripts/composer/ScriptHandler.php index cb4a53c..ecec584 100644 --- a/scripts/composer/ScriptHandler.php +++ b/scripts/composer/ScriptHandler.php @@ -39,27 +39,36 @@ public static function createRequiredFiles(Event $event) { } // Prepare the settings file for installation. - if (!$fs->exists($drupalRoot . '/sites/default/settings.php') and $fs->exists($drupalRoot . '/sites/default/default.settings.php')) { + if ($fs->exists($drupalRoot . '/sites/default/default.settings.php') && !$fs->exists($drupalRoot . '/sites/default/settings.php')) { $fs->copy($drupalRoot . '/sites/default/default.settings.php', $drupalRoot . '/sites/default/settings.php'); - require_once $drupalRoot . '/core/includes/bootstrap.inc'; - require_once $drupalRoot . '/core/includes/install.inc'; - $settings['config_directories'] = [ - CONFIG_SYNC_DIRECTORY => (object) [ - 'value' => Path::makeRelative($drupalFinder->getComposerRoot() . '/config/default', $drupalRoot), - 'required' => TRUE, - ], - ]; - drupal_rewrite_settings($settings, $drupalRoot . '/sites/default/settings.php'); - $fs->chmod($drupalRoot . '/sites/default/settings.php', 0644); - $event->getIO()->write("Create a sites/default/settings.php file with chmod 0644"); + $event->getIO()->write('Created a sites/default/settings.php file'); } + if (!$fs->exists($drupalRoot . '/sites/default/settings.php')) { + $event->getIO()->writeError('Settings file not found'); + exit(1); + } + + $fs->chmod($drupalRoot . '/sites/default', 0777); + $fs->chmod($drupalRoot . '/sites/default/settings.php', 0666); + + $configPath = Path::makeRelative($drupalFinder->getComposerRoot() . '/config/default', $drupalRoot); + $settings_string = << (object) [ + 'value' => '$configPath', + 'required' => TRUE, + ], +]; +SETTINGS; + self::appendToFile($drupalRoot . '/sites/default/settings.php', $settings_string); + // Create the files directory with chmod 0777. if (!$fs->exists($drupalRoot . '/sites/default/files')) { $oldmask = umask(0); $fs->mkdir($drupalRoot . '/sites/default/files', 0777); umask($oldmask); - $event->getIO()->write("Create a sites/default/files directory with chmod 0777"); + $event->getIO()->write('Created a sites/default/files directory with chmod 0777'); } } @@ -100,4 +109,35 @@ public static function checkComposerVersion(Event $event) { } } + /** + * Appends content to an existing file. + * + * Polyfill for older versions of Filesystem shipped with Composer phar. + * + * @param string $filename + * The file to which to append content. + * @param string $content + * The content to append. + * + * @throws \Symfony\Component\Filesystem\Exception\IOException + * If the file is not writable. + */ + protected static function appendToFile($filename, $content) { + $fs = new Filesystem(); + + $dir = \dirname($filename); + + if (!is_dir($dir)) { + $fs->mkdir($dir); + } + + if (!is_writable($dir)) { + throw new \Exception(sprintf('Unable to write to the "%s" directory.', $dir), 0, NULL, $dir); + } + + if (FALSE === @file_put_contents($filename, $content, FILE_APPEND)) { + throw new \Exception(sprintf('Failed to write file "%s".', $filename), 0, NULL, $filename); + } + } + }