<?php namespace LakeDrops\Component\Composer; /** * Manages composer.json files. */ final class Utils { /** * List of all ignored git patterns. * * @var string[] */ private static array $ignoredGitPatterns; /** * Imploded list of all git lfs patterns. * * @var string */ private static string $lfsGitPatterns; /** * Make sure that git init was called. */ protected static function gitInit(): void { if (!file_exists('.git')) { passthru(escapeshellcmd('git init --initial-branch=develop')); } } /** * Helper function to call git. * * @param string $command * The git command to execute. * @param string|null $path * The optional path where to execute the git command. */ public static function git(string $command, ?string $path = NULL): void { self::gitInit(); $prefix = $path === NULL ? '' : 'cd ' . $path . ' && '; passthru($prefix . escapeshellcmd('git -c "user.email=composer@lakedrops.com" ' . $command)); } /** * Helper function to add a new pattern to .gitignore. * * @param string $pattern * The pattern to be added. */ public static function gitIgnore(string $pattern): void { if (!isset(self::$ignoredGitPatterns)) { 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); } /** * Helper function to add a new pattern to git LFS. * * @param string $pattern * The pattern to be added. */ public static function gitLfs(string $pattern): void { if (!isset(self::$lfsGitPatterns)) { 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); } }