Skip to content

Commit 6f0df23

Browse files
feat: ✨ create main feature
1 parent 24261bd commit 6f0df23

File tree

12 files changed

+238
-71
lines changed

12 files changed

+238
-71
lines changed

config/skeleton.php

Lines changed: 0 additions & 6 deletions
This file was deleted.

config/userstamps.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
return [
4+
/*
5+
* Specify the column type used in the schema for the account ID
6+
*
7+
* Options: increments, bigIncrements, uuid, ulid
8+
* Default: bigIncrements
9+
*/
10+
"users_table_id_column_type" => "bigIncrements",
11+
12+
/*
13+
* Specify the column name used as the foreign key reference for the account ID. Make sure all user tables have an ID column name that matches the one you define below.
14+
*/
15+
"users_table_id_column_name" => "id",
16+
17+
/*
18+
* Specify the column that stores the ID of the user who initially created the entry
19+
*/
20+
"created_by_column" => "created_by",
21+
22+
/*
23+
* Specify the column that holds the ID of the user who last updated the entry
24+
*/
25+
"updated_by_column" => "updated_by",
26+
27+
/*
28+
* Specify the column that contains the ID of the user who removed the entry
29+
*/
30+
"deleted_by_column" => "deleted_by",
31+
32+
/*
33+
* Indicate whether to include soft-deleted records in queries
34+
*/
35+
"with_trashed" => false,
36+
];

src/Commands/SkeletonCommand.php

Lines changed: 0 additions & 19 deletions
This file was deleted.

src/Concerns/HasUserstamps.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
namespace SanSanLabs\Userstamps\Concerns;
4+
5+
use Illuminate\Database\Eloquent\Relations\MorphTo;
6+
use Illuminate\Database\Eloquent\SoftDeletes;
7+
use SanSanLabs\Userstamps\Observers\UserstampsObserver;
8+
9+
trait HasUserstamps {
10+
public static function bootHasUserstamps(): void {
11+
static::observe(UserstampsObserver::class);
12+
}
13+
14+
public function creator(): MorphTo {
15+
$relation = $this->morphTo(__FUNCTION__, config("userstamps.created_by_column") . "_type", config("userstamps.created_by_column"));
16+
return config("userstamps.with_trashed") ? $relation->withTrashed() : $relation;
17+
}
18+
19+
public function editor(): MorphTo {
20+
$relation = $this->morphTo(__FUNCTION__, config("userstamps.updated_by_column") . "_type", config("userstamps.updated_by_column"));
21+
return config("userstamps.with_trashed") ? $relation->withTrashed() : $relation;
22+
}
23+
24+
public function destroyer(): MorphTo {
25+
$relation = $this->morphTo(__FUNCTION__, config("userstamps.deleted_by_column") . "_type", config("userstamps.deleted_by_column"));
26+
return config("userstamps.with_trashed") ? $relation->withTrashed() : $relation;
27+
}
28+
29+
public static function usingSoftDeletes(): bool {
30+
return in_array(SoftDeletes::class, class_uses_recursive(static::class));
31+
}
32+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
namespace SanSanLabs\Userstamps\Database\Schema\Macros;
4+
5+
interface MacroInterface {
6+
public function register();
7+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?php
2+
3+
namespace SanSanLabs\Userstamps\Database\Schema\Macros;
4+
5+
use Illuminate\Database\Schema\Blueprint;
6+
7+
class UserstampsMacro implements MacroInterface {
8+
public function register(): void {
9+
$this->registerUserstamps();
10+
$this->registerSoftUserstamps();
11+
$this->registerDropUserstamps();
12+
$this->registerDropSoftUserstamps();
13+
}
14+
15+
public function addUserIdColumn(Blueprint $table, string $column): void {
16+
$type = config("userstamps.users_table_id_column_type");
17+
18+
match ($type) {
19+
"uuid" => $table->uuid($column)->nullable(),
20+
"ulid" => $table->ulid($column)->nullable(),
21+
"bigIncrements" => $table->unsignedBigInteger($column)->nullable(),
22+
default => $table->unsignedInteger($column)->nullable(),
23+
};
24+
}
25+
26+
private function registerUserstamps(): void {
27+
Blueprint::macro("userstamps", function (): void {
28+
app(UserstampsMacro::class)->addUserIdColumn($this, config("userstamps.created_by_column"));
29+
$this->string(config("userstamps.created_by_column") . "_type")->nullable();
30+
31+
app(UserstampsMacro::class)->addUserIdColumn($this, config("userstamps.updated_by_column"));
32+
$this->string(config("userstamps.updated_by_column") . "_type")->nullable();
33+
});
34+
}
35+
36+
private function registerSoftUserstamps(): void {
37+
Blueprint::macro("softUserstamps", function (): void {
38+
app(UserstampsMacro::class)->addUserIdColumn($this, config("userstamps.deleted_by_column"));
39+
$this->string(config("userstamps.deleted_by_column") . "_type")->nullable();
40+
});
41+
}
42+
43+
private function registerDropUserstamps(): void {
44+
Blueprint::macro("dropUserstamps", function (): void {
45+
$this->dropColumn([
46+
config("userstamps.created_by_column"),
47+
config("userstamps.created_by_column") . "_type",
48+
config("userstamps.updated_by_column"),
49+
config("userstamps.updated_by_column") . "_type",
50+
]);
51+
});
52+
}
53+
54+
private function registerDropSoftUserstamps(): void {
55+
Blueprint::macro("dropSoftUserstamps", function (): void {
56+
$this->dropColumn([config("userstamps.deleted_by_column"), config("userstamps.deleted_by_column") . "_type"]);
57+
});
58+
}
59+
}

src/Facades/Skeleton.php

Lines changed: 0 additions & 16 deletions
This file was deleted.
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
<?php
2+
3+
namespace SanSanLabs\Userstamps\Observers;
4+
5+
use Illuminate\Database\Eloquent\Model;
6+
7+
class UserstampsObserver {
8+
public function creating(Model $model): void {
9+
if ($model->isClean(config("userstamps.created_by_column"))) {
10+
$model->{config("userstamps.created_by_column")} = $this->getUserPrimaryValueAndClass()["id"];
11+
$model->{config("userstamps.created_by_column") . "_type"} = $this->getUserPrimaryValueAndClass()["type"];
12+
}
13+
14+
if ($model->isClean(config("userstamps.updated_by_column"))) {
15+
$model->{config("userstamps.updated_by_column")} = $this->getUserPrimaryValueAndClass()["id"];
16+
$model->{config("userstamps.updated_by_column") . "_type"} = $this->getUserPrimaryValueAndClass()["type"];
17+
}
18+
}
19+
20+
public function updating(Model $model): void {
21+
if ($model->isClean(config("userstamps.updated_by_column"))) {
22+
$model->{config("userstamps.updated_by_column")} = $this->getUserPrimaryValueAndClass()["id"];
23+
$model->{config("userstamps.updated_by_column") . "_type"} = $this->getUserPrimaryValueAndClass()["type"];
24+
}
25+
}
26+
27+
public function deleting(Model $model): void {
28+
if ($model->usingSoftDeletes()) {
29+
$model->{config("userstamps.deleted_by_column")} = $this->getUserPrimaryValueAndClass()["id"];
30+
$model->{config("userstamps.deleted_by_column") . "_type"} = $this->getUserPrimaryValueAndClass()["type"];
31+
$model->{config("userstamps.updated_by_column")} = $this->getUserPrimaryValueAndClass()["id"];
32+
$model->{config("userstamps.updated_by_column") . "_type"} = $this->getUserPrimaryValueAndClass()["type"];
33+
$this->saveWithoutEventDispatching($model);
34+
}
35+
}
36+
37+
public function restoring(Model $model): void {
38+
if ($model->usingSoftDeletes()) {
39+
$model->{config("userstamps.deleted_by_column")} = null;
40+
$model->{config("userstamps.deleted_by_column") . "_type"} = null;
41+
$model->{config("userstamps.updated_by_column")} = $this->getUserPrimaryValueAndClass()["id"];
42+
$model->{config("userstamps.updated_by_column") . "_type"} = $this->getUserPrimaryValueAndClass()["type"];
43+
$this->saveWithoutEventDispatching($model);
44+
}
45+
}
46+
47+
private function saveWithoutEventDispatching(Model $model): bool {
48+
$eventDispatcher = $model->getEventDispatcher();
49+
50+
$model->unsetEventDispatcher();
51+
$saved = $model->save();
52+
$model->setEventDispatcher($eventDispatcher);
53+
54+
return $saved;
55+
}
56+
57+
private function getUserPrimaryValueAndClass(): array {
58+
if (!auth()->check()) {
59+
return [
60+
"id" => null,
61+
"type" => null,
62+
];
63+
}
64+
65+
if (config("userstamps.users_table_id_column_name") !== "id") {
66+
return [
67+
"id" => auth()->user()->{config("userstamps.users_table_id_column_name")},
68+
"type" => get_class(auth()->user()),
69+
];
70+
}
71+
72+
return [
73+
"id" => auth()->id(),
74+
"type" => get_class(auth()->user()),
75+
];
76+
}
77+
}

src/Skeleton.php

Lines changed: 0 additions & 5 deletions
This file was deleted.

src/SkeletonServiceProvider.php

Lines changed: 0 additions & 25 deletions
This file was deleted.

0 commit comments

Comments
 (0)