You are here

protected function ManageGitIgnore::addToGitIgnore in Drupal 9

Same name and namespace in other branches
  1. 8 composer/Plugin/Scaffold/ManageGitIgnore.php \Drupal\Composer\Plugin\Scaffold\ManageGitIgnore::addToGitIgnore()

Adds a set of entries to the specified .gitignore file.

Parameters

string $dir: Path to directory where gitignore should be written.

string[] $entries: Entries to write to .gitignore file.

1 call to ManageGitIgnore::addToGitIgnore()
ManageGitIgnore::manageIgnored in composer/Plugin/Scaffold/ManageGitIgnore.php
Manages gitignore files.

File

composer/Plugin/Scaffold/ManageGitIgnore.php, line 108

Class

ManageGitIgnore
Manage the .gitignore file.

Namespace

Drupal\Composer\Plugin\Scaffold

Code

protected function addToGitIgnore($dir, array $entries) {
  sort($entries);
  $git_ignore_path = $dir . '/.gitignore';
  $contents = '';

  // Appending to existing .gitignore files.
  if (file_exists($git_ignore_path)) {
    $contents = file_get_contents($git_ignore_path);
    if (!empty($contents) && substr($contents, -1) != "\n") {
      $contents .= "\n";
    }
  }
  $contents .= implode("\n", $entries);
  file_put_contents($git_ignore_path, $contents);
}