From baa272317eae1d6faf76ca147ad49eab6ffd51af Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=99=86=E4=BA=91=E5=B3=B0?= Date: Sun, 9 Oct 2022 17:59:24 +0800 Subject: [PATCH 01/11] update readme. --- README.md | 202 +++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 199 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index a4a9e46..c80180e 100644 --- a/README.md +++ b/README.md @@ -26,9 +26,205 @@ composer require biiiiiigmonster/laravel-clearable ``` # Introductions -删除作为数据操作的生命周期最后一环,受到关注度较小,然而在业务中完整数据的关联性往往会因为这些疏忽而被破坏。 -这个包可以很方便的帮您管理这些关联数据删除关系,仅仅只需要简单的定义。 -让我们来尝试一下吧! +`relationship` is powerful, it can help us maintain complex data relation. + +一般来说,“删除”操作作为数据生命周期的最后一节,受到的关注度较小,我们往往在删除数据本身的同时可能会疏忽掉与之关联的模型数据的处理, +业务中数据完整的关联性也会因为这些残留数据而遭到破坏。 + +这个包可以很方便的帮您管理这些关联数据删除关系,仅仅只需要简单的定义。让我们来尝试一下吧! + +## Usage +For example, `User` model related `Post` model, it's also hoped that the associated `Post` model can be deleted after the `User` model deleted: + +```injectablephp +namespace App\Models; + +use Illuminate\Database\Eloquent\Model; + +class User extends Model +{ + /** + * Get the posts for the user. + * + * @return \HasMany + */ + public function posts() + { + return $this->hasMany(Post::class); + } +} +``` +To accomplish this, you may add the `BiiiiiigMonster\Clears\Concerns\HasClears` trait to the models you would like to auto-clear. +After adding one of the traits to the model, add the attribute name to the `clears` property of your model. +```injectablephp +namespace App\Models; + +use Illuminate\Database\Eloquent\Model; +use BiiiiiigMonster\Clears\Concerns\HasClears; + +class User extends Model +{ + use HasClears; + + /** + * The relationships that will be auto-clear when deleted. + * + * @var array + */ + protected $clears = ['posts']; +} +``` +Once the relationship has been added to the `clears` list, it will be auto-clear when deleted. + +## 清除配置 +### 自定义清除 +有时我们需要自定义清除的逻辑,可以通过定义一个实现`ClearsAttributes`接口的类来实现这一点。 + +要生成新的清除对象,您可以使用 `make:clear` Artisan 命令。Clearable 会将新的清除对象放在`app/Clears`目录中。 如果此目录不存在,Clearable 将在您执行 Artisan 命令创建规则时创建它: +```bash +php artisan make:clear PostClear +``` + +实现这个接口的类必须定义一个`abandon`方法,`abandon`方法将决定这个即将被清理的模型是否清除。作为示例,`User`被删除时,我们将保留他已发布状态的`Post`关联数据。 + +```injectablephp +status === 'published'; + } +} +``` + +Once you have defined a custom clear type, you may attach it to a model attribute using its class name: +```injectablephp +namespace App\Models; + +use Illuminate\Database\Eloquent\Model; +use BiiiiiigMonster\Clears\Concerns\HasClears; +use App\Clears\PostClear; + +class User extends Model +{ + use HasClears; + + /** + * The relationships that will be auto-clear when deleted. + * + * @var array + */ + protected $clears = [ + 'posts' => PostClear::class + ]; +} +``` + +### 队列执行 +When the associated data that we need to clear may be very large, it is a very good strategy to use `queue` to execute it. +Making it work is also simple, add the attribute name to the `clearQueue` property of your model. +```injectablephp +namespace App\Models; + +use Illuminate\Database\Eloquent\Model; +use BiiiiiigMonster\Clears\Concerns\HasClears; +use App\Clears\PostClear; + +class User extends Model +{ + use HasClears; + + /** + * The clearable that will be dispatch on this name queue. + * + * @var string + */ + protected $clearQueue = 'queue-name'; +} +``` +像这样定义完成后,posts关联的clear操作将放置到自定义的队列中去执行,减少了并行的压力。 + +### Clearing At Runtime +At runtime, you may instruct a model instance to using the `clear` or `setClears` method just like +[`append`](https://laravel.com/docs/9.x/eloquent-serialization#appending-at-run-time): +```injectablephp +$user->clear(['posts' => PostClear::class])->delete(); + +$user->setClears(['posts' => PostClear::class])->delete(); +``` + +## PHP8 Attribute +在php8中为我们引入了Attribute的特性,它提供了另外一种形式的配置,clear也已经为他做好了准备。 + +使用Attribute非常的简单,我们定义了一个`#[Clear]`的Attribute,你只需要在对应的关联方法中引入即可。 +```injectablephp +namespace App\Models; + +use BiiiiiigMonster\Clears\Attributes\Clear; +use BiiiiiigMonster\Clears\Concerns\HasClears; +use Illuminate\Database\Eloquent\Model; + +class User extends Model +{ + use HasClears; + + /** + * Get the posts for the user. + * + * @return \HasMany + */ + #[Clear] + public function posts() + { + return $this->hasMany(Post::class); + } +} +``` +同样的,你可以在`#[Clear]`中传入自定义清除,甚至单独配置`clearQueue`: +```injectablephp +#[Clear(PostClear::class, 'queue-name')] +public function posts() +{ + return $this->hasMany(Post::class); +} +``` +> Tips:`#[Clear]`会覆盖`protected $clears`中对应关联的配置 + +## 可清除关联类型 +数据的"删除"一般都是较为敏感的操作,我们不希望重要的数据被其他关联定义上clear,因此我们只支持在父子关联的子关联中实现`清除`。 + +支持列表: +- HasOne; +- HasOneThrough; +- HasMany; +- HasManyThrough; +- MorphMany; +- MorphOne; +- BelongsToMany; +- MorphToMany; +> Tips:`BelongsToMany`与`MorphToMany`关联定义clear时,删除的为中间表数据 + +不支持列表: +- BelongsTo; +- MorphTo; + +## Test +```bash +composer test +``` # License [MIT](./LICENSE) From 712db18106da12e3eec8705d2dd797a9aaa32181 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=99=86=E4=BA=91=E5=B3=B0?= Date: Mon, 10 Oct 2022 16:23:56 +0800 Subject: [PATCH 02/11] update readme. --- README.md | 39 ++++++++++++++++++++------------------- src/ClearManager.php | 20 +++++++++++++------- 2 files changed, 33 insertions(+), 26 deletions(-) diff --git a/README.md b/README.md index c80180e..82315a7 100644 --- a/README.md +++ b/README.md @@ -184,7 +184,7 @@ class User extends Model /** * Get the posts for the user. * - * @return \HasMany + * @return HasMany */ #[Clear] public function posts() @@ -193,7 +193,7 @@ class User extends Model } } ``` -同样的,你可以在`#[Clear]`中传入自定义清除,甚至单独配置`clearQueue`: +Similarly, you can set `Custom Clear` in `#[Clear]`, or even configure `clearQueue` separately: ```injectablephp #[Clear(PostClear::class, 'queue-name')] public function posts() @@ -201,25 +201,26 @@ public function posts() return $this->hasMany(Post::class); } ``` -> Tips:`#[Clear]`会覆盖`protected $clears`中对应关联的配置 +> Tips:`#[Clear]` will overwrite the corresponding configuration in `protected $clears` -## 可清除关联类型 +## Clearable Relationship Type 数据的"删除"一般都是较为敏感的操作,我们不希望重要的数据被其他关联定义上clear,因此我们只支持在父子关联的子关联中实现`清除`。 - -支持列表: -- HasOne; -- HasOneThrough; -- HasMany; -- HasManyThrough; -- MorphMany; -- MorphOne; -- BelongsToMany; -- MorphToMany; -> Tips:`BelongsToMany`与`MorphToMany`关联定义clear时,删除的为中间表数据 - -不支持列表: -- BelongsTo; -- MorphTo; +Data's "deletion" is generally a sensitive operation, we do not want important data to declare `clear` by other relationships. Therefore, we don't support `clear` in the `BelongsTo` relationships. + +Support-List: +- HasOne +- HasOneThrough +- HasMany +- HasManyThrough +- MorphMany +- MorphOne +- BelongsToMany +- MorphToMany +> Tips:When the `BelongsToMany` and `MorphToMany` relationship declare is `clear`, deleted is the pivot model data + +Not-Support-List: +- BelongsTo +- MorphTo ## Test ```bash diff --git a/src/ClearManager.php b/src/ClearManager.php index e88c1f3..fa0a02d 100644 --- a/src/ClearManager.php +++ b/src/ClearManager.php @@ -4,11 +4,9 @@ use BiiiiiigMonster\Clearable\Attributes\Clear; use BiiiiiigMonster\Clearable\Jobs\ClearsJob; - use Illuminate\Database\Eloquent\Model; use Illuminate\Support\Collection; use ReflectionClass; -use ReflectionException; use ReflectionMethod; class ClearManager @@ -36,18 +34,26 @@ public static function make(Model $model): static /** * ClearManager handle. - * @throws ReflectionException */ public function handle(): void { $clears = $this->parse(); foreach ($clears as $relationName => $clear) { - $param = [$this->model::class, $this->model->getOriginal(), $relationName, $relations = Collection::wrap($this->model->$relationName), $clear->clearsAttributesClassName]; + $payload = [ + $this->model::class, + $this->model->getOriginal(), + $relationName, + $relations = Collection::wrap($this->model->$relationName), + $clear->clearsAttributesClassName + ]; + if ($relations->isNotEmpty()) { - $clear->clearQueue - ? ClearsJob::dispatch(...$param)->onQueue($clear->clearQueue) - : ClearsJob::dispatchSync(...$param); + match ($clear->clearQueue) { + null => ClearsJob::dispatchSync(...$payload), + '' => ClearsJob::dispatch(...$payload), + default => ClearsJob::dispatch(...$payload)->onQueue($clear->clearQueue) + }; } } } From ea9c16575cfff0f91a27f9243d5b0d9da59c5c41 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=99=86=E4=BA=91=E5=B3=B0?= Date: Tue, 11 Oct 2022 12:16:07 +0800 Subject: [PATCH 03/11] update readme. --- src/Attributes/Clear.php | 4 ++-- src/ClearManager.php | 4 ++-- src/Concerns/HasClears.php | 10 +++++----- tests/Features/ClearQueueTest.php | 11 ++++++++++- 4 files changed, 19 insertions(+), 10 deletions(-) diff --git a/src/Attributes/Clear.php b/src/Attributes/Clear.php index cb4f951..4d127bc 100644 --- a/src/Attributes/Clear.php +++ b/src/Attributes/Clear.php @@ -11,11 +11,11 @@ class Clear * Clear constructor. * * @param string|null $clearsAttributesClassName - * @param string|null $clearQueue + * @param string|bool|null $clearQueue */ public function __construct( public ?string $clearsAttributesClassName = null, - public ?string $clearQueue = null + public string|bool|null $clearQueue = null, ) { } } diff --git a/src/ClearManager.php b/src/ClearManager.php index fa0a02d..71e91a9 100644 --- a/src/ClearManager.php +++ b/src/ClearManager.php @@ -50,8 +50,8 @@ public function handle(): void if ($relations->isNotEmpty()) { match ($clear->clearQueue) { - null => ClearsJob::dispatchSync(...$payload), - '' => ClearsJob::dispatch(...$payload), + null,false => ClearsJob::dispatchSync(...$payload), + true,'' => ClearsJob::dispatch(...$payload), default => ClearsJob::dispatch(...$payload)->onQueue($clear->clearQueue) }; } diff --git a/src/Concerns/HasClears.php b/src/Concerns/HasClears.php index 563f96c..d9c9e87 100644 --- a/src/Concerns/HasClears.php +++ b/src/Concerns/HasClears.php @@ -9,7 +9,7 @@ * Trait HasClears * * @property array $clears The relationships that will be auto-cleared when deleted. - * @property ?string $clearQueue The clearable that will be dispatch on this name queue. + * @property string|bool|null $clearQueue The clearable that will be dispatch on this name queue. * @package BiiiiiigMonster\Clears\Concerns */ trait HasClears @@ -64,9 +64,9 @@ public function clear(array|string|null $clears): static /** * Get clearQueue. * - * @return ?string + * @return string|bool|null */ - public function getClearQueue(): ?string + public function getClearQueue(): string|bool|null { return $this->clearQueue; } @@ -74,10 +74,10 @@ public function getClearQueue(): ?string /** * Set the clearQueue attributes for the model. * - * @param string $clearQueue + * @param string|bool|null $clearQueue * @return $this */ - public function setClearQueue(string $clearQueue): static + public function setClearQueue(string|null $clearQueue): static { $this->clearQueue = $clearQueue; diff --git a/tests/Features/ClearQueueTest.php b/tests/Features/ClearQueueTest.php index eeca0af..fa056ae 100644 --- a/tests/Features/ClearQueueTest.php +++ b/tests/Features/ClearQueueTest.php @@ -4,7 +4,16 @@ use BiiiiiigMonster\Clearable\Tests\Models\User; use Illuminate\Support\Facades\Queue; -test('clear queue test', function () { +test('Clear default queue test', function () { + Queue::fake(); + + $user = User::has('posts', '>=', 2)->with('posts')->first(); + $user->clear('posts')->setClearQueue(true)->delete(); + + Queue::assertPushed(ClearsJob::class); +}); + +test('Clear name queue test', function () { Queue::fake(); $user = User::has('posts', '>=', 2)->with('posts')->first(); From 54dcbdc78a83e8bf4ac7214668bd222cb12c368b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=99=86=E4=BA=91=E5=B3=B0?= Date: Tue, 11 Oct 2022 12:17:10 +0800 Subject: [PATCH 04/11] update readme. --- tests/Features/ClearQueueTest.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/Features/ClearQueueTest.php b/tests/Features/ClearQueueTest.php index fa056ae..efd0777 100644 --- a/tests/Features/ClearQueueTest.php +++ b/tests/Features/ClearQueueTest.php @@ -4,7 +4,7 @@ use BiiiiiigMonster\Clearable\Tests\Models\User; use Illuminate\Support\Facades\Queue; -test('Clear default queue test', function () { +test('Clear use queue test', function () { Queue::fake(); $user = User::has('posts', '>=', 2)->with('posts')->first(); @@ -13,7 +13,7 @@ Queue::assertPushed(ClearsJob::class); }); -test('Clear name queue test', function () { +test('Clear named queue test', function () { Queue::fake(); $user = User::has('posts', '>=', 2)->with('posts')->first(); From 0451c1defbb51c0bc04c8e53970f6f819e51eb7e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=99=86=E4=BA=91=E5=B3=B0?= Date: Tue, 11 Oct 2022 12:21:20 +0800 Subject: [PATCH 05/11] update readme. --- README.md | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 82315a7..2cec65e 100644 --- a/README.md +++ b/README.md @@ -133,8 +133,9 @@ class User extends Model } ``` -### 队列执行 -When the associated data that we need to clear may be very large, it is a very good strategy to use `queue` to execute it. +### Use Queue +When the relation data that we need to clear may be very large, it is a very good strategy to use `queue` to execute it. + Making it work is also simple, add the attribute name to the `clearQueue` property of your model. ```injectablephp namespace App\Models; @@ -150,9 +151,9 @@ class User extends Model /** * The clearable that will be dispatch on this name queue. * - * @var string + * @var bool|string */ - protected $clearQueue = 'queue-name'; + protected $clearQueue = true; } ``` 像这样定义完成后,posts关联的clear操作将放置到自定义的队列中去执行,减少了并行的压力。 @@ -204,7 +205,6 @@ public function posts() > Tips:`#[Clear]` will overwrite the corresponding configuration in `protected $clears` ## Clearable Relationship Type -数据的"删除"一般都是较为敏感的操作,我们不希望重要的数据被其他关联定义上clear,因此我们只支持在父子关联的子关联中实现`清除`。 Data's "deletion" is generally a sensitive operation, we do not want important data to declare `clear` by other relationships. Therefore, we don't support `clear` in the `BelongsTo` relationships. Support-List: From 67e6cdce6f681615161f1fdb672b82cc71e3965f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=99=86=E4=BA=91=E5=B3=B0?= Date: Tue, 11 Oct 2022 17:03:43 +0800 Subject: [PATCH 06/11] rename --- README.md | 27 ++++++++++--------- src/Attributes/Clear.php | 4 +-- src/ClearManager.php | 10 +++---- src/ClearsServiceProvider.php | 4 +-- ...mand.php => InvokableClearMakeCommand.php} | 4 +-- ...ears-attributes.stub => invoke-clear.stub} | 6 ++--- ...learsAttributes.php => InvokableClear.php} | 4 +-- src/Jobs/ClearsJob.php | 12 +++++---- tests/Clears/NormalClear.php | 6 ++--- tests/Clears/PostVotesOddClear.php | 6 ++--- tests/Clears/RoleExceptSystemTypeClear.php | 6 ++--- tests/Clears/SupplierClear.php | 6 ++--- 12 files changed, 49 insertions(+), 46 deletions(-) rename src/Console/{ClearsAttributesMakeCommand.php => InvokableClearMakeCommand.php} (91%) rename src/Console/stubs/{clears-attributes.stub => invoke-clear.stub} (59%) rename src/Contracts/{ClearsAttributes.php => InvokableClear.php} (75%) diff --git a/README.md b/README.md index 2cec65e..9538620 100644 --- a/README.md +++ b/README.md @@ -76,26 +76,27 @@ class User extends Model ``` Once the relationship has been added to the `clears` list, it will be auto-clear when deleted. -## 清除配置 -### 自定义清除 -有时我们需要自定义清除的逻辑,可以通过定义一个实现`ClearsAttributes`接口的类来实现这一点。 +## Clear Configuration +### Custom Clear +Sometimes you may occasionally need to define your own clear's logic, You may accomplish this by defining a class that implements the `InvokableClear` interface. -要生成新的清除对象,您可以使用 `make:clear` Artisan 命令。Clearable 会将新的清除对象放在`app/Clears`目录中。 如果此目录不存在,Clearable 将在您执行 Artisan 命令创建规则时创建它: +To generate a new clear object, you may use the `make:clear` Artisan command. we will place the new rule in the `app/Clears` directory. If this directory does not exist, We will create it when you execute the Artisan command to create your clear: ```bash -php artisan make:clear PostClear +php artisan make:clear PostWithoutReleasedClear ``` -实现这个接口的类必须定义一个`abandon`方法,`abandon`方法将决定这个即将被清理的模型是否清除。作为示例,`User`被删除时,我们将保留他已发布状态的`Post`关联数据。 +Once the clear has been created, we are ready to define its behavior. A clear object contains a single method: `__invoke`. +This method will determine whether the relation data is cleared. ```injectablephp status === 'published'; + return $post->status != 'published'; } } ``` @@ -204,8 +205,8 @@ public function posts() ``` > Tips:`#[Clear]` will overwrite the corresponding configuration in `protected $clears` -## Clearable Relationship Type -Data's "deletion" is generally a sensitive operation, we do not want important data to declare `clear` by other relationships. Therefore, we don't support `clear` in the `BelongsTo` relationships. +## Support Relationship +Data's "deletion" is generally a sensitive operation, we do not want important data to declare `clear` by any relationships. Therefore, we don't support `clear` in the `BelongsTo` relationships. Support-List: - HasOne @@ -222,7 +223,7 @@ Not-Support-List: - BelongsTo - MorphTo -## Test +# Test ```bash composer test ``` diff --git a/src/Attributes/Clear.php b/src/Attributes/Clear.php index 4d127bc..c7d5e59 100644 --- a/src/Attributes/Clear.php +++ b/src/Attributes/Clear.php @@ -10,11 +10,11 @@ class Clear /** * Clear constructor. * - * @param string|null $clearsAttributesClassName + * @param string|null $invokableClearClassName * @param string|bool|null $clearQueue */ public function __construct( - public ?string $clearsAttributesClassName = null, + public ?string $invokableClearClassName = null, public string|bool|null $clearQueue = null, ) { } diff --git a/src/ClearManager.php b/src/ClearManager.php index 71e91a9..20ffdab 100644 --- a/src/ClearManager.php +++ b/src/ClearManager.php @@ -45,7 +45,7 @@ public function handle(): void $this->model->getOriginal(), $relationName, $relations = Collection::wrap($this->model->$relationName), - $clear->clearsAttributesClassName + $clear->invokableClearClassName ]; if ($relations->isNotEmpty()) { @@ -68,13 +68,13 @@ protected function parse(): array $clears = []; // from clears array - foreach ($this->model->getClears() as $relationName => $clearsAttributesClassName) { + foreach ($this->model->getClears() as $relationName => $invokableClearClassName) { if (is_numeric($relationName)) { - $relationName = $clearsAttributesClassName; - $clearsAttributesClassName = null; + $relationName = $invokableClearClassName; + $invokableClearClassName = null; } - $clears[$relationName] = new Clear($clearsAttributesClassName, $this->model->getClearQueue()); + $clears[$relationName] = new Clear($invokableClearClassName, $this->model->getClearQueue()); } // from clear attribute diff --git a/src/ClearsServiceProvider.php b/src/ClearsServiceProvider.php index aa20058..b8f6c6e 100644 --- a/src/ClearsServiceProvider.php +++ b/src/ClearsServiceProvider.php @@ -2,7 +2,7 @@ namespace BiiiiiigMonster\Clearable; -use BiiiiiigMonster\Clearable\Console\ClearsAttributesMakeCommand; +use BiiiiiigMonster\Clearable\Console\InvokableClearMakeCommand; use Illuminate\Support\ServiceProvider; class ClearsServiceProvider extends ServiceProvider @@ -14,6 +14,6 @@ class ClearsServiceProvider extends ServiceProvider */ public function register() { - $this->commands(ClearsAttributesMakeCommand::class); + $this->commands(InvokableClearMakeCommand::class); } } diff --git a/src/Console/ClearsAttributesMakeCommand.php b/src/Console/InvokableClearMakeCommand.php similarity index 91% rename from src/Console/ClearsAttributesMakeCommand.php rename to src/Console/InvokableClearMakeCommand.php index 5b5eff4..b1ca6cd 100644 --- a/src/Console/ClearsAttributesMakeCommand.php +++ b/src/Console/InvokableClearMakeCommand.php @@ -4,7 +4,7 @@ use Illuminate\Console\GeneratorCommand; -class ClearsAttributesMakeCommand extends GeneratorCommand +class InvokableClearMakeCommand extends GeneratorCommand { /** * The console command name. @@ -43,7 +43,7 @@ class ClearsAttributesMakeCommand extends GeneratorCommand */ protected function getStub() { - $relativePath = '/stubs/clears-attributes.stub'; + $relativePath = '/stubs/invoke-clear.stub'; return file_exists($customPath = $this->laravel->basePath(trim($relativePath, '/'))) ? $customPath diff --git a/src/Console/stubs/clears-attributes.stub b/src/Console/stubs/invoke-clear.stub similarity index 59% rename from src/Console/stubs/clears-attributes.stub rename to src/Console/stubs/invoke-clear.stub index ea90523..37647c6 100644 --- a/src/Console/stubs/clears-attributes.stub +++ b/src/Console/stubs/invoke-clear.stub @@ -2,10 +2,10 @@ namespace {{ namespace }}; -use BiiiiiigMonster\Clearable\Contracts\ClearsAttributes; +use BiiiiiigMonster\Clearable\Contracts\InvokableClear; use Illuminate\Database\Eloquent\Model; -class {{ class }} implements ClearsAttributes +class {{ class }} implements InvokableClear { /** * Decide if the clearable cleared. @@ -13,7 +13,7 @@ class {{ class }} implements ClearsAttributes * @param Model $clear * @return bool */ - public function abandon($clear): bool + public function __invoke($clear): bool { // } diff --git a/src/Contracts/ClearsAttributes.php b/src/Contracts/InvokableClear.php similarity index 75% rename from src/Contracts/ClearsAttributes.php rename to src/Contracts/InvokableClear.php index c0f6cc5..6ef74dc 100644 --- a/src/Contracts/ClearsAttributes.php +++ b/src/Contracts/InvokableClear.php @@ -4,7 +4,7 @@ use Illuminate\Database\Eloquent\Model; -interface ClearsAttributes +interface InvokableClear { /** * Decide if the clearable cleared. @@ -12,5 +12,5 @@ interface ClearsAttributes * @param Model $clear * @return bool */ - public function abandon($clear): bool; + public function __invoke($clear): bool; } diff --git a/src/Jobs/ClearsJob.php b/src/Jobs/ClearsJob.php index 9c213a1..761008f 100644 --- a/src/Jobs/ClearsJob.php +++ b/src/Jobs/ClearsJob.php @@ -29,14 +29,14 @@ class ClearsJob implements ShouldQueue * @param array $original * @param string $relationName * @param Collection $collection - * @param string|null $clearsAttributesClassName + * @param string|null $invokableClearClassName */ public function __construct( protected string $className, protected array $original, protected string $relationName, protected Collection $collection, - protected ?string $clearsAttributesClassName = null, + protected ?string $invokableClearClassName = null, ) { } @@ -51,9 +51,11 @@ public function handle(): void $relation = $model->{$this->relationName}(); // to be cleared model. - $clears = $this->clearsAttributesClassName - ? $this->collection->filter(fn (Model $clear) => (new $this->clearsAttributesClassName())->abandon($clear, $model)) - : $this->collection; + $clears = $this->collection; + if ($this->invokableClearClassName) { + $invoke = new $this->invokableClearClassName(); + $clears = $clears->filter(fn (Model $clear) => $invoke($clear, $model)); + } switch (true) { case $relation instanceof HasOneOrMany: diff --git a/tests/Clears/NormalClear.php b/tests/Clears/NormalClear.php index 7d5f1ee..cd7c610 100644 --- a/tests/Clears/NormalClear.php +++ b/tests/Clears/NormalClear.php @@ -2,11 +2,11 @@ namespace BiiiiiigMonster\Clearable\Tests\Clears; -use BiiiiiigMonster\Clearable\Contracts\ClearsAttributes; +use BiiiiiigMonster\Clearable\Contracts\InvokableClear; -class NormalClear implements ClearsAttributes +class NormalClear implements InvokableClear { - public function abandon($clear): bool + public function __invoke($clear): bool { return true; } diff --git a/tests/Clears/PostVotesOddClear.php b/tests/Clears/PostVotesOddClear.php index 8a5c969..0c99c05 100644 --- a/tests/Clears/PostVotesOddClear.php +++ b/tests/Clears/PostVotesOddClear.php @@ -2,11 +2,11 @@ namespace BiiiiiigMonster\Clearable\Tests\Clears; -use BiiiiiigMonster\Clearable\Contracts\ClearsAttributes; +use BiiiiiigMonster\Clearable\Contracts\InvokableClear; -class PostVotesOddClear implements ClearsAttributes +class PostVotesOddClear implements InvokableClear { - public function abandon($clear): bool + public function __invoke($clear): bool { return $clear->votes % 2; } diff --git a/tests/Clears/RoleExceptSystemTypeClear.php b/tests/Clears/RoleExceptSystemTypeClear.php index eda3e74..f622669 100644 --- a/tests/Clears/RoleExceptSystemTypeClear.php +++ b/tests/Clears/RoleExceptSystemTypeClear.php @@ -2,16 +2,16 @@ namespace BiiiiiigMonster\Clearable\Tests\Clears; -use BiiiiiigMonster\Clearable\Contracts\ClearsAttributes; +use BiiiiiigMonster\Clearable\Contracts\InvokableClear; use BiiiiiigMonster\Clearable\Tests\Models\Role; -class RoleExceptSystemTypeClear implements ClearsAttributes +class RoleExceptSystemTypeClear implements InvokableClear { /** * @param Role $clear * @return bool */ - public function abandon($clear): bool + public function __invoke($clear): bool { return !($clear->name && $clear->pivot->type % 2); } diff --git a/tests/Clears/SupplierClear.php b/tests/Clears/SupplierClear.php index 5ddb9c3..0b0dffb 100644 --- a/tests/Clears/SupplierClear.php +++ b/tests/Clears/SupplierClear.php @@ -2,11 +2,11 @@ namespace BiiiiiigMonster\Clearable\Tests\Clears; -use BiiiiiigMonster\Clearable\Contracts\ClearsAttributes; +use BiiiiiigMonster\Clearable\Contracts\InvokableClear; -class SupplierClear implements ClearsAttributes +class SupplierClear implements InvokableClear { - public function abandon($clear): bool + public function __invoke($clear): bool { return true; } From b0a159362b6ed6e02b438f9cab2938493a431162 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=99=86=E4=BA=91=E5=B3=B0?= Date: Tue, 11 Oct 2022 17:50:50 +0800 Subject: [PATCH 07/11] readme --- README.md | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index 9538620..d1359e0 100644 --- a/README.md +++ b/README.md @@ -117,7 +117,7 @@ namespace App\Models; use Illuminate\Database\Eloquent\Model; use BiiiiiigMonster\Clears\Concerns\HasClears; -use App\Clears\PostClear; +use App\Clears\PostWithoutReleasedClear; class User extends Model { @@ -129,7 +129,7 @@ class User extends Model * @var array */ protected $clears = [ - 'posts' => PostClear::class + 'posts' => PostWithoutReleasedClear::class ]; } ``` @@ -143,7 +143,7 @@ namespace App\Models; use Illuminate\Database\Eloquent\Model; use BiiiiiigMonster\Clears\Concerns\HasClears; -use App\Clears\PostClear; +use App\Clears\PostWithoutReleasedClear; class User extends Model { @@ -157,21 +157,22 @@ class User extends Model protected $clearQueue = true; } ``` -像这样定义完成后,posts关联的clear操作将放置到自定义的队列中去执行,减少了并行的压力。 +Once the `clearQueue` has been declared, the `posts`'s clear behavior will be executed using the queue, reducing the serial pressure. +> Tips: You can also set it as a string `protected $clearQueue = 'queue-name';`,which will run in the named queue ### Clearing At Runtime At runtime, you may instruct a model instance to using the `clear` or `setClears` method just like [`append`](https://laravel.com/docs/9.x/eloquent-serialization#appending-at-run-time): ```injectablephp -$user->clear(['posts' => PostClear::class])->delete(); +$user->clear(['posts' => PostWithoutReleasedClear::class])->delete(); -$user->setClears(['posts' => PostClear::class])->delete(); +$user->setClears(['posts' => PostWithoutReleasedClear::class])->delete(); ``` ## PHP8 Attribute -在php8中为我们引入了Attribute的特性,它提供了另外一种形式的配置,clear也已经为他做好了准备。 +The 'Attribute' feature is added to php8, which provides another form of configuration, and clear is ready for it. -使用Attribute非常的简单,我们定义了一个`#[Clear]`的Attribute,你只需要在对应的关联方法中引入即可。 +It is very simple to use `Attribute`, we have defined an attribute of `#[Clear]`, just only need to relate the method. ```injectablephp namespace App\Models; @@ -195,9 +196,10 @@ class User extends Model } } ``` + Similarly, you can set `Custom Clear` in `#[Clear]`, or even configure `clearQueue` separately: ```injectablephp -#[Clear(PostClear::class, 'queue-name')] +#[Clear(PostWithoutReleasedClear::class, 'queue-name')] public function posts() { return $this->hasMany(Post::class); From 36ba102a133bccdd9b76fb031fbac6d276ac368d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=99=86=E4=BA=91=E5=B3=B0?= Date: Tue, 11 Oct 2022 18:17:18 +0800 Subject: [PATCH 08/11] readme --- README.md | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index d1359e0..869adf0 100644 --- a/README.md +++ b/README.md @@ -26,12 +26,11 @@ composer require biiiiiigmonster/laravel-clearable ``` # Introductions -`relationship` is powerful, it can help us maintain complex data relation. +`relation` is powerful, it can help us manage complex relationship's data. +Usually, as the last section of the data life cycle, the "delete" behavior receives less attention. +We often neglect the processing of the associated model data while deleting the data itself, the business will also be damaged due to these residual data. -一般来说,“删除”操作作为数据生命周期的最后一节,受到的关注度较小,我们往往在删除数据本身的同时可能会疏忽掉与之关联的模型数据的处理, -业务中数据完整的关联性也会因为这些残留数据而遭到破坏。 - -这个包可以很方便的帮您管理这些关联数据删除关系,仅仅只需要简单的定义。让我们来尝试一下吧! +This package can easily help you manage these related data's deletion relationships, with simple definitions. Let's try it! ## Usage For example, `User` model related `Post` model, it's also hoped that the associated `Post` model can be deleted after the `User` model deleted: From b1161ffde4d931c0be88ff98a255643e98a820cc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=99=86=E4=BA=91=E5=B3=B0?= Date: Tue, 11 Oct 2022 18:17:44 +0800 Subject: [PATCH 09/11] readme --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 869adf0..4632396 100644 --- a/README.md +++ b/README.md @@ -45,7 +45,7 @@ class User extends Model /** * Get the posts for the user. * - * @return \HasMany + * @return HasMany */ public function posts() { From 391608b3031cc55c91b5060b6e7baa75ba654832 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=99=86=E4=BA=91=E5=B3=B0?= Date: Tue, 11 Oct 2022 18:19:36 +0800 Subject: [PATCH 10/11] readme --- README.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 4632396..1e9a1f7 100644 --- a/README.md +++ b/README.md @@ -157,7 +157,7 @@ class User extends Model } ``` Once the `clearQueue` has been declared, the `posts`'s clear behavior will be executed using the queue, reducing the serial pressure. -> Tips: You can also set it as a string `protected $clearQueue = 'queue-name';`,which will run in the named queue +> Tips: You can also set it as a string `protected $clearQueue = 'queue-name';`, which will run in the named queue. ### Clearing At Runtime At runtime, you may instruct a model instance to using the `clear` or `setClears` method just like @@ -204,7 +204,7 @@ public function posts() return $this->hasMany(Post::class); } ``` -> Tips:`#[Clear]` will overwrite the corresponding configuration in `protected $clears` +> Tips:`#[Clear]` will overwrite the corresponding configuration in `protected $clears`. ## Support Relationship Data's "deletion" is generally a sensitive operation, we do not want important data to declare `clear` by any relationships. Therefore, we don't support `clear` in the `BelongsTo` relationships. @@ -218,7 +218,7 @@ Support-List: - MorphOne - BelongsToMany - MorphToMany -> Tips:When the `BelongsToMany` and `MorphToMany` relationship declare is `clear`, deleted is the pivot model data +> Tips:When the `BelongsToMany` and `MorphToMany` relationship declare is `clear`, deleted is the pivot model data. Not-Support-List: - BelongsTo From 2ac6d5a6b3be7dce4e851942b217604d92d4aeb0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=99=86=E4=BA=91=E5=B3=B0?= Date: Tue, 11 Oct 2022 18:22:24 +0800 Subject: [PATCH 11/11] readme --- src/Concerns/HasClears.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Concerns/HasClears.php b/src/Concerns/HasClears.php index d9c9e87..b427839 100644 --- a/src/Concerns/HasClears.php +++ b/src/Concerns/HasClears.php @@ -77,7 +77,7 @@ public function getClearQueue(): string|bool|null * @param string|bool|null $clearQueue * @return $this */ - public function setClearQueue(string|null $clearQueue): static + public function setClearQueue(string|bool|null $clearQueue): static { $this->clearQueue = $clearQueue;