diff --git a/composer/Plugin/Scaffold/GenerateAutoloadReferenceFile.php b/composer/Plugin/Scaffold/GenerateAutoloadReferenceFile.php index 9fde8b6c9c0792610a061f2d7fe55ff2734b9e57..55e27debfe04afbc41a5dc7988497bada6ea463c 100644 --- a/composer/Plugin/Scaffold/GenerateAutoloadReferenceFile.php +++ b/composer/Plugin/Scaffold/GenerateAutoloadReferenceFile.php @@ -38,12 +38,11 @@ private function __construct() { */ public static function generateAutoload(IOInterface $io, $package_name, $web_root, $vendor) { $autoload_path = static::autoloadPath($package_name, $web_root); - $location = dirname($autoload_path->fullPath()); // Calculate the relative path from the webroot (location of the project // autoload.php) to the vendor directory. $fs = new Filesystem(); - $relative_vendor_path = $fs->findShortestPath(realpath($location), $vendor); - file_put_contents($autoload_path->fullPath(), static::autoLoadContents($relative_vendor_path)); + $relative_autoload_path = $fs->findShortestPath($autoload_path->fullPath(), "$vendor/autoload.php"); + file_put_contents($autoload_path->fullPath(), static::autoLoadContents($relative_autoload_path)); return new ScaffoldResult($autoload_path, TRUE); } @@ -91,14 +90,14 @@ protected static function autoloadPath($package_name, $web_root) { /** * Builds the contents of the autoload file. * - * @param string $vendor_path - * The relative path to vendor. + * @param string $relative_autoload_path + * The relative path to the autoloader in vendor. * * @return string * Return the contents for the autoload.php. */ - protected static function autoLoadContents($vendor_path) { - $vendor_path = rtrim($vendor_path, '/'); + protected static function autoLoadContents($relative_autoload_path) { + $relative_autoload_path = preg_replace('#^\./#', '', $relative_autoload_path); return <<<EOF <?php @@ -115,7 +114,7 @@ protected static function autoLoadContents($vendor_path) { * @see core/modules/statistics/statistics.php */ -return require __DIR__ . '/{$vendor_path}/autoload.php'; +return require __DIR__ . '/{$relative_autoload_path}'; EOF; } diff --git a/composer/Plugin/Scaffold/ScaffoldFilePath.php b/composer/Plugin/Scaffold/ScaffoldFilePath.php index b8ecd77dec5cb29e30043542712298aa9d1541ab..f225a226282ff36f2a6a7165249874b933bed6cc 100644 --- a/composer/Plugin/Scaffold/ScaffoldFilePath.php +++ b/composer/Plugin/Scaffold/ScaffoldFilePath.php @@ -2,6 +2,8 @@ namespace Drupal\Composer\Plugin\Scaffold; +use Composer\Util\Filesystem; + /** * Manage the path to a file to scaffold. * @@ -61,6 +63,14 @@ public function __construct($path_type, $package_name, $rel_path, $full_path) { $this->packageName = $package_name; $this->relativePath = $rel_path; $this->fullPath = $full_path; + + // Ensure that the full path really is a full path. We do not use + // 'realpath' here because the file specified by the full path might + // not exist yet. + $fs = new Filesystem(); + if (!$fs->isAbsolutePath($this->fullPath)) { + $this->fullPath = getcwd() . '/' . $this->fullPath; + } } /** diff --git a/core/tests/Drupal/Tests/Composer/Plugin/Scaffold/Functional/ScaffoldTest.php b/core/tests/Drupal/Tests/Composer/Plugin/Scaffold/Functional/ScaffoldTest.php index 52bd9f8c1e5b5d7c8f8e75a4da4d667d9fbf947f..3585c4cc5b92dde12d323e55e113c19e435c3240 100644 --- a/core/tests/Drupal/Tests/Composer/Plugin/Scaffold/Functional/ScaffoldTest.php +++ b/core/tests/Drupal/Tests/Composer/Plugin/Scaffold/Functional/ScaffoldTest.php @@ -194,6 +194,7 @@ public function testProjectThatScaffoldsEmptyProject() { $result = $this->scaffoldSut($fixture_name, FALSE, FALSE); $this->assertContains('The allowed package fixtures/empty-fixture does not provide a file mapping for Composer Scaffold', $result->scaffoldOutput()); $this->assertCommonDrupalAssetsWereScaffolded($result->docroot(), FALSE); + $this->assertAutoloadFileCorrect($result->docroot()); } public function scaffoldOverridingSettingsExcludingHtaccessValues() { @@ -229,6 +230,7 @@ public function testScaffoldOverridingSettingsExcludingHtaccess($fixture_name, $ $result = $this->scaffoldSut($fixture_name, $is_link, $relocated_docroot); $this->assertCommonDrupalAssetsWereScaffolded($result->docroot(), $is_link); + $this->assertAutoloadFileCorrect($result->docroot(), $relocated_docroot); $this->assertDefaultSettingsFromScaffoldOverride($result->docroot(), $is_link); $this->assertHtaccessExcluded($result->docroot()); } @@ -248,6 +250,7 @@ public function testDrupalDrupalFileWasReplaced() { $this->assertScaffoldedFile($result->docroot() . '/keep-me.txt', FALSE, 'File in drupal-drupal-test-overwrite that is not replaced'); $this->assertScaffoldedFile($result->docroot() . '/make-me.txt', FALSE, 'from assets that replaces file'); $this->assertCommonDrupalAssetsWereScaffolded($result->docroot(), FALSE); + $this->assertAutoloadFileCorrect($result->docroot()); $this->assertScaffoldedFile($result->docroot() . '/robots.txt', FALSE, $fixture_name); } @@ -322,6 +325,7 @@ public function testDrupalDrupalFileWasAppended($fixture_name, $is_link, $robots $this->assertScaffoldedFile($result->docroot() . '/robots.txt', FALSE, $robots_txt_contents); $this->assertCommonDrupalAssetsWereScaffolded($result->docroot(), $is_link); + $this->assertAutoloadFileCorrect($result->docroot()); } /** @@ -361,9 +365,7 @@ protected function assertHtaccessExcluded($docroot) { * Whether or not symlinking is used. */ protected function assertCommonDrupalAssetsWereScaffolded($docroot, $is_link) { - // Ensure that the autoload.php file was written. - $this->assertFileExists($docroot . '/autoload.php'); - // Assert other scaffold files are written in the correct locations. + // Assert scaffold files are written in the correct locations. $this->assertScaffoldedFile($docroot . '/.csslintrc', $is_link, 'Test version of .csslintrc from drupal/core.'); $this->assertScaffoldedFile($docroot . '/.editorconfig', $is_link, 'Test version of .editorconfig from drupal/core.'); $this->assertScaffoldedFile($docroot . '/.eslintignore', $is_link, 'Test version of .eslintignore from drupal/core.'); @@ -378,4 +380,27 @@ protected function assertCommonDrupalAssetsWereScaffolded($docroot, $is_link) { $this->assertScaffoldedFile($docroot . '/web.config', $is_link, 'Test version of web.config from drupal/core.'); } + /** + * Assert that the autoload file was scaffolded and contains correct path. + * + * @param string $docroot + * Location of the doc root, where autoload.php should be written. + * @param bool $relocated_docroot + * Whether the document root is relocated or now. + */ + protected function assertAutoloadFileCorrect($docroot, $relocated_docroot = FALSE) { + $autoload_path = $docroot . '/autoload.php'; + + // Ensure that the autoload.php file was written. + $this->assertFileExists($autoload_path); + $contents = file_get_contents($autoload_path); + + $expected = "return require __DIR__ . '/vendor/autoload.php';"; + if ($relocated_docroot) { + $expected = "return require __DIR__ . '/../vendor/autoload.php';"; + } + + $this->assertContains($expected, $contents); + } + }