sunlab / wn-measures-plugin
允许您在任意模型上创建/增加并显示任何您想要的度量
Requires
- php: >=7.0
- composer/installers: ~1.0
README
此插件允许您在模型上创建、更新并显示任何您想要的度量,一些例子包括
- 博客文章的阅读量
- 成员创建的论坛主题数量
- 成员连续每日连接数的计数
- API资源获取计数
- ...
此插件旨在用于更复杂的插件或直接用于注册网站事件的一些统计信息。
可度量行为
您可以将可度量行为(将其视为特征)添加到任何您想要的模型。使用 Storm 扩展。
从插件注册文件扩展现有模型
Winter\Blog\Models\Post::extend(function ($postModel) { $postModel->implement[] = 'SunLab.Measures.Behaviors.Measurable'; });
或直接扩展到您的插件模型
class Link extends Model { public $implement = ['SunLab.Measures.Behaviors.Measurable']; //... }
[实验性功能] 插件的本地和通用事件监听器
对于最基本的事件,您甚至不需要编写一行代码。此插件创建了一个通用事件监听器,可以处理最基本的使用场景。例如,它将监听所有在 Winter\Forum\Models\Topic 上的 model.afterCreate 事件,并在登录用户上增加一个 topic_created 度量。 
创建/增加度量
一旦您将可度量行为添加到模型,您就可以使用模型的方法 incrementMeasure。
$post->incrementMeasure('views'); // Optional amount of incrementation can be passed $post->incrementMeasure('views', 5);
注意:您无需处理度量的任何 初始化,只需使用它即可。
实用示例 1,统计用户编辑其帖子次数
// In Plugin.php file function boot() { // Add the Measurable behavior to User model Winter\User\Models\User::extend(function($user) { // Verify it has not been already added by another plugin if (!$user->isClassExtendedWith('SunLab.Measures.Behaviors.Measurable')) { $user->extendClassWith('SunLab.Measures.Behaviors.Measurable'); } }); Winter\Forum\Models\Post::extend(function($post) { // Bind listener to update event $post->bindEvent('model.afterUpdate', function() use ($model) { // Increment measure on the member $post->member->incrementMeasure('post_edit'); }); }); }
实用示例 2,在 Winter.Blog 中创建帖子阅读量
title = "Blog post page" url = "/blog/article/:slug" layout = "default" is_hidden = 0 [blogPost] slug = "{{ :slug }}" categoryPage = "blog/category" == function onEnd() { $this->blogPost->post->incrementMeasure('views'); } == {% component 'blogPost' %}
批量增加
您可以一次性增加多个模型的度量,这在您想要衡量从 API 获取的模型数量时很有用。
要使用批量增加,您需要将查询的 Builder 实例传递给 MeasureManager
// Passing to the MeasureManager a Builder instance $products = Product::where('name', 'like', '%shoes%'); MeasureManager::incrementMeasure($products, 'appearedInSearchResults'); return new JsonResponse([ 'products' => $products->get() ]);
孤儿度量
由于某种原因,您可能想要增加一些孤儿度量
// Count how many users log-in Event::listen('winter.user.login', function() { MeasureManager::incrementOrphanMeasure('users_login'); // incrementMeasure also support orphan measure. // Same as: MeasureManager::incrementMeasure('users_login'); });
减少或重置度量
此插件支持在模型或孤儿度量上减少和重置度量
// On model implementing Measurable $model->decrementMeasure('post_edit'); $model->decrementMeasure('post_edit', 3); // An amount of decrementation can be passed // With orphan measures MeasureManager::decrementOrphanMeasure('users_login'); MeasureManager::decrementOrphanMeasure('users_login', 3); // An amount of decrementation can be passed
显示度量
要从模型中显示度量,只需使用其 getMeasure 或 getAmountOf 方法。 getMeasure 返回一个包含 amount 属性的 Measure 模型
// From a backend controller/view $post = Post::first(); $views = $post->getMeasure('views')->amount; // Same as: $views = $post->getAmountOf('views');
// From a frontend Twig block
{{ post.getMeasure('view').amount }}
// Same as:
{{ post.getAmountOf('view') }}
事件
如前所述,此插件在与其他插件一起使用时非常强大且方便。在增加、减少和重置度量时触发事件。所有事件都包含模型(孤儿度量时为 null),新的度量对象及其数量。例如,您可以查看 SunLab/Gamification 以获取完整示例
// Plugin.php Event::listen('sunlab.measures.incrementMeasure', function ($model, $measure) { // Filter the model we need if (!$model instanceof User) { return; } // Process some custom logic depending on the measure name and current amount // In that case, SunLab/Gamification assign "badges" depending on some measures incrementation $correspondingBadges = Badge::where([['measure_name', $measure->name], ['amount_needed', '<=', $measure->amount]]) ->whereDoesntHave('users', function ($query) use ($model) { $query->where('user_id', $model->id); })->get(); if (!blank($correspondingBadges)) { $now = now(); $attachedBadges = array_combine( $correspondingBadges->pluck('id')->toArray(), array_fill(0, count($correspondingBadges), ['updated_at' => $now, 'created_at' => $now]) ); $model->badges()->attach($attachedBadges); } });
MeasureManager 和模型
MeasureManager 静态类处理孤儿和批量度量的修改,但也可以增加模型度量
// Using the MeasureManager static helpers: use SunLab\Measures\Classes\MeasureManager; $post = \Winter\Blog\Models\Post::first(); // Increment a model's measure MeasureManager::incrementMeasure($post, 'views'); MeasureManager::incrementMeasure($post, 'views', 5); // Decrement: MeasureManager::decrementMeasure($model, 'views'); MeasureManager::decrementMeasure($model, 'views', 3); // Reset MeasureManager::resetMeasure('views'); MeasureManager::resetMeasure('views', 2);
待办事项列表
在不久的将来,我将添加一些功能,例如
- 从模型集合而不是 Builder 批量增加
- 显示一些特定度量的 ReportWidget