PHP code example of thecodingmachine / gitlab-hook-middleware

1. Go to this page and download the library: Download thecodingmachine/gitlab-hook-middleware 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/ */

    

thecodingmachine / gitlab-hook-middleware example snippets



namespace Test;

class Listener implements HookListenerInterface {
    /**
     * @param \TheCodingMachine\GitlabHook\EventInterface $event
     */
     public function onEvent(EventInterface $event) {
         // Compute Push event
         if($event instanceof TheCodingMachine\GitlabHook\Model\Push::class) {
             // Display before
             echo $event->getBefore();
             // Display the project name
             echo $event->getProject()->getName();
         }
         // Compute MergeRequest event
         if($event instanceof TheCodingMachine\GitlabHook\Model\MergeRequest::class) {
             // Display target branch (this is in object_attributes)
             echo $event->getTargetBranch();
             // Get initial payload
             var_dump($event->getPayload());
         }
     }
}


// Create your listener
$listener = new Test\Listerner();

// Register your listener in the main HookReceiver instance
$hookReceiver = new TheCodingMachine\GitlabHook\HookReceiver([$listener]);

// Call handler function to execute check
// $payload is array (json_decode) of data send by Gitlab webhook
// $header is the result of HTTP_X_GITLAB_TOKEN header 
$hookReceiver->handle($payload, $header);


// Create your listener
$listener = new Test\Listerner();

// Register your listener in the main HookReceiver instance
$hookReceiver = new TheCodingMachine\GitlabHook\HookReceiver([$listener]);

// Create a PSR-3 logger
$logger = new Psr\Log\NullLogger(); 

// Inject hookReceiver in Gitlab middleware
// You must inject this middleware in the middleware pipe of your favorite framework.
// See your framework documentation on how to do that.
$middleware = new TheCodingMachine\GitlabHook\GitlabHookMiddleware($hookReceiver, 'secret', $logger);