Composer.php 2.06 KB
Newer Older
1 2 3 4 5 6 7 8 9
<?php

/**
 * @file
 * Contains \Drupal\Core\Composer\Composer.
 */

namespace Drupal\Core\Composer;

10
use Drupal\Component\PhpStorage\FileStorage;
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
use Composer\Script\Event;

/**
 * Provides static functions for composer script events.
 *
 * @see https://getcomposer.org/doc/articles/scripts.md
 */
class Composer {

  /**
   * Add vendor classes to composers static classmap.
   */
  public static function preAutoloadDump(Event $event) {
    $composer = $event->getComposer();
    $package = $composer->getPackage();
    $autoload = $package->getAutoload();
    $autoload['classmap'] = array_merge($autoload['classmap'], array(
28 29 30 31 32 33 34 35
      'vendor/symfony/http-foundation/Request.php',
      'vendor/symfony/http-foundation/ParameterBag.php',
      'vendor/symfony/http-foundation/FileBag.php',
      'vendor/symfony/http-foundation/ServerBag.php',
      'vendor/symfony/http-foundation/HeaderBag.php',
      'vendor/symfony/http-kernel/HttpKernel.php',
      'vendor/symfony/http-kernel/HttpKernelInterface.php',
      'vendor/symfony/http-kernel/TerminableInterface.php',
36 37 38 39
    ));
    $package->setAutoload($autoload);
  }

40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
  /**
   * Ensures that .htaccess and web.config files are present in Composer root.
   *
   * @param \Composer\Script\Event $event
   */
  public static function ensureHtaccess(Event $event) {

    // The current working directory for composer scripts is where you run
    // composer from.
    $vendor_dir = $event->getComposer()->getConfig()->get('vendor-dir');

    // Prevent access to vendor directory on Apache servers.
    $htaccess_file = $vendor_dir . '/.htaccess';
    if (!file_exists($htaccess_file)) {
      file_put_contents($htaccess_file, FileStorage::htaccessLines(TRUE) . "\n");
    }

    // Prevent access to vendor directory on IIS servers.
    $webconfig_file = $vendor_dir . '/web.config';
    if (!file_exists($webconfig_file)) {
      $lines = <<<EOT
<configuration>
  <system.webServer>
    <authorization>
      <deny users="*">
    </authorization>
  </system.webServer>
</configuration>
EOT;
      file_put_contents($webconfig_file, $lines . "\n");
    }
  }

73
}