Skip to content

Commit

Permalink
Delegate to Drupal scaffold to handle creating settings.php and servi…
Browse files Browse the repository at this point in the history
…ces.yml.

Squashed commit of the following:

Merged feature branch 'feature/less-work-for-scaffold'

commit 5d1bbeb
Author: Michael Priest <[email protected]>
Date:   Wed Sep 6 12:52:06 2017 +0930

    Invoke Drupal scaffold from Shepherd scaffold to fix order of operations.

commit 165bb60
Author: Michael Priest <[email protected]>
Date:   Wed Sep 6 11:14:50 2017 +0930

    Drupal scaffold can handle creating settings.php and services.yml.
  • Loading branch information
pingers committed Sep 6, 2017
1 parent 4a68a78 commit a2ec830
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 71 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"license": "GPL-2.0+",
"require": {
"php": ">=5.4.5",
"composer-plugin-api": "^1.0.0",
"composer-plugin-api": "^1.1",
"drupal-composer/drupal-scaffold": "^2.2",
"symfony/filesystem": "~2.8|~3.0"
},
Expand Down
138 changes: 68 additions & 70 deletions src/Handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Composer\Composer;
use Composer\IO\IOInterface;
use Composer\Util\Filesystem as ComposerFilesystem;
use DrupalComposer\DrupalScaffold\Handler as DrupalScaffoldHandler;
use Symfony\Component\Filesystem\Filesystem;

class Handler
Expand Down Expand Up @@ -45,18 +46,30 @@ public function __construct(Composer $composer, IOInterface $io)
*/
public function onPostCmdEvent(\Composer\Script\Event $event)
{
$event->getIO()->write("Execute Drupal scaffold.");
$this->executeDrupalScaffold($event);
$event->getIO()->write("Updating Shepherd scaffold files.");
$this->updateShepherdScaffoldFiles();
$event->getIO()->write("Creating necessary directories.");
$this->createDirectories();
$event->getIO()->write("Creating settings.php file if not present.");
$this->createSettingsFile();
$event->getIO()->write("Creating services.yml file if not present.");
$this->createServicesFile();
$this->modifySettingsFile();
$event->getIO()->write("Removing write permissions on settings files.");
$this->removeWritePermissions();
}

/**
* Run Drupal scaffold handler.
*/
public function executeDrupalScaffold($event)
{
$root = $this->getDrupalRootPath();
$this->filesystem->chmod($root . '/sites/default', 0775);
$drupalScaffoldHandler = new DrupalScaffoldHandler($event->getComposer(), $event->getIO());
$drupalScaffoldHandler->downloadScaffold();
$drupalScaffoldHandler->generateAutoload();
}

/**
* Update the Shepherd scaffold files.
*/
Expand Down Expand Up @@ -113,77 +126,62 @@ public function createDirectories()
*
* Note: does nothing if the file already exists.
*/
public function createSettingsFile()
public function modifySettingsFile()
{
$root = $this->getDrupalRootPath();

// If the settings.php is not present, and the default version is...
if (!$this->filesystem->exists($root . '/sites/default/settings.php') && $this->filesystem->exists($root . '/sites/default/default.settings.php')) {
$this->filesystem->copy($root . '/sites/default/default.settings.php', $root . '/sites/default/settings.php');
$this->filesystem->chmod($root . '/sites/default/settings.php', 0666);
// Assume Drupal scaffold created the settings.php
$this->filesystem->chmod($root . '/sites/default/settings.php', 0664);

$shepherdSettings = "\n/**\n * START SHEPHERD CONFIG\n */\n" .
"\$databases['default']['default'] = array (\n" .
" 'database' => getenv('DATABASE_NAME'),\n" .
" 'username' => getenv('DATABASE_USER'),\n" .
" 'password' => getenv('DATABASE_PASSWORD_FILE') ? file_get_contents(getenv('DATABASE_PASSWORD_FILE')) : getenv('DATABASE_PASSWORD'),\n" .
" 'host' => getenv('DATABASE_HOST'),\n" .
" 'port' => getenv('DATABASE_PORT') ?: '3306',\n" .
" 'driver' => getenv('DATABASE_DRIVER') ?: 'mysql',\n" .
" 'prefix' => getenv('DATABASE_PREFIX') ?: '',\n" .
" 'collation' => getenv('DATABASE_COLLATION') ?: 'utf8mb4_general_ci',\n" .
" 'namespace' => getenv('DATABASE_NAMESPACE') ?: 'Drupal\\\\Core\\\\Database\\\\Driver\\\\mysql',\n" .
");\n" .
"\$settings['file_private_path'] = getenv('PRIVATE_DIR');\n" .
"\$settings['hash_salt'] = getenv('HASH_SALT') ?: '" . str_replace(['+', '/', '='], ['-', '_', ''], base64_encode(random_bytes(55))) . "';\n" .
"\$config_directories['sync'] = getenv('CONFIG_SYNC_DIRECTORY') ?: 'sites/default/files/config_" . str_replace(['+', '/', '='], ['-', '_', ''], base64_encode(random_bytes(55))) . "/sync';\n" .
"if (! is_dir(\$app_root . '/' . \$config_directories['sync'])) mkdir(\$app_root . '/' . \$config_directories['sync'], 0777, true);\n" .
"\$settings['shepherd_site_id'] = getenv('SHEPHERD_SITE_ID');\n" .
"\$settings['shepherd_url'] = getenv('SHEPHERD_URL');\n" .
"\$settings['shepherd_token'] = getenv('SHEPHERD_TOKEN_FILE') ? file_get_contents(getenv('SHEPHERD_TOKEN_FILE')) : getenv('SHEPHERD_TOKEN');\n\n" .
"\$settings['install_profile'] = getenv('SHEPHERD_INSTALL_PROFILE') ?: 'standard';\n" .
"if (getenv('REDIS_ENABLED')) {\n" .
" \$settings['redis.connection']['interface'] = 'PhpRedis';\n" .
" \$settings['redis.connection']['host'] = getenv('REDIS_HOST') ?: 'redis';\n" .
" \$settings['cache']['default'] = 'cache.backend.redis';\n\n" .
" // Always set the fast backend for bootstrap, discover and config, otherwise\n" .
" // this gets lost when redis is enabled.\n" .
" \$settings['cache']['bins']['bootstrap'] = 'cache.backend.chainedfast';\n" .
" \$settings['cache']['bins']['discovery'] = 'cache.backend.chainedfast';\n" .
" \$settings['cache']['bins']['config'] = 'cache.backend.chainedfast';\n\n" .
" // If we're not installing, include the redis services.\n" .
" if (!isset(\$GLOBALS['install_state'])) {\n" .
" \$settings['container_yamls'][] = 'modules/contrib/redis/example.services.yml';\n" .
" }\n" .
"}\n" .
"/**\n * END SHEPHERD CONFIG\n */\n" .
"\n" .
"/**\n * START LOCAL CONFIG\n */\n" .
"if (file_exists(__DIR__ . '/settings.local.php')) {\n" .
" include __DIR__ . '/settings.local.php';\n" .
"}\n" .
"/**\n * END LOCAL CONFIG\n */\n"
;
$shepherdSettings = "\n/**\n * START SHEPHERD CONFIG\n */\n" .
"\$databases['default']['default'] = array (\n" .
" 'database' => getenv('DATABASE_NAME'),\n" .
" 'username' => getenv('DATABASE_USER'),\n" .
" 'password' => getenv('DATABASE_PASSWORD_FILE') ? file_get_contents(getenv('DATABASE_PASSWORD_FILE')) : getenv('DATABASE_PASSWORD'),\n" .
" 'host' => getenv('DATABASE_HOST'),\n" .
" 'port' => getenv('DATABASE_PORT') ?: '3306',\n" .
" 'driver' => getenv('DATABASE_DRIVER') ?: 'mysql',\n" .
" 'prefix' => getenv('DATABASE_PREFIX') ?: '',\n" .
" 'collation' => getenv('DATABASE_COLLATION') ?: 'utf8mb4_general_ci',\n" .
" 'namespace' => getenv('DATABASE_NAMESPACE') ?: 'Drupal\\\\Core\\\\Database\\\\Driver\\\\mysql',\n" .
");\n" .
"\$settings['file_private_path'] = getenv('PRIVATE_DIR');\n" .
"\$settings['hash_salt'] = getenv('HASH_SALT') ?: '" . str_replace(['+', '/', '='], ['-', '_', ''], base64_encode(random_bytes(55))) . "';\n" .
"\$config_directories['sync'] = getenv('CONFIG_SYNC_DIRECTORY') ?: 'sites/default/files/config_" . str_replace(['+', '/', '='], ['-', '_', ''], base64_encode(random_bytes(55))) . "/sync';\n" .
"if (! is_dir(\$app_root . '/' . \$config_directories['sync'])) mkdir(\$app_root . '/' . \$config_directories['sync'], 0777, true);\n" .
"\$settings['shepherd_site_id'] = getenv('SHEPHERD_SITE_ID');\n" .
"\$settings['shepherd_url'] = getenv('SHEPHERD_URL');\n" .
"\$settings['shepherd_token'] = getenv('SHEPHERD_TOKEN_FILE') ? file_get_contents(getenv('SHEPHERD_TOKEN_FILE')) : getenv('SHEPHERD_TOKEN');\n\n" .
"\$settings['install_profile'] = getenv('SHEPHERD_INSTALL_PROFILE') ?: 'standard';\n" .
"if (getenv('REDIS_ENABLED')) {\n" .
" \$settings['redis.connection']['interface'] = 'PhpRedis';\n" .
" \$settings['redis.connection']['host'] = getenv('REDIS_HOST') ?: 'redis';\n" .
" \$settings['cache']['default'] = 'cache.backend.redis';\n\n" .
" // Always set the fast backend for bootstrap, discover and config, otherwise\n" .
" // this gets lost when redis is enabled.\n" .
" \$settings['cache']['bins']['bootstrap'] = 'cache.backend.chainedfast';\n" .
" \$settings['cache']['bins']['discovery'] = 'cache.backend.chainedfast';\n" .
" \$settings['cache']['bins']['config'] = 'cache.backend.chainedfast';\n\n" .
" // If we're not installing, include the redis services.\n" .
" if (!isset(\$GLOBALS['install_state'])) {\n" .
" \$settings['container_yamls'][] = 'modules/contrib/redis/example.services.yml';\n" .
" }\n" .
"}\n" .
"/**\n * END SHEPHERD CONFIG\n */\n" .
"\n" .
"/**\n * START LOCAL CONFIG\n */\n" .
"if (file_exists(__DIR__ . '/settings.local.php')) {\n" .
" include __DIR__ . '/settings.local.php';\n" .
"}\n" .
"/**\n * END LOCAL CONFIG\n */\n"
;

// Append Shepherd-specific environment variable settings to settings.php.
file_put_contents(
$root . '/sites/default/settings.php',
$shepherdSettings,
FILE_APPEND
);
}
}

/**
* Create services.yml file if not present.
*/
public function createServicesFile()
{
$root = $this->getDrupalRootPath();

if (!$this->filesystem->exists($root . '/sites/default/services.yml') && $this->filesystem->exists($root . '/sites/default/default.services.yml')) {
$this->filesystem->copy($root . '/sites/default/default.services.yml', $root . '/sites/default/services.yml');
}
// Append Shepherd-specific environment variable settings to settings.php.
file_put_contents(
$root . '/sites/default/settings.php',
$shepherdSettings,
FILE_APPEND
);
}

/**
Expand All @@ -198,7 +196,7 @@ public function removeWritePermissions()
}

/**
* Copy files from origin to destination, optionally overwritting existing.
* Copy files from origin to destination, optionally overwriting existing.
*
* @param bool $overwriteExisting
* If true, replace existing files. Defaults to false.
Expand Down

0 comments on commit a2ec830

Please sign in to comment.