Skip to content

Commit

Permalink
Autoloader: display an error message if a non-default vendor director…
Browse files Browse the repository at this point in the history
…y is used (#15364)

The autoloader package only supports the default composer vendor directory. If
a consuming plugin uses a non-default vendor directory, generate an error during
composer dump-autoload.
  • Loading branch information
kbrown9 committed May 22, 2020
1 parent a209e15 commit 7e9bc52
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
2 changes: 2 additions & 0 deletions packages/autoloader/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,5 @@ Current Limitations
-----

We currently only support packages that autoload via psr-4 definition in their package.

Your project must use the default composer vendor directory, `vendor`.
12 changes: 10 additions & 2 deletions packages/autoloader/src/CustomAutoloaderPlugin.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?php
<?php //phpcs:ignore WordPress.Files.FileName.NotHyphenatedLowercase
/**
* Custom Autoloader Composer Plugin, hooks into composer events to generate the custom autoloader.
*
Expand Down Expand Up @@ -71,11 +71,19 @@ public static function getSubscribedEvents() {
*/
public function postAutoloadDump( Event $event ) {

$config = $this->composer->getConfig();

if ( 'vendor' !== $config->raw()['config']['vendor-dir'] ) {
$this->io->writeError( "\n<error>An error occurred while generating the autoloader files:", true );
$this->io->writeError( 'The project\'s composer.json or composer environment set a non-default vendor directory.', true );
$this->io->writeError( 'The default composer vendor directory must be used.</error>', true );
exit();
}

$installationManager = $this->composer->getInstallationManager();
$repoManager = $this->composer->getRepositoryManager();
$localRepo = $repoManager->getLocalRepository();
$package = $this->composer->getPackage();
$config = $this->composer->getConfig();
$optimize = true;
$suffix = $config->get( 'autoloader-suffix' )
? $config->get( 'autoloader-suffix' )
Expand Down

0 comments on commit 7e9bc52

Please sign in to comment.