You are here

public static function Composer::ensureHtaccess in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/lib/Drupal/Core/Composer/Composer.php \Drupal\Core\Composer\Composer::ensureHtaccess()

Ensures that .htaccess and web.config files are present in Composer root.

Parameters

\Composer\Script\Event $event:

File

core/lib/Drupal/Core/Composer/Composer.php, line 114
Contains \Drupal\Core\Composer\Composer.

Class

Composer
Provides static functions for composer script events.

Namespace

Drupal\Core\Composer

Code

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");
  }
}