edulazaro/larakeep 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

edulazaro/larakeep

Composer 安装命令:

composer require edulazaro/larakeep

包简介

Maintenance Tasks for Laravel Models Fields

README 文档

README

Larakeep

Keepers for Laravel

Total Downloads Latest Stable Version

Introduction

Larakeep is a package which allows to create and handle Keepers. Keepers allow to process the value of the desired model fields.

Sometimes, a field value must be configured when creating or updating a model instance. A common place to do this are the observers. This can lead to bloat the observers with repeated code or bloating the the models with functions, resulting in big files.

keepers allow to set the value of the desired fields on separate classes, keeping the code cleaner.

Actions VS Keepers

While Keepers and Actions share similarities, they serve different purposes in Laravel applications:

Feature Actions Pattern Keepers (Larakeep)
Purpose Perform a self-contained operation (e.g., CreateUserAction, DeletePostAction) Maintain or compute model field values dynamically
Scope Often used for operations that affect multiple models or require business logic Specific to maintaining values inside a model
Implementation Typically standalone classes invoked as SomeAction::run($params) Assigned to models using attributes or manually (keep())
Examples CreateInvoiceAction, SendNotificationAction getFormattedName(), computeRankings()
  • Use Keepers when: You need to centralize computed fields, enforce model-based transformations, or derive values dynamically.
  • Use Actions when: You are performing operations that modify multiple models, involve services, or handle workflow logic.

How to install Larakeep

Execute this command on the Laravel root project folder:

composer require edulazaro/larakeep

How to create a Keeper

You can create a Keeper manually or using the make command:

php artisan make:keeper MyModelKeeper

The Keeper will be created by default for the model \App\Models\MyModel.

You can also specify the model you are creating the Keeper for by using a second argument:

php artisan make:keeper MyClassKeeper  "\App\Models\Whatever\MyModel"

In this case, the keeper will be created for the model \App\Models\Whatever\MyModel.

How to configure a Keeper

After creating a Keeper, you will need to add the HasKeepers concern to the referenced model:

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use EduLazaro\Larakeep\Concerns\HasKeepers;

class MyModel extends Model
{
    use HasKeepers;
}

Registering Keepers manually

You can manually assign a Keeper to a model in a service provider's boot method:

namespace App\Providers;

use App\Models\MyModel;
use App\Keepers\MyModelKeeper;

class AppServiceProvider extends Model
{
    // ...
    public function boot()
    {
        // ...
        MyModel::keep(MyModelKeeper::class);
    }
}

You can add many Keepers to a single model:

MyModel::keep(MyModelKeeperA::class);
MyModel::keep(MyModelKeeperB::class);

Registering Keepers using Attributes

Alternatively, you can register Keepers using attributes directly in the model:

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use EduLazaro\Larakeep\Concerns\HasKeepers;
use EduLazaro\Larakeep\Attributes\KeptBy;
use App\Keepers\MyModelKeeper;

#[KeptBy(MyModelKeeper::class)]
class MyModel extends Model
{
    use HasKeepers;
}

This method allows you to keep the registration clean and self-contained within the model itself, without needing to modify the service provider.

Larakeep supports assigning multiple Keepers to a model using multiple #[KeptBy] attributes:

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use EduLazaro\Larakeep\Concerns\HasKeepers;
use EduLazaro\Larakeep\Attributes\KeptBy;
use App\Keepers\MyModelKeeperA;
use App\Keepers\MyModelKeeperB;

#[KeptBy(MyModelKeeperA::class)]
#[KeptBy(MyModelKeeperB::class)]
class MyModel extends Model
{
    use HasKeepers;
}

How to use a Keeper

To use Keeper you need to use the word get + the camel case version of the model attribute to process. For example, it's common to keep separate the model name and the search string for the model. In this case a keeper method is handy to set the attribute value:

namespace App\Keepers;

use \App\Models\MyClass;

class MyClassKeeper
{
    // ...
    public function getSearchText()
    {
        return $this->myClass->name . ' ' . $this->myClass->tag;
    }
}

The method can also accept parameters, adding the keyword With to the method name:

namespace App\Keepers;

use \App\Models\MyClass;

class MyClassKeeper
{
    // ...
    public function getSearchTextWith($whatever)
    {
        return $this->myClass->name . ' ' . $this->myClass->tag;
    }
}

Now, on an observer, you can process the search_text field to set its value:

$myClassInstance->process('search_text'); // To execute the getSearchText method.

Or if the method has parameters:

$myClassInstance->processWith('search_text', 'Any string'); // To execute the getSearchTextWith method.

You can also pass an array of fields to process all of them at the same time:

$myClassInstance->process(['search_text', 'word_count']);

The model will still need to be saved.

How to add Tasks

You can prepend any other word thanget to the keeper methods, like configure:

namespace App\Keepers;

use \App\Models\MyClass;

class MyClassKeeper
{
    // ...
    public function configureSearchText()
    {
        return $this->myClass->name . ' ' . $this->myClass->tag;
    }
}

However to process these fields with these methods you will need to use the processTask method:

$myClassInstance->processTaskWith('configure','search_text');

In the same way, these methods can also accept parameters:

namespace App\Keepers;

use \App\Models\MyClass;

class MyClassKeeper
{
    // ...
    public function configureSearchTextWith($whatever)
    {
        return $this->myClass->name . ' ' . $this->myClass->tag;
    }
}

The you would do:

$myClassInstance->processTaskWith('configure','search_text', 'Any string');

You can also pass an array of fields to process all of them at the same time:

$myClassInstance->processTask('configure', ['search_text', 'word_count']);

Saving data

The attributes will not be saved to the database. In order to do that, call the save method as usually:

$myClassInstance->process('configure')->save();

Sponsors

Larakeep is supported by the following sponsors. Thank you for keeping it growing:

Kenodo Kenodo     AndorraDev AndorraDev

Author

Created by Edu Lazaro

License

Larakeep is open-sourced software licensed under the MIT license.

edulazaro/larakeep 适用场景与选型建议

edulazaro/larakeep 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 536 次下载、GitHub Stars 达 0, 最近一次更新时间为 2023 年 08 月 29 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 edulazaro/larakeep 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 edulazaro/larakeep 我们能提供哪些服务?
定制开发 / 二次开发

基于 edulazaro/larakeep 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 536
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 7
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2023-08-29