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