diff --git a/readme.md b/readme.md index 13dd12e..f8fe081 100644 --- a/readme.md +++ b/readme.md @@ -91,6 +91,20 @@ $panel->plugins([ ]); ``` +### Git Branch + +You can enable the display of the current git branch in the badge via `->showGitBranch()`. This requires the `exec()` function to be enabled in your PHP configuration. + +```php +use pxlrbt\FilamentEnvironmentIndicator\EnvironmentIndicatorPlugin; +use Filament\Support\Colors\Color; + +$panel->plugins([ + EnvironmentIndicatorPlugin::make() + ->showGitBranch() +]); +``` + ## Contributing If you want to contribute to this packages, you may want to test it in a real Filament project: diff --git a/resources/views/badge.blade.php b/resources/views/badge.blade.php index eb41424..c57b38e 100644 --- a/resources/views/badge.blade.php +++ b/resources/views/badge.blade.php @@ -13,4 +13,8 @@ class=" " > {{ $environment }} + + @isset($branch) + ({{ $branch }}) + @endisset diff --git a/src/EnvironmentIndicatorPlugin.php b/src/EnvironmentIndicatorPlugin.php index 4f56b83..328ec3b 100644 --- a/src/EnvironmentIndicatorPlugin.php +++ b/src/EnvironmentIndicatorPlugin.php @@ -9,6 +9,7 @@ use Filament\Support\Concerns\EvaluatesClosures; use Illuminate\Support\Facades\View; use Illuminate\Support\HtmlString; +use Throwable; class EnvironmentIndicatorPlugin implements Plugin { @@ -22,6 +23,8 @@ class EnvironmentIndicatorPlugin implements Plugin public array|Closure|null $color = null; + public bool|Closure|null $showGitBranch = null; + public static function make(): static { $plugin = app(static::class); @@ -83,6 +86,7 @@ public function register(Panel $panel): void return View::make('filament-environment-indicator::badge', [ 'color' => $this->getColor(), 'environment' => ucfirst(app()->environment()), + 'branch' => $this->getGitBranch() ]); }); @@ -131,6 +135,13 @@ public function showBorder(bool|Closure $showBorder = true): static return $this; } + public function showGitBranch(bool|Closure $showGitBranch = true): static + { + $this->showGitBranch = $showGitBranch; + + return $this; + } + public function color(array|Closure $color = Color::Pink): static { $this->color = $color; @@ -142,4 +153,17 @@ protected function getColor(): array { return $this->evaluate($this->color); } + + protected function getGitBranch(): ?string + { + if (! $this->evaluate($this->showGitBranch)) { + return null; + } + + try { + return trim(exec('git branch --show-current')); + } catch (Throwable $th) { + return null; + } + } }