View source
<?php
namespace Drupal\Core\Composer;
use Composer\Installer\PackageEvent;
use Composer\Script\Event;
use Composer\Semver\Constraint\Constraint;
use Composer\Util\ProcessExecutor;
use Drupal\Component\FileSecurity\FileSecurity;
class Composer {
protected static $packageToCleanup = [
'behat/mink' => [
'tests',
'driver-testsuite',
],
'behat/mink-browserkit-driver' => [
'tests',
],
'behat/mink-goutte-driver' => [
'tests',
],
'behat/mink-selenium2-driver' => [
'tests',
],
'brumann/polyfill-unserialize' => [
'tests',
],
'composer/composer' => [
'bin',
],
'drupal/coder' => [
'coder_sniffer/Drupal/Test',
'coder_sniffer/DrupalPractice/Test',
],
'doctrine/cache' => [
'tests',
],
'doctrine/collections' => [
'tests',
],
'doctrine/common' => [
'tests',
],
'doctrine/inflector' => [
'tests',
],
'doctrine/instantiator' => [
'tests',
],
'easyrdf/easyrdf' => [
'scripts',
],
'egulias/email-validator' => [
'documentation',
'tests',
],
'fabpot/goutte' => [
'Goutte/Tests',
],
'guzzlehttp/promises' => [
'tests',
],
'guzzlehttp/psr7' => [
'tests',
],
'instaclick/php-webdriver' => [
'doc',
'test',
],
'jcalderonzumba/gastonjs' => [
'docs',
'examples',
'tests',
],
'jcalderonzumba/mink-phantomjs-driver' => [
'tests',
],
'justinrainbow/json-schema' => [
'demo',
],
'laminas/laminas-escaper' => [
'doc',
],
'laminas/laminas-feed' => [
'doc',
],
'laminas/laminas-stdlib' => [
'doc',
],
'masterminds/html5' => [
'bin',
'test',
],
'mikey179/vfsStream' => [
'src/test',
],
'myclabs/deep-copy' => [
'doc',
],
'paragonie/random_compat' => [
'tests',
],
'pear/archive_tar' => [
'docs',
'tests',
],
'pear/console_getopt' => [
'tests',
],
'pear/pear-core-minimal' => [
'tests',
],
'pear/pear_exception' => [
'tests',
],
'phar-io/manifest' => [
'examples',
'tests',
],
'phar-io/version' => [
'tests',
],
'phpdocumentor/reflection-docblock' => [
'tests',
],
'phpspec/prophecy' => [
'fixtures',
'spec',
'tests',
],
'phpunit/php-code-coverage' => [
'tests',
],
'phpunit/php-timer' => [
'tests',
],
'phpunit/php-token-stream' => [
'tests',
],
'phpunit/phpunit' => [
'tests',
],
'phpunit/phpunit-mock-objects' => [
'tests',
],
'sebastian/code-unit-reverse-lookup' => [
'tests',
],
'sebastian/comparator' => [
'tests',
],
'sebastian/diff' => [
'tests',
],
'sebastian/environment' => [
'tests',
],
'sebastian/exporter' => [
'tests',
],
'sebastian/global-state' => [
'tests',
],
'sebastian/object-enumerator' => [
'tests',
],
'sebastian/object-reflector' => [
'tests',
],
'sebastian/recursion-context' => [
'tests',
],
'seld/jsonlint' => [
'tests',
],
'squizlabs/php_codesniffer' => [
'tests',
],
'stack/builder' => [
'tests',
],
'symfony/browser-kit' => [
'Tests',
],
'symfony/class-loader' => [
'Tests',
],
'symfony/console' => [
'Tests',
],
'symfony/css-selector' => [
'Tests',
],
'symfony/debug' => [
'Tests',
],
'symfony/dependency-injection' => [
'Tests',
],
'symfony/dom-crawler' => [
'Tests',
],
'symfony/filesystem' => [
'Tests',
],
'symfony/finder' => [
'Tests',
],
'symfony/event-dispatcher' => [
'Tests',
],
'symfony/http-foundation' => [
'Tests',
],
'symfony/http-kernel' => [
'Tests',
],
'symfony/phpunit-bridge' => [
'Tests',
],
'symfony/process' => [
'Tests',
],
'symfony/psr-http-message-bridge' => [
'Tests',
],
'symfony/routing' => [
'Tests',
],
'symfony/serializer' => [
'Tests',
],
'symfony/translation' => [
'Tests',
],
'symfony/validator' => [
'Tests',
'Resources',
],
'symfony/yaml' => [
'Tests',
],
'symfony-cmf/routing' => [
'Test',
'Tests',
],
'theseer/tokenizer' => [
'tests',
],
'twig/twig' => [
'doc',
'ext',
'test',
'tests',
],
];
public static function preAutoloadDump(Event $event) {
$vendor_dir = $event
->getComposer()
->getConfig()
->get('vendor-dir');
$package = $event
->getComposer()
->getPackage();
$repository = $event
->getComposer()
->getRepositoryManager()
->getLocalRepository();
$constraint = new Constraint('>', '');
$autoload = $package
->getAutoload();
if (!isset($autoload['classmap'])) {
$autoload['classmap'] = [];
}
if ($repository
->findPackage('symfony/http-foundation', $constraint)) {
$autoload['classmap'] = array_merge($autoload['classmap'], [
$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',
]);
}
if ($repository
->findPackage('symfony/http-kernel', $constraint)) {
$autoload['classmap'] = array_merge($autoload['classmap'], [
$vendor_dir . '/symfony/http-kernel/HttpKernel.php',
$vendor_dir . '/symfony/http-kernel/HttpKernelInterface.php',
$vendor_dir . '/symfony/http-kernel/TerminableInterface.php',
]);
}
$package
->setAutoload($autoload);
}
public static function ensureHtaccess(Event $event) {
$vendor_dir = $event
->getComposer()
->getConfig()
->get('vendor-dir');
FileSecurity::writeHtaccess($vendor_dir);
FileSecurity::writeWebConfig($vendor_dir);
}
public static function vendorTestCodeCleanup(PackageEvent $event) {
$vendor_dir = $event
->getComposer()
->getConfig()
->get('vendor-dir');
$io = $event
->getIO();
$op = $event
->getOperation();
if ($op
->getJobType() == 'update') {
$package = $op
->getTargetPackage();
}
else {
$package = $op
->getPackage();
}
$package_key = static::findPackageKey($package
->getName());
$message = sprintf(" Processing <comment>%s</comment>", $package
->getPrettyName());
if ($io
->isVeryVerbose()) {
$io
->write($message);
}
if ($package_key) {
foreach (static::$packageToCleanup[$package_key] as $path) {
$dir_to_remove = $vendor_dir . '/' . $package_key . '/' . $path;
$print_message = $io
->isVeryVerbose();
if (is_dir($dir_to_remove)) {
if (static::deleteRecursive($dir_to_remove)) {
$message = sprintf(" <info>Removing directory '%s'</info>", $path);
}
else {
$print_message = TRUE;
$message = sprintf(" <error>Failure removing directory '%s'</error> in package <comment>%s</comment>.", $path, $package
->getPrettyName());
}
}
else {
$message = sprintf(" Directory '%s' does not exist", $path);
}
if ($print_message) {
$io
->write($message);
}
}
if ($io
->isVeryVerbose()) {
$io
->write("");
}
}
}
protected static function findPackageKey($package_name) {
$package_key = NULL;
if (isset(static::$packageToCleanup[$package_name])) {
$package_key = $package_name;
}
else {
foreach (static::$packageToCleanup as $key => $dirs) {
if (strtolower($key) === $package_name) {
$package_key = $key;
break;
}
}
}
return $package_key;
}
public static function removeTimeout() {
ProcessExecutor::setTimeout(0);
}
protected static function deleteRecursive($path) {
if (is_file($path) || is_link($path)) {
return unlink($path);
}
$success = TRUE;
$dir = dir($path);
while (($entry = $dir
->read()) !== FALSE) {
if ($entry == '.' || $entry == '..') {
continue;
}
$entry_path = $path . '/' . $entry;
$success = static::deleteRecursive($entry_path) && $success;
}
$dir
->close();
return rmdir($path) && $success;
}
public static function upgradePHPUnit(Event $event) {
$repository = $event
->getComposer()
->getRepositoryManager()
->getLocalRepository();
$constraint = new Constraint('>', '');
$phpunit_package = $repository
->findPackage('phpunit/phpunit', $constraint);
if (!$phpunit_package) {
return;
}
if (!static::upgradePHPUnitCheck($phpunit_package
->getVersion())) {
$event
->getComposer()
->getEventDispatcher()
->dispatchScript('drupal-phpunit-upgrade');
}
}
public static function upgradePHPUnitCheck($phpunit_version) {
return !(version_compare(PHP_MAJOR_VERSION . '.' . PHP_MINOR_VERSION, '7.3') >= 0 && version_compare($phpunit_version, '7.0') < 0);
}
}