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 * Remove possibly problematic test files from vendored projects.
149 * @param \Composer\Installer\PackageEvent $event
150 * A PackageEvent object to get the configured composer vendor directories
153 public static function vendorTestCodeCleanup(PackageEvent $event) {
154 $vendor_dir = $event->getComposer()->getConfig()->get('vendor-dir');
155 $io = $event->getIO();
156 $op = $event->getOperation();
157 if ($op->getJobType() == 'update') {
158 $package = $op->getTargetPackage();
161 $package = $op->getPackage();
163 $package_key = static::findPackageKey($package->getName());
164 $message = sprintf(" Processing <comment>%s</comment>", $package->getPrettyName());
165 if ($io->isVeryVerbose()) {
166 $io->write($message);
169 foreach (static::$packageToCleanup[$package_key] as $path) {
170 $dir_to_remove = $vendor_dir . '/' . $package_key . '/' . $path;
171 $print_message = $io->isVeryVerbose();
172 if (is_dir($dir_to_remove)) {
173 if (static::deleteRecursive($dir_to_remove)) {
174 $message = sprintf(" <info>Removing directory '%s'</info>", $path);
177 // Always display a message if this fails as it means something has
178 // gone wrong. Therefore the message has to include the package name
179 // as the first informational message might not exist.
180 $print_message = TRUE;
181 $message = sprintf(" <error>Failure removing directory '%s'</error> in package <comment>%s</comment>.", $path, $package->getPrettyName());
185 // If the package has changed or the --prefer-dist version does not
186 // include the directory this is not an error.
187 $message = sprintf(" Directory '%s' does not exist", $path);
189 if ($print_message) {
190 $io->write($message);
194 if ($io->isVeryVerbose()) {
195 // Add a new line to separate this output from the next package.
202 * Find the array key for a given package name with a case-insensitive search.
204 * @param string $package_name
205 * The package name from composer. This is always already lower case.
207 * @return string|null
208 * The string key, or NULL if none was found.
210 protected static function findPackageKey($package_name) {
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;
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) {
231 * Helper method to remove directories and the files they contain.
233 * @param string $path
234 * The directory or file to remove. It must exist.
237 * TRUE on success or FALSE on failure.
239 protected static function deleteRecursive($path) {
240 if (is_file($path) || is_link($path)) {
241 return unlink($path);
245 while (($entry = $dir->read()) !== FALSE) {
246 if ($entry == '.' || $entry == '..') {
249 $entry_path = $path . '/' . $entry;
250 $success = static::deleteRecursive($entry_path) && $success;
254 return rmdir($path) && $success;