Installation:
composer require bourdeau/githook-bundle
Ensure your project is Symfony 3.x (not compatible with newer versions).
Register the Bundle:
Add to app/AppKernel.php:
new Bourdeau\Bundle\GitHookBundle\GitHookBundle(),
First Use Case: Create a pre-commit hook in your project root:
mkdir -p .git/hooks
touch .git/hooks/pre-commit
chmod +x .git/hooks/pre-commit
The bundle will automatically detect and execute PHP-based hooks in src/Bourdeau/GitHookBundle/Resources/hooks/.
Hook Organization:
Store hooks in src/Bourdeau/GitHookBundle/Resources/hooks/ (e.g., pre-commit.php, pre-push.php).
Example pre-commit.php:
<?php
// src/Bourdeau/GitHookBundle/Resources/hooks/pre-commit.php
$files = glob('src/**/*.php');
foreach ($files as $file) {
if (!preg_match('/^declare\(.*\)\s*strict_types=1;?$/m', file_get_contents($file))) {
die('PHP files must declare strict types!');
}
}
Symfony Integration: Use Symfony services in hooks via dependency injection:
use Symfony\Component\DependencyInjection\ContainerInterface;
$container = $this->get('service_container');
$logger = $container->get('logger');
$logger->info('Running pre-commit checks...');
Conditional Hooks: Disable hooks in CI/CD by checking environment variables:
if (getenv('CI') === 'true') {
exit(0); // Skip hook in CI
}
0 on success, non-zero on failure.file_put_contents('var/log/githook.log', print_r($files, true), FILE_APPEND);
git commit --no-verify to bypass them.Symfony 3.x Only: The bundle will not work in Symfony 4+ or Laravel. Avoid installing in non-Symfony 3 projects.
Hook Path Hardcoding:
The bundle expects hooks in Resources/hooks/. Custom paths require manual symlinking or forking the bundle.
Permission Issues:
Git hooks must be executable (chmod +x). The bundle does not handle this automatically.
No Configuration: Hardcoded behavior (e.g., hook paths) may require monkey-patching the bundle for customization.
GIT_TRACE=1 git commit to see if hooks are triggered.bin/console debug:container to verify service availability in hooks.php src/Bourdeau/GitHookBundle/Resources/hooks/pre-commit.php --dry-run
Custom Hook Directories:
Override the bundle’s HookManager to load hooks from alternative paths:
# config.yml (hypothetical)
bourdeau_githook:
hooks_directory: '%kernel.project_dir%/custom_hooks'
(Note: Requires bundle modification.)
Event Dispatching: Trigger Symfony events from hooks:
$dispatcher = $container->get('event_dispatcher');
$dispatcher->dispatch(new PreCommitEvent($files), 'pre_commit');
Laravel Workaround:
For Laravel, use the underlying logic by copying the bundle’s HookManager class and adapting it to Laravel’s service container. Example:
// app/Providers/AppServiceProvider.php
use Bourdeau\Bundle\GitHookBundle\Manager\HookManager;
$hookManager = new HookManager($this->app->basePath('hooks'));
How can I help you explore Laravel packages today?