| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace Spatie\Activitylog\Traits; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use Illuminate\Database\Eloquent\Model; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use Illuminate\Database\Eloquent\Relations\MorphMany; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use Illuminate\Database\Eloquent\SoftDeletes; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use Illuminate\Support\Arr; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use Illuminate\Support\Collection; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use Spatie\Activitylog\ActivityLogger; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | use Spatie\Activitylog\ActivitylogServiceProvider; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | use Spatie\Activitylog\ActivityLogStatus; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | trait LogsActivity | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |     use DetectsChanges; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |     protected $enableLoggingModelsEvents = true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 | 216 |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |     protected static function bootLogsActivity() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |         static::eventsToBeRecorded()->each(function ($eventName) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 | 212 |  |             return static::$eventName(function (Model $model) use ($eventName) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 | 12 |  |                 if (! $model->shouldLogEvent($eventName)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |                     return; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 | 208 |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |                 $description = $model->getDescriptionForEvent($eventName); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 | 208 |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |                 $logName = $model->getLogNameToUse($eventName); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 | 208 |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |                 if ($description == '') { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |                     return; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 | 208 |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |                 $attrs = $model->attributeValuesToBeLogged($eventName); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 | 208 |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 | 4 |  |                 if ($model->isLogEmpty($attrs) && ! $model->shouldSubmitEmptyLogs()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |                     return; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 | 208 |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 | 208 |  |                 $logger = app(ActivityLogger::class) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 | 208 |  |                     ->useLog($logName) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 | 208 |  |                     ->performedOn($model) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |                     ->withProperties($attrs); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 | 208 |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 | 8 |  |                 if (method_exists($model, 'tapActivity')) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |                     $logger->tap([$model, 'tapActivity'], $eventName); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 | 208 |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 | 216 |  |                 $logger->log($description); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 | 216 |  |             }); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 | 216 |  |         }); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 | 72 |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |     public function shouldSubmitEmptyLogs(): bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 | 72 |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |         return ! isset(static::$submitEmptyLogs) ? true : static::$submitEmptyLogs; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 | 208 |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |     public function isLogEmpty($attrs): bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 | 208 |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |         return empty($attrs['attributes'] ?? []) && empty($attrs['old'] ?? []); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 | 12 |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |     public function disableLogging() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 | 12 |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |         $this->enableLoggingModelsEvents = false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 | 12 |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |         return $this; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 | 4 |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |     public function enableLogging() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 | 4 |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |         $this->enableLoggingModelsEvents = true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 | 4 |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |         return $this; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 | 28 |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |     public function activities(): MorphMany | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 | 28 |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |         return $this->morphMany(ActivitylogServiceProvider::determineActivityModel(), 'subject'); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 | 204 |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |     public function getDescriptionForEvent(string $eventName): string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 | 204 |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |         return $eventName; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 | 204 |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |     public function getLogNameToUse(string $eventName = ''): string | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 | 204 |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 | 4 |  |         if (isset(static::$logName)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |             return static::$logName; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 | 200 |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |         return config('activitylog.default_log_name'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |     /* | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |      * Get the event names that should be recorded. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 | 216 |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |     protected static function eventsToBeRecorded(): Collection | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 | 216 |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |         if (isset(static::$recordEvents)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |             return collect(static::$recordEvents); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 | 216 |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 | 216 |  |         $events = collect([ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |             'created', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |             'updated', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |             'deleted', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |         ]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 | 216 |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 | 76 |  |         if (collect(class_uses_recursive(static::class))->contains(SoftDeletes::class)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |             $events->push('restored'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 | 216 |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |         return $events; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 | 208 |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |     public function attributesToBeIgnored(): array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 | 208 |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 | 208 |  |         if (! isset(static::$ignoreChangedAttributes)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |             return []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |         return static::$ignoreChangedAttributes; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 129 | 212 |  |  | 
            
                                                        
            
                                    
            
            
                | 130 |  |  |     protected function shouldLogEvent(string $eventName): bool | 
            
                                                        
            
                                    
            
            
                | 131 | 212 |  |     { | 
            
                                                        
            
                                    
            
            
                | 132 | 8 |  |         $logStatus = app(ActivityLogStatus::class); | 
            
                                                        
            
                                    
            
            
                | 133 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 134 |  |  |         if (! $this->enableLoggingModelsEvents || $logStatus->disabled()) { | 
            
                                                        
            
                                    
            
            
                | 135 | 208 |  |             return false; | 
            
                                                        
            
                                    
            
            
                | 136 | 24 |  |         } | 
            
                                                        
            
                                    
            
            
                | 137 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 138 |  |  |         if (! in_array($eventName, ['created', 'updated'])) { | 
            
                                                        
            
                                    
            
            
                | 139 | 208 |  |             return true; | 
            
                                                        
            
                                    
            
            
                | 140 | 4 |  |         } | 
            
                                                        
            
                                    
            
            
                | 141 | 4 |  |  | 
            
                                                        
            
                                    
            
            
                | 142 |  |  |         if (Arr::has($this->getDirty(), 'deleted_at')) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                        
            
                                    
            
            
                | 143 |  |  |             if ($this->getDirty()['deleted_at'] === null) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                        
            
                                    
            
            
                | 144 |  |  |                 return false; | 
            
                                                        
            
                                    
            
            
                | 145 |  |  |             } | 
            
                                                        
            
                                    
            
            
                | 146 | 208 |  |         } | 
            
                                                        
            
                                    
            
            
                | 147 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 148 |  |  |         //do not log update event if only ignored attributes are changed | 
            
                                                        
            
                                    
            
            
                | 149 |  |  |         return (bool) count(Arr::except($this->getDirty(), $this->attributesToBeIgnored())); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                        
            
                                    
            
            
                | 150 |  |  |     } | 
            
                                                        
            
                                    
            
            
                | 151 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 152 |  |  |  | 
            
                        
This check looks for methods that are used by a trait but not required by it.
To illustrate, let’s look at the following code example
The trait
Idableprovides a methodequalsIdthat in turn relies on the methodgetId(). If this method does not exist on a class mixing in this trait, the method will fail.Adding the
getId()as an abstract method to the trait will make sure it is available.