use Composer\Script\Event;
use Composer\Installer\PackageEvent;
use Composer\Semver\Constraint\Constraint;
+use Composer\Util\ProcessExecutor;
/**
* Provides static functions for composer script events.
* Add vendor classes to Composer's static classmap.
*/
public static function preAutoloadDump(Event $event) {
+ // Get the configured vendor directory.
+ $vendor_dir = $event->getComposer()->getConfig()->get('vendor-dir');
+
// We need the root package so we can add our classmaps to its loader.
$package = $event->getComposer()->getPackage();
// We need the local repository so that we can query and see if it's likely
// that our files are present there.
$repository = $event->getComposer()->getRepositoryManager()->getLocalRepository();
// This is, essentially, a null constraint. We only care whether the package
- // is present in vendor/ yet, but findPackage() requires it.
+ // is present in the vendor directory yet, but findPackage() requires it.
$constraint = new Constraint('>', '');
+ // It's possible that there is no classmap specified in a custom project
+ // composer.json file. We need one so we can optimize lookup for some of our
+ // dependencies.
+ $autoload = $package->getAutoload();
+ if (!isset($autoload['classmap'])) {
+ $autoload['classmap'] = [];
+ }
// Check for our packages, and then optimize them if they're present.
if ($repository->findPackage('symfony/http-foundation', $constraint)) {
- $autoload = $package->getAutoload();
$autoload['classmap'] = array_merge($autoload['classmap'], [
- 'vendor/symfony/http-foundation/Request.php',
- 'vendor/symfony/http-foundation/ParameterBag.php',
- 'vendor/symfony/http-foundation/FileBag.php',
- 'vendor/symfony/http-foundation/ServerBag.php',
- 'vendor/symfony/http-foundation/HeaderBag.php',
+ $vendor_dir . '/symfony/http-foundation/Request.php',
+ $vendor_dir . '/symfony/http-foundation/ParameterBag.php',
+ $vendor_dir . '/symfony/http-foundation/FileBag.php',
+ $vendor_dir . '/symfony/http-foundation/ServerBag.php',
+ $vendor_dir . '/symfony/http-foundation/HeaderBag.php',
]);
- $package->setAutoload($autoload);
}
if ($repository->findPackage('symfony/http-kernel', $constraint)) {
- $autoload = $package->getAutoload();
$autoload['classmap'] = array_merge($autoload['classmap'], [
- 'vendor/symfony/http-kernel/HttpKernel.php',
- 'vendor/symfony/http-kernel/HttpKernelInterface.php',
- 'vendor/symfony/http-kernel/TerminableInterface.php',
+ $vendor_dir . '/symfony/http-kernel/HttpKernel.php',
+ $vendor_dir . '/symfony/http-kernel/HttpKernelInterface.php',
+ $vendor_dir . '/symfony/http-kernel/TerminableInterface.php',
]);
- $package->setAutoload($autoload);
}
+ $package->setAutoload($autoload);
}
/**
}
}
+ /**
+ * Fires the drupal-phpunit-upgrade script event if necessary.
+ *
+ * @param \Composer\Script\Event $event
+ */
+ public static function upgradePHPUnit(Event $event) {
+ $repository = $event->getComposer()->getRepositoryManager()->getLocalRepository();
+ // This is, essentially, a null constraint. We only care whether the package
+ // is present in the vendor directory yet, but findPackage() requires it.
+ $constraint = new Constraint('>', '');
+ $phpunit_package = $repository->findPackage('phpunit/phpunit', $constraint);
+ if (!$phpunit_package) {
+ // There is nothing to do. The user is probably installing using the
+ // --no-dev flag.
+ return;
+ }
+
+ // If the PHP version is 7.0 or above and PHPUnit is less than version 6
+ // call the drupal-phpunit-upgrade script to upgrade PHPUnit.
+ if (!static::upgradePHPUnitCheck($phpunit_package->getVersion())) {
+ $event->getComposer()
+ ->getEventDispatcher()
+ ->dispatchScript('drupal-phpunit-upgrade');
+ }
+ }
+
+ /**
+ * Determines if PHPUnit needs to be upgraded.
+ *
+ * This method is located in this file because it is possible that it is
+ * called before the autoloader is available.
+ *
+ * @param string $phpunit_version
+ * The PHPUnit version string.
+ *
+ * @return bool
+ * TRUE if the PHPUnit needs to be upgraded, FALSE if not.
+ */
+ public static function upgradePHPUnitCheck($phpunit_version) {
+ return !(version_compare(PHP_MAJOR_VERSION . '.' . PHP_MINOR_VERSION, '7.0') >= 0 && version_compare($phpunit_version, '6.1') < 0);
+ }
+
/**
* Remove possibly problematic test files from vendored projects.
*
return $package_key;
}
+ /**
+ * Removes Composer's timeout so that scripts can run indefinitely.
+ */
+ public static function removeTimeout() {
+ ProcessExecutor::setTimeout(0);
+ }
+
/**
* Helper method to remove directories and the files they contain.
*