Skip to content

Commit

Permalink
v3 (#441)
Browse files Browse the repository at this point in the history
  • Loading branch information
freekmurze committed Oct 16, 2018
1 parent 28f4814 commit c1dd493
Show file tree
Hide file tree
Showing 31 changed files with 399 additions and 256 deletions.
1 change: 0 additions & 1 deletion .travis.yml
@@ -1,7 +1,6 @@
language: php

php:
- 7.0
- 7.1
- 7.2

Expand Down
8 changes: 8 additions & 0 deletions CHANGELOG.md
Expand Up @@ -2,6 +2,14 @@

All notable changes to `spatie/laravel-activitylog` will be documented in this file

## 3.0.0 - 2018-10-16
- the preferred way to get changes on an `Activity` model is through the `changes` property instead of the `changes()` function
- the `activity` relation of the `CausesActivity` trait has been renamed to `actions`
- the `activity` relation of the `LogsActivity` trait has been renamed to `activities`
- the deprecated `loggedActivity` relation has been removed
- fix for setting a custom table name for the `Activity` model via the `$table` property
- support for PHP 7.0 has been dropped

## 2.8.4. - 2018-09-23
- improve migration

Expand Down
15 changes: 10 additions & 5 deletions README.md
Expand Up @@ -138,28 +138,33 @@ return [
'subject_returns_soft_deleted_models' => false,

/*
* This model will be used to log activity. The only requirement is that
* it should be or extend the Spatie\Activitylog\Models\Activity model.
* This model will be used to log activity.
* It should be implements the Spatie\Activitylog\Contracts\Activity interface
* and extend Illuminate\Database\Eloquent\Model.
*/
'activity_model' => \Spatie\Activitylog\Models\Activity::class,

/*
* This is the name of the table that will be created by the migration and
* used by the Activity model shipped with this package.
*/
'table_name' => 'activity_log',
];

```

## Changelog

Please see [CHANGELOG](CHANGELOG.md) for more information about recent changes.

## Upgrading

Please see [UPGRADING](UPGRADING.md) for details.


## Testing

``` bash
$ composer test
composer test
```

## Contributing
Expand Down
8 changes: 8 additions & 0 deletions UPGRADING.md
@@ -0,0 +1,8 @@
## From v2 to v3

- if you are using a custom `Activity` model, you should let it implement the new `Spatie\Activitylog\Contracts\Activity` interface
- the preferred way to get changes on an `Activity` model is through the `changes` property instead of the `changes()` function. Change all usages from
`$activity->changes()` to `$activity->changes`
- the `activity` relation of the `CausesActivity` trait has been renamed to `actions`. Rename all uses from `$user->activity` to `$user->actions`
- the `activity` relation of the `LogsActivity` trait has been renamed to `activities`. Rename all uses from `$yourModel->activity` to `$yourModel->activities`.
- the deprecated `loggedActivity` relation has been removed. Use `activities` instead.
4 changes: 2 additions & 2 deletions composer.json
Expand Up @@ -25,15 +25,15 @@
}
],
"require": {
"php" : "^7.0",
"php" : "^7.1",
"illuminate/config": "~5.5.0|~5.6.0|~5.7.0",
"illuminate/database": "~5.5.0|~5.6.0|~5.7.0",
"illuminate/support": "~5.5.0|~5.6.0|~5.7.0",
"spatie/string": "^2.1"

},
"require-dev": {
"phpunit/phpunit": "^6.2|^7.0",
"phpunit/phpunit": "^7.4",
"orchestra/testbench": "~3.5.0|~3.6.0|~3.7.0"
},
"autoload": {
Expand Down
5 changes: 3 additions & 2 deletions config/activitylog.php
Expand Up @@ -31,8 +31,9 @@
'subject_returns_soft_deleted_models' => false,

/*
* This model will be used to log activity. The only requirement is that
* it should be or extend the Spatie\Activitylog\Models\Activity model.
* This model will be used to log activity.
* It should be implements the Spatie\Activitylog\Contracts\Activity interface
* and extend Illuminate\Database\Eloquent\Model.
*/
'activity_model' => \Spatie\Activitylog\Models\Activity::class,

Expand Down
3 changes: 3 additions & 0 deletions phpunit.xml.dist
Expand Up @@ -19,4 +19,7 @@
<directory suffix=".php">src/</directory>
</whitelist>
</filter>
<php>
<env name="DB_CONNECTION" value="testing" />
</php>
</phpunit>
30 changes: 1 addition & 29 deletions src/ActivityLogger.php
Expand Up @@ -4,8 +4,8 @@

use Illuminate\Auth\AuthManager;
use Illuminate\Database\Eloquent\Model;
use Spatie\Activitylog\Models\Activity;
use Illuminate\Support\Traits\Macroable;
use Spatie\Activitylog\Contracts\Activity;
use Illuminate\Contracts\Config\Repository;
use Spatie\Activitylog\Exceptions\CouldNotLogActivity;

Expand Down Expand Up @@ -73,11 +73,6 @@ public function on(Model $model)
return $this->performedOn($model);
}

/**
* @param \Illuminate\Database\Eloquent\Model|int|string $modelOrId
*
* @return $this
*/
public function causedBy($modelOrId)
{
if ($modelOrId === null) {
Expand All @@ -96,24 +91,13 @@ public function by($modelOrId)
return $this->causedBy($modelOrId);
}

/**
* @param array|\Illuminate\Support\Collection $properties
*
* @return $this
*/
public function withProperties($properties)
{
$this->properties = collect($properties);

return $this;
}

/**
* @param string $key
* @param mixed $value
*
* @return $this
*/
public function withProperty(string $key, $value)
{
$this->properties->put($key, $value);
Expand Down Expand Up @@ -147,11 +131,6 @@ public function disableLogging()
return $this;
}

/**
* @param string $description
*
* @return null|mixed
*/
public function log(string $description)
{
if ($this->logStatus->disabled()) {
Expand Down Expand Up @@ -179,13 +158,6 @@ public function log(string $description)
return $activity;
}

/**
* @param \Illuminate\Database\Eloquent\Model|int|string $modelOrId
*
* @throws \Spatie\Activitylog\Exceptions\CouldNotLogActivity
*
* @return \Illuminate\Database\Eloquent\Model
*/
protected function normalizeCauser($modelOrId): Model
{
if ($modelOrId instanceof Model) {
Expand Down
8 changes: 5 additions & 3 deletions src/ActivitylogServiceProvider.php
Expand Up @@ -4,8 +4,9 @@

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\ServiceProvider;
use Spatie\Activitylog\Models\Activity;
use Spatie\Activitylog\Contracts\Activity;
use Spatie\Activitylog\Exceptions\InvalidConfiguration;
use Spatie\Activitylog\Models\Activity as ActivityModel;

class ActivitylogServiceProvider extends ServiceProvider
{
Expand Down Expand Up @@ -41,9 +42,10 @@ public function register()

public static function determineActivityModel(): string
{
$activityModel = config('activitylog.activity_model') ?? Activity::class;
$activityModel = config('activitylog.activity_model') ?? ActivityModel::class;

if (! is_a($activityModel, Activity::class, true)) {
if (! is_a($activityModel, Activity::class, true)
|| ! is_a($activityModel, Model::class, true)) {
throw InvalidConfiguration::modelIsNotValid($activityModel);
}

Expand Down
10 changes: 0 additions & 10 deletions src/CleanActivitylogCommand.php
Expand Up @@ -8,19 +8,9 @@

class CleanActivitylogCommand extends Command
{
/**
* The console command name.
*
* @var string
*/
protected $signature = 'activitylog:clean
{log? : (optional) The log name that will be cleaned.}';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Clean up old records from the activity log.';

public function handle()
Expand Down
25 changes: 25 additions & 0 deletions src/Contracts/Activity.php
@@ -0,0 +1,25 @@
<?php

namespace Spatie\Activitylog\Contracts;

use Illuminate\Support\Collection;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Relations\MorphTo;

interface Activity
{
public function subject(): MorphTo;

public function causer(): MorphTo;

public function getExtraProperty(string $propertyName);

public function changes(): Collection;

public function scopeInLog(Builder $query, ...$logNames): Builder;

public function scopeCausedBy(Builder $query, Model $causer): Builder;

public function scopeForSubject(Builder $query, Model $subject): Builder;
}
5 changes: 3 additions & 2 deletions src/Exceptions/InvalidConfiguration.php
Expand Up @@ -3,12 +3,13 @@
namespace Spatie\Activitylog\Exceptions;

use Exception;
use Spatie\Activitylog\Models\Activity;
use Illuminate\Database\Eloquent\Model;
use Spatie\Activitylog\Contracts\Activity;

class InvalidConfiguration extends Exception
{
public static function modelIsNotValid(string $className)
{
return new static("The given model class `$className` does not extend `".Activity::class.'`');
return new static("The given model class `$className` does not implement `".Activity::class.'` or it does not extend `'.Model::class.'`');
}
}
41 changes: 11 additions & 30 deletions src/Models/Activity.php
Expand Up @@ -6,11 +6,10 @@
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Relations\MorphTo;
use Spatie\Activitylog\Contracts\Activity as ActivityContract;

class Activity extends Model
class Activity extends Model implements ActivityContract
{
protected $table;

public $guarded = [];

protected $casts = [
Expand All @@ -19,7 +18,9 @@ class Activity extends Model

public function __construct(array $attributes = [])
{
$this->table = config('activitylog.table_name');
if (! isset($this->table)) {
$this->setTable(config('activitylog.table_name'));
}

parent::__construct($attributes);
}
Expand All @@ -38,13 +39,6 @@ public function causer(): MorphTo
return $this->morphTo();
}

/**
* Get the extra properties with the given name.
*
* @param string $propertyName
*
* @return mixed
*/
public function getExtraProperty(string $propertyName)
{
return array_get($this->properties->toArray(), $propertyName);
Expand All @@ -56,9 +50,12 @@ public function changes(): Collection
return new Collection();
}

return collect(array_filter($this->properties->toArray(), function ($key) {
return in_array($key, ['attributes', 'old']);
}, ARRAY_FILTER_USE_KEY));
return $this->properties->only(['attributes', 'old']);
}

public function getChangesAttribute(): Collection
{
return $this->changes();
}

public function scopeInLog(Builder $query, ...$logNames): Builder
Expand All @@ -70,29 +67,13 @@ public function scopeInLog(Builder $query, ...$logNames): Builder
return $query->whereIn('log_name', $logNames);
}

/**
* Scope a query to only include activities by a given causer.
*
* @param \Illuminate\Database\Eloquent\Builder $query
* @param \Illuminate\Database\Eloquent\Model $causer
*
* @return \Illuminate\Database\Eloquent\Builder
*/
public function scopeCausedBy(Builder $query, Model $causer): Builder
{
return $query
->where('causer_type', $causer->getMorphClass())
->where('causer_id', $causer->getKey());
}

/**
* Scope a query to only include activities for a given subject.
*
* @param \Illuminate\Database\Eloquent\Builder $query
* @param \Illuminate\Database\Eloquent\Model $subject
*
* @return \Illuminate\Database\Eloquent\Builder
*/
public function scopeForSubject(Builder $query, Model $subject): Builder
{
return $query
Expand Down
13 changes: 5 additions & 8 deletions src/Traits/CausesActivity.php
Expand Up @@ -7,14 +7,11 @@

trait CausesActivity
{
public function activity(): MorphMany
public function actions(): MorphMany
{
return $this->morphMany(ActivitylogServiceProvider::determineActivityModel(), 'causer');
}

/** @deprecated Use activity() instead */
public function loggedActivity(): MorphMany
{
return $this->activity();
return $this->morphMany(
ActivitylogServiceProvider::determineActivityModel(),
'causer'
);
}
}
16 changes: 0 additions & 16 deletions src/Traits/HasActivity.php

This file was deleted.

2 changes: 1 addition & 1 deletion src/Traits/LogsActivity.php
Expand Up @@ -54,7 +54,7 @@ public function enableLogging()
return $this;
}

public function activity(): MorphMany
public function activities(): MorphMany
{
return $this->morphMany(ActivitylogServiceProvider::determineActivityModel(), 'subject');
}
Expand Down

0 comments on commit c1dd493

Please sign in to comment.