Composer.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. <?php
  2. namespace Drupal\Core\Composer;
  3. use Composer\Installer\PackageEvent;
  4. use Composer\Script\Event;
  5. use Composer\Semver\Constraint\Constraint;
  6. use Composer\Util\ProcessExecutor;
  7. use Drupal\Component\FileSecurity\FileSecurity;
  8. /**
  9. * Provides static functions for composer script events.
  10. *
  11. * @see https://getcomposer.org/doc/articles/scripts.md
  12. */
  13. class Composer {
  14. protected static $packageToCleanup = [
  15. 'behat/mink' => ['tests', 'driver-testsuite'],
  16. 'behat/mink-browserkit-driver' => ['tests'],
  17. 'behat/mink-goutte-driver' => ['tests'],
  18. 'behat/mink-selenium2-driver' => ['tests'],
  19. 'brumann/polyfill-unserialize' => ['tests'],
  20. 'composer/composer' => ['bin'],
  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. 'easyrdf/easyrdf' => ['scripts'],
  28. 'egulias/email-validator' => ['documentation', 'tests'],
  29. 'fabpot/goutte' => ['Goutte/Tests'],
  30. 'guzzlehttp/promises' => ['tests'],
  31. 'guzzlehttp/psr7' => ['tests'],
  32. 'instaclick/php-webdriver' => ['doc', 'test'],
  33. 'jcalderonzumba/gastonjs' => ['docs', 'examples', 'tests'],
  34. 'jcalderonzumba/mink-phantomjs-driver' => ['tests'],
  35. 'justinrainbow/json-schema' => ['demo'],
  36. 'laminas/laminas-escaper' => ['doc'],
  37. 'laminas/laminas-feed' => ['doc'],
  38. 'laminas/laminas-stdlib' => ['doc'],
  39. 'masterminds/html5' => ['bin', 'test'],
  40. 'mikey179/vfsStream' => ['src/test'],
  41. 'myclabs/deep-copy' => ['doc'],
  42. 'paragonie/random_compat' => ['tests'],
  43. 'pear/archive_tar' => ['docs', 'tests'],
  44. 'pear/console_getopt' => ['tests'],
  45. 'pear/pear-core-minimal' => ['tests'],
  46. 'pear/pear_exception' => ['tests'],
  47. 'phar-io/manifest' => ['examples', 'tests'],
  48. 'phar-io/version' => ['tests'],
  49. 'phpdocumentor/reflection-docblock' => ['tests'],
  50. 'phpspec/prophecy' => ['fixtures', 'spec', 'tests'],
  51. 'phpunit/php-code-coverage' => ['tests'],
  52. 'phpunit/php-timer' => ['tests'],
  53. 'phpunit/php-token-stream' => ['tests'],
  54. 'phpunit/phpunit' => ['tests'],
  55. 'phpunit/phpunit-mock-objects' => ['tests'],
  56. 'sebastian/code-unit-reverse-lookup' => ['tests'],
  57. 'sebastian/comparator' => ['tests'],
  58. 'sebastian/diff' => ['tests'],
  59. 'sebastian/environment' => ['tests'],
  60. 'sebastian/exporter' => ['tests'],
  61. 'sebastian/global-state' => ['tests'],
  62. 'sebastian/object-enumerator' => ['tests'],
  63. 'sebastian/object-reflector' => ['tests'],
  64. 'sebastian/recursion-context' => ['tests'],
  65. 'seld/jsonlint' => ['tests'],
  66. 'squizlabs/php_codesniffer' => ['tests'],
  67. 'stack/builder' => ['tests'],
  68. 'symfony/browser-kit' => ['Tests'],
  69. 'symfony/class-loader' => ['Tests'],
  70. 'symfony/console' => ['Tests'],
  71. 'symfony/css-selector' => ['Tests'],
  72. 'symfony/debug' => ['Tests'],
  73. 'symfony/dependency-injection' => ['Tests'],
  74. 'symfony/dom-crawler' => ['Tests'],
  75. 'symfony/filesystem' => ['Tests'],
  76. 'symfony/finder' => ['Tests'],
  77. 'symfony/event-dispatcher' => ['Tests'],
  78. 'symfony/http-foundation' => ['Tests'],
  79. 'symfony/http-kernel' => ['Tests'],
  80. 'symfony/phpunit-bridge' => ['Tests'],
  81. 'symfony/process' => ['Tests'],
  82. 'symfony/psr-http-message-bridge' => ['Tests'],
  83. 'symfony/routing' => ['Tests'],
  84. 'symfony/serializer' => ['Tests'],
  85. 'symfony/translation' => ['Tests'],
  86. 'symfony/validator' => ['Tests', 'Resources'],
  87. 'symfony/yaml' => ['Tests'],
  88. 'symfony-cmf/routing' => ['Test', 'Tests'],
  89. 'theseer/tokenizer' => ['tests'],
  90. 'twig/twig' => ['doc', 'ext', 'test', 'tests'],
  91. ];
  92. /**
  93. * Add vendor classes to Composer's static classmap.
  94. *
  95. * @param \Composer\Script\Event $event
  96. */
  97. public static function preAutoloadDump(Event $event) {
  98. // Get the configured vendor directory.
  99. $vendor_dir = $event->getComposer()->getConfig()->get('vendor-dir');
  100. // We need the root package so we can add our classmaps to its loader.
  101. $package = $event->getComposer()->getPackage();
  102. // We need the local repository so that we can query and see if it's likely
  103. // that our files are present there.
  104. $repository = $event->getComposer()->getRepositoryManager()->getLocalRepository();
  105. // This is, essentially, a null constraint. We only care whether the package
  106. // is present in the vendor directory yet, but findPackage() requires it.
  107. $constraint = new Constraint('>', '');
  108. // It's possible that there is no classmap specified in a custom project
  109. // composer.json file. We need one so we can optimize lookup for some of our
  110. // dependencies.
  111. $autoload = $package->getAutoload();
  112. if (!isset($autoload['classmap'])) {
  113. $autoload['classmap'] = [];
  114. }
  115. // Check for our packages, and then optimize them if they're present.
  116. if ($repository->findPackage('symfony/http-foundation', $constraint)) {
  117. $autoload['classmap'] = array_merge($autoload['classmap'], [
  118. $vendor_dir . '/symfony/http-foundation/Request.php',
  119. $vendor_dir . '/symfony/http-foundation/ParameterBag.php',
  120. $vendor_dir . '/symfony/http-foundation/FileBag.php',
  121. $vendor_dir . '/symfony/http-foundation/ServerBag.php',
  122. $vendor_dir . '/symfony/http-foundation/HeaderBag.php',
  123. ]);
  124. }
  125. if ($repository->findPackage('symfony/http-kernel', $constraint)) {
  126. $autoload['classmap'] = array_merge($autoload['classmap'], [
  127. $vendor_dir . '/symfony/http-kernel/HttpKernel.php',
  128. $vendor_dir . '/symfony/http-kernel/HttpKernelInterface.php',
  129. $vendor_dir . '/symfony/http-kernel/TerminableInterface.php',
  130. ]);
  131. }
  132. $package->setAutoload($autoload);
  133. }
  134. /**
  135. * Ensures that .htaccess and web.config files are present in Composer root.
  136. *
  137. * @param \Composer\Script\Event $event
  138. */
  139. public static function ensureHtaccess(Event $event) {
  140. // The current working directory for composer scripts is where you run
  141. // composer from.
  142. $vendor_dir = $event->getComposer()->getConfig()->get('vendor-dir');
  143. // Prevent access to vendor directory on Apache servers.
  144. FileSecurity::writeHtaccess($vendor_dir);
  145. // Prevent access to vendor directory on IIS servers.
  146. FileSecurity::writeWebConfig($vendor_dir);
  147. }
  148. /**
  149. * Remove possibly problematic test files from vendored projects.
  150. *
  151. * @param \Composer\Installer\PackageEvent $event
  152. * A PackageEvent object to get the configured composer vendor directories
  153. * from.
  154. */
  155. public static function vendorTestCodeCleanup(PackageEvent $event) {
  156. $vendor_dir = $event->getComposer()->getConfig()->get('vendor-dir');
  157. $io = $event->getIO();
  158. $op = $event->getOperation();
  159. if ($op->getJobType() == 'update') {
  160. $package = $op->getTargetPackage();
  161. }
  162. else {
  163. $package = $op->getPackage();
  164. }
  165. $package_key = static::findPackageKey($package->getName());
  166. $message = sprintf(" Processing <comment>%s</comment>", $package->getPrettyName());
  167. if ($io->isVeryVerbose()) {
  168. $io->write($message);
  169. }
  170. if ($package_key) {
  171. foreach (static::$packageToCleanup[$package_key] as $path) {
  172. $dir_to_remove = $vendor_dir . '/' . $package_key . '/' . $path;
  173. $print_message = $io->isVeryVerbose();
  174. if (is_dir($dir_to_remove)) {
  175. if (static::deleteRecursive($dir_to_remove)) {
  176. $message = sprintf(" <info>Removing directory '%s'</info>", $path);
  177. }
  178. else {
  179. // Always display a message if this fails as it means something has
  180. // gone wrong. Therefore the message has to include the package name
  181. // as the first informational message might not exist.
  182. $print_message = TRUE;
  183. $message = sprintf(" <error>Failure removing directory '%s'</error> in package <comment>%s</comment>.", $path, $package->getPrettyName());
  184. }
  185. }
  186. else {
  187. // If the package has changed or the --prefer-dist version does not
  188. // include the directory this is not an error.
  189. $message = sprintf(" Directory '%s' does not exist", $path);
  190. }
  191. if ($print_message) {
  192. $io->write($message);
  193. }
  194. }
  195. if ($io->isVeryVerbose()) {
  196. // Add a new line to separate this output from the next package.
  197. $io->write("");
  198. }
  199. }
  200. }
  201. /**
  202. * Find the array key for a given package name with a case-insensitive search.
  203. *
  204. * @param string $package_name
  205. * The package name from composer. This is always already lower case.
  206. *
  207. * @return string|null
  208. * The string key, or NULL if none was found.
  209. */
  210. protected static function findPackageKey($package_name) {
  211. $package_key = NULL;
  212. // In most cases the package name is already used as the array key.
  213. if (isset(static::$packageToCleanup[$package_name])) {
  214. $package_key = $package_name;
  215. }
  216. else {
  217. // Handle any mismatch in case between the package name and array key.
  218. // For example, the array key 'mikey179/vfsStream' needs to be found
  219. // when composer returns a package name of 'mikey179/vfsstream'.
  220. foreach (static::$packageToCleanup as $key => $dirs) {
  221. if (strtolower($key) === $package_name) {
  222. $package_key = $key;
  223. break;
  224. }
  225. }
  226. }
  227. return $package_key;
  228. }
  229. /**
  230. * Removes Composer's timeout so that scripts can run indefinitely.
  231. */
  232. public static function removeTimeout() {
  233. ProcessExecutor::setTimeout(0);
  234. }
  235. /**
  236. * Helper method to remove directories and the files they contain.
  237. *
  238. * @param string $path
  239. * The directory or file to remove. It must exist.
  240. *
  241. * @return bool
  242. * TRUE on success or FALSE on failure.
  243. */
  244. protected static function deleteRecursive($path) {
  245. if (is_file($path) || is_link($path)) {
  246. return unlink($path);
  247. }
  248. $success = TRUE;
  249. $dir = dir($path);
  250. while (($entry = $dir->read()) !== FALSE) {
  251. if ($entry == '.' || $entry == '..') {
  252. continue;
  253. }
  254. $entry_path = $path . '/' . $entry;
  255. $success = static::deleteRecursive($entry_path) && $success;
  256. }
  257. $dir->close();
  258. return rmdir($path) && $success;
  259. }
  260. /**
  261. * Fires the drupal-phpunit-upgrade script event if necessary.
  262. *
  263. * @param \Composer\Script\Event $event
  264. */
  265. public static function upgradePHPUnit(Event $event) {
  266. $repository = $event->getComposer()->getRepositoryManager()->getLocalRepository();
  267. // This is, essentially, a null constraint. We only care whether the package
  268. // is present in the vendor directory yet, but findPackage() requires it.
  269. $constraint = new Constraint('>', '');
  270. $phpunit_package = $repository->findPackage('phpunit/phpunit', $constraint);
  271. if (!$phpunit_package) {
  272. // There is nothing to do. The user is probably installing using the
  273. // --no-dev flag.
  274. return;
  275. }
  276. // If the PHP version is 7.3 or above and PHPUnit is less than version 7
  277. // call the drupal-phpunit-upgrade script to upgrade PHPUnit.
  278. if (!static::upgradePHPUnitCheck($phpunit_package->getVersion())) {
  279. $event->getComposer()
  280. ->getEventDispatcher()
  281. ->dispatchScript('drupal-phpunit-upgrade');
  282. }
  283. }
  284. /**
  285. * Determines if PHPUnit needs to be upgraded.
  286. *
  287. * This method is located in this file because it is possible that it is
  288. * called before the autoloader is available.
  289. *
  290. * @param string $phpunit_version
  291. * The PHPUnit version string.
  292. *
  293. * @return bool
  294. * TRUE if the PHPUnit needs to be upgraded, FALSE if not.
  295. */
  296. public static function upgradePHPUnitCheck($phpunit_version) {
  297. return !(version_compare(PHP_MAJOR_VERSION . '.' . PHP_MINOR_VERSION, '7.3') >= 0 && version_compare($phpunit_version, '7.0') < 0);
  298. }
  299. }