016f93a348563fedff4cf50db03e7b87b8245b1d
[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
10 /**
11  * Provides static functions for composer script events.
12  *
13  * @see https://getcomposer.org/doc/articles/scripts.md
14  */
15 class Composer {
16
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'],
69   ];
70
71   /**
72    * Add vendor classes to Composer's static classmap.
73    */
74   public static function preAutoloadDump(Event $event) {
75     // Get the configured vendor directory.
76     $vendor_dir = $event->getComposer()->getConfig()->get('vendor-dir');
77
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
88     // dependencies.
89     $autoload = $package->getAutoload();
90     if (!isset($autoload['classmap'])) {
91       $autoload['classmap'] = [];
92     }
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',
101       ]);
102     }
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',
108       ]);
109     }
110     $package->setAutoload($autoload);
111   }
112
113   /**
114    * Ensures that .htaccess and web.config files are present in Composer root.
115    *
116    * @param \Composer\Script\Event $event
117    */
118   public static function ensureHtaccess(Event $event) {
119
120     // The current working directory for composer scripts is where you run
121     // composer from.
122     $vendor_dir = $event->getComposer()->getConfig()->get('vendor-dir');
123
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");
128     }
129
130     // Prevent access to vendor directory on IIS servers.
131     $webconfig_file = $vendor_dir . '/web.config';
132     if (!file_exists($webconfig_file)) {
133       $lines = <<<EOT
134 <configuration>
135   <system.webServer>
136     <authorization>
137       <deny users="*">
138     </authorization>
139   </system.webServer>
140 </configuration>
141 EOT;
142       file_put_contents($webconfig_file, $lines . "\n");
143     }
144   }
145
146   /**
147    * Remove possibly problematic test files from vendored projects.
148    *
149    * @param \Composer\Installer\PackageEvent $event
150    *   A PackageEvent object to get the configured composer vendor directories
151    *   from.
152    */
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();
159     }
160     else {
161       $package = $op->getPackage();
162     }
163     $package_key = static::findPackageKey($package->getName());
164     $message = sprintf("    Processing <comment>%s</comment>", $package->getPrettyName());
165     if ($io->isVeryVerbose()) {
166       $io->write($message);
167     }
168     if ($package_key) {
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);
175           }
176           else {
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());
182           }
183         }
184         else {
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);
188         }
189         if ($print_message) {
190           $io->write($message);
191         }
192       }
193
194       if ($io->isVeryVerbose()) {
195         // Add a new line to separate this output from the next package.
196         $io->write("");
197       }
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   /**
231    * Helper method to remove directories and the files they contain.
232    *
233    * @param string $path
234    *   The directory or file to remove. It must exist.
235    *
236    * @return bool
237    *   TRUE on success or FALSE on failure.
238    */
239   protected static function deleteRecursive($path) {
240     if (is_file($path) || is_link($path)) {
241       return unlink($path);
242     }
243     $success = TRUE;
244     $dir = dir($path);
245     while (($entry = $dir->read()) !== FALSE) {
246       if ($entry == '.' || $entry == '..') {
247         continue;
248       }
249       $entry_path = $path . '/' . $entry;
250       $success = static::deleteRecursive($entry_path) && $success;
251     }
252     $dir->close();
253
254     return rmdir($path) && $success;
255   }
256
257 }