Skip to content

Commit

Permalink
Merge pull request #866 from nagi1/v4
Browse files Browse the repository at this point in the history
V4 is getting ready
  • Loading branch information
Gummibeer authored Apr 19, 2021
2 parents 44f5315 + 3a8dc7b commit 964ab9d
Show file tree
Hide file tree
Showing 25 changed files with 1,571 additions and 491 deletions.
8 changes: 2 additions & 6 deletions .github/workflows/run-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,13 @@ jobs:
strategy:
fail-fast: false
matrix:
php: ["8.0", "7.4", "7.3"]
laravel: [8.*, 7.*, 6.*]
php: ["8.0"]
laravel: [8.*]
dependency-version: [prefer-lowest, prefer-stable]
os: [ubuntu-latest, windows-latest]
include:
- laravel: 8.*
testbench: 6.*
- laravel: 7.*
testbench: 5.*
- laravel: 6.*
testbench: 4.*

name: P${{ matrix.php }} - L${{ matrix.laravel }} - ${{ matrix.dependency-version }} - ${{ matrix.os }}

Expand Down
11 changes: 6 additions & 5 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,15 @@
}
],
"require": {
"php": "^7.3 || ^8.0",
"illuminate/config": "^6.0 || ^7.0 || ^8.0",
"illuminate/database": "^6.0 || ^7.0 || ^8.0",
"illuminate/support": "^6.0 || ^7.0 || ^8.0"
"php": "^8.0",
"illuminate/config": "^8.0",
"illuminate/database": "^8.0",
"illuminate/support": "^8.0",
"spatie/laravel-package-tools": "^1.6"
},
"require-dev": {
"ext-json": "*",
"orchestra/testbench": "^4.0 || ^5.0 || ^6.0",
"orchestra/testbench": "^6.0",
"phpunit/phpunit": "^9.3"
},
"config": {
Expand Down
28 changes: 28 additions & 0 deletions migrations/add_batch_uuid_column_to_activity_log_table.php.stub
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class AddBatchUuidColumnToActivityLogTable extends Migration
{
/**
* Run the migrations.
*/
public function up()
{
Schema::connection(config('activitylog.database_connection'))->table(config('activitylog.table_name'), function (Blueprint $table) {
$table->uuid('batch_uuid')->nullable()->after('properties');
});
}

/**
* Reverse the migrations.
*/
public function down()
{
Schema::connection(config('activitylog.database_connection'))->table(config('activitylog.table_name'), function (Blueprint $table) {
$table->dropColumn('batch_uuid');
});
}
}
89 changes: 35 additions & 54 deletions src/ActivityLogger.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,165 +2,161 @@

namespace Spatie\Activitylog;

use Illuminate\Auth\AuthManager;
use Closure;
use DateTimeInterface;
use Illuminate\Contracts\Config\Repository;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Arr;
use Illuminate\Support\Carbon;
use Illuminate\Support\Str;
use Illuminate\Support\Traits\Macroable;
use Spatie\Activitylog\Contracts\Activity as ActivityContract;
use Spatie\Activitylog\Exceptions\CouldNotLogActivity;

class ActivityLogger
{
use Macroable;

/** @var \Illuminate\Auth\AuthManager */
protected $auth;
protected ?string $defaultLogName = null;

protected $defaultLogName = '';
protected CauserResolver $causerResolver;

/** @var string */
protected $authDriver;
protected ActivityLogStatus $logStatus;

/** @var \Spatie\Activitylog\ActivityLogStatus */
protected $logStatus;
protected ?ActivityContract $activity = null;

/** @var \Spatie\Activitylog\Contracts\Activity */
protected $activity;
protected LogBatch $batch;

public function __construct(AuthManager $auth, Repository $config, ActivityLogStatus $logStatus)
public function __construct(Repository $config, ActivityLogStatus $logStatus, LogBatch $batch, CauserResolver $causerResolver)
{
$this->auth = $auth;
$this->causerResolver = $causerResolver;

$this->authDriver = $config['activitylog']['default_auth_driver'] ?? $auth->getDefaultDriver();
$this->batch = $batch;

$this->defaultLogName = $config['activitylog']['default_log_name'];

$this->logStatus = $logStatus;
}

public function setLogStatus(ActivityLogStatus $logStatus)
public function setLogStatus(ActivityLogStatus $logStatus): static
{
$this->logStatus = $logStatus;

return $this;
}

public function performedOn(Model $model)
public function performedOn(Model $model): static
{
$this->getActivity()->subject()->associate($model);

return $this;
}

public function on(Model $model)
public function on(Model $model): static
{
return $this->performedOn($model);
}

public function causedBy($modelOrId)
public function causedBy(Model | int | string | null $modelOrId): static
{
if ($modelOrId === null) {
return $this;
}

$model = $this->normalizeCauser($modelOrId);
$model = $this->causerResolver->resolve($modelOrId);

$this->getActivity()->causer()->associate($model);

return $this;
}

public function by($modelOrId)
public function by(Model | int | string | null $modelOrId): static
{
return $this->causedBy($modelOrId);
}

public function causedByAnonymous()
public function causedByAnonymous(): static
{
$this->activity->causer_id = null;
$this->activity->causer_type = null;

return $this;
}

public function byAnonymous()
public function byAnonymous(): static
{
return $this->causedByAnonymous();
}

public function event(string $event)
public function event(string $event): static
{
return $this->setEvent($event);
}

public function setEvent(string $event)
public function setEvent(string $event): static
{
$this->activity->event = $event;

return $this;
}

public function withProperties($properties)
public function withProperties(mixed $properties): static
{
$this->getActivity()->properties = collect($properties);

return $this;
}

public function withProperty(string $key, $value)
public function withProperty(string $key, mixed $value): static
{
$this->getActivity()->properties = $this->getActivity()->properties->put($key, $value);

return $this;
}

public function createdAt(Carbon $dateTime)
public function createdAt(DateTimeInterface $dateTime): static
{
$this->getActivity()->created_at = $dateTime;
$this->getActivity()->created_at = Carbon::instance($dateTime);

return $this;
}

public function useLog(string $logName)
public function useLog(string $logName): static
{
$this->getActivity()->log_name = $logName;

return $this;
}

public function inLog(string $logName)
public function inLog(string $logName): static
{
return $this->useLog($logName);
}

public function tap(callable $callback, string $eventName = null)
public function tap(callable $callback, string $eventName = null): static
{
call_user_func($callback, $this->getActivity(), $eventName);

return $this;
}

public function enableLogging()
public function enableLogging(): static
{
$this->logStatus->enable();

return $this;
}

public function disableLogging()
public function disableLogging(): static
{
$this->logStatus->disable();

return $this;
}

public function log(string $description)
public function log(string $description): ?ActivityContract
{
if ($this->logStatus->disabled()) {
return;
return null;
}

$activity = $this->activity;
Expand All @@ -177,7 +173,7 @@ public function log(string $description)
return $activity;
}

public function withoutLogs(callable $callback)
public function withoutLogs(Closure $callback): mixed
{
if ($this->logStatus->disabled()) {
return $callback();
Expand All @@ -192,23 +188,6 @@ public function withoutLogs(callable $callback)
}
}

protected function normalizeCauser($modelOrId): Model
{
if ($modelOrId instanceof Model) {
return $modelOrId;
}

$guard = $this->auth->guard($this->authDriver);
$provider = method_exists($guard, 'getProvider') ? $guard->getProvider() : null;
$model = method_exists($provider, 'retrieveById') ? $provider->retrieveById($modelOrId) : null;

if ($model instanceof Model) {
return $model;
}

throw CouldNotLogActivity::couldNotDetermineUser($modelOrId);
}

protected function replacePlaceholders(string $description, ActivityContract $activity): string
{
return preg_replace_callback('/:[a-z0-9._-]+/i', function ($match) use ($activity) {
Expand Down Expand Up @@ -241,7 +220,9 @@ protected function getActivity(): ActivityContract
$this
->useLog($this->defaultLogName)
->withProperties([])
->causedBy($this->auth->guard($this->authDriver)->user());
->causedBy($this->causerResolver->resolve());

$this->activity->batch_uuid = $this->batch->getUuid();
}

return $this->activity;
Expand Down
57 changes: 18 additions & 39 deletions src/ActivitylogServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,36 @@
namespace Spatie\Activitylog;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Str;
use Spatie\Activitylog\Contracts\Activity;
use Spatie\Activitylog\Contracts\Activity as ActivityContract;
use Spatie\Activitylog\Exceptions\InvalidConfiguration;
use Spatie\Activitylog\Models\Activity as ActivityModel;
use Spatie\LaravelPackageTools\Package;
use Spatie\LaravelPackageTools\PackageServiceProvider;

class ActivitylogServiceProvider extends ServiceProvider
class ActivitylogServiceProvider extends PackageServiceProvider
{
public function boot()
public function configurePackage(Package $package): void
{
$this->bootConfig();
$this->bootMigrations();
$package
->name('laravel-activitylog')
->hasConfigFile('activitylog')
->hasMigrations([
'CreateActivityLogTable',
'AddEventColumnToActivityLogTable',
'AddBatchUuidColumnToActivityLogTable',
])
->hasCommand(CleanActivitylogCommand::class);
}

public function register()

public function registeringPackage()
{
$this->app->bind('command.activitylog:clean', CleanActivitylogCommand::class);
$this->app->bind(ActivityLogger::class);

$this->commands([
'command.activitylog:clean',
]);
$this->app->singleton(LogBatch::class);

$this->app->bind(ActivityLogger::class);
$this->app->singleton(CauserResolver::class);

$this->app->singleton(ActivityLogStatus::class);
}
Expand All @@ -49,31 +55,4 @@ public static function getActivityModelInstance(): ActivityContract

return new $activityModelClassName();
}

protected function bootConfig(): void
{
$this->publishes([
__DIR__.'/../config/activitylog.php' => config_path('activitylog.php'),
], 'config');

$this->mergeConfigFrom(__DIR__.'/../config/activitylog.php', 'activitylog');
}

protected function bootMigrations(): void
{
foreach ([
'CreateActivityLogTable',
'AddEventColumnToActivityLogTable',
] as $i => $migration) {
if (! class_exists($migration)) {
$this->publishes([
__DIR__.'/../migrations/'.Str::snake($migration).'.php.stub' => database_path(sprintf(
'/migrations/%s_%s.php',
date('Y_m_d_His', time() + $i),
Str::snake($migration)
)),
], 'migrations');
}
}
}
}
Loading

0 comments on commit 964ab9d

Please sign in to comment.