Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Githook Bundle Laravel Package

bourdeau/githook-bundle

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Steps

  1. Installation:

    composer require bourdeau/githook-bundle
    

    Ensure your project is Symfony 3.x (not compatible with newer versions).

  2. Register the Bundle: Add to app/AppKernel.php:

    new Bourdeau\Bundle\GitHookBundle\GitHookBundle(),
    
  3. 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/.


Implementation Patterns

Workflows

  1. 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!');
        }
    }
    
  2. 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...');
    
  3. Conditional Hooks: Disable hooks in CI/CD by checking environment variables:

    if (getenv('CI') === 'true') {
        exit(0); // Skip hook in CI
    }
    

Best Practices

  • Idempotency: Ensure hooks exit with 0 on success, non-zero on failure.
  • Logging: Redirect output to a file or Symfony logger for debugging:
    file_put_contents('var/log/githook.log', print_r($files, true), FILE_APPEND);
    
  • Testing: Mock hooks locally with git commit --no-verify to bypass them.

Gotchas and Tips

Pitfalls

  1. Symfony 3.x Only: The bundle will not work in Symfony 4+ or Laravel. Avoid installing in non-Symfony 3 projects.

  2. Hook Path Hardcoding: The bundle expects hooks in Resources/hooks/. Custom paths require manual symlinking or forking the bundle.

  3. Permission Issues: Git hooks must be executable (chmod +x). The bundle does not handle this automatically.

  4. No Configuration: Hardcoded behavior (e.g., hook paths) may require monkey-patching the bundle for customization.

Debugging Tips

  • Check Hook Execution: Run GIT_TRACE=1 git commit to see if hooks are triggered.
  • Symfony Debug Toolbar: Use bin/console debug:container to verify service availability in hooks.
  • Dry Runs: Test hooks locally with:
    php src/Bourdeau/GitHookBundle/Resources/hooks/pre-commit.php --dry-run
    

Extension Points

  1. 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.)

  2. Event Dispatching: Trigger Symfony events from hooks:

    $dispatcher = $container->get('event_dispatcher');
    $dispatcher->dispatch(new PreCommitEvent($files), 'pre_commit');
    
  3. 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'));
    
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
codeflextech/permission-manager
karnoweb/livewire-datepicker
sayedenam/sayed-dashboard
milito/query-filter
apiboxsym/user-bundle
apiboxsym/health-check-bundle
jayeshmepani/jpl-moshier-ephemeris-php
elnasnato/laraliveui
labrodev/rest-sdk
sampaui/sampaui
babelqueue/php-sdk
facebook/capi-param-builder-php
babelqueue/symfony
hamzi/corewatch
minionfactory/raw-hydrator
hexters/coinpayment
rjcodes/rjcms
act-training/laravel-permissions-manager
alimarchal/laravel-chart-of-accounts
babenkoivan/elastic-scout-driver