From 0bc657271b6f5bd57bdcf046d22def09700b9045 Mon Sep 17 00:00:00 2001 From: hsbmaulana Date: Tue, 14 Nov 2023 07:21:07 +0700 Subject: [PATCH] fix(core): initial --- README.md | 6 -- composer.json | 2 +- src/Events/Logs/Any.php | 40 -------------- src/Events/Logs/Update.php | 40 -------------- src/Listeners/Logs/AnyListener.php | 79 --------------------------- src/Listeners/Logs/UpdateListener.php | 41 -------------- src/Providers/LogServiceProvider.php | 8 --- src/Traits/LogTrait.php | 42 ++------------ 8 files changed, 7 insertions(+), 251 deletions(-) delete mode 100644 src/Events/Logs/Any.php delete mode 100644 src/Events/Logs/Update.php delete mode 100644 src/Listeners/Logs/AnyListener.php delete mode 100644 src/Listeners/Logs/UpdateListener.php diff --git a/README.md b/README.md index f70cb21..a89108c 100644 --- a/README.md +++ b/README.md @@ -37,12 +37,6 @@ php artisan vendor:publish --tag=tripteki-laravelphp-log-migrations $ php artisan migrate ``` -- Emit Event-Listener. - -``` -php artisan queue:work -``` - - Publish tests file into your project's directory with running (optionally) : ``` diff --git a/composer.json b/composer.json index af47163..45b6c13 100644 --- a/composer.json +++ b/composer.json @@ -1,6 +1,6 @@ { "name": "tripteki/laravelphp-log", - "version": "1.0.0", + "version": "1.0.1", "description": "Trip Teknologi's Laravel.php Logs", "readme": "README.md", diff --git a/src/Events/Logs/Any.php b/src/Events/Logs/Any.php deleted file mode 100644 index 043e1c1..0000000 --- a/src/Events/Logs/Any.php +++ /dev/null @@ -1,40 +0,0 @@ -event = $event; - $this->subject = $subject; - $this->object = $object; - } -}; diff --git a/src/Events/Logs/Update.php b/src/Events/Logs/Update.php deleted file mode 100644 index c20b521..0000000 --- a/src/Events/Logs/Update.php +++ /dev/null @@ -1,40 +0,0 @@ -event = $event; - $this->subject = $subject; - $this->object = $object; - } -}; diff --git a/src/Listeners/Logs/AnyListener.php b/src/Listeners/Logs/AnyListener.php deleted file mode 100644 index 3279d18..0000000 --- a/src/Listeners/Logs/AnyListener.php +++ /dev/null @@ -1,79 +0,0 @@ -object->activitylogOptions = $event->object->getActivitylogOptions(); - - if (! $event->object->shouldLogEvent($event->event)) return; - - $changes = $event->object->attributeValuesToBeLogged($event->event); - $description = $event->object->getDescriptionForEvent($event->event); - $log = $event->object->getLogNameToUse(); - - if ($description == "") return; - if ($event->object->isLogEmpty($changes) && ! $event->object->activitylogOptions->submitEmptyLogs) return; - - /** - * Pipeline. - */ - $event = app(Pipeline::class) - ->send(new EventLogBag($event->event, $event->object, $changes, $event->object->activitylogOptions)) - ->through(call_user_func($event->subject."::"."changesPipes")) - ->thenReturn(); - - /** - * Log. - */ - $logger = app(ActivityLogger::class) - ->useLog($log) - ->event($event->event) - ->performedOn($event->object) - ->withProperties($event->changes); - - if (method_exists($event->object, "tapActivity")) $logger->tap([ $event->object, "tapActivity", ], $event->event); - - $logger->log($description); - - /** - * Transaction. - */ - $event->object->activitylogOptions = null; - $event->object->save(); - - DB::commit(); - - } catch (Exception $exception) { - - DB::rollback(); - } - } -}; diff --git a/src/Listeners/Logs/UpdateListener.php b/src/Listeners/Logs/UpdateListener.php deleted file mode 100644 index 180171d..0000000 --- a/src/Listeners/Logs/UpdateListener.php +++ /dev/null @@ -1,41 +0,0 @@ -object->oldAttributes = call_user_func($event->subject."::"."logChanges", app($event->subject)->setRawAttributes($object->getRawOriginal())); - $event->object->save(); - - DB::commit(); - - } catch (Exception $exception) { - - DB::rollback(); - } - } -}; diff --git a/src/Providers/LogServiceProvider.php b/src/Providers/LogServiceProvider.php index 277acc5..f7f3d7d 100644 --- a/src/Providers/LogServiceProvider.php +++ b/src/Providers/LogServiceProvider.php @@ -2,15 +2,10 @@ namespace Tripteki\Log\Providers; -use Tripteki\Log\Events\Logs\Update; -use Tripteki\Log\Events\Logs\Any; -use Tripteki\Log\Listeners\Logs\UpdateListener; -use Tripteki\Log\Listeners\Logs\AnyListener; use Tripteki\Log\Models\Admin\Log; use Tripteki\Uid\Observers\UniqueIdObserver; use Tripteki\Log\Console\Commands\InstallCommand; use Tripteki\Repository\Providers\RepositoryServiceProvider as ServiceProvider; -use Illuminate\Support\Facades\Event; class LogServiceProvider extends ServiceProvider { @@ -174,8 +169,5 @@ protected function registerPublishers() public function dataEventListener() { Log::observe(UniqueIdObserver::class); - - Event::listen(Update::class, [ UpdateListener::class, "handle", ]); - Event::listen(Any::class, [ AnyListener::class, "handle", ]); } }; diff --git a/src/Traits/LogTrait.php b/src/Traits/LogTrait.php index 7d41921..d63c0ba 100644 --- a/src/Traits/LogTrait.php +++ b/src/Traits/LogTrait.php @@ -2,48 +2,14 @@ namespace Tripteki\Log\Traits; -use Tripteki\Log\Events\Logs\Update; -use Tripteki\Log\Events\Logs\Any; +use Illuminate\Support\Str; use Spatie\Activitylog\Traits\LogsActivity; use Spatie\Activitylog\LogOptions; -use Illuminate\Database\Eloquent\Model; trait LogTrait { use LogsActivity; - /** - * @return void - */ - protected static function bootLogsActivity(): void - { - $class = static::class; - - static::eventsToBeRecorded()->each(function ($event) use ($class) { - - if ($event === "updated") { - - static::updating(function (Model $model) use ($event, $class) { - - event(new Update($event, $class, $model)); - }); - } - - static::$event(function (Model $model) use ($event, $class) { - - event(new Any($event, $class, $model)); - }); - }); - } - - /** - * @return array - */ - public static function changesPipes(): array - { - return static::$changesPipes; - } - /** * @return \Spatie\Activitylog\LogOptions */ @@ -51,7 +17,11 @@ public function getActivitylogOptions(): LogOptions { $log = LogOptions::defaults() ->logOnlyDirty() - ->useLogName(isset(static::$recordName) ? static::$recordName : get_class($this)); + ->useLogName(isset(static::$recordName) ? static::$recordName : Str::beforeLast(get_class($this), "\\")) + ->setDescriptionForEvent(function ($event) { + + return get_class($this).".".$event.".".$this->{$this->getKeyName()}; + }); if (isset(static::$recordLists)) {