PHP code example of butschster / laravel-git-hooks

1. Go to this page and download the library: Download butschster/laravel-git-hooks library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

butschster / laravel-git-hooks example snippets


// App/Providers/AppServiceProvider

class AppServiceProvider extends ServiceProvider {
    pubflic function register() {
    
        $this->app->bind('my-first-hook', function ($app) {
            return new \App\Console\GitHooks\MyPreCommitHook();
        });

    }
}

// config/git_hooks.php
return [
    ...
    'pre-commit' => [
        // Just class name
        \App\Console\GitHooks\MyPreCommitHook::class,
        
        // Class name with params, which will be pass in constructor
        \App\Console\GitHooks\MyPreCommitHook::class => [
            'param1' => 'hello',
            'param2' => 'world'
        ], 
        
        // Closure function
        function(ChangedFiles $files, Closure $next) {
            return $next($files);
        },
        
        // Service container
        'my-first-hook'
    ],
    ...
];


// config/git_hooks.php
return [
    ...
    'pre-commit' => [
        \App\Console\GitHooks\MyPreCommitHook::class,
    ],
    ...
];

// App/Console/GitHooks/MyPreCommitHook.php

namespace \App\Console\GitHooks;

use \Butschster\GitHooks\Git\ChangedFiles;
use Closure;

class MyPreCommitHook implements \Butschster\GitHooks\Contracts\PreCommitHook
{

    public function getName() : string
    {
        return '...';
    }

    public function handle(ChangedFiles $files, Closure $next)
    {
        // do something

        // If you want to cancel commit, you have to throw an exception.

        // run next hook
        return $next($files);
    }
}

// config/git_hooks.php
return [
    ...
    'prepare-commit-msg' => [
        \App\Console\GitHooks\MyFirstPrepareCommitHook::class,
    ],
    ...
];

// App/Console/GitHooks/MyFirstPrepareCommitHook.php

namespace \App\Console\GitHooks;

use Butschster\GitHooks\Git\CommitMessage;
use Closure;

class MyFirstPrepareCommitHook implements \Butschster\GitHooks\Contracts\MessageHook
{
    public function getName() : string
    {
        return '...';
    }

    public function handle(CommitMessage $message, Closure $next)
    {
        // do something

        $currentMessage = $message->getMessage();

        // You can update commit message text
        $message->setMessage(str_replace('issue', 'fixed', $currentMessage));

        // If you want to cancel commit, you have to throw an exception.

        // run next hook
        return $next($message);
    }
}

// config/git_hooks.php
return [
    ...
    'commit-msg' => [
        \App\Console\GitHooks\MyFirstCommitMessageHook::class,
    ],
    ...
];

// App/Console/GitHooks/MyFirstCommitMessageHook.php

namespace \App\Console\GitHooks;

use Butschster\GitHooks\Git\CommitMessage;
use Closure;

class MyFirstCommitMessageHook implements \Butschster\GitHooks\Contracts\MessageHook
{
    public function getName() : string
    {
        return '...';
    }

    public function handle(CommitMessage $message, Closure $next)
    {
        // do something

        $currentMessage = $message->getMessage();

        // You can update commit message text
        $message->setMessage(str_replace('issue', 'fixed', $currentMessage));

        // If you want to cancel commit, you have to throw an exception.

        // run next hook
        return $next($message);
    }
}

// config/git_hooks.php
return [
    ...
    'post-commit' => [
        \App\Console\GitHooks\NotifyAboutNewCommit::class,
    ],
    ...
];

// App/Console/GitHooks/NotifyAboutNewCommit.php

namespace \App\Console\GitHooks;

use Butschster\GitHooks\Git\Log;
use Closure;

class NotifyAboutNewCommit implements \Butschster\GitHooks\Contracts\PostCommitHook
{
    public function getName() : string
    {
        return '...';
    }

    public function handle(Log $log, Closure $next)
    {
        $hash = $log->getHash();
        $author = $log->getAuthor();
        $date = $log->getDate();
        $message = $log->getMessage();

        // do something

        // If you want to cancel, you have to throw an exception.

        // run next hook
        return $next($log);
    }
}