9833b7ede1964211f00fead94d29574c5601041a
[yaffs-website] / web / core / lib / Drupal / Core / Composer / Composer.php
1 <?php
2
3 namespace Drupal\Core\Composer;
4
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;
10
11 /**
12  * Provides static functions for composer script events.
13  *
14  * @see https://getcomposer.org/doc/articles/scripts.md
15  */
16 class Composer {
17
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'],
70   ];
71
72   /**
73    * Add vendor classes to Composer's static classmap.
74    */
75   public static function preAutoloadDump(Event $event) {
76     // Get the configured vendor directory.
77     $vendor_dir = $event->getComposer()->getConfig()->get('vendor-dir');
78
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
89     // dependencies.
90     $autoload = $package->getAutoload();
91     if (!isset($autoload['classmap'])) {
92       $autoload['classmap'] = [];
93     }
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',
102       ]);
103     }
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',
109       ]);
110     }
111     $package->setAutoload($autoload);
112   }
113
114   /**
115    * Ensures that .htaccess and web.config files are present in Composer root.
116    *
117    * @param \Composer\Script\Event $event
118    */
119   public static function ensureHtaccess(Event $event) {
120
121     // The current working directory for composer scripts is where you run
122     // composer from.
123     $vendor_dir = $event->getComposer()->getConfig()->get('vendor-dir');
124
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");
129     }
130
131     // Prevent access to vendor directory on IIS servers.
132     $webconfig_file = $vendor_dir . '/web.config';
133     if (!file_exists($webconfig_file)) {
134       $lines = <<<EOT
135 <configuration>
136   <system.webServer>
137     <authorization>
138       <deny users="*">
139     </authorization>
140   </system.webServer>
141 </configuration>
142 EOT;
143       file_put_contents($webconfig_file, $lines . "\n");
144     }
145   }
146
147   /**
148    * Fires the drupal-phpunit-upgrade script event if necessary.
149    *
150    * @param \Composer\Script\Event $event
151    */
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
160       // --no-dev flag.
161       return;
162     }
163
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');
170     }
171   }
172
173   /**
174    * Determines if PHPUnit needs to be upgraded.
175    *
176    * This method is located in this file because it is possible that it is
177    * called before the autoloader is available.
178    *
179    * @param string $phpunit_version
180    *   The PHPUnit version string.
181    *
182    * @return bool
183    *   TRUE if the PHPUnit needs to be upgraded, FALSE if not.
184    */
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);
187   }
188
189   /**
190    * Remove possibly problematic test files from vendored projects.
191    *
192    * @param \Composer\Installer\PackageEvent $event
193    *   A PackageEvent object to get the configured composer vendor directories
194    *   from.
195    */
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();
202     }
203     else {
204       $package = $op->getPackage();
205     }
206     $package_key = static::findPackageKey($package->getName());
207     $message = sprintf("    Processing <comment>%s</comment>", $package->getPrettyName());
208     if ($io->isVeryVerbose()) {
209       $io->write($message);
210     }
211     if ($package_key) {
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);
218           }
219           else {
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());
225           }
226         }
227         else {
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);
231         }
232         if ($print_message) {
233           $io->write($message);
234         }
235       }
236
237       if ($io->isVeryVerbose()) {
238         // Add a new line to separate this output from the next package.
239         $io->write("");
240       }
241     }
242   }
243
244   /**
245    * Find the array key for a given package name with a case-insensitive search.
246    *
247    * @param string $package_name
248    *   The package name from composer. This is always already lower case.
249    *
250    * @return string|null
251    *   The string key, or NULL if none was found.
252    */
253   protected static function findPackageKey($package_name) {
254     $package_key = NULL;
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;
258     }
259     else {
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) {
265           $package_key = $key;
266           break;
267         }
268       }
269     }
270     return $package_key;
271   }
272
273   /**
274    * Removes Composer's timeout so that scripts can run indefinitely.
275    */
276   public static function removeTimeout() {
277     ProcessExecutor::setTimeout(0);
278   }
279
280   /**
281    * Helper method to remove directories and the files they contain.
282    *
283    * @param string $path
284    *   The directory or file to remove. It must exist.
285    *
286    * @return bool
287    *   TRUE on success or FALSE on failure.
288    */
289   protected static function deleteRecursive($path) {
290     if (is_file($path) || is_link($path)) {
291       return unlink($path);
292     }
293     $success = TRUE;
294     $dir = dir($path);
295     while (($entry = $dir->read()) !== FALSE) {
296       if ($entry == '.' || $entry == '..') {
297         continue;
298       }
299       $entry_path = $path . '/' . $entry;
300       $success = static::deleteRecursive($entry_path) && $success;
301     }
302     $dir->close();
303
304     return rmdir($path) && $success;
305   }
306
307 }