<?php namespace LakeDrops\Component\Composer; /** * Manages composer.json files. */ final class Utils { /** * @var string[] */ private static $ignoredGitPatterns; /** * @var string */ private static $lfsGitPatterns; /** * Make sure that git init was called. */ protected static function gitInit(): void { if (!file_exists('.git')) { passthru(escapeshellcmd('git init')); } } /** * @param string $command */ public static function git(string $command): void { self::gitInit(); passthru(escapeshellcmd('git -c "user.email=composer@lakedrops.com" ' . $command)); } /** * @param string $pattern */ public static function gitIgnore(string $pattern): void { if (self::$ignoredGitPatterns === NULL) { if (file_exists('.gitignore')) { self::$ignoredGitPatterns = explode(PHP_EOL, file_get_contents('.gitignore')); } else { self::$ignoredGitPatterns = []; } } if (in_array($pattern, self::$ignoredGitPatterns, TRUE)) { return; } self::$ignoredGitPatterns[] = $pattern; self::git('ignore ' . $pattern); } /** * @param string $pattern */ public static function gitLFS(string $pattern): void { if (self::$lfsGitPatterns === NULL) { self::gitInit(); $output = []; exec('git lfs track', $output); self::$lfsGitPatterns = implode(PHP_EOL, $output); } if (strpos(self::$lfsGitPatterns, $pattern)) { return; } self::$lfsGitPatterns = $pattern . PHP_EOL; self::git('lfs track ' . $pattern); } }