Installation:
composer require cypresslab/gitelephant-bundle:dev-master
Register the bundle in AppKernel.php (only in dev/test environments):
if (in_array($this->getEnvironment(), ['dev', 'test'])) {
$bundles[] = new Cypress\GitElephantBundle\CypressGitElephantBundle();
}
First Use Case:
Inject the GitElephant service in a controller or command:
use Cypress\GitElephantBundle\Service\GitElephant;
class GitController extends Controller
{
public function __construct(GitElephant $gitElephant)
{
$this->gitElephant = $gitElephant;
}
public function showRepoStatus()
{
$status = $this->gitElephant->status();
return $this->render('git/status.html.twig', ['status' => $status]);
}
}
Where to Look First:
services.yml for configuration (if any).Repository Operations: Use the service to interact with Git repositories directly from Symfony:
$this->gitElephant->pull(); // Pull latest changes
$this->gitElephant->commit('msg'); // Commit changes
$this->gitElephant->push(); // Push to remote
Command-Line Integration: Wrap GitElephant in a Symfony command for CLI tasks:
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
class GitPullCommand extends Command
{
protected function execute(InputInterface $input, OutputInterface $output)
{
$this->gitElephant->pull();
$output->writeln('Repository pulled successfully.');
}
}
Event-Driven Hooks: Trigger actions on Git events (e.g., post-commit hooks) via Symfony events:
// In a service or event subscriber
$this->gitElephant->addPostCommitListener(function() {
// Notify Slack/email/etc.
});
Dependency Injection: Prefer constructor injection for testability:
class GitService
{
public function __construct(private GitElephant $gitElephant) {}
}
dev/test environments to avoid exposing Git operations in production.services.yml:
cypress_git_elephant:
repo_path: '%kernel.project_dir%/../custom-repo'
try {
$this->gitElephant->push();
} catch (\RuntimeException $e) {
$this->addFlash('error', 'Git push failed: ' . $e->getMessage());
}
Environment Mismatch:
dev/test environments may expose sensitive Git operations in production.in_array($this->getEnvironment(), ['dev', 'test']).Path Assumptions:
repo_path in services.yml or pass it to the service constructor.Outdated Dependencies:
Permission Errors:
.git directory inaccessible).chmod -R 755 /path/to/repo
Verbose Output: Enable GitElephant’s debug mode via configuration:
cypress_git_elephant:
debug: true
This logs raw Git commands executed.
Command Inspection:
Use strace or git --trace to debug low-level Git interactions:
GIT_TRACE=1 git status # Check raw Git output
Custom Git Commands:
Extend the bundle by adding new methods to the GitElephant service:
// In a custom service
$this->gitElephant->customCommand('git custom-subcommand --args');
Event Listeners: Subscribe to Git events (e.g., pre-push) via Symfony’s event dispatcher:
$dispatcher->addListener('git.pre_push', function() {
// Validate commit messages, etc.
});
Repository Abstraction:
Create a decorator service to wrap GitElephant and add domain-specific logic:
class CustomGitService
{
public function __construct(private GitElephant $gitElephant) {}
public function safePush()
{
if ($this->hasUnstagedChanges()) {
throw new \RuntimeException('Unstaged changes detected!');
}
$this->gitElephant->push();
}
}
GitElephant methods directly. Check the underlying service definition for available methods:
# services.yml
cypress_git_elephant:
class: Cypress\GitElephantBundle\Service\GitElephant
arguments: ['%kernel.root_dir%']
AppKernel) may require polyfills or adjustments. Test thoroughly.How can I help you explore Laravel packages today?