55use Composer \Composer ;
66use Composer \IO \IOInterface ;
77use Composer \Util \Filesystem as ComposerFilesystem ;
8- use DrupalComposer \DrupalScaffold \Handler as DrupalScaffoldHandler ;
98use Symfony \Component \Filesystem \Filesystem ;
109
1110class Handler
@@ -51,7 +50,7 @@ public function onPostCmdEvent(\Composer\Script\Event $event)
5150 $ event ->getIO ()->write ("Creating necessary directories. " );
5251 $ this ->createDirectories ();
5352 $ event ->getIO ()->write ("Creating settings.php file if not present. " );
54- $ this ->modifySettingsFile ();
53+ $ this ->populateSettingsFile ();
5554 $ event ->getIO ()->write ("Removing write permissions on settings files. " );
5655 $ this ->removeWritePermissions ();
5756 }
@@ -118,7 +117,7 @@ public function createDirectories()
118117 *
119118 * Note: does nothing if the file already exists.
120119 */
121- public function modifySettingsFile ()
120+ public function populateSettingsFile ()
122121 {
123122 $ root = $ this ->getDrupalRootPath ();
124123
@@ -127,64 +126,83 @@ public function modifySettingsFile()
127126
128127 // If we haven't already written to settings.php.
129128 if (!(strpos (file_get_contents ($ root . '/sites/default/settings.php ' ), 'START SHEPHERD CONFIG ' ) !== false )) {
130- $ shepherdSettings = "\n/** \n * START SHEPHERD CONFIG \n */ \n" .
131- "\$databases['default']['default'] = array ( \n" .
132- " 'database' => getenv('DATABASE_NAME') ?: 'drupal', \n" .
133- " 'username' => getenv('DATABASE_USER') ?: 'user', \n" .
134- " 'password' => getenv('DATABASE_PASSWORD_FILE') ? file_get_contents(getenv('DATABASE_PASSWORD_FILE')) : 'password', \n" .
135- " 'host' => getenv('DATABASE_HOST') ?: '127.0.0.1', \n" .
136- " 'port' => getenv('DATABASE_PORT') ?: '3306', \n" .
137- " 'driver' => getenv('DATABASE_DRIVER') ?: 'mysql', \n" .
138- " 'prefix' => getenv('DATABASE_PREFIX') ?: '', \n" .
139- " 'collation' => getenv('DATABASE_COLLATION') ?: 'utf8mb4_general_ci', \n" .
140- " 'namespace' => getenv('DATABASE_NAMESPACE') ?: 'Drupal \\\\Core \\\\Database \\\\Driver \\\\mysql', \n" .
141- "); \n" .
142- "\$settings['file_private_path'] = getenv('PRIVATE_DIR') ?: '/shared/private'; \n" .
143- "\$settings['file_temporary_path'] = getenv('TMP_DIR') ?: '/shared/tmp'; \n" .
144- "\$settings['hash_salt'] = getenv('HASH_SALT') ?: ' " . str_replace (['+ ' , '/ ' , '= ' ], ['- ' , '_ ' , '' ], base64_encode (random_bytes (55 ))) . "'; \n" .
145- "\$config_directories['sync'] = getenv('CONFIG_SYNC_DIRECTORY') ?: DRUPAL_ROOT . '/../config-export'; \n" .
146- "\$settings['shepherd_site_id'] = getenv('SHEPHERD_SITE_ID'); \n" .
147- "\$settings['shepherd_url'] = getenv('SHEPHERD_URL'); \n" .
148- "\$settings['shepherd_token'] = getenv('SHEPHERD_TOKEN_FILE') ? file_get_contents(getenv('SHEPHERD_TOKEN_FILE')) : getenv('SHEPHERD_TOKEN'); \n\n" .
149- "\$settings['install_profile'] = getenv('SHEPHERD_INSTALL_PROFILE') ?: 'standard'; \n" .
150- "if (getenv('REDIS_ENABLED')) { \n" .
151- " \$settings['redis.connection']['interface'] = 'PhpRedis'; \n" .
152- " \$settings['redis.connection']['host'] = getenv('REDIS_HOST') ?: '127.0.0.1'; \n" .
153- " // Always set the fast backend for bootstrap, discover and config, otherwise \n" .
154- " // this gets lost when redis is enabled. \n" .
155- " \$settings['cache']['bins']['bootstrap'] = 'cache.backend.chainedfast'; \n" .
156- " \$settings['cache']['bins']['discovery'] = 'cache.backend.chainedfast'; \n" .
157- " \$settings['cache']['bins']['config'] = 'cache.backend.chainedfast'; \n\n" .
158- " // If we're not installing, include the redis services. \n" .
159- " if (!isset( \$GLOBALS['install_state'])) { \n" .
160- " \$settings['cache']['default'] = 'cache.backend.redis'; \n\n" .
161- " \$settings['container_yamls'][] = 'modules/contrib/redis/example.services.yml'; \n" .
162- " } \n" .
163- "} \n" .
164- "if (getenv('SHEPHERD_SECRET_PATH')) { \n" .
165- " \$settings['shepherd_secrets'] = []; \n" .
166- " // Glob the secret path for secrets, that match pattern \n" .
167- " foreach( glob( rtrim(getenv('SHEPHERD_SECRET_PATH'),DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR . 'SHEPHERD_*') as \$secret) { \n" .
168- " \$settings['shepherd_secrets'][pathinfo( \$secret)['filename']] = file_get_contents( \$secret); \n" .
169- " } \n" .
170- "} \n" .
171- "/** \n * END SHEPHERD CONFIG \n */ \n" .
172- "\n" .
173- "/** \n * START LOCAL CONFIG \n */ \n" .
174- "if (file_exists(__DIR__ . '/settings.local.php')) { \n" .
175- " include __DIR__ . '/settings.local.php'; \n" .
176- "} \n" .
177- "/** \n * END LOCAL CONFIG \n */ \n" ;
178-
179- // Append Shepherd-specific environment variable settings to settings.php.
129+ // Append Shepherd-specific environment variable settings to settings.php.
180130 file_put_contents (
181131 $ root .'/sites/default/settings.php ' ,
182- $ shepherdSettings ,
132+ $ this -> generateSettings () ,
183133 FILE_APPEND
184134 );
185135 }
186136 }
187137
138+ /**
139+ * Generates the "template" settings.php configuration.
140+ *
141+ * @return string
142+ * PHP code.
143+ * @throws \Exception
144+ */
145+ public function generateSettings ()
146+ {
147+ return "\n/** \n * START SHEPHERD CONFIG \n */ \n" .
148+ "\$databases['default']['default'] = array ( \n" .
149+ " 'database' => getenv('DATABASE_NAME') ?: 'drupal', \n" .
150+ " 'username' => getenv('DATABASE_USER') ?: 'user', \n" .
151+ " 'password' => getenv('DATABASE_PASSWORD_FILE') ? file_get_contents(getenv('DATABASE_PASSWORD_FILE')) : 'password', \n" .
152+ " 'host' => getenv('DATABASE_HOST') ?: '127.0.0.1', \n" .
153+ " 'port' => getenv('DATABASE_PORT') ?: '3306', \n" .
154+ " 'driver' => getenv('DATABASE_DRIVER') ?: 'mysql', \n" .
155+ " 'prefix' => getenv('DATABASE_PREFIX') ?: '', \n" .
156+ " 'collation' => getenv('DATABASE_COLLATION') ?: 'utf8mb4_general_ci', \n" .
157+ " 'namespace' => getenv('DATABASE_NAMESPACE') ?: 'Drupal \\\\Core \\\\Database \\\\Driver \\\\mysql', \n" .
158+ "); \n" .
159+ "\$settings['file_private_path'] = getenv('PRIVATE_DIR') ?: '/shared/private'; \n" .
160+ "\$settings['file_temporary_path'] = getenv('TMP_DIR') ?: '/shared/tmp'; \n" .
161+ "\$settings['hash_salt'] = getenv('HASH_SALT') ?: ' " . str_replace (['+ ' , '/ ' , '= ' ], ['- ' , '_ ' , '' ], base64_encode (random_bytes (55 ))) . "'; \n" .
162+ "\$config_directories['sync'] = getenv('CONFIG_SYNC_DIRECTORY') ?: DRUPAL_ROOT . '/../config-export'; \n" .
163+ "\$settings['shepherd_site_id'] = getenv('SHEPHERD_SITE_ID'); \n" .
164+ "\$settings['shepherd_url'] = getenv('SHEPHERD_URL'); \n" .
165+ "\$settings['shepherd_token'] = getenv('SHEPHERD_TOKEN_FILE') ? file_get_contents(getenv('SHEPHERD_TOKEN_FILE')) : getenv('SHEPHERD_TOKEN'); \n\n" .
166+ "\$settings['install_profile'] = getenv('SHEPHERD_INSTALL_PROFILE') ?: 'standard'; \n" .
167+ "if (getenv('REDIS_ENABLED')) { \n" .
168+ " \$settings['redis.connection']['interface'] = 'PhpRedis'; \n" .
169+ " \$settings['redis.connection']['host'] = getenv('REDIS_HOST') ?: '127.0.0.1'; \n" .
170+ " // Always set the fast backend for bootstrap, discover and config, otherwise \n" .
171+ " // this gets lost when redis is enabled. \n" .
172+ " \$settings['cache']['bins']['bootstrap'] = 'cache.backend.chainedfast'; \n" .
173+ " \$settings['cache']['bins']['discovery'] = 'cache.backend.chainedfast'; \n" .
174+ " \$settings['cache']['bins']['config'] = 'cache.backend.chainedfast'; \n\n" .
175+ " // If we're not installing, include the redis services. \n" .
176+ " if (!isset( \$GLOBALS['install_state'])) { \n" .
177+ " \$settings['cache']['default'] = 'cache.backend.redis'; \n\n" .
178+ " \$settings['container_yamls'][] = 'modules/contrib/redis/example.services.yml'; \n" .
179+ " } \n" .
180+ "} \n" .
181+ "if (getenv('SHEPHERD_SECRET_PATH')) { \n" .
182+ " \$settings['shepherd_secrets'] = []; \n" .
183+ " // Glob the secret path for secrets, that match pattern \n" .
184+ " foreach ( glob( rtrim(getenv('SHEPHERD_SECRET_PATH'),DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR . 'SHEPHERD_*') as \$secret) { \n" .
185+ " \$settings['shepherd_secrets'][pathinfo( \$secret)['filename']] = file_get_contents( \$secret); \n" .
186+ " } \n" .
187+ "} \n" .
188+ "if (getenv('SHEPHERD_REVERSE_PROXY')) { \n" .
189+ " \$settings['reverse_proxy'] = TRUE; \n" .
190+ " \$settings['reverse_proxy_header'] = getenv('SHEPHERD_REVERSE_PROXY_HEADER') ?: 'X_CLUSTER_CLIENT_IP'; \n" .
191+ " \$settings['reverse_proxy_addresses'] = !empty(getenv('SHEPHERD_REVERSE_PROXY_ADDRESSES')) ? explode(',', getenv('SHEPHERD_REVERSE_PROXY_ADDRESSES')) : []; \n" .
192+ " \$settings['reverse_proxy_proto_header'] = getenv('SHEPHERD_REVERSE_PROXY_PROTO_HEADER') ?: 'X_FORWARDED_PROTO'; \n" .
193+ " \$settings['reverse_proxy_host_header'] = getenv('SHEPHERD_REVERSE_PROXY_HOST_HEADER') ?: 'X_FORWARDED_HOST'; \n" .
194+ " \$settings['reverse_proxy_port_header'] = getenv('SHEPHERD_REVERSE_PROXY_PORT_HEADER') ?: 'X_FORWARDED_PORT'; \n" .
195+ " \$settings['reverse_proxy_forwarded_header'] = getenv('SHEPHERD_REVERSE_PROXY_FORWARDED_HEADER') ?: 'FORWARDED'; \n" .
196+ "} \n" .
197+ "/** \n * END SHEPHERD CONFIG \n */ \n" .
198+ "\n" .
199+ "/** \n * START LOCAL CONFIG \n */ \n" .
200+ "if (file_exists(__DIR__ . '/settings.local.php')) { \n" .
201+ " include __DIR__ . '/settings.local.php'; \n" .
202+ "} \n" .
203+ "/** \n * END LOCAL CONFIG \n */ \n" ;
204+ }
205+
188206 /**
189207 * Remove all write permissions on Drupal configuration files and folder.
190208 */
0 commit comments