Installation
composer require darkikim/git-helper
Ensure your composer.json includes the package under require (not require-dev unless explicitly needed for CI/dev).
Enable the Bundle
Add to config/bundles.php (Symfony 5.2+):
return [
// ...
Kikim\GitHelper\GitHelperBundle::class => ['dev' => true],
// ...
];
Note: Set dev: true to avoid cluttering production toolbars.
First Use Case Visit any route in your dev environment. The Symfony Toolbar will now display:
abc1234)johndoe)feature/login).Verify: Check the "Git" tab in the Toolbar (or the default "GitHelper" section if not customized).
Local Development
abc1234).git blame for line-level context:
git blame app/Http/Controllers/UserController.php
Then cross-reference the SHA in the Toolbar.CI/CD Integration
dev: false in bundles.php).use Kikim\GitHelper\GitHelper;
$gitHelper = $this->container->get(GitHelper::class);
$lastCommit = $gitHelper->getLastCommit();
// Log or use $lastCommit['sha'], $lastCommit['message'], etc.
Customizing Toolbar Display Override the Twig template to modify the output:
vendor/kikim/git-helper/src/Resources/views/GitHelper/toolbar.html.twig to templates/bundles/GitHelper/toolbar.html.twig.main):
{% extends 'GitHelper/toolbar.html.twig' %}
{% block git_helper_content %}
{{ parent() }}
<div class="git-helper-distance">
Ahead: {{ gitHelper.getCommitDistance('main') }} commits
</div>
{% endblock %}
Command-Line Access Use the bundle’s service in console commands:
use Kikim\GitHelper\GitHelper;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
class GitInfoCommand extends Command {
protected static $defaultName = 'app:git-info';
private GitHelper $gitHelper;
public function __construct(GitHelper $gitHelper) {
$this->gitHelper = $gitHelper;
parent::__construct();
}
protected function execute(InputInterface $input, OutputInterface $output): int {
$commit = $this->gitHelper->getLastCommit();
$output->writeln(sprintf(
'Last commit: %s by %s - %s',
$commit['sha'],
$commit['author'],
$commit['message']
));
return Command::SUCCESS;
}
}
Git Repository Not Detected
.git directory is inaccessible.$this->container->get('kikim_git_helper.git_helper')->isGitRepo();
Return false if outside a repo.Performance Overhead
# config/services.yaml
Kikim\GitHelper\GitHelper:
public: false
Symfony Cache Conflicts
cache:clear --no-warmup.Multi-Repo Projects
$gitHelper->setRepoPath(__DIR__ . '/../submodule-repo');
Log Git Data
Enable debug mode in config/packages/dev/kikim_git_helper.yaml:
kikim_git_helper:
debug: true
Logs will appear in var/log/dev.log.
Check GitHelper Service Dump the service’s raw data:
dd($this->container->get('kikim_git_helper.git_helper')->getLastCommit());
Add Custom Git Commands
Extend the bundle’s GitHelper service to support additional Git commands (e.g., getBranches()):
use Kikim\GitHelper\GitHelper;
class CustomGitHelper extends GitHelper {
public function getBranches(): array {
exec('git branch --format "%(refname:short)"', $branches);
return array_filter($branches);
}
}
Override the service in config/services.yaml:
Kikim\GitHelper\GitHelper: '@custom_git_helper'
Localization Translate commit messages or timestamps:
{{ gitHelper.lastCommit.message|trans }}
{{ gitHelper.lastCommit.timestamp|date('d/m/Y H:i') }}
Security
.gitignore or pre-commit hooks to sanitize messages.APP_ENV checks in a custom service).Testing
Mock the GitHelper service in PHPUnit:
$this->container->set('kikim_git_helper.git_helper', $this->createMock(GitHelper::class));
$mockGitHelper->method('getLastCommit')->willReturn([
'sha' => 'test123',
'author' => 'testuser',
'message' => 'Test commit',
]);
How can I help you explore Laravel packages today?