bdb0127c467c39029d6fe8b94bb30c518b19cb3e
[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     // We need the root package so we can add our classmaps to its loader.
76     $package = $event->getComposer()->getPackage();
77     // We need the local repository so that we can query and see if it's likely
78     // that our files are present there.
79     $repository = $event->getComposer()->getRepositoryManager()->getLocalRepository();
80     // This is, essentially, a null constraint. We only care whether the package
81     // is present in vendor/ yet, but findPackage() requires it.
82     $constraint = new Constraint('>', '');
83     // Check for our packages, and then optimize them if they're present.
84     if ($repository->findPackage('symfony/http-foundation', $constraint)) {
85       $autoload = $package->getAutoload();
86       $autoload['classmap'] = array_merge($autoload['classmap'], [
87         'vendor/symfony/http-foundation/Request.php',
88         'vendor/symfony/http-foundation/ParameterBag.php',
89         'vendor/symfony/http-foundation/FileBag.php',
90         'vendor/symfony/http-foundation/ServerBag.php',
91         'vendor/symfony/http-foundation/HeaderBag.php',
92       ]);
93       $package->setAutoload($autoload);
94     }
95     if ($repository->findPackage('symfony/http-kernel', $constraint)) {
96       $autoload = $package->getAutoload();
97       $autoload['classmap'] = array_merge($autoload['classmap'], [
98         'vendor/symfony/http-kernel/HttpKernel.php',
99         'vendor/symfony/http-kernel/HttpKernelInterface.php',
100         'vendor/symfony/http-kernel/TerminableInterface.php',
101       ]);
102       $package->setAutoload($autoload);
103     }
104   }
105
106   /**
107    * Ensures that .htaccess and web.config files are present in Composer root.
108    *
109    * @param \Composer\Script\Event $event
110    */
111   public static function ensureHtaccess(Event $event) {
112
113     // The current working directory for composer scripts is where you run
114     // composer from.
115     $vendor_dir = $event->getComposer()->getConfig()->get('vendor-dir');
116
117     // Prevent access to vendor directory on Apache servers.
118     $htaccess_file = $vendor_dir . '/.htaccess';
119     if (!file_exists($htaccess_file)) {
120       file_put_contents($htaccess_file, FileStorage::htaccessLines(TRUE) . "\n");
121     }
122
123     // Prevent access to vendor directory on IIS servers.
124     $webconfig_file = $vendor_dir . '/web.config';
125     if (!file_exists($webconfig_file)) {
126       $lines = <<<EOT
127 <configuration>
128   <system.webServer>
129     <authorization>
130       <deny users="*">
131     </authorization>
132   </system.webServer>
133 </configuration>
134 EOT;
135       file_put_contents($webconfig_file, $lines . "\n");
136     }
137   }
138
139   /**
140    * Remove possibly problematic test files from vendored projects.
141    *
142    * @param \Composer\Installer\PackageEvent $event
143    *   A PackageEvent object to get the configured composer vendor directories
144    *   from.
145    */
146   public static function vendorTestCodeCleanup(PackageEvent $event) {
147     $vendor_dir = $event->getComposer()->getConfig()->get('vendor-dir');
148     $io = $event->getIO();
149     $op = $event->getOperation();
150     if ($op->getJobType() == 'update') {
151       $package = $op->getTargetPackage();
152     }
153     else {
154       $package = $op->getPackage();
155     }
156     $package_key = static::findPackageKey($package->getName());
157     $message = sprintf("    Processing <comment>%s</comment>", $package->getPrettyName());
158     if ($io->isVeryVerbose()) {
159       $io->write($message);
160     }
161     if ($package_key) {
162       foreach (static::$packageToCleanup[$package_key] as $path) {
163         $dir_to_remove = $vendor_dir . '/' . $package_key . '/' . $path;
164         $print_message = $io->isVeryVerbose();
165         if (is_dir($dir_to_remove)) {
166           if (static::deleteRecursive($dir_to_remove)) {
167             $message = sprintf("      <info>Removing directory '%s'</info>", $path);
168           }
169           else {
170             // Always display a message if this fails as it means something has
171             // gone wrong. Therefore the message has to include the package name
172             // as the first informational message might not exist.
173             $print_message = TRUE;
174             $message = sprintf("      <error>Failure removing directory '%s'</error> in package <comment>%s</comment>.", $path, $package->getPrettyName());
175           }
176         }
177         else {
178           // If the package has changed or the --prefer-dist version does not
179           // include the directory this is not an error.
180           $message = sprintf("      Directory '%s' does not exist", $path);
181         }
182         if ($print_message) {
183           $io->write($message);
184         }
185       }
186
187       if ($io->isVeryVerbose()) {
188         // Add a new line to separate this output from the next package.
189         $io->write("");
190       }
191     }
192   }
193
194   /**
195    * Find the array key for a given package name with a case-insensitive search.
196    *
197    * @param string $package_name
198    *   The package name from composer. This is always already lower case.
199    *
200    * @return string|null
201    *   The string key, or NULL if none was found.
202    */
203   protected static function findPackageKey($package_name) {
204     $package_key = NULL;
205     // In most cases the package name is already used as the array key.
206     if (isset(static::$packageToCleanup[$package_name])) {
207       $package_key = $package_name;
208     }
209     else {
210       // Handle any mismatch in case between the package name and array key.
211       // For example, the array key 'mikey179/vfsStream' needs to be found
212       // when composer returns a package name of 'mikey179/vfsstream'.
213       foreach (static::$packageToCleanup as $key => $dirs) {
214         if (strtolower($key) === $package_name) {
215           $package_key = $key;
216           break;
217         }
218       }
219     }
220     return $package_key;
221   }
222
223   /**
224    * Helper method to remove directories and the files they contain.
225    *
226    * @param string $path
227    *   The directory or file to remove. It must exist.
228    *
229    * @return bool
230    *   TRUE on success or FALSE on failure.
231    */
232   protected static function deleteRecursive($path) {
233     if (is_file($path) || is_link($path)) {
234       return unlink($path);
235     }
236     $success = TRUE;
237     $dir = dir($path);
238     while (($entry = $dir->read()) !== FALSE) {
239       if ($entry == '.' || $entry == '..') {
240         continue;
241       }
242       $entry_path = $path . '/' . $entry;
243       $success = static::deleteRecursive($entry_path) && $success;
244     }
245     $dir->close();
246
247     return rmdir($path) && $success;
248   }
249
250 }