Skip to content

Commit

Permalink
ref: macros service provider
Browse files Browse the repository at this point in the history
  • Loading branch information
fzldn committed Sep 20, 2024
1 parent e3415d3 commit 7bab53a
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 32 deletions.
31 changes: 15 additions & 16 deletions app/Models/Traits/LogsModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,13 @@ protected function getLogSubjectName(): string
return $this->name;
}

protected function getLogCauserName(): HtmlString
protected function getLogCauserName(): string
{
$user = auth('web')->user();
if ($user = auth('web')->user()) {
return $user->name;
}

return str($user->name ?? __('system'))
->wrap($user ? '**' : '*')
->inlineMarkdown()
->toHtmlString();
return __('System');
}

protected function getLogFirstSubject(): object
Expand All @@ -80,32 +79,32 @@ protected function getLogSecondSubjectName(): string

protected function logDescription(string $eventName): string
{
return __(':subject.type <strong>:subject.name</strong> was <strong>:event</strong> by :causer.name', [
return __(':subject.type :subject.name was :event by :causer.name', [
'subject.type' => str(class_basename(get_class($this)))->headline(),
'subject.name' => e($this->getLogSubjectName()),
'event' => $eventName,
'causer.name' => $this->getLogCauserName(),
'subject.name' => str(e($this->getLogSubjectName()))->wrapHtmlTag('strong'),
'event' => str($eventName)->wrapHtmlTag('em')->wrapHtmlTag('strong'),
'causer.name' => str(e($this->getLogCauserName()))->wrapHtmlTag('strong'),
]);
}

protected function logDescriptionPivot(string $eventName): string
{
return __(':subject.first.type <strong>:subject.first.name</strong> was <strong>:event</strong> :to :subject.second.type <strong>:subject.second.name</strong> by :causer.name', [
return __(':subject.first.type :subject.first.name was :event :to :subject.second.type :subject.second.name by :causer.name', [
'subject.first.type' => str(class_basename(get_class($this->getLogFirstSubject())))->headline(),
'subject.first.name' => e($this->getLogFirstSubjectName()),
'event' => match ($eventName) {
'subject.first.name' => str(e($this->getLogFirstSubjectName()))->wrapHtmlTag('strong'),
'event' => str(match ($eventName) {
'created' => __('attached'),
'deleted' => __('detached'),
default => $eventName,
},
})->wrapHtmlTag('em')->wrapHtmlTag('strong'),
'to' => match ($eventName) {
'created' => __('to'),
'deleted' => __('from'),
default => __('for'),
},
'subject.second.type' => str(class_basename(get_class($this->getLogSecondSubject())))->headline(),
'subject.second.name' => e($this->getLogSecondSubjectName()),
'causer.name' => $this->getLogCauserName(),
'subject.second.name' => str(e($this->getLogSecondSubjectName()))->wrapHtmlTag('strong'),
'causer.name' => str(e($this->getLogCauserName()))->wrapHtmlTag('strong'),
]);
}
}
16 changes: 0 additions & 16 deletions app/Providers/AppServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
use App\Models\User;
use Illuminate\Support\Facades\Gate;
use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Str;

class AppServiceProvider extends ServiceProvider
{
Expand Down Expand Up @@ -50,19 +49,4 @@ public function boot(): void
return null;
});
}

protected function macros(): void
{
Str::macro('wrapHtmlTag', function ($string, $tag = 'span', $attributes = []) {
$attrString = '';

if (!empty($attributes)) {
foreach ($attributes as $key => $value) {
$attrString .= " {$key}=\"{$value}\"";
}
}

return "<{$tag}{$attrString}>{$string}</{$tag}>";
});
}
}
40 changes: 40 additions & 0 deletions app/Providers/MacrosServiceProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

namespace App\Providers;

use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Str;
use Illuminate\Support\Stringable;

class MacrosServiceProvider extends ServiceProvider
{
/**
* Register services.
*/
public function register(): void
{
//
}

/**
* Bootstrap services.
*/
public function boot(): void
{
Str::macro('wrapHtmlTag', function (string $string, string $tag = 'span', array $attributes = []): string {
$attrString = '';

if (!empty($attributes)) {
foreach ($attributes as $key => $value) {
$attrString .= " {$key}=\"{$value}\"";
}
}

return "<{$tag}{$attrString}>{$string}</{$tag}>";
});

Stringable::macro('wrapHtmlTag', function (string $tag = 'span', array $attributes = []): Stringable {
return new Stringable(Str::wrapHtmlTag((string) $this, $tag, $attributes));
});
}
}
1 change: 1 addition & 0 deletions bootstrap/providers.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@

return [
App\Providers\AppServiceProvider::class,
App\Providers\MacrosServiceProvider::class,
App\Providers\Filament\AdminPanelProvider::class,
];

0 comments on commit 7bab53a

Please sign in to comment.