Installation Add the package via Composer:
composer require symplify/git-wrapper --dev
(Note: Marked as --dev since the package is archived but may still be useful for legacy projects.)
Basic Usage Initialize the wrapper in a service or helper class:
use Symplify\GitWrapper\GitWrapper;
$gitWrapper = new GitWrapper();
First Use Case Check git status in a Laravel command:
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
class GitStatusCommand extends Command
{
protected function execute(InputInterface $input, OutputInterface $output): int
{
$gitWrapper = new GitWrapper();
$status = $gitWrapper->execute('status', '--porcelain');
$output->writeln('Git status:');
$output->writeln($status);
return Command::SUCCESS;
}
}
Running Git Commands
Use execute() for raw output or executeSilently() for suppressed output:
$log = $gitWrapper->execute('log', '--oneline', '-n', 5);
$gitWrapper->executeSilently('branch', '-D', 'old-branch');
Integration with Laravel Artisan Wrap git operations in Artisan commands for CLI tooling:
$gitWrapper->execute('pull', 'origin', 'main');
$gitWrapper->execute('push', 'origin', 'feature-branch');
Git Hooks & Automation
Use in Laravel events (e.g., Illuminate\Foundation\Bootstrap\LoadConfiguration) to trigger git actions:
$gitWrapper->execute('add', '.');
$gitWrapper->execute('commit', '-m', 'Auto-commit via Laravel');
Dependency Injection Bind the wrapper in Laravel’s service container for reusable access:
$this->app->singleton(GitWrapper::class, function ($app) {
return new GitWrapper();
});
Git Operations in Controllers (Caution: Avoid in production controllers; use services instead.)
$gitWrapper->execute('fetch', '--all');
try-catch to handle git failures gracefully:
try {
$gitWrapper->execute('checkout', 'nonexistent-branch');
} catch (\Symplify\GitWrapper\Exception\GitException $e) {
Log::error('Git error: ' . $e->getMessage());
}
.git directory existence before operations:
if (!file_exists('.git')) {
throw new \RuntimeException('Not a git repository');
}
Archived Package
league/git or phpseclib/phpseclib for new projects.Git Command Injection
execute() can lead to command injection.$allowedCommands = ['status', 'log', 'branch'];
if (!in_array($command, $allowedCommands)) {
throw new \InvalidArgumentException('Command not allowed');
}
Output Parsing
git log --pretty=format:"%h").execute() + preg_match() for structured data:
preg_match('/^([a-f0-9]{7}) .*/m', $logOutput, $matches);
Performance
git status in a middleware).Windows Line Endings
str_replace("\r\n", "\n", $output) to normalize.$gitWrapper->setDebug(true);
$gitWrapper->getExitCode() after execution.Log::debug('Executing git command: ' . $gitWrapper->getLastCommand());
Custom Git Commands Extend the wrapper for project-specific commands:
class CustomGitWrapper extends GitWrapper
{
public function deploy()
{
$this->execute('add', '.');
$this->execute('commit', '-m', 'Deploy');
$this->execute('push', 'origin', 'main');
}
}
GitHub/GitLab API Integration
Combine with APIs for hybrid workflows (e.g., git-wrapper for local ops + API for remote).
Laravel Events
Trigger git actions on events (e.g., Illuminate\Queue\Events\JobProcessed):
event(new GitCommitEvent('Auto-commit after job'));
Testing
Mock GitWrapper in PHPUnit:
$mock = $this->createMock(GitWrapper::class);
$mock->method('execute')->willReturn('mocked output');
$gitWrapper = new GitWrapper('/custom/path/to/git');
.env for sensitive git credentials (e.g., GIT_USER, GIT_TOKEN).How can I help you explore Laravel packages today?