3 namespace Drupal\Core\Composer;
5 use Drupal\Component\PhpStorage\FileStorage;
6 use Composer\Script\Event;
7 use Composer\Installer\PackageEvent;
8 use Composer\Semver\Constraint\Constraint;
9 use Composer\Util\ProcessExecutor;
12 * Provides static functions for composer script events.
14 * @see https://getcomposer.org/doc/articles/scripts.md
18 protected static $packageToCleanup = [
19 'behat/mink' => ['tests', 'driver-testsuite'],
20 'behat/mink-browserkit-driver' => ['tests'],
21 'behat/mink-goutte-driver' => ['tests'],
22 'drupal/coder' => ['coder_sniffer/Drupal/Test', 'coder_sniffer/DrupalPractice/Test'],
23 'doctrine/cache' => ['tests'],
24 'doctrine/collections' => ['tests'],
25 'doctrine/common' => ['tests'],
26 'doctrine/inflector' => ['tests'],
27 'doctrine/instantiator' => ['tests'],
28 'egulias/email-validator' => ['documentation', 'tests'],
29 'fabpot/goutte' => ['Goutte/Tests'],
30 'guzzlehttp/promises' => ['tests'],
31 'guzzlehttp/psr7' => ['tests'],
32 'jcalderonzumba/gastonjs' => ['docs', 'examples', 'tests'],
33 'jcalderonzumba/mink-phantomjs-driver' => ['tests'],
34 'masterminds/html5' => ['test'],
35 'mikey179/vfsStream' => ['src/test'],
36 'paragonie/random_compat' => ['tests'],
37 'phpdocumentor/reflection-docblock' => ['tests'],
38 'phpunit/php-code-coverage' => ['tests'],
39 'phpunit/php-timer' => ['tests'],
40 'phpunit/php-token-stream' => ['tests'],
41 'phpunit/phpunit' => ['tests'],
42 'phpunit/php-mock-objects' => ['tests'],
43 'sebastian/comparator' => ['tests'],
44 'sebastian/diff' => ['tests'],
45 'sebastian/environment' => ['tests'],
46 'sebastian/exporter' => ['tests'],
47 'sebastian/global-state' => ['tests'],
48 'sebastian/recursion-context' => ['tests'],
49 'stack/builder' => ['tests'],
50 'symfony/browser-kit' => ['Tests'],
51 'symfony/class-loader' => ['Tests'],
52 'symfony/console' => ['Tests'],
53 'symfony/css-selector' => ['Tests'],
54 'symfony/debug' => ['Tests'],
55 'symfony/dependency-injection' => ['Tests'],
56 'symfony/dom-crawler' => ['Tests'],
57 // @see \Drupal\Tests\Component\EventDispatcher\ContainerAwareEventDispatcherTest
58 // 'symfony/event-dispatcher' => ['Tests'],
59 'symfony/http-foundation' => ['Tests'],
60 'symfony/http-kernel' => ['Tests'],
61 'symfony/process' => ['Tests'],
62 'symfony/psr-http-message-bridge' => ['Tests'],
63 'symfony/routing' => ['Tests'],
64 'symfony/serializer' => ['Tests'],
65 'symfony/translation' => ['Tests'],
66 'symfony/validator' => ['Tests', 'Resources'],
67 'symfony/yaml' => ['Tests'],
68 'symfony-cmf/routing' => ['Test', 'Tests'],
69 'twig/twig' => ['doc', 'ext', 'test'],
73 * Add vendor classes to Composer's static classmap.
75 public static function preAutoloadDump(Event $event) {
76 // Get the configured vendor directory.
77 $vendor_dir = $event->getComposer()->getConfig()->get('vendor-dir');
79 // We need the root package so we can add our classmaps to its loader.
80 $package = $event->getComposer()->getPackage();
81 // We need the local repository so that we can query and see if it's likely
82 // that our files are present there.
83 $repository = $event->getComposer()->getRepositoryManager()->getLocalRepository();
84 // This is, essentially, a null constraint. We only care whether the package
85 // is present in the vendor directory yet, but findPackage() requires it.
86 $constraint = new Constraint('>', '');
87 // It's possible that there is no classmap specified in a custom project
88 // composer.json file. We need one so we can optimize lookup for some of our
90 $autoload = $package->getAutoload();
91 if (!isset($autoload['classmap'])) {
92 $autoload['classmap'] = [];
94 // Check for our packages, and then optimize them if they're present.
95 if ($repository->findPackage('symfony/http-foundation', $constraint)) {
96 $autoload['classmap'] = array_merge($autoload['classmap'], [
97 $vendor_dir . '/symfony/http-foundation/Request.php',
98 $vendor_dir . '/symfony/http-foundation/ParameterBag.php',
99 $vendor_dir . '/symfony/http-foundation/FileBag.php',
100 $vendor_dir . '/symfony/http-foundation/ServerBag.php',
101 $vendor_dir . '/symfony/http-foundation/HeaderBag.php',
104 if ($repository->findPackage('symfony/http-kernel', $constraint)) {
105 $autoload['classmap'] = array_merge($autoload['classmap'], [
106 $vendor_dir . '/symfony/http-kernel/HttpKernel.php',
107 $vendor_dir . '/symfony/http-kernel/HttpKernelInterface.php',
108 $vendor_dir . '/symfony/http-kernel/TerminableInterface.php',
111 $package->setAutoload($autoload);
115 * Ensures that .htaccess and web.config files are present in Composer root.
117 * @param \Composer\Script\Event $event
119 public static function ensureHtaccess(Event $event) {
121 // The current working directory for composer scripts is where you run
123 $vendor_dir = $event->getComposer()->getConfig()->get('vendor-dir');
125 // Prevent access to vendor directory on Apache servers.
126 $htaccess_file = $vendor_dir . '/.htaccess';
127 if (!file_exists($htaccess_file)) {
128 file_put_contents($htaccess_file, FileStorage::htaccessLines(TRUE) . "\n");
131 // Prevent access to vendor directory on IIS servers.
132 $webconfig_file = $vendor_dir . '/web.config';
133 if (!file_exists($webconfig_file)) {
143 file_put_contents($webconfig_file, $lines . "\n");
148 * Fires the drupal-phpunit-upgrade script event if necessary.
150 * @param \Composer\Script\Event $event
152 public static function upgradePHPUnit(Event $event) {
153 $repository = $event->getComposer()->getRepositoryManager()->getLocalRepository();
154 // This is, essentially, a null constraint. We only care whether the package
155 // is present in the vendor directory yet, but findPackage() requires it.
156 $constraint = new Constraint('>', '');
157 $phpunit_package = $repository->findPackage('phpunit/phpunit', $constraint);
158 if (!$phpunit_package) {
159 // There is nothing to do. The user is probably installing using the
164 // If the PHP version is 7.0 or above and PHPUnit is less than version 6
165 // call the drupal-phpunit-upgrade script to upgrade PHPUnit.
166 if (!static::upgradePHPUnitCheck($phpunit_package->getVersion())) {
167 $event->getComposer()
168 ->getEventDispatcher()
169 ->dispatchScript('drupal-phpunit-upgrade');
174 * Determines if PHPUnit needs to be upgraded.
176 * This method is located in this file because it is possible that it is
177 * called before the autoloader is available.
179 * @param string $phpunit_version
180 * The PHPUnit version string.
183 * TRUE if the PHPUnit needs to be upgraded, FALSE if not.
185 public static function upgradePHPUnitCheck($phpunit_version) {
186 return !(version_compare(PHP_MAJOR_VERSION . '.' . PHP_MINOR_VERSION, '7.0') >= 0 && version_compare($phpunit_version, '6.1') < 0);
190 * Remove possibly problematic test files from vendored projects.
192 * @param \Composer\Installer\PackageEvent $event
193 * A PackageEvent object to get the configured composer vendor directories
196 public static function vendorTestCodeCleanup(PackageEvent $event) {
197 $vendor_dir = $event->getComposer()->getConfig()->get('vendor-dir');
198 $io = $event->getIO();
199 $op = $event->getOperation();
200 if ($op->getJobType() == 'update') {
201 $package = $op->getTargetPackage();
204 $package = $op->getPackage();
206 $package_key = static::findPackageKey($package->getName());
207 $message = sprintf(" Processing <comment>%s</comment>", $package->getPrettyName());
208 if ($io->isVeryVerbose()) {
209 $io->write($message);
212 foreach (static::$packageToCleanup[$package_key] as $path) {
213 $dir_to_remove = $vendor_dir . '/' . $package_key . '/' . $path;
214 $print_message = $io->isVeryVerbose();
215 if (is_dir($dir_to_remove)) {
216 if (static::deleteRecursive($dir_to_remove)) {
217 $message = sprintf(" <info>Removing directory '%s'</info>", $path);
220 // Always display a message if this fails as it means something has
221 // gone wrong. Therefore the message has to include the package name
222 // as the first informational message might not exist.
223 $print_message = TRUE;
224 $message = sprintf(" <error>Failure removing directory '%s'</error> in package <comment>%s</comment>.", $path, $package->getPrettyName());
228 // If the package has changed or the --prefer-dist version does not
229 // include the directory this is not an error.
230 $message = sprintf(" Directory '%s' does not exist", $path);
232 if ($print_message) {
233 $io->write($message);
237 if ($io->isVeryVerbose()) {
238 // Add a new line to separate this output from the next package.
245 * Find the array key for a given package name with a case-insensitive search.
247 * @param string $package_name
248 * The package name from composer. This is always already lower case.
250 * @return string|null
251 * The string key, or NULL if none was found.
253 protected static function findPackageKey($package_name) {
255 // In most cases the package name is already used as the array key.
256 if (isset(static::$packageToCleanup[$package_name])) {
257 $package_key = $package_name;
260 // Handle any mismatch in case between the package name and array key.
261 // For example, the array key 'mikey179/vfsStream' needs to be found
262 // when composer returns a package name of 'mikey179/vfsstream'.
263 foreach (static::$packageToCleanup as $key => $dirs) {
264 if (strtolower($key) === $package_name) {
274 * Removes Composer's timeout so that scripts can run indefinitely.
276 public static function removeTimeout() {
277 ProcessExecutor::setTimeout(0);
281 * Helper method to remove directories and the files they contain.
283 * @param string $path
284 * The directory or file to remove. It must exist.
287 * TRUE on success or FALSE on failure.
289 protected static function deleteRecursive($path) {
290 if (is_file($path) || is_link($path)) {
291 return unlink($path);
295 while (($entry = $dir->read()) !== FALSE) {
296 if ($entry == '.' || $entry == '..') {
299 $entry_path = $path . '/' . $entry;
300 $success = static::deleteRecursive($entry_path) && $success;
304 return rmdir($path) && $success;