src/DataCollector/GitDataCollector.php line 15

Open in your IDE?
  1. <?php
  2. namespace App\DataCollector;
  3. use Symfony\Component\HttpFoundation\Request;
  4. use Symfony\Component\HttpFoundation\Response;
  5. use Symfony\Component\HttpKernel\DataCollector\DataCollector;
  6. use Symfony\Component\Process\Process;
  7. class GitDataCollector extends DataCollector
  8. {
  9.     public function collect(Request $requestResponse $response, \Exception $exception null)
  10.     {
  11.         $this->data = [
  12.             'commit_id' => $this->getCommitId(),
  13.             'commit_message' => $this->getCommitMessage(),
  14.         ];
  15.     }
  16.     // we will use this name in the config later
  17.     public function getName()
  18.     {
  19.         return 'app.git_data_collector';
  20.     }
  21.     public function reset()
  22.     {
  23.         $this->data = array();
  24.     }
  25.     public function getGitCommitId()
  26.     {
  27.         return $this->data['commit_id'];
  28.     }
  29.     public function getGitCommitMessage()
  30.     {
  31.         return $this->data['commit_message'];
  32.     }
  33.     protected function getCommitId()
  34.     {
  35.         $process = new Process('git rev-parse --short HEAD');
  36.         $process->run();
  37.         if (!$process->isSuccessful()) {
  38.             return 'not found';
  39.         }
  40.         return $process->getOutput();
  41.     }
  42.     protected function getCommitMessage()
  43.     {
  44.         $process = new Process('git log -1 --oneline');
  45.         $process->run();
  46.         if (!$process->isSuccessful()) {
  47.             return 'not found';
  48.         }
  49.         return $process->getOutput();
  50.     }
  51. }