hyperlink/laravel-model-locking 问题修复 & 功能扩展

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

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

hyperlink/laravel-model-locking

Composer 安装命令:

composer require hyperlink/laravel-model-locking

包简介

Wordpress like locking feature for Laravel-Models.

README 文档

README

Installation

You can install the package via composer:

composer require hyperlink/laravel-model-locking

You can publish the config file with:

php artisan vendor:publish --provider="Hylk\Locking\ModelLockingServiceProvider" --tag="model-locking-config"

You can publish the translation files with:

php artisan vendor:publish --provider="Hylk\Locking\ModelLockingServiceProvider" --tag="model-locking-translations"

You can publish the vue-components via:

php artisan vendor:publish --provider="Hylk\Locking\ModelLockingServiceProvider" --tag="model-locking-vue"

Usage

Setting up your models

Within a model just use the IsLockable-Trait.

class Post extends Model {
    use \Hylk\ModelLocking\IsLockable;
    
    ...
}

Additionally you have to extend the database-tables for the Model.

return new class extends Migration {
    public function up()
    {
        Schema::table('posts', function (Blueprint $table) {
            $table->lockfields();
        });
    }
    
    public function down()
    {
        Schema::table('posts', function (Blueprint $table) {
            $table->dropLockfields();
        });
    }
}

simple Model-Locking

If you want just a simple version of the locking, just use the Traits methods within your controller. HEARTBEAT_LOCK_DURATION should be set to something like 15 minutes (900 seconds).

class PostController {
    public function show(Post $post) {
      $post->lock();
    }
  
    public function update(Request $request, Post $post) {
      $post->update($request->all());
      $post->unlock();
    }
}

To make sure no locks are missed you should use the locking:release Artisan command in your scheduler. Additionally, you should publish the config and set the lock duration to around 15 minutes.

Model-Locking by heartbeat (Vue)

The more advanced approach is to handle the locks via a heartbeat. This only works for Vue and axios.

  1. Publish the vue-components
  2. register the global HeartbeatManager
    import Vue from 'vue';
    import HeartbeatManager from './vendor/hylk/laravel-model-locking/heartbeat-manager';
    ...
    window.axios = require('axios');
    ...
    Vue.use(HeartbeatManager);
  3. register the Listener-Components
    1. for index-pages
    <template>
        <div>
            <HeartbeatListener model-class="App\Models\Post"
                :model-id="model_id"
                @locked="setLockState"
                @unlocked="deleteLockState" />
            ...
        </div>
    </template>
    Handle the wished behavior like showing the current locker by the lock event and delete this information on the unlock-event.
    ...
       <span v-if="isLocked(model_id)">Locked by {{ getLock(model_id).locked_by.name }}</span>
    ...
  4. register the LockRefresher on your Edit-form.
    <template>
       <div>
          <HeartbeatLockRefresher model-class="App\Models\Post"
              :model-id="model_id"
              @lost="reloadRoute()" />
          ...
       </div>
     </template>
    The lost-Event shows if the component tries to render if the model is locked by another user than the logged in.

Environment variables

Variable Default Description
HEARTBEAT_LOCK_DURATION 70 The time in seconds a model is locked.
MIX_HEARTBEAT_REFRESH 60 The time in seconds between the heartbeats. Should be a multiple of the MIX_HEARTBEAT_STATUS-interval.
MIX_HEARTBEAT_STATUS 30 The time in seconds between the heartbeats for status-request (index-Listener).
MIX_HEARTBEAT_ENABLED true Activates or deactivates the heartbeats.

config

Beside the environment variables, there is a middleware key to determine the middleware(s) used by the heartbeat-route. Default it's set to api.

hyperlink/laravel-model-locking 适用场景与选型建议

hyperlink/laravel-model-locking 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 6.95k 次下载、GitHub Stars 达 2, 最近一次更新时间为 2023 年 01 月 31 日, 在 PHP 生态内属于活跃度较高的组件。

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

围绕 hyperlink/laravel-model-locking 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 6.95k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 2
  • 点击次数: 22
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 2
  • Watchers: 3
  • Forks: 1
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2023-01-31