@@ -82,6 +82,7 @@ public function onPostCmdEvent(\Composer\Script\Event $event) {
8282 // AND there are no scaffolding files present.
8383 if (isset ($ this ->drupalCorePackage ) && $ this ->checkAction ($ event )) {
8484 $ this ->downloadScaffold ();
85+ $ this ->generateAutoload ();
8586 }
8687 }
8788
@@ -149,6 +150,65 @@ public static function array_to_csv($data, $delimiter = ',', $enclosure = '"', $
149150 }, $ data ));
150151 }
151152
153+ /**
154+ * Generate the autoload file at the project root. Include the
155+ * autoload file that Composer generated.
156+ */
157+ public function generateAutoload () {
158+ $ vendorPath = $ this ->getVendorPath ();
159+ $ webroot = $ this ->getWebRoot ();
160+
161+ // Calculate the relative path from the webroot (location of the
162+ // project autoload.php) to the vendor directory.
163+ $ fs = new SymfonyFilesystem ();
164+ $ relativeVendorPath = $ fs ->makePathRelative ($ vendorPath , realpath ($ webroot ));
165+
166+ $ fs ->dumpFile ($ webroot . "/autoload.php " , $ this ->autoLoadContents ($ relativeVendorPath ));
167+ }
168+
169+ /**
170+ * Build the contents of the autoload file.
171+ *
172+ * @return string
173+ */
174+ protected function autoLoadContents ($ relativeVendorPath ) {
175+ $ autoloadContents = <<<EOF
176+ <?php
177+
178+ /**
179+ * @file
180+ * Includes the autoloader created by Composer.
181+ * This file was generated by drupal-composer/drupal-scaffold.
182+ * https://github.com/drupal-composer/drupal-scaffold
183+ *
184+ * @see composer.json
185+ * @see index.php
186+ * @see core/install.php
187+ * @see core/rebuild.php
188+ * @see core/modules/statistics/statistics.php
189+ */
190+
191+ return require __DIR__ . '/ $ relativeVendorPath/autoload.php';
192+
193+ EOF ;
194+ return $ autoloadContents ;
195+ }
196+
197+ /**
198+ * Get the path to the 'vendor' directory.
199+ *
200+ * @return string
201+ */
202+ public function getVendorPath () {
203+ $ config = $ this ->composer ->getConfig ();
204+ $ filesystem = new Filesystem ();
205+ $ filesystem ->ensureDirectoryExists ($ config ->get ('vendor-dir ' ));
206+ $ basePath = $ filesystem ->normalizePath (realpath (getcwd ()));
207+ $ vendorPath = $ filesystem ->normalizePath (realpath ($ config ->get ('vendor-dir ' )));
208+
209+ return $ vendorPath ;
210+ }
211+
152212 /**
153213 * Look up the Drupal core package object, or return it from where we cached
154214 * it in the $drupalCorePackage field.
0 commit comments