55use Composer \Composer ;
66use Composer \IO \IOInterface ;
77use Composer \Util \Filesystem as ComposerFilesystem ;
8+ use DrupalComposer \DrupalScaffold \Handler as DrupalScaffoldHandler ;
89use Symfony \Component \Filesystem \Filesystem ;
910
1011class Handler
@@ -45,18 +46,30 @@ public function __construct(Composer $composer, IOInterface $io)
4546 */
4647 public function onPostCmdEvent (\Composer \Script \Event $ event )
4748 {
49+ $ event ->getIO ()->write ("Execute Drupal scaffold. " );
50+ $ this ->executeDrupalScaffold ($ event );
4851 $ event ->getIO ()->write ("Updating Shepherd scaffold files. " );
4952 $ this ->updateShepherdScaffoldFiles ();
5053 $ event ->getIO ()->write ("Creating necessary directories. " );
5154 $ this ->createDirectories ();
5255 $ event ->getIO ()->write ("Creating settings.php file if not present. " );
53- $ this ->createSettingsFile ();
54- $ event ->getIO ()->write ("Creating services.yml file if not present. " );
55- $ this ->createServicesFile ();
56+ $ this ->modifySettingsFile ();
5657 $ event ->getIO ()->write ("Removing write permissions on settings files. " );
5758 $ this ->removeWritePermissions ();
5859 }
5960
61+ /**
62+ * Run Drupal scaffold handler.
63+ */
64+ public function executeDrupalScaffold ($ event )
65+ {
66+ $ root = $ this ->getDrupalRootPath ();
67+ $ this ->filesystem ->chmod ($ root . '/sites/default ' , 0775 );
68+ $ drupalScaffoldHandler = new DrupalScaffoldHandler ($ event ->getComposer (), $ event ->getIO ());
69+ $ drupalScaffoldHandler ->downloadScaffold ();
70+ $ drupalScaffoldHandler ->generateAutoload ();
71+ }
72+
6073 /**
6174 * Update the Shepherd scaffold files.
6275 */
@@ -113,77 +126,62 @@ public function createDirectories()
113126 *
114127 * Note: does nothing if the file already exists.
115128 */
116- public function createSettingsFile ()
129+ public function modifySettingsFile ()
117130 {
118131 $ root = $ this ->getDrupalRootPath ();
119132
120- // If the settings.php is not present, and the default version is...
121- if (!$ this ->filesystem ->exists ($ root . '/sites/default/settings.php ' ) && $ this ->filesystem ->exists ($ root . '/sites/default/default.settings.php ' )) {
122- $ this ->filesystem ->copy ($ root . '/sites/default/default.settings.php ' , $ root . '/sites/default/settings.php ' );
123- $ this ->filesystem ->chmod ($ root . '/sites/default/settings.php ' , 0666 );
133+ // Assume Drupal scaffold created the settings.php
134+ $ this ->filesystem ->chmod ($ root . '/sites/default/settings.php ' , 0664 );
124135
125- $ shepherdSettings = "\n/** \n * START SHEPHERD CONFIG \n */ \n" .
126- "\$databases['default']['default'] = array ( \n" .
127- " 'database' => getenv('DATABASE_NAME'), \n" .
128- " 'username' => getenv('DATABASE_USER'), \n" .
129- " 'password' => getenv('DATABASE_PASSWORD_FILE') ? file_get_contents(getenv('DATABASE_PASSWORD_FILE')) : getenv('DATABASE_PASSWORD'), \n" .
130- " 'host' => getenv('DATABASE_HOST'), \n" .
131- " 'port' => getenv('DATABASE_PORT') ?: '3306', \n" .
132- " 'driver' => getenv('DATABASE_DRIVER') ?: 'mysql', \n" .
133- " 'prefix' => getenv('DATABASE_PREFIX') ?: '', \n" .
134- " 'collation' => getenv('DATABASE_COLLATION') ?: 'utf8mb4_general_ci', \n" .
135- " 'namespace' => getenv('DATABASE_NAMESPACE') ?: 'Drupal \\\\Core \\\\Database \\\\Driver \\\\mysql', \n" .
136- "); \n" .
137- "\$settings['file_private_path'] = getenv('PRIVATE_DIR'); \n" .
138- "\$settings['hash_salt'] = getenv('HASH_SALT') ?: ' " . str_replace (['+ ' , '/ ' , '= ' ], ['- ' , '_ ' , '' ], base64_encode (random_bytes (55 ))) . "'; \n" .
139- "\$config_directories['sync'] = getenv('CONFIG_SYNC_DIRECTORY') ?: 'sites/default/files/config_ " . str_replace (['+ ' , '/ ' , '= ' ], ['- ' , '_ ' , '' ], base64_encode (random_bytes (55 ))) . "/sync'; \n" .
140- "if (! is_dir( \$app_root . '/' . \$config_directories['sync'])) mkdir( \$app_root . '/' . \$config_directories['sync'], 0777, true); \n" .
141- "\$settings['shepherd_site_id'] = getenv('SHEPHERD_SITE_ID'); \n" .
142- "\$settings['shepherd_url'] = getenv('SHEPHERD_URL'); \n" .
143- "\$settings['shepherd_token'] = getenv('SHEPHERD_TOKEN_FILE') ? file_get_contents(getenv('SHEPHERD_TOKEN_FILE')) : getenv('SHEPHERD_TOKEN'); \n\n" .
144- "\$settings['install_profile'] = getenv('SHEPHERD_INSTALL_PROFILE') ?: 'standard'; \n" .
145- "if (getenv('REDIS_ENABLED')) { \n" .
146- " \$settings['redis.connection']['interface'] = 'PhpRedis'; \n" .
147- " \$settings['redis.connection']['host'] = getenv('REDIS_HOST') ?: 'redis'; \n" .
148- " \$settings['cache']['default'] = 'cache.backend.redis'; \n\n" .
149- " // Always set the fast backend for bootstrap, discover and config, otherwise \n" .
150- " // this gets lost when redis is enabled. \n" .
151- " \$settings['cache']['bins']['bootstrap'] = 'cache.backend.chainedfast'; \n" .
152- " \$settings['cache']['bins']['discovery'] = 'cache.backend.chainedfast'; \n" .
153- " \$settings['cache']['bins']['config'] = 'cache.backend.chainedfast'; \n\n" .
154- " // If we're not installing, include the redis services. \n" .
155- " if (!isset( \$GLOBALS['install_state'])) { \n" .
156- " \$settings['container_yamls'][] = 'modules/contrib/redis/example.services.yml'; \n" .
157- " } \n" .
158- "} \n" .
159- "/** \n * END SHEPHERD CONFIG \n */ \n" .
160- "\n" .
161- "/** \n * START LOCAL CONFIG \n */ \n" .
162- "if (file_exists(__DIR__ . '/settings.local.php')) { \n" .
163- " include __DIR__ . '/settings.local.php'; \n" .
164- "} \n" .
165- "/** \n * END LOCAL CONFIG \n */ \n"
166- ;
136+ $ shepherdSettings = "\n/** \n * START SHEPHERD CONFIG \n */ \n" .
137+ "\$databases['default']['default'] = array ( \n" .
138+ " 'database' => getenv('DATABASE_NAME'), \n" .
139+ " 'username' => getenv('DATABASE_USER'), \n" .
140+ " 'password' => getenv('DATABASE_PASSWORD_FILE') ? file_get_contents(getenv('DATABASE_PASSWORD_FILE')) : getenv('DATABASE_PASSWORD'), \n" .
141+ " 'host' => getenv('DATABASE_HOST'), \n" .
142+ " 'port' => getenv('DATABASE_PORT') ?: '3306', \n" .
143+ " 'driver' => getenv('DATABASE_DRIVER') ?: 'mysql', \n" .
144+ " 'prefix' => getenv('DATABASE_PREFIX') ?: '', \n" .
145+ " 'collation' => getenv('DATABASE_COLLATION') ?: 'utf8mb4_general_ci', \n" .
146+ " 'namespace' => getenv('DATABASE_NAMESPACE') ?: 'Drupal \\\\Core \\\\Database \\\\Driver \\\\mysql', \n" .
147+ "); \n" .
148+ "\$settings['file_private_path'] = getenv('PRIVATE_DIR'); \n" .
149+ "\$settings['hash_salt'] = getenv('HASH_SALT') ?: ' " . str_replace (['+ ' , '/ ' , '= ' ], ['- ' , '_ ' , '' ], base64_encode (random_bytes (55 ))) . "'; \n" .
150+ "\$config_directories['sync'] = getenv('CONFIG_SYNC_DIRECTORY') ?: 'sites/default/files/config_ " . str_replace (['+ ' , '/ ' , '= ' ], ['- ' , '_ ' , '' ], base64_encode (random_bytes (55 ))) . "/sync'; \n" .
151+ "if (! is_dir( \$app_root . '/' . \$config_directories['sync'])) mkdir( \$app_root . '/' . \$config_directories['sync'], 0777, true); \n" .
152+ "\$settings['shepherd_site_id'] = getenv('SHEPHERD_SITE_ID'); \n" .
153+ "\$settings['shepherd_url'] = getenv('SHEPHERD_URL'); \n" .
154+ "\$settings['shepherd_token'] = getenv('SHEPHERD_TOKEN_FILE') ? file_get_contents(getenv('SHEPHERD_TOKEN_FILE')) : getenv('SHEPHERD_TOKEN'); \n\n" .
155+ "\$settings['install_profile'] = getenv('SHEPHERD_INSTALL_PROFILE') ?: 'standard'; \n" .
156+ "if (getenv('REDIS_ENABLED')) { \n" .
157+ " \$settings['redis.connection']['interface'] = 'PhpRedis'; \n" .
158+ " \$settings['redis.connection']['host'] = getenv('REDIS_HOST') ?: 'redis'; \n" .
159+ " \$settings['cache']['default'] = 'cache.backend.redis'; \n\n" .
160+ " // Always set the fast backend for bootstrap, discover and config, otherwise \n" .
161+ " // this gets lost when redis is enabled. \n" .
162+ " \$settings['cache']['bins']['bootstrap'] = 'cache.backend.chainedfast'; \n" .
163+ " \$settings['cache']['bins']['discovery'] = 'cache.backend.chainedfast'; \n" .
164+ " \$settings['cache']['bins']['config'] = 'cache.backend.chainedfast'; \n\n" .
165+ " // If we're not installing, include the redis services. \n" .
166+ " if (!isset( \$GLOBALS['install_state'])) { \n" .
167+ " \$settings['container_yamls'][] = 'modules/contrib/redis/example.services.yml'; \n" .
168+ " } \n" .
169+ "} \n" .
170+ "/** \n * END SHEPHERD CONFIG \n */ \n" .
171+ "\n" .
172+ "/** \n * START LOCAL CONFIG \n */ \n" .
173+ "if (file_exists(__DIR__ . '/settings.local.php')) { \n" .
174+ " include __DIR__ . '/settings.local.php'; \n" .
175+ "} \n" .
176+ "/** \n * END LOCAL CONFIG \n */ \n"
177+ ;
167178
168- // Append Shepherd-specific environment variable settings to settings.php.
169- file_put_contents (
170- $ root . '/sites/default/settings.php ' ,
171- $ shepherdSettings ,
172- FILE_APPEND
173- );
174- }
175- }
176-
177- /**
178- * Create services.yml file if not present.
179- */
180- public function createServicesFile ()
181- {
182- $ root = $ this ->getDrupalRootPath ();
183-
184- if (!$ this ->filesystem ->exists ($ root . '/sites/default/services.yml ' ) && $ this ->filesystem ->exists ($ root . '/sites/default/default.services.yml ' )) {
185- $ this ->filesystem ->copy ($ root . '/sites/default/default.services.yml ' , $ root . '/sites/default/services.yml ' );
186- }
179+ // Append Shepherd-specific environment variable settings to settings.php.
180+ file_put_contents (
181+ $ root . '/sites/default/settings.php ' ,
182+ $ shepherdSettings ,
183+ FILE_APPEND
184+ );
187185 }
188186
189187 /**
@@ -198,7 +196,7 @@ public function removeWritePermissions()
198196 }
199197
200198 /**
201- * Copy files from origin to destination, optionally overwritting existing.
199+ * Copy files from origin to destination, optionally overwriting existing.
202200 *
203201 * @param bool $overwriteExisting
204202 * If true, replace existing files. Defaults to false.
0 commit comments