Subversion-Projekte lars-tiefland.laravel_shop

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
148 lars 1
<?php
2
 
3
namespace Spatie\FlareClient\FlareMiddleware;
4
 
5
use Closure;
6
use Spatie\FlareClient\Report;
7
use Symfony\Component\Process\Process;
8
use Throwable;
9
 
10
class AddGitInformation
11
{
12
    protected ?string $baseDir = null;
13
 
14
    public function handle(Report $report, Closure $next)
15
    {
16
        try {
17
            $this->baseDir = $this->getGitBaseDirectory();
18
 
19
            if (! $this->baseDir) {
20
                return $next($report);
21
            }
22
 
23
            $report->group('git', [
24
                'hash' => $this->hash(),
25
                'message' => $this->message(),
26
                'tag' => $this->tag(),
27
                'remote' => $this->remote(),
28
                'isDirty' => ! $this->isClean(),
29
            ]);
30
        } catch (Throwable) {
31
        }
32
 
33
        return $next($report);
34
    }
35
 
36
    protected function hash(): ?string
37
    {
38
        return $this->command("git log --pretty=format:'%H' -n 1") ?: null;
39
    }
40
 
41
    protected function message(): ?string
42
    {
43
        return $this->command("git log --pretty=format:'%s' -n 1") ?: null;
44
    }
45
 
46
    protected function tag(): ?string
47
    {
48
        return $this->command('git describe --tags --abbrev=0') ?: null;
49
    }
50
 
51
    protected function remote(): ?string
52
    {
53
        return $this->command('git config --get remote.origin.url') ?: null;
54
    }
55
 
56
    protected function isClean(): bool
57
    {
58
        return empty($this->command('git status -s'));
59
    }
60
 
61
    protected function getGitBaseDirectory(): ?string
62
    {
63
        /** @var Process $process */
64
        $process = Process::fromShellCommandline("echo $(git rev-parse --show-toplevel)")->setTimeout(1);
65
 
66
        $process->run();
67
 
68
        if (! $process->isSuccessful()) {
69
            return null;
70
        }
71
 
72
        $directory = trim($process->getOutput());
73
 
74
        if (! file_exists($directory)) {
75
            return null;
76
        }
77
 
78
        return $directory;
79
    }
80
 
81
    protected function command($command)
82
    {
83
        $process = Process::fromShellCommandline($command, $this->baseDir)->setTimeout(1);
84
 
85
        $process->run();
86
 
87
        return trim($process->getOutput());
88
    }
89
}