定制 fr3nch13/cakephp-stats 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

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

fr3nch13/cakephp-stats

Composer 安装命令:

composer require fr3nch13/cakephp-stats

包简介

Statistics plugin for CakePHP Projects

README 文档

README

Build Status Total Downloads PHPStan codecov

Used to track and display statistics, and Trends.

Installation

You can install this plugin into your CakePHP application using composer.

The recommended way to install composer packages is:

composer require fr3nch13/cakephp-stats

Usage

This all revolves around the event listener StatsListener.

To use this plugin, you need to extend the StatsListener, and define your StatsObject keys in it. As an example of how to extend the StatsListener and define your objects.

See CakePHP's Event System

src/Event/TestListener.php:

<?php
declare(strict_types=1);

namespace App\Event;

use Cake\Event\Event;

class ArticleListener extends StatsListener
{
    // Define your events here
    public function implementedEvents(): array
    {
        return [
            'App.Article.hit' => 'onHit',
        ];
    }

    public function onHit(Event $event, int $articleId, int $count = 1): bool
    {
        // track if any articles were viewed
        // Article.hits is a StatsObject key.
        parent::recordCount($event, 'Articles.hits'); // leave out count to just increment by one.

        // track the specific article
        // Article.hits.[id] is a seperate StatsObject key from above.
        parent::recordCount($event, 'Articles.hits.' . $articleId, $count);
    }
}

Once you've created your Listener that has been extended from the StatsListener, you need to register it. In either your Application.php (if you're directly using it within an app), or your Plugin.php (if your using this within another plugin), you need to Use the EventManager to register your Listener in the bootstrap() method. For an example, See: StatsPlugin.php's bootstrap().

src/BlogPlugin.php

<?php
declare(strict_types=1);

namespace Fr3nch13\Blog;

use Cake\Core\BasePlugin;
use Cake\Core\PluginApplicationInterface;
use Cake\Event\EventManager;
use Fr3nch13\Stats\Event\TestListener;


class BlogPlugin extends BasePlugin
{
    // other code

    public function bootstrap(PluginApplicationInterface $app): void
    {
        // Register your listener with the Event Manager
        EventManager::instance()->on(new ArticleListener());

        parent::bootstrap($app);
    }

    /// other code
}

src/Controller/ArticlesController.php

<?php
declare(strict_types=1);

namespace Fr3nch13\Blog\Controller;

use Cake\Event\Event;
use Fr3nch13\Blog\AppController;

class ArticlesController extends AppController
{
    /**
     * Example of how to register a hit
     */
    public function view(int $id): ?Response
    {
        $article = $this->Articles->get($id);

        // do this reight before rendering the view incase your code above throws an error,
        // or redirects somewhere else.
        $this->getEventManager()->dispatch(new Event('App.Article.hit', $this, [
            'articleId' => $id,
            'count' => 1,
        ]));
    }
}

To use the controller trait, you can do so like:

See: TestsController.

src/Controller/Admin/ArticlesController.php

<?php
declare(strict_types=1);

namespace Fr3nch13\Blog\Admin\Controller;

use Fr3nch13\Stats\Controller\ChartJsTrait;
use Fr3nch13\Blog\Admin\AppController;

class ArticlesController extends AppController
{
    /**
     * Used to do the common tasks for chartjs graphs.
     */
    use ChartJsTrait;

    // other code

    public function line(?int $range = null, ?string $timeperiod = null): ?Response
    {
        $keys = [
            'Articles.hits',
            'Articles.hits.1',
            'Articles.hits.2',
            'Articles.hits.3',
        ];

        return $this->chartJsLine($keys, $range, $timeperiod);
    }

    // other code

    /**
     * To get the stats in a dashboard
     *
     * @return ?\Cake\Http\Response Renders view
     */
    public function dashboard(): ?Response
    {
        /** @var \Fr3nch13\Stats\Model\Table\StatsCountsTable $StatsCounts */
        $StatsCounts = $this->getTableLocator()->get('Fr3nch13/Stats.StatsCounts');

        $stats = $this->StatsCounts->getObjectStats('Articles.hits');

        /*
        $stats will look like:
        $stats = [
            'year' => 12001, <-- counts
            'month' => 3001,
            'week' => 701,
            'day' => 101,
            'hour' => 11,
        ];
        */

        $this->set(compact('stats'));
        $this->viewBuilder()->setOption('serialize', ['stats']);

        return null;
    }
}

fr3nch13/cakephp-stats 适用场景与选型建议

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

它主要适用于以下技术方向: 「plugin」 「cakephp」 「stats」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

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

围绕 fr3nch13/cakephp-stats 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2023-10-26