<?php
namespace App\DataCollector;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\DataCollector\DataCollector;
use Symfony\Component\Process\Process;
class GitDataCollector extends DataCollector
{
public function collect(Request $request, Response $response, \Exception $exception = null)
{
$this->data = [
'commit_id' => $this->getCommitId(),
'commit_message' => $this->getCommitMessage(),
];
}
// we will use this name in the config later
public function getName()
{
return 'app.git_data_collector';
}
public function reset()
{
$this->data = array();
}
public function getGitCommitId()
{
return $this->data['commit_id'];
}
public function getGitCommitMessage()
{
return $this->data['commit_message'];
}
protected function getCommitId()
{
$process = new Process('git rev-parse --short HEAD');
$process->run();
if (!$process->isSuccessful()) {
return 'not found';
}
return $process->getOutput();
}
protected function getCommitMessage()
{
$process = new Process('git log -1 --oneline');
$process->run();
if (!$process->isSuccessful()) {
return 'not found';
}
return $process->getOutput();
}
}