Skip to content

Commit 178e8a8

Browse files
Merge pull request #17 from drupal-composer/generate-autoload
Fixes #16: Generate the autoload.php file at the Drupal project root.
2 parents a5ea0b3 + c386a1a commit 178e8a8

File tree

3 files changed

+80
-1
lines changed

3 files changed

+80
-1
lines changed

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,13 @@
33
Composer plugin for automatically downloading Drupal scaffold files (like
44
`index.php`, `update.php`, …) when using `drupal/core` via Composer.
55

6+
It is recommended that the vendor directory be placed in its standard location
7+
at the project root, outside of the Drupal root; however, the location of the
8+
vendor directory and the name of the Drupal root may be placed in whatever
9+
location suits the project. Drupal-scaffold will generate the autoload.php
10+
file at the Drupal root to require the Composer-generated autoload file in the
11+
vendor directory.
12+
613
## Usage
714

815
`composer require drupal-composer/drupal-scaffold:dev-master` in your composer

src/Handler.php

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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.

src/Plugin.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,12 +68,24 @@ public function postCmd(\Composer\Script\Event $event) {
6868
}
6969

7070
/**
71-
* Script callback for putting in composer scripts.
71+
* Script callback for putting in composer scripts to download the
72+
* scaffold files.
7273
*
7374
* @param \Composer\Script\Event $event
7475
*/
7576
public static function scaffold(\Composer\Script\Event $event) {
7677
$handler = new Handler($event->getComposer(), $event->getIO());
7778
$handler->downloadScaffold();
7879
}
80+
81+
/**
82+
* Script callback for putting in composer scripts to generate the
83+
* autoload file at the project root.
84+
*
85+
* @param \Composer\Script\Event $event
86+
*/
87+
public static function generateAutoload(\Composer\Script\Event $event) {
88+
$handler = new Handler($event->getComposer(), $event->getIO());
89+
$handler->generateAutoload();
90+
}
7991
}

0 commit comments

Comments
 (0)